cpu plugin: Check if all required sysctl defines are present.
[collectd.git] / src / cpu.c
1 /**
2  * collectd - src/cpu.c
3  * Copyright (C) 2005-2007  Florian octo Forster
4  *
5  * This program is free software; you can redistribute it and/or modify it
6  * under the terms of the GNU General Public License as published by the
7  * Free Software Foundation; only version 2 of the License is applicable.
8  *
9  * This program is distributed in the hope that it will be useful, but
10  * WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License along
15  * with this program; if not, write to the Free Software Foundation, Inc.,
16  * 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
17  *
18  * Authors:
19  *   Florian octo Forster <octo at verplant.org>
20  **/
21
22 #include "collectd.h"
23 #include "common.h"
24 #include "plugin.h"
25
26 #ifdef HAVE_MACH_KERN_RETURN_H
27 # include <mach/kern_return.h>
28 #endif
29 #ifdef HAVE_MACH_MACH_INIT_H
30 # include <mach/mach_init.h>
31 #endif
32 #ifdef HAVE_MACH_HOST_PRIV_H
33 # include <mach/host_priv.h>
34 #endif
35 #if HAVE_MACH_MACH_ERROR_H
36 #  include <mach/mach_error.h>
37 #endif
38 #ifdef HAVE_MACH_PROCESSOR_INFO_H
39 # include <mach/processor_info.h>
40 #endif
41 #ifdef HAVE_MACH_PROCESSOR_H
42 # include <mach/processor.h>
43 #endif
44 #ifdef HAVE_MACH_VM_MAP_H
45 # include <mach/vm_map.h>
46 #endif
47
48 #ifdef HAVE_LIBKSTAT
49 # include <sys/sysinfo.h>
50 #endif /* HAVE_LIBKSTAT */
51
52 #if (defined(HAVE_SYSCTL) && HAVE_SYSCTL) \
53         || (defined(HAVE_SYSCTLBYNAME) && HAVE_SYSCTLBYNAME)
54 # ifdef HAVE_SYS_SYSCTL_H
55 #  include <sys/sysctl.h>
56 # endif
57
58 # ifdef HAVE_SYS_DKSTAT_H
59 #  include <sys/dkstat.h>
60 # endif
61
62 # if !defined(CP_USER) || !defined(CP_NICE) || !defined(CP_SYS) || !defined(CP_INTR) || !defined(CP_IDLE) || !defined(CPUSTATES)
63 #  define CP_USER   0
64 #  define CP_NICE   1
65 #  define CP_SYS    2
66 #  define CP_INTR   3
67 #  define CP_IDLE   4
68 #  define CPUSTATES 5
69 # endif
70 #endif /* HAVE_SYSCTL || HAVE_SYSCTLBYNAME */
71
72 #if HAVE_SYSCTL
73 # if defined(CTL_HW) && defined(HW_NCPU) \
74         && defined(CTL_KERN) && defined(KERN_CPTIME) && defined(CPUSTATES)
75 #  define CAN_USE_SYSCTL 1
76 # else
77 #  define CAN_USE_SYSCTL 0
78 # endif
79 #else
80 # define CAN_USE_SYSCTL 0
81 #endif
82
83 #if HAVE_STATGRAB_H
84 # include <statgrab.h>
85 #endif
86
87 #if !PROCESSOR_CPU_LOAD_INFO && !KERNEL_LINUX && !HAVE_LIBKSTAT \
88         && !CAN_USE_SYSCTL && !HAVE_SYSCTLBYNAME && !HAVE_LIBSTATGRAB
89 # error "No applicable input method."
90 #endif
91
92 #ifdef PROCESSOR_CPU_LOAD_INFO
93 static mach_port_t port_host;
94 static processor_port_array_t cpu_list;
95 static mach_msg_type_number_t cpu_list_len;
96
97 #if PROCESSOR_TEMPERATURE
98 static int cpu_temp_retry_counter = 0;
99 static int cpu_temp_retry_step    = 1;
100 static int cpu_temp_retry_max     = 1;
101 #endif /* PROCESSOR_TEMPERATURE */
102 /* #endif PROCESSOR_CPU_LOAD_INFO */
103
104 #elif defined(KERNEL_LINUX)
105 /* no variables needed */
106 /* #endif KERNEL_LINUX */
107
108 #elif defined(HAVE_LIBKSTAT)
109 /* colleague tells me that Sun doesn't sell systems with more than 100 or so CPUs.. */
110 # define MAX_NUMCPU 256
111 extern kstat_ctl_t *kc;
112 static kstat_t *ksp[MAX_NUMCPU];
113 static int numcpu;
114 /* #endif HAVE_LIBKSTAT */
115
116 #elif CAN_USE_SYSCTL
117 static int numcpu;
118 /* #endif CAN_USE_SYSCTL */
119
120 #elif defined(HAVE_SYSCTLBYNAME)
121 static int numcpu;
122 /* #endif HAVE_SYSCTLBYNAME */
123
124 #elif defined(HAVE_LIBSTATGRAB)
125 /* no variables needed */
126 #endif /* HAVE_LIBSTATGRAB */
127
128 static int init (void)
129 {
130 #if PROCESSOR_CPU_LOAD_INFO || PROCESSOR_TEMPERATURE
131         kern_return_t status;
132
133         port_host = mach_host_self ();
134
135         /* FIXME: Free `cpu_list' if it's not NULL */
136         if ((status = host_processors (port_host, &cpu_list, &cpu_list_len)) != KERN_SUCCESS)
137         {
138                 ERROR ("cpu plugin: host_processors returned %i", (int) status);
139                 cpu_list_len = 0;
140                 return (-1);
141         }
142
143         DEBUG ("host_processors returned %i %s", (int) cpu_list_len, cpu_list_len == 1 ? "processor" : "processors");
144         INFO ("cpu plugin: Found %i processor%s.", (int) cpu_list_len, cpu_list_len == 1 ? "" : "s");
145
146         cpu_temp_retry_max = 86400 / interval_g;
147 /* #endif PROCESSOR_CPU_LOAD_INFO */
148
149 #elif defined(HAVE_LIBKSTAT)
150         kstat_t *ksp_chain;
151
152         numcpu = 0;
153
154         if (kc == NULL)
155                 return (-1);
156
157         /* Solaris doesn't count linear.. *sigh* */
158         for (numcpu = 0, ksp_chain = kc->kc_chain;
159                         (numcpu < MAX_NUMCPU) && (ksp_chain != NULL);
160                         ksp_chain = ksp_chain->ks_next)
161                 if (strncmp (ksp_chain->ks_module, "cpu_stat", 8) == 0)
162                         ksp[numcpu++] = ksp_chain;
163 /* #endif HAVE_LIBKSTAT */
164
165 #elif CAN_USE_SYSCTL
166         size_t numcpu_size;
167         int mib[2] = {CTL_HW, HW_NCPU};
168         int status;
169
170         numcpu = 0;
171         numcpu_size = sizeof (numcpu);
172
173         status = sysctl (mib, STATIC_ARRAY_SIZE (mib),
174                         &numcpu, &numcpu_size, NULL, 0);
175         if (status == -1)
176         {
177                 char errbuf[1024];
178                 WARNING ("cpu plugin: sysctl: %s",
179                                 sstrerror (errno, errbuf, sizeof (errbuf)));
180                 return (-1);
181         }
182 /* #endif CAN_USE_SYSCTL */
183
184 #elif defined (HAVE_SYSCTLBYNAME)
185         size_t numcpu_size;
186
187         numcpu_size = sizeof (numcpu);
188
189         if (sysctlbyname ("hw.ncpu", &numcpu, &numcpu_size, NULL, 0) < 0)
190         {
191                 char errbuf[1024];
192                 WARNING ("cpu plugin: sysctlbyname: %s",
193                                 sstrerror (errno, errbuf, sizeof (errbuf)));
194                 return (-1);
195         }
196
197         if (numcpu != 1)
198                 NOTICE ("cpu: Only one processor supported when using `sysctlbyname' (found %i)", numcpu);
199 /* #endif HAVE_SYSCTLBYNAME */
200
201 #elif defined(HAVE_LIBSTATGRAB)
202         /* nothing to initialize */
203 #endif /* HAVE_LIBSTATGRAB */
204
205         return (0);
206 } /* int init */
207
208 static void submit (int cpu_num, const char *type_instance, counter_t value)
209 {
210         value_t values[1];
211         value_list_t vl = VALUE_LIST_INIT;
212
213         values[0].counter = value;
214
215         vl.values = values;
216         vl.values_len = 1;
217         sstrncpy (vl.host, hostname_g, sizeof (vl.host));
218         sstrncpy (vl.plugin, "cpu", sizeof (vl.plugin));
219         ssnprintf (vl.plugin_instance, sizeof (vl.type_instance),
220                         "%i", cpu_num);
221         sstrncpy (vl.type, "cpu", sizeof (vl.type));
222         sstrncpy (vl.type_instance, type_instance, sizeof (vl.type_instance));
223
224         plugin_dispatch_values (&vl);
225 }
226
227 static int cpu_read (void)
228 {
229 #if PROCESSOR_CPU_LOAD_INFO || PROCESSOR_TEMPERATURE
230         int cpu;
231
232         kern_return_t status;
233         
234 #if PROCESSOR_CPU_LOAD_INFO
235         processor_cpu_load_info_data_t cpu_info;
236         mach_msg_type_number_t         cpu_info_len;
237 #endif
238 #if PROCESSOR_TEMPERATURE
239         processor_info_data_t          cpu_temp;
240         mach_msg_type_number_t         cpu_temp_len;
241 #endif
242
243         host_t cpu_host;
244
245         for (cpu = 0; cpu < cpu_list_len; cpu++)
246         {
247 #if PROCESSOR_CPU_LOAD_INFO
248                 cpu_host = 0;
249                 cpu_info_len = PROCESSOR_BASIC_INFO_COUNT;
250
251                 if ((status = processor_info (cpu_list[cpu],
252                                                 PROCESSOR_CPU_LOAD_INFO, &cpu_host,
253                                                 (processor_info_t) &cpu_info, &cpu_info_len)) != KERN_SUCCESS)
254                 {
255                         ERROR ("cpu plugin: processor_info failed with status %i", (int) status);
256                         continue;
257                 }
258
259                 if (cpu_info_len < CPU_STATE_MAX)
260                 {
261                         ERROR ("cpu plugin: processor_info returned only %i elements..", cpu_info_len);
262                         continue;
263                 }
264
265                 submit (cpu, "user", (counter_t) cpu_info.cpu_ticks[CPU_STATE_USER]);
266                 submit (cpu, "nice", (counter_t) cpu_info.cpu_ticks[CPU_STATE_NICE]);
267                 submit (cpu, "system", (counter_t) cpu_info.cpu_ticks[CPU_STATE_SYSTEM]);
268                 submit (cpu, "idle", (counter_t) cpu_info.cpu_ticks[CPU_STATE_IDLE]);
269 #endif /* PROCESSOR_CPU_LOAD_INFO */
270 #if PROCESSOR_TEMPERATURE
271                 /*
272                  * Not all Apple computers do have this ability. To minimize
273                  * the messages sent to the syslog we do an exponential
274                  * stepback if `processor_info' fails. We still try ~once a day
275                  * though..
276                  */
277                 if (cpu_temp_retry_counter > 0)
278                 {
279                         cpu_temp_retry_counter--;
280                         continue;
281                 }
282
283                 cpu_temp_len = PROCESSOR_INFO_MAX;
284
285                 status = processor_info (cpu_list[cpu],
286                                 PROCESSOR_TEMPERATURE,
287                                 &cpu_host,
288                                 cpu_temp, &cpu_temp_len);
289                 if (status != KERN_SUCCESS)
290                 {
291                         ERROR ("cpu plugin: processor_info failed: %s",
292                                         mach_error_string (status));
293
294                         cpu_temp_retry_counter = cpu_temp_retry_step;
295                         cpu_temp_retry_step *= 2;
296                         if (cpu_temp_retry_step > cpu_temp_retry_max)
297                                 cpu_temp_retry_step = cpu_temp_retry_max;
298
299                         continue;
300                 }
301
302                 if (cpu_temp_len != 1)
303                 {
304                         DEBUG ("processor_info (PROCESSOR_TEMPERATURE) returned %i elements..?",
305                                         (int) cpu_temp_len);
306                         continue;
307                 }
308
309                 cpu_temp_retry_counter = 0;
310                 cpu_temp_retry_step    = 1;
311
312                 DEBUG ("cpu_temp = %i", (int) cpu_temp);
313 #endif /* PROCESSOR_TEMPERATURE */
314         }
315 /* #endif PROCESSOR_CPU_LOAD_INFO */
316
317 #elif defined(KERNEL_LINUX)
318         int cpu;
319         counter_t user, nice, syst, idle;
320         counter_t wait, intr, sitr; /* sitr == soft interrupt */
321         FILE *fh;
322         char buf[1024];
323
324         char *fields[9];
325         int numfields;
326
327         if ((fh = fopen ("/proc/stat", "r")) == NULL)
328         {
329                 char errbuf[1024];
330                 ERROR ("cpu plugin: fopen (/proc/stat) failed: %s",
331                                 sstrerror (errno, errbuf, sizeof (errbuf)));
332                 return (-1);
333         }
334
335         while (fgets (buf, 1024, fh) != NULL)
336         {
337                 if (strncmp (buf, "cpu", 3))
338                         continue;
339                 if ((buf[3] < '0') || (buf[3] > '9'))
340                         continue;
341
342                 numfields = strsplit (buf, fields, 9);
343                 if (numfields < 5)
344                         continue;
345
346                 cpu = atoi (fields[0] + 3);
347                 user = atoll (fields[1]);
348                 nice = atoll (fields[2]);
349                 syst = atoll (fields[3]);
350                 idle = atoll (fields[4]);
351
352                 submit (cpu, "user", user);
353                 submit (cpu, "nice", nice);
354                 submit (cpu, "system", syst);
355                 submit (cpu, "idle", idle);
356
357                 if (numfields >= 8)
358                 {
359                         wait = atoll (fields[5]);
360                         intr = atoll (fields[6]);
361                         sitr = atoll (fields[7]);
362
363                         submit (cpu, "wait", wait);
364                         submit (cpu, "interrupt", intr);
365                         submit (cpu, "softirq", sitr);
366
367                         if (numfields >= 9)
368                                 submit (cpu, "steal", atoll (fields[8]));
369                 }
370         }
371
372         fclose (fh);
373 /* #endif defined(KERNEL_LINUX) */
374
375 #elif defined(HAVE_LIBKSTAT)
376         int cpu;
377         counter_t user, syst, idle, wait;
378         static cpu_stat_t cs;
379
380         if (kc == NULL)
381                 return (-1);
382
383         for (cpu = 0; cpu < numcpu; cpu++)
384         {
385                 if (kstat_read (kc, ksp[cpu], &cs) == -1)
386                         continue; /* error message? */
387
388                 idle = (counter_t) cs.cpu_sysinfo.cpu[CPU_IDLE];
389                 user = (counter_t) cs.cpu_sysinfo.cpu[CPU_USER];
390                 syst = (counter_t) cs.cpu_sysinfo.cpu[CPU_KERNEL];
391                 wait = (counter_t) cs.cpu_sysinfo.cpu[CPU_WAIT];
392
393                 submit (ksp[cpu]->ks_instance, "user", user);
394                 submit (ksp[cpu]->ks_instance, "system", syst);
395                 submit (ksp[cpu]->ks_instance, "idle", idle);
396                 submit (ksp[cpu]->ks_instance, "wait", wait);
397         }
398 /* #endif defined(HAVE_LIBKSTAT) */
399
400 #elif CAN_USE_SYSCTL
401         uint64_t cpuinfo[numcpu][CPUSTATES];
402         size_t cpuinfo_size;
403         int status;
404         int i;
405
406         if (numcpu < 1)
407         {
408                 ERROR ("cpu plugin: Could not determine number of "
409                                 "installed CPUs using sysctl(3).");
410                 return (-1);
411         }
412
413         memset (cpuinfo, 0, sizeof (cpuinfo));
414
415 #if defined(KERN_CPTIME2)
416         if (numcpu > 1) {
417                 for (i = 0; i < numcpu; i++) {
418                         int mib[] = {CTL_KERN, KERN_CPTIME2, i};
419
420                         cpuinfo_size = sizeof (cpuinfo[0]);
421
422                         status = sysctl (mib, STATIC_ARRAY_SIZE (mib),
423                                         cpuinfo[i], &cpuinfo_size, NULL, 0);
424                         if (status == -1) {
425                                 char errbuf[1024];
426                                 ERROR ("cpu plugin: sysctl failed: %s.",
427                                                 sstrerror (errno, errbuf, sizeof (errbuf)));
428                                 return (-1);
429                         }
430                 }
431         }
432         else
433 #endif /* defined(KERN_CPTIME2) */
434         {
435                 int mib[] = {CTL_KERN, KERN_CPTIME};
436                 long cpuinfo_tmp[CPUSTATES];
437
438                 cpuinfo_size = sizeof(cpuinfo_tmp);
439
440                 status = sysctl (mib, STATIC_ARRAY_SIZE (mib),
441                                         &cpuinfo_tmp, &cpuinfo_size, NULL, 0);
442                 if (status == -1)
443                 {
444                         char errbuf[1024];
445                         ERROR ("cpu plugin: sysctl failed: %s.",
446                                         sstrerror (errno, errbuf, sizeof (errbuf)));
447                         return (-1);
448                 }
449
450                 for(i = 0; i < CPUSTATES; i++) {
451                         cpuinfo[0][i] = cpuinfo_tmp[i];
452                 }
453         }
454
455         for (i = 0; i < numcpu; i++) {
456                 submit (i, "user",      cpuinfo[i][CP_USER]);
457                 submit (i, "nice",      cpuinfo[i][CP_NICE]);
458                 submit (i, "system",    cpuinfo[i][CP_SYS]);
459                 submit (i, "idle",      cpuinfo[i][CP_IDLE]);
460                 submit (i, "interrupt", cpuinfo[i][CP_INTR]);
461         }
462 /* #endif CAN_USE_SYSCTL */
463
464 #elif defined(HAVE_SYSCTLBYNAME)
465         long cpuinfo[CPUSTATES];
466         size_t cpuinfo_size;
467
468         cpuinfo_size = sizeof (cpuinfo);
469
470         if (sysctlbyname("kern.cp_time", &cpuinfo, &cpuinfo_size, NULL, 0) < 0)
471         {
472                 char errbuf[1024];
473                 ERROR ("cpu plugin: sysctlbyname failed: %s.",
474                                 sstrerror (errno, errbuf, sizeof (errbuf)));
475                 return (-1);
476         }
477
478         submit (0, "user", cpuinfo[CP_USER]);
479         submit (0, "nice", cpuinfo[CP_NICE]);
480         submit (0, "system", cpuinfo[CP_SYS]);
481         submit (0, "idle", cpuinfo[CP_IDLE]);
482         submit (0, "interrupt", cpuinfo[CP_INTR]);
483 /* #endif HAVE_SYSCTLBYNAME */
484
485 #elif defined(HAVE_LIBSTATGRAB)
486         sg_cpu_stats *cs;
487         cs = sg_get_cpu_stats ();
488
489         if (cs == NULL)
490         {
491                 ERROR ("cpu plugin: sg_get_cpu_stats failed.");
492                 return (-1);
493         }
494
495         submit (0, "idle",   (counter_t) cs->idle);
496         submit (0, "nice",   (counter_t) cs->nice);
497         submit (0, "swap",   (counter_t) cs->swap);
498         submit (0, "system", (counter_t) cs->kernel);
499         submit (0, "user",   (counter_t) cs->user);
500         submit (0, "wait",   (counter_t) cs->iowait);
501 #endif /* HAVE_LIBSTATGRAB */
502
503         return (0);
504 }
505
506 void module_register (void)
507 {
508         plugin_register_init ("cpu", init);
509         plugin_register_read ("cpu", cpu_read);
510 } /* void module_register */