Add ReportByCpu and ValuesPercentage for cpu plugin
[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 *submit_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
176 static _Bool report_by_cpu = 1;
177 static _Bool report_percent = 0;
178
179 static const char *config_keys[] =
180 {
181         "ReportByCpu",
182         "ValuesPercentage"
183 };
184 static int config_keys_num = STATIC_ARRAY_SIZE (config_keys);
185
186 static int cpu_config (const char *key, const char *value)
187 {
188         if (strcasecmp (key, "ReportByCpu") == 0) {
189                 report_by_cpu = IS_TRUE (value) ? 1 : 0;
190                 if (!report_by_cpu)
191                         report_percent = 1;
192         }
193         if (strcasecmp (key, "ValuesPercentage") == 0)
194                 report_percent = IS_TRUE (value) ? 1 : 0;
195         return (-1);
196 }
197
198 static int init (void)
199 {
200 #if PROCESSOR_CPU_LOAD_INFO || PROCESSOR_TEMPERATURE
201         kern_return_t status;
202
203         port_host = mach_host_self ();
204
205         /* FIXME: Free `cpu_list' if it's not NULL */
206         if ((status = host_processors (port_host, &cpu_list, &cpu_list_len)) != KERN_SUCCESS)
207         {
208                 ERROR ("cpu plugin: host_processors returned %i", (int) status);
209                 cpu_list_len = 0;
210                 return (-1);
211         }
212
213         DEBUG ("host_processors returned %i %s", (int) cpu_list_len, cpu_list_len == 1 ? "processor" : "processors");
214         INFO ("cpu plugin: Found %i processor%s.", (int) cpu_list_len, cpu_list_len == 1 ? "" : "s");
215
216         cpu_temp_retry_max = 86400 / CDTIME_T_TO_TIME_T (plugin_get_interval ());
217 /* #endif PROCESSOR_CPU_LOAD_INFO */
218
219 #elif defined(HAVE_LIBKSTAT)
220         kstat_t *ksp_chain;
221
222         numcpu = 0;
223
224         if (kc == NULL)
225                 return (-1);
226
227         /* Solaris doesn't count linear.. *sigh* */
228         for (numcpu = 0, ksp_chain = kc->kc_chain;
229                         (numcpu < MAX_NUMCPU) && (ksp_chain != NULL);
230                         ksp_chain = ksp_chain->ks_next)
231                 if (strncmp (ksp_chain->ks_module, "cpu_stat", 8) == 0)
232                         ksp[numcpu++] = ksp_chain;
233 /* #endif HAVE_LIBKSTAT */
234
235 #elif CAN_USE_SYSCTL
236         size_t numcpu_size;
237         int mib[2] = {CTL_HW, HW_NCPU};
238         int status;
239
240         numcpu = 0;
241         numcpu_size = sizeof (numcpu);
242
243         status = sysctl (mib, STATIC_ARRAY_SIZE (mib),
244                         &numcpu, &numcpu_size, NULL, 0);
245         if (status == -1)
246         {
247                 char errbuf[1024];
248                 WARNING ("cpu plugin: sysctl: %s",
249                                 sstrerror (errno, errbuf, sizeof (errbuf)));
250                 return (-1);
251         }
252 /* #endif CAN_USE_SYSCTL */
253
254 #elif defined (HAVE_SYSCTLBYNAME)
255         size_t numcpu_size;
256
257         numcpu_size = sizeof (numcpu);
258
259         if (sysctlbyname ("hw.ncpu", &numcpu, &numcpu_size, NULL, 0) < 0)
260         {
261                 char errbuf[1024];
262                 WARNING ("cpu plugin: sysctlbyname(hw.ncpu): %s",
263                                 sstrerror (errno, errbuf, sizeof (errbuf)));
264                 return (-1);
265         }
266
267 #ifdef HAVE_SYSCTL_KERN_CP_TIMES
268         numcpu_size = sizeof (maxcpu);
269
270         if (sysctlbyname("kern.smp.maxcpus", &maxcpu, &numcpu_size, NULL, 0) < 0)
271         {
272                 char errbuf[1024];
273                 WARNING ("cpu plugin: sysctlbyname(kern.smp.maxcpus): %s",
274                                 sstrerror (errno, errbuf, sizeof (errbuf)));
275                 return (-1);
276         }
277 #else
278         if (numcpu != 1)
279                 NOTICE ("cpu: Only one processor supported when using `sysctlbyname' (found %i)", numcpu);
280 #endif
281 /* #endif HAVE_SYSCTLBYNAME */
282
283 #elif defined(HAVE_LIBSTATGRAB)
284         /* nothing to initialize */
285 /* #endif HAVE_LIBSTATGRAB */
286
287 #elif defined(HAVE_PERFSTAT)
288         /* nothing to initialize */
289 #endif /* HAVE_PERFSTAT */
290
291         return (0);
292 } /* int init */
293
294 static void submit_value (int plugin_instance, int type_instance,
295                           derive_t val_d,  gauge_t val_g)
296 {
297         value_t values[1];
298         value_list_t vl = VALUE_LIST_INIT;
299
300
301         vl.values = values;
302         vl.values_len = 1;
303
304         if (report_percent)
305                 values[0].gauge = val_g;
306         else
307                 values[0].derive = val_d;
308
309         sstrncpy (vl.host, hostname_g, sizeof (vl.host));
310         sstrncpy (vl.plugin, "cpu", sizeof (vl.plugin));
311         sstrncpy (vl.type, report_percent?"percent":"cpu", sizeof (vl.type));
312         sstrncpy (vl.type_instance, submit_names[type_instance],
313                   sizeof (vl.type_instance));
314
315         if (plugin_instance > 0) {
316                 ssnprintf (vl.plugin_instance, sizeof (vl.plugin_instance),
317                            "%i", plugin_instance);
318         }
319         plugin_dispatch_values (&vl);
320 }
321
322 static void submit (int cpu_num, derive_t *derives)
323 {
324
325         int i = 0;
326         derive_t cpu_active = 0;
327         derive_t cpu_total = 0;
328         static int numcpu = 0;
329         static gauge_t percents[CPU_SUBMIT_MAX] = {
330                 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0
331         };
332         gauge_t gauges[CPU_SUBMIT_MAX] = {
333                 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1
334         };
335
336         if (cpu_num == CPU_SUBMIT_FLUSH)
337         {
338                 if (report_by_cpu)
339                         return;
340                 for (i = 1; i < CPU_SUBMIT_MAX; i++) {
341                         if (percents[i] == -1)
342                                 continue;
343                         submit_value (-1, i, 0, percents[i] / numcpu);
344                 }
345                 numcpu = 0;
346                 memset(percents, 0, sizeof(percents));
347                 return;
348         }
349
350         if (!report_percent && report_by_cpu) {
351
352                 for (i = 1; i < CPU_SUBMIT_ACTIVE; i++)
353                 {
354                         if (derives[i] == -1)
355                                 continue;
356
357                         if (i != CPU_SUBMIT_IDLE)
358                                 cpu_active += derives[i];
359
360                         submit_value(cpu_num, i, derives[i], 0);
361                 }
362                 submit_value(cpu_num, CPU_SUBMIT_ACTIVE, cpu_active, 0);
363         }
364         else /* we are reporting percents */
365         {
366                 for (i = 1; i < CPU_SUBMIT_ACTIVE; i++) {
367                         if (derives[i] == -1)
368                                 continue;
369                         cpu_total += derives[i];
370                         if (i != CPU_SUBMIT_IDLE)
371                                 cpu_active += derives[i];
372                 }
373                 derives[CPU_SUBMIT_ACTIVE] = cpu_active;
374
375                 numcpu++;
376                 for (i = 1; i < CPU_SUBMIT_MAX; i++) {
377                         if (derives[i] == -1) {
378                                 percents[i] = -1;
379                                 continue;
380                         }
381                         gauges[i] = ((gauge_t)derives[i] / (gauge_t)cpu_total) * 100.0;
382
383                         if (report_by_cpu)
384                         {
385                                 submit_value (cpu_num, i, 0, gauges[i]);
386                         } else if (gauges[i] != -1) {
387                                 percents[i] += gauges[i];
388                         }
389                 }
390         }
391 }
392
393 static int cpu_read (void)
394 {
395 #if PROCESSOR_CPU_LOAD_INFO || PROCESSOR_TEMPERATURE
396         int cpu;
397
398         kern_return_t status;
399
400 #if PROCESSOR_CPU_LOAD_INFO
401         derive_t                       cpu_active;
402         processor_cpu_load_info_data_t cpu_info;
403         mach_msg_type_number_t         cpu_info_len;
404 #endif
405 #if PROCESSOR_TEMPERATURE
406         processor_info_data_t          cpu_temp;
407         mach_msg_type_number_t         cpu_temp_len;
408 #endif
409
410         host_t cpu_host;
411         derive_t derives[CPU_SUBMIT_MAX];
412
413         for (cpu = 0; cpu < cpu_list_len; cpu++)
414         {
415 #if PROCESSOR_CPU_LOAD_INFO
416                 memset(derives, -1, sizeof(derives));
417                 cpu_host = 0;
418                 cpu_info_len = PROCESSOR_BASIC_INFO_COUNT;
419
420                 if ((status = processor_info (cpu_list[cpu],
421                                                 PROCESSOR_CPU_LOAD_INFO, &cpu_host,
422                                                 (processor_info_t) &cpu_info, &cpu_info_len)) != KERN_SUCCESS)
423                 {
424                         ERROR ("cpu plugin: processor_info failed with status %i", (int) status);
425                         continue;
426                 }
427
428                 if (cpu_info_len < CPU_STATE_MAX)
429                 {
430                         ERROR ("cpu plugin: processor_info returned only %i elements..", cpu_info_len);
431                         continue;
432                 }
433
434                 derives[CPU_USER] = (derive_t) cpu_info.cpu_ticks[CPU_STATE_USER];
435                 derives[CPU_NICE] = (derive_t) cpu_info.cpu_ticks[CPU_STATE_NICE];
436                 derives[CPU_SYSTEM] = (derive_t) cpu_info.cpu_ticks[CPU_STATE_SYSTEM];
437                 derives[CPU_IDLE] = (derive_t) cpu_info.cpu_ticks[CPU_STATE_IDLE];
438                 submit (cpu, derives);
439
440 #endif /* PROCESSOR_CPU_LOAD_INFO */
441 #if PROCESSOR_TEMPERATURE
442                 /*
443                  * Not all Apple computers do have this ability. To minimize
444                  * the messages sent to the syslog we do an exponential
445                  * stepback if `processor_info' fails. We still try ~once a day
446                  * though..
447                  */
448                 if (cpu_temp_retry_counter > 0)
449                 {
450                         cpu_temp_retry_counter--;
451                         continue;
452                 }
453
454                 cpu_temp_len = PROCESSOR_INFO_MAX;
455
456                 status = processor_info (cpu_list[cpu],
457                                 PROCESSOR_TEMPERATURE,
458                                 &cpu_host,
459                                 cpu_temp, &cpu_temp_len);
460                 if (status != KERN_SUCCESS)
461                 {
462                         ERROR ("cpu plugin: processor_info failed: %s",
463                                         mach_error_string (status));
464
465                         cpu_temp_retry_counter = cpu_temp_retry_step;
466                         cpu_temp_retry_step *= 2;
467                         if (cpu_temp_retry_step > cpu_temp_retry_max)
468                                 cpu_temp_retry_step = cpu_temp_retry_max;
469
470                         continue;
471                 }
472
473                 if (cpu_temp_len != 1)
474                 {
475                         DEBUG ("processor_info (PROCESSOR_TEMPERATURE) returned %i elements..?",
476                                         (int) cpu_temp_len);
477                         continue;
478                 }
479
480                 cpu_temp_retry_counter = 0;
481                 cpu_temp_retry_step    = 1;
482 #endif /* PROCESSOR_TEMPERATURE */
483         }
484         submit(CPU_SUBMIT_FLUSH, NULL);
485 /* #endif PROCESSOR_CPU_LOAD_INFO */
486
487 #elif defined(KERNEL_LINUX)
488         int cpu;
489         derive_t derives[CPU_SUBMIT_MAX];
490         FILE *fh;
491         char buf[1024];
492
493         char *fields[9];
494         int numfields;
495
496         if ((fh = fopen ("/proc/stat", "r")) == NULL)
497         {
498                 char errbuf[1024];
499                 ERROR ("cpu plugin: fopen (/proc/stat) failed: %s",
500                                 sstrerror (errno, errbuf, sizeof (errbuf)));
501                 return (-1);
502         }
503
504         while (fgets (buf, 1024, fh) != NULL)
505         {
506                 if (strncmp (buf, "cpu", 3))
507                         continue;
508                 if ((buf[3] < '0') || (buf[3] > '9'))
509                         continue;
510
511                 numfields = strsplit (buf, fields, 9);
512                 if (numfields < 5)
513                         continue;
514
515                 cpu = atoi (fields[0] + 3);
516                 memset(derives, -1, sizeof(derives));
517                 derives[CPU_SUBMIT_USER] = atoll(fields[1]);
518                 derives[CPU_SUBMIT_NICE] = atoll(fields[2]);
519                 derives[CPU_SUBMIT_SYSTEM] = atoll(fields[3]);
520                 derives[CPU_SUBMIT_IDLE] = atoll(fields[4]);
521
522                 if (numfields >= 8)
523                 {
524                         derives[CPU_SUBMIT_WAIT] = atoll(fields[5]);
525                         derives[CPU_SUBMIT_INTERRUPT] = atoll(fields[6]);
526                         derives[CPU_SUBMIT_SOFTIRQ] = atoll(fields[6]);
527
528                         if (numfields >= 9)
529                                 derives[CPU_SUBMIT_STEAL] = atoll(fields[8]);
530                 }
531                 submit(cpu, derives);
532         }
533         submit(CPU_SUBMIT_FLUSH, NULL);
534
535         fclose (fh);
536 /* #endif defined(KERNEL_LINUX) */
537
538 #elif defined(HAVE_LIBKSTAT)
539         int cpu;
540         derive_t derives[]user, syst, idle, wait;
541         static cpu_stat_t cs;
542
543         if (kc == NULL)
544                 return (-1);
545
546         for (cpu = 0; cpu < numcpu; cpu++)
547         {
548                 if (kstat_read (kc, ksp[cpu], &cs) == -1)
549                         continue; /* error message? */
550
551                 memset(derives, -1, sizeof(derives));
552                 derives[CPU_SUBMIT_IDLE] = cs.cpu_sysinfo.cpu[CPU_IDLE];
553                 derives[CPU_SUBMIT_USER] = cs.cpu_sysinfo.cpu[CPU_USER];
554                 derives[CPU_SUBMIT_SYSTEM] = cs.cpu_sysinfo.cpu[CPU_KERNEL];
555                 derives[CPU_SUBMIT_WAIT] = cs.cpu_sysinfo.cpu[CPU_WAIT];
556                 submit (ksp[cpu]->ks_instance, derives);
557         }
558         submit (CPU_SUBMIT_FLUSH, NULL);
559 /* #endif defined(HAVE_LIBKSTAT) */
560
561 #elif CAN_USE_SYSCTL
562         uint64_t cpuinfo[numcpu][CPUSTATES];
563         size_t cpuinfo_size;
564         int status;
565         int i;
566
567         if (numcpu < 1)
568         {
569                 ERROR ("cpu plugin: Could not determine number of "
570                                 "installed CPUs using sysctl(3).");
571                 return (-1);
572         }
573
574         memset (cpuinfo, 0, sizeof (cpuinfo));
575
576 #if defined(KERN_CPTIME2)
577         if (numcpu > 1) {
578                 for (i = 0; i < numcpu; i++) {
579                         int mib[] = {CTL_KERN, KERN_CPTIME2, i};
580
581                         cpuinfo_size = sizeof (cpuinfo[0]);
582
583                         status = sysctl (mib, STATIC_ARRAY_SIZE (mib),
584                                         cpuinfo[i], &cpuinfo_size, NULL, 0);
585                         if (status == -1) {
586                                 char errbuf[1024];
587                                 ERROR ("cpu plugin: sysctl failed: %s.",
588                                                 sstrerror (errno, errbuf, sizeof (errbuf)));
589                                 return (-1);
590                         }
591                 }
592         }
593         else
594 #endif /* defined(KERN_CPTIME2) */
595         {
596                 int mib[] = {CTL_KERN, KERN_CPTIME};
597                 long cpuinfo_tmp[CPUSTATES];
598
599                 cpuinfo_size = sizeof(cpuinfo_tmp);
600
601                 status = sysctl (mib, STATIC_ARRAY_SIZE (mib),
602                                         &cpuinfo_tmp, &cpuinfo_size, NULL, 0);
603                 if (status == -1)
604                 {
605                         char errbuf[1024];
606                         ERROR ("cpu plugin: sysctl failed: %s.",
607                                         sstrerror (errno, errbuf, sizeof (errbuf)));
608                         return (-1);
609                 }
610
611                 for(i = 0; i < CPUSTATES; i++) {
612                         cpuinfo[0][i] = cpuinfo_tmp[i];
613                 }
614         }
615
616         for (i = 0; i < numcpu; i++) {
617                 memset(derives, -1, sizeof(derives));
618                 derives[CPU_SUBMIT_USER] = cpuinfo[i][CP_USER];
619                 derives[CPU_SUBMIT_NICE] = cpuinfo[i][CP_NICE];
620                 derives[CPU_SUBMIT_SYSTEM] = cpuinfo[i][CP_SYS];
621                 derives[CPU_SUBMIT_IDLE] = cpuinfo[i][CP_IDLE];
622                 derives[CPU_SUBMIT_INTERRUPT] = cpuinfo[i][CP_INTR];
623                 submit(i, derives);
624         }
625         submit(CPU_SUBMIT_FLUSH, NULL);
626 /* #endif CAN_USE_SYSCTL */
627 #elif defined(HAVE_SYSCTLBYNAME) && defined(HAVE_SYSCTL_KERN_CP_TIMES)
628         long cpuinfo[maxcpu][CPUSTATES];
629         size_t cpuinfo_size;
630         int i;
631         derive_t derives[CPU_SUBMIT_MAX];
632
633         memset (cpuinfo, 0, sizeof (cpuinfo));
634
635         cpuinfo_size = sizeof (cpuinfo);
636         if (sysctlbyname("kern.cp_times", &cpuinfo, &cpuinfo_size, NULL, 0) < 0)
637         {
638                 char errbuf[1024];
639                 ERROR ("cpu plugin: sysctlbyname failed: %s.",
640                                 sstrerror (errno, errbuf, sizeof (errbuf)));
641                 return (-1);
642         }
643
644         for (i = 0; i < numcpu; i++) {
645                 memset(derives, -1, sizeof(derives));
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         }
652         submit(CPU_SUBMIT_FLUSH, NULL);
653
654 /* #endif HAVE_SYSCTL_KERN_CP_TIMES */
655 #elif defined(HAVE_SYSCTLBYNAME)
656         long cpuinfo[CPUSTATES];
657         size_t cpuinfo_size;
658         derive_t derives[CPU_SUBMIT_MAX];
659
660         cpuinfo_size = sizeof (cpuinfo);
661
662         if (sysctlbyname("kern.cp_time", &cpuinfo, &cpuinfo_size, NULL, 0) < 0)
663         {
664                 char errbuf[1024];
665                 ERROR ("cpu plugin: sysctlbyname failed: %s.",
666                                 sstrerror (errno, errbuf, sizeof (errbuf)));
667                 return (-1);
668         }
669
670         memset(derives, -1, sizeof(derives));
671         derives[CPU_SUBMIT_USER] = cpuinfo[CP_USER];
672         derives[CPU_SUBMIT_SYSTEM] = cpuinfo[CP_SYS];
673         derives[CPU_SUBMIT_NICE] = cpuinfo[CP_NICE];
674         derives[CPU_SUBMIT_IDLE] = cpuinfo[CP_IDLE];
675         derives[CPU_SUBMIT_INTERRUPT] = cpuinfo[CP_INTR];
676         submit(0, derives);
677         submit(CPU_SUBMIT_FLUSH, NULL);
678
679 /* #endif HAVE_SYSCTLBYNAME */
680
681 #elif defined(HAVE_LIBSTATGRAB)
682         sg_cpu_stats *cs;
683         derive_t derives[CPU_SUBMIT_MAX];
684         cs = sg_get_cpu_stats ();
685
686         if (cs == NULL)
687         {
688                 ERROR ("cpu plugin: sg_get_cpu_stats failed.");
689                 return (-1);
690         }
691
692         memset(derives, -1, sizeof(derives));
693         derives[CPU_SUBMIT_IDLE] = (derive_t) cs->idle;
694         derives[CPU_SUBMIT_NICE] = (derive_t) cs->nice;
695         derives[CPU_SUBMIT_SWAP] = (derive_t) cs->swap;
696         derives[CPU_SUBMIT_SYSTEM] = (derive_t) cs->kernel;
697         derives[CPU_SUBMIT_USER] = (derive_t) cs->user;
698         derives[CPU_SUBMIT_WAIT] = (derive_t) cs->iowait;
699 /* #endif HAVE_LIBSTATGRAB */
700
701 #elif defined(HAVE_PERFSTAT)
702         perfstat_id_t id;
703         int i, cpus;
704         derive_t derives[CPU_SUBMIT_MAX];
705
706         numcpu =  perfstat_cpu(NULL, NULL, sizeof(perfstat_cpu_t), 0);
707         if(numcpu == -1)
708         {
709                 char errbuf[1024];
710                 WARNING ("cpu plugin: perfstat_cpu: %s",
711                         sstrerror (errno, errbuf, sizeof (errbuf)));
712                 return (-1);
713         }
714
715         if (pnumcpu != numcpu || perfcpu == NULL)
716         {
717                 if (perfcpu != NULL)
718                         free(perfcpu);
719                 perfcpu = malloc(numcpu * sizeof(perfstat_cpu_t));
720         }
721         pnumcpu = numcpu;
722
723         id.name[0] = '\0';
724         if ((cpus = perfstat_cpu(&id, perfcpu, sizeof(perfstat_cpu_t), numcpu)) < 0)
725         {
726                 char errbuf[1024];
727                 WARNING ("cpu plugin: perfstat_cpu: %s",
728                         sstrerror (errno, errbuf, sizeof (errbuf)));
729                 return (-1);
730         }
731
732         for (i = 0; i < cpus; i++)
733         {
734                 memset(derives, -1, sizeof(derives));
735                 derives[CPU_SUBMIT_IDLE] = perfcpu[i].idle;
736                 derives[CPU_SUBMIT_SYSTEM] = perfcpu[i].sys;
737                 derives[CPU_SUBMIT_USER] = perfcpu[i].user;
738                 derives[CPU_SUBMIT_WAIT] = perfcpu[i].wait;
739         }
740         submit(CPU_SUBMIT_FLUSH, NULL);
741 #endif /* HAVE_PERFSTAT */
742
743         return (0);
744 }
745
746 void module_register (void)
747 {
748         plugin_register_init ("cpu", init);
749         plugin_register_config ("cpu", cpu_config, config_keys, config_keys_num);
750         plugin_register_read ("cpu", cpu_read);
751 } /* void module_register */