Merge branch 'sb/iptables'
[collectd.git] / src / cpu.c
1 /**
2  * collectd - src/cpu.c
3  * Copyright (C) 2005,2006  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; either version 2 of the License, or (at your
8  * option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful, but
11  * WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License along
16  * with this program; if not, write to the Free Software Foundation, Inc.,
17  * 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
18  *
19  * Authors:
20  *   Florian octo Forster <octo at verplant.org>
21  **/
22
23 #include "collectd.h"
24 #include "common.h"
25 #include "plugin.h"
26 #include "utils_debug.h"
27
28 #define MODULE_NAME "cpu"
29
30 #ifdef HAVE_MACH_KERN_RETURN_H
31 # include <mach/kern_return.h>
32 #endif
33 #ifdef HAVE_MACH_MACH_INIT_H
34 # include <mach/mach_init.h>
35 #endif
36 #ifdef HAVE_MACH_HOST_PRIV_H
37 # include <mach/host_priv.h>
38 #endif
39 #if HAVE_MACH_MACH_ERROR_H
40 #  include <mach/mach_error.h>
41 #endif
42 #ifdef HAVE_MACH_PROCESSOR_INFO_H
43 # include <mach/processor_info.h>
44 #endif
45 #ifdef HAVE_MACH_PROCESSOR_H
46 # include <mach/processor.h>
47 #endif
48 #ifdef HAVE_MACH_VM_MAP_H
49 # include <mach/vm_map.h>
50 #endif
51
52 #ifdef HAVE_LIBKSTAT
53 # include <sys/sysinfo.h>
54 #endif /* HAVE_LIBKSTAT */
55
56 #ifdef HAVE_SYSCTLBYNAME
57 # ifdef HAVE_SYS_SYSCTL_H
58 #  include <sys/sysctl.h>
59 # endif
60
61 # ifdef HAVE_SYS_DKSTAT_H
62 #  include <sys/dkstat.h>
63 # endif
64
65 # if !defined(CP_USER) || !defined(CP_NICE) || !defined(CP_SYS) || !defined(CP_INTR) || !defined(CP_IDLE) || !defined(CPUSTATES)
66 #  define CP_USER   0
67 #  define CP_NICE   1
68 #  define CP_SYS    2
69 #  define CP_INTR   3
70 #  define CP_IDLE   4
71 #  define CPUSTATES 5
72 # endif
73 #endif /* HAVE_SYSCTLBYNAME */
74
75 #if defined(PROCESSOR_CPU_LOAD_INFO) || defined(KERNEL_LINUX) || defined(HAVE_LIBKSTAT) || defined(HAVE_SYSCTLBYNAME)
76 # define CPU_HAVE_READ 1
77 #else
78 # define CPU_HAVE_READ 0
79 #endif
80
81 #ifdef PROCESSOR_CPU_LOAD_INFO
82 static mach_port_t port_host;
83 static processor_port_array_t cpu_list;
84 static mach_msg_type_number_t cpu_list_len;
85
86 #if PROCESSOR_TEMPERATURE
87 static int cpu_temp_retry_counter = 0;
88 static int cpu_temp_retry_step    = 1;
89 static int cpu_temp_retry_max     = 1;
90 #endif /* PROCESSOR_TEMPERATURE */
91 /* #endif PROCESSOR_CPU_LOAD_INFO */
92
93 #elif defined(KERNEL_LINUX)
94 /* no variables needed */
95 /* #endif KERNEL_LINUX */
96
97 #elif defined(HAVE_LIBKSTAT)
98 /* colleague tells me that Sun doesn't sell systems with more than 100 or so CPUs.. */
99 # define MAX_NUMCPU 256
100 extern kstat_ctl_t *kc;
101 static kstat_t *ksp[MAX_NUMCPU];
102 static int numcpu;
103 /* #endif HAVE_LIBKSTAT */
104
105 #elif defined(HAVE_SYSCTLBYNAME)
106 static int numcpu;
107 #endif /* HAVE_SYSCTLBYNAME */
108
109 static char *cpu_filename = "cpu-%s.rrd";
110
111 static char *ds_def[] =
112 {
113         "DS:user:COUNTER:"COLLECTD_HEARTBEAT":0:U",
114         "DS:nice:COUNTER:"COLLECTD_HEARTBEAT":0:U",
115         "DS:syst:COUNTER:"COLLECTD_HEARTBEAT":0:U",
116         "DS:idle:COUNTER:"COLLECTD_HEARTBEAT":0:U",
117         "DS:wait:COUNTER:"COLLECTD_HEARTBEAT":0:U",
118         NULL
119 };
120 static int ds_num = 5;
121
122 static void cpu_init (void)
123 {
124 #if PROCESSOR_CPU_LOAD_INFO || PROCESSOR_TEMPERATURE
125         kern_return_t status;
126         int collectd_step;
127
128         port_host = mach_host_self ();
129
130         /* FIXME: Free `cpu_list' if it's not NULL */
131         if ((status = host_processors (port_host, &cpu_list, &cpu_list_len)) != KERN_SUCCESS)
132         {
133                 syslog (LOG_ERR, "cpu-plugin: host_processors returned %i\n", (int) status);
134                 cpu_list_len = 0;
135                 return;
136         }
137
138         DBG ("host_processors returned %i %s", (int) cpu_list_len, cpu_list_len == 1 ? "processor" : "processors");
139         syslog (LOG_INFO, "cpu-plugin: Found %i processor%s.", (int) cpu_list_len, cpu_list_len == 1 ? "" : "s");
140
141         collectd_step = atoi (COLLECTD_STEP);
142         if ((collectd_step > 0) && (collectd_step <= 86400))
143                 cpu_temp_retry_max = 86400 / collectd_step;
144                 
145 /* #endif PROCESSOR_CPU_LOAD_INFO */
146
147 #elif defined(HAVE_LIBKSTAT)
148         kstat_t *ksp_chain;
149
150         numcpu = 0;
151
152         if (kc == NULL)
153                 return;
154
155         /* Solaris doesn't count linear.. *sigh* */
156         for (numcpu = 0, ksp_chain = kc->kc_chain;
157                         (numcpu < MAX_NUMCPU) && (ksp_chain != NULL);
158                         ksp_chain = ksp_chain->ks_next)
159                 if (strncmp (ksp_chain->ks_module, "cpu_stat", 8) == 0)
160                         ksp[numcpu++] = ksp_chain;
161 /* #endif HAVE_LIBKSTAT */
162
163 #elif defined (HAVE_SYSCTLBYNAME)
164         size_t numcpu_size;
165
166         numcpu_size = sizeof (numcpu);
167
168         if (sysctlbyname ("hw.ncpu", &numcpu, &numcpu_size, NULL, 0) < 0)
169         {
170                 syslog (LOG_WARNING, "cpu: sysctlbyname: %s", strerror (errno));
171                 return;
172         }
173
174         if (numcpu != 1)
175                 syslog (LOG_NOTICE, "cpu: Only one processor supported when using `sysctlbyname' (found %i)", numcpu);
176 #endif
177
178         return;
179 }
180
181 static void cpu_write (char *host, char *inst, char *val)
182 {
183         char file[512];
184         int status;
185
186         status = snprintf (file, 512, cpu_filename, inst);
187         if (status < 1)
188                 return;
189         else if (status >= 512)
190                 return;
191
192         rrd_update_file (host, file, val, ds_def, ds_num);
193 }
194
195 #if CPU_HAVE_READ
196 #define BUFSIZE 512
197 static void cpu_submit (int cpu_num, unsigned long long user,
198                 unsigned long long nice, unsigned long long syst,
199                 unsigned long long idle, unsigned long long wait)
200 {
201         char buf[BUFSIZE];
202         char cpu[16];
203
204         if (snprintf (buf, BUFSIZE, "%u:%llu:%llu:%llu:%llu:%llu", (unsigned int) curtime,
205                                 user, nice, syst, idle, wait) >= BUFSIZE)
206                 return;
207         snprintf (cpu, 16, "%i", cpu_num);
208
209         plugin_submit (MODULE_NAME, cpu, buf);
210 }
211 #undef BUFSIZE
212
213 static void cpu_read (void)
214 {
215 #if PROCESSOR_CPU_LOAD_INFO || PROCESSOR_TEMPERATURE
216         int cpu;
217
218         kern_return_t status;
219         
220 #if PROCESSOR_CPU_LOAD_INFO
221         processor_cpu_load_info_data_t cpu_info;
222         mach_msg_type_number_t         cpu_info_len;
223 #endif
224 #if PROCESSOR_TEMPERATURE
225         processor_info_data_t          cpu_temp;
226         mach_msg_type_number_t         cpu_temp_len;
227 #endif
228
229         host_t cpu_host;
230
231         for (cpu = 0; cpu < cpu_list_len; cpu++)
232         {
233 #if PROCESSOR_CPU_LOAD_INFO
234                 cpu_host = 0;
235                 cpu_info_len = PROCESSOR_BASIC_INFO_COUNT;
236
237                 if ((status = processor_info (cpu_list[cpu],
238                                                 PROCESSOR_CPU_LOAD_INFO, &cpu_host,
239                                                 (processor_info_t) &cpu_info, &cpu_info_len)) != KERN_SUCCESS)
240                 {
241                         syslog (LOG_ERR, "cpu-plugin: processor_info failed with status %i\n", (int) status);
242                         continue;
243                 }
244
245                 if (cpu_info_len < CPU_STATE_MAX)
246                 {
247                         syslog (LOG_ERR, "cpu-plugin: processor_info returned only %i elements..\n", cpu_info_len);
248                         continue;
249                 }
250
251                 cpu_submit (cpu, cpu_info.cpu_ticks[CPU_STATE_USER],
252                                 cpu_info.cpu_ticks[CPU_STATE_NICE],
253                                 cpu_info.cpu_ticks[CPU_STATE_SYSTEM],
254                                 cpu_info.cpu_ticks[CPU_STATE_IDLE],
255                                 0ULL);
256 #endif /* PROCESSOR_CPU_LOAD_INFO */
257 #if PROCESSOR_TEMPERATURE
258                 /*
259                  * Not all Apple computers do have this ability. To minimize
260                  * the messages sent to the syslog we do an exponential
261                  * stepback if `processor_info' fails. We still try ~once a day
262                  * though..
263                  */
264                 if (cpu_temp_retry_counter > 0)
265                 {
266                         cpu_temp_retry_counter--;
267                         continue;
268                 }
269
270                 cpu_temp_len = PROCESSOR_INFO_MAX;
271
272                 status = processor_info (cpu_list[cpu],
273                                 PROCESSOR_TEMPERATURE,
274                                 &cpu_host,
275                                 cpu_temp, &cpu_temp_len);
276                 if (status != KERN_SUCCESS)
277                 {
278                         syslog (LOG_ERR, "cpu-plugin: processor_info failed: %s",
279                                         mach_error_string (status));
280
281                         cpu_temp_retry_counter = cpu_temp_retry_step;
282                         cpu_temp_retry_step *= 2;
283                         if (cpu_temp_retry_step > cpu_temp_retry_max)
284                                 cpu_temp_retry_step = cpu_temp_retry_max;
285
286                         continue;
287                 }
288
289                 if (cpu_temp_len != 1)
290                 {
291                         DBG ("processor_info (PROCESSOR_TEMPERATURE) returned %i elements..?",
292                                         (int) cpu_temp_len);
293                         continue;
294                 }
295
296                 cpu_temp_retry_counter = 0;
297                 cpu_temp_retry_step    = 1;
298
299                 DBG ("cpu_temp = %i", (int) cpu_temp);
300 #endif /* PROCESSOR_TEMPERATURE */
301         }
302 /* #endif PROCESSOR_CPU_LOAD_INFO */
303
304 #elif defined(KERNEL_LINUX)
305 # define BUFSIZE 1024
306         int cpu;
307         unsigned long long user, nice, syst, idle;
308         unsigned long long wait, intr, sitr; /* sitr == soft interrupt */
309         FILE *fh;
310         char buf[BUFSIZE];
311
312         char *fields[9];
313         int numfields;
314
315         static complain_t complain_obj;
316
317         if ((fh = fopen ("/proc/stat", "r")) == NULL)
318         {
319                 plugin_complain (LOG_ERR, &complain_obj, "cpu plugin: "
320                                 "fopen (/proc/stat) failed: %s",
321                                 strerror (errno));
322                 return;
323         }
324
325         plugin_relief (LOG_NOTICE, &complain_obj, "cpu plugin: "
326                         "fopen (/proc/stat) succeeded.");
327
328         while (fgets (buf, BUFSIZE, fh) != NULL)
329         {
330                 if (strncmp (buf, "cpu", 3))
331                         continue;
332                 if ((buf[3] < '0') || (buf[3] > '9'))
333                         continue;
334
335                 numfields = strsplit (buf, fields, 9);
336                 if (numfields < 5)
337                         continue;
338
339                 cpu = atoi (fields[0] + 3);
340                 user = atoll (fields[1]);
341                 nice = atoll (fields[2]);
342                 syst = atoll (fields[3]);
343                 idle = atoll (fields[4]);
344
345                 if (numfields >= 8)
346                 {
347                         wait = atoll (fields[5]);
348                         intr = atoll (fields[6]);
349                         sitr = atoll (fields[7]);
350
351                         /* I doubt anyone cares about the time spent in
352                          * interrupt handlers.. */
353                         syst += intr + sitr;
354                 }
355                 else
356                 {
357                         wait = 0LL;
358                 }
359
360                 cpu_submit (cpu, user, nice, syst, idle, wait);
361         }
362
363         fclose (fh);
364 #undef BUFSIZE
365 /* #endif defined(KERNEL_LINUX) */
366
367 #elif defined(HAVE_LIBKSTAT)
368         int cpu;
369         unsigned long long user, syst, idle, wait;
370         static cpu_stat_t cs;
371
372         if (kc == NULL)
373                 return;
374
375         for (cpu = 0; cpu < numcpu; cpu++)
376         {
377                 if (kstat_read (kc, ksp[cpu], &cs) == -1)
378                         continue; /* error message? */
379
380                 idle = (unsigned long long) cs.cpu_sysinfo.cpu[CPU_IDLE];
381                 user = (unsigned long long) cs.cpu_sysinfo.cpu[CPU_USER];
382                 syst = (unsigned long long) cs.cpu_sysinfo.cpu[CPU_KERNEL];
383                 wait = (unsigned long long) cs.cpu_sysinfo.cpu[CPU_WAIT];
384
385                 cpu_submit (ksp[cpu]->ks_instance,
386                                 user, 0LL, syst, idle, wait);
387         }
388 /* #endif defined(HAVE_LIBKSTAT) */
389
390 #elif defined(HAVE_SYSCTLBYNAME)
391         long cpuinfo[CPUSTATES];
392         size_t cpuinfo_size;
393
394         static complain_t complain_obj;
395
396         cpuinfo_size = sizeof (cpuinfo);
397
398         if (sysctlbyname("kern.cp_time", &cpuinfo, &cpuinfo_size, NULL, 0) < 0)
399         {
400                 plugin_complain (LOG_ERR, &complain_obj, "cpu plugin: "
401                                 "sysctlbyname failed: %s.",
402                                 strerror (errno));
403                 return;
404         }
405
406         plugin_relief (LOG_NOTICE, &complain_obj, "cpu plugin: "
407                         "sysctlbyname succeeded.");
408
409         cpuinfo[CP_SYS] += cpuinfo[CP_INTR];
410
411         /* FIXME: Instance is always `0' */
412         cpu_submit (0, cpuinfo[CP_USER], cpuinfo[CP_NICE], cpuinfo[CP_SYS], cpuinfo[CP_IDLE], 0LL);
413 #endif
414
415         return;
416 }
417 #else
418 # define cpu_read NULL
419 #endif /* CPU_HAVE_READ */
420
421 void module_register (void)
422 {
423         plugin_register (MODULE_NAME, cpu_init, cpu_read, cpu_write);
424 }
425
426 #undef MODULE_NAME