keep plugin instance for first cpu
[collectd.git] / src / cpu.c
1 /**
2  * collectd - src/cpu.c
3  * Copyright (C) 2005-2010  Florian octo Forster
4  * Copyright (C) 2008       Oleg King
5  * Copyright (C) 2009       Simon Kuhnle
6  * Copyright (C) 2009       Manuel Sanmartin
7  *
8  * This program is free software; you can redistribute it and/or modify it
9  * under the terms of the GNU General Public License as published by the
10  * Free Software Foundation; only version 2 of the License is applicable.
11  *
12  * This program is distributed in the hope that it will be useful, but
13  * WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License along
18  * with this program; if not, write to the Free Software Foundation, Inc.,
19  * 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
20  *
21  * Authors:
22  *   Florian octo Forster <octo at verplant.org>
23  *   Oleg King <king2 at kaluga.ru>
24  *   Simon Kuhnle <simon at blarzwurst.de>
25  *   Manuel Sanmartin
26  **/
27
28 #include "collectd.h"
29 #include "common.h"
30 #include "plugin.h"
31
32 #ifdef HAVE_MACH_KERN_RETURN_H
33 # include <mach/kern_return.h>
34 #endif
35 #ifdef HAVE_MACH_MACH_INIT_H
36 # include <mach/mach_init.h>
37 #endif
38 #ifdef HAVE_MACH_HOST_PRIV_H
39 # include <mach/host_priv.h>
40 #endif
41 #if HAVE_MACH_MACH_ERROR_H
42 #  include <mach/mach_error.h>
43 #endif
44 #ifdef HAVE_MACH_PROCESSOR_INFO_H
45 # include <mach/processor_info.h>
46 #endif
47 #ifdef HAVE_MACH_PROCESSOR_H
48 # include <mach/processor.h>
49 #endif
50 #ifdef HAVE_MACH_VM_MAP_H
51 # include <mach/vm_map.h>
52 #endif
53
54 #ifdef HAVE_LIBKSTAT
55 # include <sys/sysinfo.h>
56 #endif /* HAVE_LIBKSTAT */
57
58 #if (defined(HAVE_SYSCTL) && HAVE_SYSCTL) \
59         || (defined(HAVE_SYSCTLBYNAME) && HAVE_SYSCTLBYNAME)
60 # ifdef HAVE_SYS_SYSCTL_H
61 #  include <sys/sysctl.h>
62 # endif
63
64 # ifdef HAVE_SYS_DKSTAT_H
65 #  include <sys/dkstat.h>
66 # endif
67
68 # if !defined(CP_USER) || !defined(CP_NICE) || !defined(CP_SYS) || !defined(CP_INTR) || !defined(CP_IDLE) || !defined(CPUSTATES)
69 #  define CP_USER   0
70 #  define CP_NICE   1
71 #  define CP_SYS    2
72 #  define CP_INTR   3
73 #  define CP_IDLE   4
74 #  define CPUSTATES 5
75 # endif
76 #endif /* HAVE_SYSCTL || HAVE_SYSCTLBYNAME */
77
78 #if HAVE_SYSCTL
79 # if defined(CTL_HW) && defined(HW_NCPU) \
80         && defined(CTL_KERN) && defined(KERN_CPTIME) && defined(CPUSTATES)
81 #  define CAN_USE_SYSCTL 1
82 # else
83 #  define CAN_USE_SYSCTL 0
84 # endif
85 #else
86 # define CAN_USE_SYSCTL 0
87 #endif
88
89 #define CPU_SUBMIT_USER 1
90 #define CPU_SUBMIT_SYSTEM 2
91 #define CPU_SUBMIT_WAIT 3
92 #define CPU_SUBMIT_NICE 4
93 #define CPU_SUBMIT_SWAP 5
94 #define CPU_SUBMIT_INTERRUPT 6
95 #define CPU_SUBMIT_SOFTIRQ 7
96 #define CPU_SUBMIT_STEAL 8
97 #define CPU_SUBMIT_IDLE 9
98 #define CPU_SUBMIT_ACTIVE 10
99 #define CPU_SUBMIT_MAX 11
100 #define CPU_SUBMIT_FLUSH -1
101
102 #if HAVE_STATGRAB_H
103 # include <statgrab.h>
104 #endif
105
106 # ifdef HAVE_PERFSTAT
107 #  include <sys/protosw.h>
108 #  include <libperfstat.h>
109 # endif /* HAVE_PERFSTAT */
110
111 #if !PROCESSOR_CPU_LOAD_INFO && !KERNEL_LINUX && !HAVE_LIBKSTAT \
112         && !CAN_USE_SYSCTL && !HAVE_SYSCTLBYNAME && !HAVE_LIBSTATGRAB && !HAVE_PERFSTAT
113 # error "No applicable input method."
114 #endif
115
116 static const char *cpu_state_names[] = {
117         "none",
118         "user",
119         "system",
120         "wait",
121         "nice",
122         "swap",
123         "interrupt",
124         "softirq",
125         "steal",
126         "idle",
127         "active"
128 };
129
130 #ifdef PROCESSOR_CPU_LOAD_INFO
131 static mach_port_t port_host;
132 static processor_port_array_t cpu_list;
133 static mach_msg_type_number_t cpu_list_len;
134
135 #if PROCESSOR_TEMPERATURE
136 static int cpu_temp_retry_counter = 0;
137 static int cpu_temp_retry_step    = 1;
138 static int cpu_temp_retry_max     = 1;
139 #endif /* PROCESSOR_TEMPERATURE */
140 /* #endif PROCESSOR_CPU_LOAD_INFO */
141
142 #elif defined(KERNEL_LINUX)
143 /* no variables needed */
144 /* #endif KERNEL_LINUX */
145
146 #elif defined(HAVE_LIBKSTAT)
147 /* colleague tells me that Sun doesn't sell systems with more than 100 or so CPUs.. */
148 # define MAX_NUMCPU 256
149 extern kstat_ctl_t *kc;
150 static kstat_t *ksp[MAX_NUMCPU];
151 static int numcpu;
152 /* #endif HAVE_LIBKSTAT */
153
154 #elif CAN_USE_SYSCTL
155 static int numcpu;
156 /* #endif CAN_USE_SYSCTL */
157
158 #elif defined(HAVE_SYSCTLBYNAME)
159 static int numcpu;
160 #  ifdef HAVE_SYSCTL_KERN_CP_TIMES
161 static int maxcpu;
162 #  endif /* HAVE_SYSCTL_KERN_CP_TIMES */
163 /* #endif HAVE_SYSCTLBYNAME */
164
165 #elif defined(HAVE_LIBSTATGRAB)
166 /* no variables needed */
167 /* #endif  HAVE_LIBSTATGRAB */
168
169 #elif defined(HAVE_PERFSTAT)
170 static perfstat_cpu_t *perfcpu;
171 static int numcpu;
172 static int pnumcpu;
173 #endif /* HAVE_PERFSTAT */
174
175 static gauge_t percents[CPU_SUBMIT_MAX] = {
176         0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
177 };
178 int cpu_count = 0;
179
180 static _Bool report_by_cpu = 1;
181 static _Bool report_percent = 0;
182 static _Bool report_active = 0;
183
184 static const char *config_keys[] =
185 {
186         "ReportByCpu",
187         "ReportActive",
188         "ValuesPercentage"
189 };
190 static int config_keys_num = STATIC_ARRAY_SIZE (config_keys);
191
192 static int cpu_config (const char *key, const char *value)
193 {
194         if (strcasecmp (key, "ReportByCpu") == 0) {
195                 report_by_cpu = IS_TRUE (value) ? 1 : 0;
196                 if (!report_by_cpu)
197                         report_percent = 1;
198         }
199         if (strcasecmp (key, "ValuesPercentage") == 0)
200                 report_percent = IS_TRUE (value) ? 1 : 0;
201         if (strcasecmp (key, "ReportActive") == 0)
202                 report_active = IS_TRUE (value) ? 1 : 0;
203         return (-1);
204 }
205
206 static int init (void)
207 {
208 #if PROCESSOR_CPU_LOAD_INFO || PROCESSOR_TEMPERATURE
209         kern_return_t status;
210
211         port_host = mach_host_self ();
212
213         /* FIXME: Free `cpu_list' if it's not NULL */
214         if ((status = host_processors (port_host, &cpu_list, &cpu_list_len)) != KERN_SUCCESS)
215         {
216                 ERROR ("cpu plugin: host_processors returned %i", (int) status);
217                 cpu_list_len = 0;
218                 return (-1);
219         }
220
221         DEBUG ("host_processors returned %i %s", (int) cpu_list_len, cpu_list_len == 1 ? "processor" : "processors");
222         INFO ("cpu plugin: Found %i processor%s.", (int) cpu_list_len, cpu_list_len == 1 ? "" : "s");
223
224         cpu_temp_retry_max = 86400 / CDTIME_T_TO_TIME_T (plugin_get_interval ());
225 /* #endif PROCESSOR_CPU_LOAD_INFO */
226
227 #elif defined(HAVE_LIBKSTAT)
228         kstat_t *ksp_chain;
229
230         numcpu = 0;
231
232         if (kc == NULL)
233                 return (-1);
234
235         /* Solaris doesn't count linear.. *sigh* */
236         for (numcpu = 0, ksp_chain = kc->kc_chain;
237                         (numcpu < MAX_NUMCPU) && (ksp_chain != NULL);
238                         ksp_chain = ksp_chain->ks_next)
239                 if (strncmp (ksp_chain->ks_module, "cpu_stat", 8) == 0)
240                         ksp[numcpu++] = ksp_chain;
241 /* #endif HAVE_LIBKSTAT */
242
243 #elif CAN_USE_SYSCTL
244         size_t numcpu_size;
245         int mib[2] = {CTL_HW, HW_NCPU};
246         int status;
247
248         numcpu = 0;
249         numcpu_size = sizeof (numcpu);
250
251         status = sysctl (mib, STATIC_ARRAY_SIZE (mib),
252                         &numcpu, &numcpu_size, NULL, 0);
253         if (status == -1)
254         {
255                 char errbuf[1024];
256                 WARNING ("cpu plugin: sysctl: %s",
257                                 sstrerror (errno, errbuf, sizeof (errbuf)));
258                 return (-1);
259         }
260 /* #endif CAN_USE_SYSCTL */
261
262 #elif defined (HAVE_SYSCTLBYNAME)
263         size_t numcpu_size;
264
265         numcpu_size = sizeof (numcpu);
266
267         if (sysctlbyname ("hw.ncpu", &numcpu, &numcpu_size, NULL, 0) < 0)
268         {
269                 char errbuf[1024];
270                 WARNING ("cpu plugin: sysctlbyname(hw.ncpu): %s",
271                                 sstrerror (errno, errbuf, sizeof (errbuf)));
272                 return (-1);
273         }
274
275 #ifdef HAVE_SYSCTL_KERN_CP_TIMES
276         numcpu_size = sizeof (maxcpu);
277
278         if (sysctlbyname("kern.smp.maxcpus", &maxcpu, &numcpu_size, NULL, 0) < 0)
279         {
280                 char errbuf[1024];
281                 WARNING ("cpu plugin: sysctlbyname(kern.smp.maxcpus): %s",
282                                 sstrerror (errno, errbuf, sizeof (errbuf)));
283                 return (-1);
284         }
285 #else
286         if (numcpu != 1)
287                 NOTICE ("cpu: Only one processor supported when using `sysctlbyname' (found %i)", numcpu);
288 #endif
289 /* #endif HAVE_SYSCTLBYNAME */
290
291 #elif defined(HAVE_LIBSTATGRAB)
292         /* nothing to initialize */
293 /* #endif HAVE_LIBSTATGRAB */
294
295 #elif defined(HAVE_PERFSTAT)
296         /* nothing to initialize */
297 #endif /* HAVE_PERFSTAT */
298
299         return (0);
300 } /* int init */
301
302 static void submit_value (int cpu_num, int cpu_state, const char *type, value_t value)
303 {
304         value_t values[1];
305         value_list_t vl = VALUE_LIST_INIT;
306
307         memcpy(&values[0], &value, sizeof(value));
308
309         vl.values = values;
310         vl.values_len = 1;
311
312         sstrncpy (vl.host, hostname_g, sizeof (vl.host));
313         sstrncpy (vl.plugin, "cpu", sizeof (vl.plugin));
314         sstrncpy (vl.type, type, sizeof (vl.type));
315         sstrncpy (vl.type_instance, cpu_state_names[cpu_state],
316                   sizeof (vl.type_instance));
317
318         if (cpu_num >= 0) {
319                 ssnprintf (vl.plugin_instance, sizeof (vl.plugin_instance),
320                            "%i", cpu_num);
321         }
322         plugin_dispatch_values (&vl);
323 }
324
325 static void submit_percent(int cpu_num, int cpu_state, gauge_t percent)
326 {
327         value_t value;
328
329         value.gauge = percent;
330         submit_value (cpu_num, cpu_state, "percent", value);
331 }
332
333 static void submit_derive(int cpu_num, int cpu_state, derive_t derive)
334 {
335         value_t value;
336
337         value.derive = derive;
338         submit_value (cpu_num, cpu_state, "cpu", value);
339 }
340
341 static void submit_flush (void)
342 {
343         int i = 0;
344
345         if (report_by_cpu)
346                 return;
347
348         for (i = 1; i < CPU_SUBMIT_MAX; i++) {
349                 if (percents[i] == -1)
350                         continue;
351                 submit_percent (-1, i, percents[i] / cpu_count);
352         }
353         cpu_count = 0;
354         memset(percents, 0, sizeof(percents));
355 }
356
357 static void submit (int cpu_num, derive_t *derives)
358 {
359
360         int i = 0;
361         derive_t cpu_active = 0;
362         derive_t cpu_total = 0;
363         gauge_t percent;
364         
365         if (!report_percent && report_by_cpu) {
366                 for (i = 1; i < CPU_SUBMIT_ACTIVE; i++)
367                 {
368                         if (derives[i] == -1)
369                                 continue;
370
371                         if (i != CPU_SUBMIT_IDLE)
372                                 cpu_active += derives[i];
373
374                         submit_derive(cpu_num, i, derives[i]);
375                 }
376                 if (report_active)
377                         submit_derive(cpu_num, CPU_SUBMIT_ACTIVE, cpu_active);
378         }
379         else /* we are reporting percents */
380         {
381                 for (i = 1; i < CPU_SUBMIT_ACTIVE; i++) {
382                         if (derives[i] == -1)
383                                 continue;
384                         cpu_total += derives[i];
385                         if (i != CPU_SUBMIT_IDLE)
386                                 cpu_active += derives[i];
387                 }
388                 if (report_active)
389                         derives[CPU_SUBMIT_ACTIVE] = cpu_active;
390                 else
391                         derives[CPU_SUBMIT_ACTIVE] = -1;
392
393                 cpu_count++;
394                 for (i = 1; i < CPU_SUBMIT_MAX; i++) {
395                         if (derives[i] == -1) {
396                                 percents[i] = -1;
397                                 continue;
398                         }
399                         percent = ((gauge_t)derives[i] / (gauge_t)cpu_total) * 100.0;
400
401                         if (report_by_cpu)
402                         {
403                                 submit_percent (cpu_num, i, percent);
404                         } else if (percents[i] != -1) {
405                                 percents[i] += percent;
406                         }
407                 }
408         }
409 }
410
411 static int cpu_read (void)
412 {
413 #if PROCESSOR_CPU_LOAD_INFO || PROCESSOR_TEMPERATURE
414         int cpu;
415
416         kern_return_t status;
417
418 #if PROCESSOR_CPU_LOAD_INFO
419         derive_t                       cpu_active;
420         processor_cpu_load_info_data_t cpu_info;
421         mach_msg_type_number_t         cpu_info_len;
422 #endif
423 #if PROCESSOR_TEMPERATURE
424         processor_info_data_t          cpu_temp;
425         mach_msg_type_number_t         cpu_temp_len;
426 #endif
427
428         host_t cpu_host;
429
430         for (cpu = 0; cpu < cpu_list_len; cpu++)
431         {
432 #if PROCESSOR_CPU_LOAD_INFO
433                 derive_t derives[CPU_SUBMIT_MAX] = {
434                         -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1      
435                 };
436                 memset(derives, -1, sizeof(derives));
437                 cpu_host = 0;
438                 cpu_info_len = PROCESSOR_BASIC_INFO_COUNT;
439
440                 if ((status = processor_info (cpu_list[cpu],
441                                                 PROCESSOR_CPU_LOAD_INFO, &cpu_host,
442                                                 (processor_info_t) &cpu_info, &cpu_info_len)) != KERN_SUCCESS)
443                 {
444                         ERROR ("cpu plugin: processor_info failed with status %i", (int) status);
445                         continue;
446                 }
447
448                 if (cpu_info_len < CPU_STATE_MAX)
449                 {
450                         ERROR ("cpu plugin: processor_info returned only %i elements..", cpu_info_len);
451                         continue;
452                 }
453
454                 derives[CPU_USER] = (derive_t) cpu_info.cpu_ticks[CPU_STATE_USER];
455                 derives[CPU_NICE] = (derive_t) cpu_info.cpu_ticks[CPU_STATE_NICE];
456                 derives[CPU_SYSTEM] = (derive_t) cpu_info.cpu_ticks[CPU_STATE_SYSTEM];
457                 derives[CPU_IDLE] = (derive_t) cpu_info.cpu_ticks[CPU_STATE_IDLE];
458                 submit (cpu, derives);
459
460 #endif /* PROCESSOR_CPU_LOAD_INFO */
461 #if PROCESSOR_TEMPERATURE
462                 /*
463                  * Not all Apple computers do have this ability. To minimize
464                  * the messages sent to the syslog we do an exponential
465                  * stepback if `processor_info' fails. We still try ~once a day
466                  * though..
467                  */
468                 if (cpu_temp_retry_counter > 0)
469                 {
470                         cpu_temp_retry_counter--;
471                         continue;
472                 }
473
474                 cpu_temp_len = PROCESSOR_INFO_MAX;
475
476                 status = processor_info (cpu_list[cpu],
477                                 PROCESSOR_TEMPERATURE,
478                                 &cpu_host,
479                                 cpu_temp, &cpu_temp_len);
480                 if (status != KERN_SUCCESS)
481                 {
482                         ERROR ("cpu plugin: processor_info failed: %s",
483                                         mach_error_string (status));
484
485                         cpu_temp_retry_counter = cpu_temp_retry_step;
486                         cpu_temp_retry_step *= 2;
487                         if (cpu_temp_retry_step > cpu_temp_retry_max)
488                                 cpu_temp_retry_step = cpu_temp_retry_max;
489
490                         continue;
491                 }
492
493                 if (cpu_temp_len != 1)
494                 {
495                         DEBUG ("processor_info (PROCESSOR_TEMPERATURE) returned %i elements..?",
496                                         (int) cpu_temp_len);
497                         continue;
498                 }
499
500                 cpu_temp_retry_counter = 0;
501                 cpu_temp_retry_step    = 1;
502 #endif /* PROCESSOR_TEMPERATURE */
503         }
504         submit_flush ();
505 /* #endif PROCESSOR_CPU_LOAD_INFO */
506
507 #elif defined(KERNEL_LINUX)
508         int cpu;
509         FILE *fh;
510         char buf[1024];
511
512         char *fields[9];
513         int numfields;
514
515         if ((fh = fopen ("/proc/stat", "r")) == NULL)
516         {
517                 char errbuf[1024];
518                 ERROR ("cpu plugin: fopen (/proc/stat) failed: %s",
519                                 sstrerror (errno, errbuf, sizeof (errbuf)));
520                 return (-1);
521         }
522
523         while (fgets (buf, 1024, fh) != NULL)
524         {
525                 derive_t derives[CPU_SUBMIT_MAX] = {
526                         -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1      
527                 };
528                 
529                 if (strncmp (buf, "cpu", 3))
530                         continue;
531                 if ((buf[3] < '0') || (buf[3] > '9'))
532                         continue;
533
534                 numfields = strsplit (buf, fields, 9);
535                 if (numfields < 5)
536                         continue;
537
538                 cpu = atoi (fields[0] + 3);
539                 derives[CPU_SUBMIT_USER] = atoll(fields[1]);
540                 derives[CPU_SUBMIT_NICE] = atoll(fields[2]);
541                 derives[CPU_SUBMIT_SYSTEM] = atoll(fields[3]);
542                 derives[CPU_SUBMIT_IDLE] = atoll(fields[4]);
543
544                 if (numfields >= 8)
545                 {
546                         derives[CPU_SUBMIT_WAIT] = atoll(fields[5]);
547                         derives[CPU_SUBMIT_INTERRUPT] = atoll(fields[6]);
548                         derives[CPU_SUBMIT_SOFTIRQ] = atoll(fields[6]);
549
550                         if (numfields >= 9)
551                                 derives[CPU_SUBMIT_STEAL] = atoll(fields[8]);
552                 }
553                 submit(cpu, derives);
554         }
555         submit_flush();
556
557         fclose (fh);
558 /* #endif defined(KERNEL_LINUX) */
559
560 #elif defined(HAVE_LIBKSTAT)
561         int cpu;
562         static cpu_stat_t cs;
563
564         if (kc == NULL)
565                 return (-1);
566
567         for (cpu = 0; cpu < numcpu; cpu++)
568         {
569                 derive_t derives[CPU_SUBMIT_MAX] = {
570                         -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1      
571                 };
572                 
573                 if (kstat_read (kc, ksp[cpu], &cs) == -1)
574                         continue; /* error message? */
575
576                 memset(derives, -1, sizeof(derives));
577                 derives[CPU_SUBMIT_IDLE] = cs.cpu_sysinfo.cpu[CPU_IDLE];
578                 derives[CPU_SUBMIT_USER] = cs.cpu_sysinfo.cpu[CPU_USER];
579                 derives[CPU_SUBMIT_SYSTEM] = cs.cpu_sysinfo.cpu[CPU_KERNEL];
580                 derives[CPU_SUBMIT_WAIT] = cs.cpu_sysinfo.cpu[CPU_WAIT];
581                 submit (ksp[cpu]->ks_instance, derives);
582         }
583         submit_flush ();
584 /* #endif defined(HAVE_LIBKSTAT) */
585
586 #elif CAN_USE_SYSCTL
587         uint64_t cpuinfo[numcpu][CPUSTATES];
588         size_t cpuinfo_size;
589         int status;
590         int i;
591
592         if (numcpu < 1)
593         {
594                 ERROR ("cpu plugin: Could not determine number of "
595                                 "installed CPUs using sysctl(3).");
596                 return (-1);
597         }
598
599         memset (cpuinfo, 0, sizeof (cpuinfo));
600
601 #if defined(KERN_CPTIME2)
602         if (numcpu > 1) {
603                 for (i = 0; i < numcpu; i++) {
604                         int mib[] = {CTL_KERN, KERN_CPTIME2, i};
605
606                         cpuinfo_size = sizeof (cpuinfo[0]);
607
608                         status = sysctl (mib, STATIC_ARRAY_SIZE (mib),
609                                         cpuinfo[i], &cpuinfo_size, NULL, 0);
610                         if (status == -1) {
611                                 char errbuf[1024];
612                                 ERROR ("cpu plugin: sysctl failed: %s.",
613                                                 sstrerror (errno, errbuf, sizeof (errbuf)));
614                                 return (-1);
615                         }
616                 }
617         }
618         else
619 #endif /* defined(KERN_CPTIME2) */
620         {
621                 int mib[] = {CTL_KERN, KERN_CPTIME};
622                 long cpuinfo_tmp[CPUSTATES];
623
624                 cpuinfo_size = sizeof(cpuinfo_tmp);
625
626                 status = sysctl (mib, STATIC_ARRAY_SIZE (mib),
627                                         &cpuinfo_tmp, &cpuinfo_size, NULL, 0);
628                 if (status == -1)
629                 {
630                         char errbuf[1024];
631                         ERROR ("cpu plugin: sysctl failed: %s.",
632                                         sstrerror (errno, errbuf, sizeof (errbuf)));
633                         return (-1);
634                 }
635
636                 for(i = 0; i < CPUSTATES; i++) {
637                         cpuinfo[0][i] = cpuinfo_tmp[i];
638                 }
639         }
640
641         for (i = 0; i < numcpu; i++) {
642                 derive_t derives[CPU_SUBMIT_MAX] = {
643                         -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1      
644                 };
645                 
646                 derives[CPU_SUBMIT_USER] = cpuinfo[i][CP_USER];
647                 derives[CPU_SUBMIT_NICE] = cpuinfo[i][CP_NICE];
648                 derives[CPU_SUBMIT_SYSTEM] = cpuinfo[i][CP_SYS];
649                 derives[CPU_SUBMIT_IDLE] = cpuinfo[i][CP_IDLE];
650                 derives[CPU_SUBMIT_INTERRUPT] = cpuinfo[i][CP_INTR];
651                 submit(i, derives);
652         }
653         submit_flush();
654 /* #endif CAN_USE_SYSCTL */
655 #elif defined(HAVE_SYSCTLBYNAME) && defined(HAVE_SYSCTL_KERN_CP_TIMES)
656         long cpuinfo[maxcpu][CPUSTATES];
657         size_t cpuinfo_size;
658         int i;
659
660         memset (cpuinfo, 0, sizeof (cpuinfo));
661
662         cpuinfo_size = sizeof (cpuinfo);
663         if (sysctlbyname("kern.cp_times", &cpuinfo, &cpuinfo_size, NULL, 0) < 0)
664         {
665                 char errbuf[1024];
666                 ERROR ("cpu plugin: sysctlbyname failed: %s.",
667                                 sstrerror (errno, errbuf, sizeof (errbuf)));
668                 return (-1);
669         }
670
671         for (i = 0; i < numcpu; i++) {
672                 derive_t derives[CPU_SUBMIT_MAX] = {
673                         -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1      
674                 };
675
676                 derives[CPU_SUBMIT_USER] = cpuinfo[i][CP_USER];
677                 derives[CPU_SUBMIT_NICE] = cpuinfo[i][CP_NICE];
678                 derives[CPU_SUBMIT_SYSTEM] = cpuinfo[i][CP_SYS];
679                 derives[CPU_SUBMIT_IDLE] = cpuinfo[i][CP_IDLE];
680                 derives[CPU_SUBMIT_INTERRUPT] = cpuinfo[i][CP_INTR];
681                 submit(i, derives);
682         }
683         submit_flush();
684
685 /* #endif HAVE_SYSCTL_KERN_CP_TIMES */
686 #elif defined(HAVE_SYSCTLBYNAME)
687         long cpuinfo[CPUSTATES];
688         size_t cpuinfo_size;
689         derive_t derives[CPU_SUBMIT_MAX] = {
690                 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1      
691         };
692
693         cpuinfo_size = sizeof (cpuinfo);
694
695         if (sysctlbyname("kern.cp_time", &cpuinfo, &cpuinfo_size, NULL, 0) < 0)
696         {
697                 char errbuf[1024];
698                 ERROR ("cpu plugin: sysctlbyname failed: %s.",
699                                 sstrerror (errno, errbuf, sizeof (errbuf)));
700                 return (-1);
701         }
702
703         derives[CPU_SUBMIT_USER] = cpuinfo[CP_USER];
704         derives[CPU_SUBMIT_SYSTEM] = cpuinfo[CP_SYS];
705         derives[CPU_SUBMIT_NICE] = cpuinfo[CP_NICE];
706         derives[CPU_SUBMIT_IDLE] = cpuinfo[CP_IDLE];
707         derives[CPU_SUBMIT_INTERRUPT] = cpuinfo[CP_INTR];
708         submit(0, derives);
709         submit_flush();
710
711 /* #endif HAVE_SYSCTLBYNAME */
712
713 #elif defined(HAVE_LIBSTATGRAB)
714         sg_cpu_stats *cs;
715         derive_t derives[CPU_SUBMIT_MAX] = {
716                 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1      
717         };
718         cs = sg_get_cpu_stats ();
719
720         if (cs == NULL)
721         {
722                 ERROR ("cpu plugin: sg_get_cpu_stats failed.");
723                 return (-1);
724         }
725
726         derives[CPU_SUBMIT_IDLE] = (derive_t) cs->idle;
727         derives[CPU_SUBMIT_NICE] = (derive_t) cs->nice;
728         derives[CPU_SUBMIT_SWAP] = (derive_t) cs->swap;
729         derives[CPU_SUBMIT_SYSTEM] = (derive_t) cs->kernel;
730         derives[CPU_SUBMIT_USER] = (derive_t) cs->user;
731         derives[CPU_SUBMIT_WAIT] = (derive_t) cs->iowait;
732         submit(0, derives);
733         submit_flush();
734 /* #endif HAVE_LIBSTATGRAB */
735
736 #elif defined(HAVE_PERFSTAT)
737         perfstat_id_t id;
738         int i, cpus;
739
740         numcpu =  perfstat_cpu(NULL, NULL, sizeof(perfstat_cpu_t), 0);
741         if(numcpu == -1)
742         {
743                 char errbuf[1024];
744                 WARNING ("cpu plugin: perfstat_cpu: %s",
745                         sstrerror (errno, errbuf, sizeof (errbuf)));
746                 return (-1);
747         }
748
749         if (pnumcpu != numcpu || perfcpu == NULL)
750         {
751                 if (perfcpu != NULL)
752                         free(perfcpu);
753                 perfcpu = malloc(numcpu * sizeof(perfstat_cpu_t));
754         }
755         pnumcpu = numcpu;
756
757         id.name[0] = '\0';
758         if ((cpus = perfstat_cpu(&id, perfcpu, sizeof(perfstat_cpu_t), numcpu)) < 0)
759         {
760                 char errbuf[1024];
761                 WARNING ("cpu plugin: perfstat_cpu: %s",
762                         sstrerror (errno, errbuf, sizeof (errbuf)));
763                 return (-1);
764         }
765
766         for (i = 0; i < cpus; i++)
767         {
768                 derive_t derives[CPU_SUBMIT_MAX] = {
769                         -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1      
770                 };
771                 derives[CPU_SUBMIT_IDLE] = perfcpu[i].idle;
772                 derives[CPU_SUBMIT_SYSTEM] = perfcpu[i].sys;
773                 derives[CPU_SUBMIT_USER] = perfcpu[i].user;
774                 derives[CPU_SUBMIT_WAIT] = perfcpu[i].wait;
775                 submit(i, derives);
776         }
777         submit_flush();
778 #endif /* HAVE_PERFSTAT */
779
780         return (0);
781 }
782
783 void module_register (void)
784 {
785         plugin_register_init ("cpu", init);
786         plugin_register_config ("cpu", cpu_config, config_keys, config_keys_num);
787         plugin_register_read ("cpu", cpu_read);
788 } /* void module_register */