Replace all calls to `strerror' with `sstrerror'
[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 #ifdef HAVE_SYSCTLBYNAME
53 # ifdef HAVE_SYS_SYSCTL_H
54 #  include <sys/sysctl.h>
55 # endif
56
57 # ifdef HAVE_SYS_DKSTAT_H
58 #  include <sys/dkstat.h>
59 # endif
60
61 # if !defined(CP_USER) || !defined(CP_NICE) || !defined(CP_SYS) || !defined(CP_INTR) || !defined(CP_IDLE) || !defined(CPUSTATES)
62 #  define CP_USER   0
63 #  define CP_NICE   1
64 #  define CP_SYS    2
65 #  define CP_INTR   3
66 #  define CP_IDLE   4
67 #  define CPUSTATES 5
68 # endif
69 #endif /* HAVE_SYSCTLBYNAME */
70
71 #if defined(PROCESSOR_CPU_LOAD_INFO) || defined(KERNEL_LINUX) || defined(HAVE_LIBKSTAT) || defined(HAVE_SYSCTLBYNAME)
72 # define CPU_HAVE_READ 1
73 #else
74 # define CPU_HAVE_READ 0
75 #endif
76
77 static data_source_t dsrc[1] =
78 {
79         {"value", DS_TYPE_COUNTER, 0, 4294967295.0}
80 };
81
82 static data_set_t ds =
83 {
84         "cpu", 1, dsrc
85 };
86
87 #if CPU_HAVE_READ
88 #ifdef PROCESSOR_CPU_LOAD_INFO
89 static mach_port_t port_host;
90 static processor_port_array_t cpu_list;
91 static mach_msg_type_number_t cpu_list_len;
92
93 #if PROCESSOR_TEMPERATURE
94 static int cpu_temp_retry_counter = 0;
95 static int cpu_temp_retry_step    = 1;
96 static int cpu_temp_retry_max     = 1;
97 #endif /* PROCESSOR_TEMPERATURE */
98 /* #endif PROCESSOR_CPU_LOAD_INFO */
99
100 #elif defined(KERNEL_LINUX)
101 /* no variables needed */
102 /* #endif KERNEL_LINUX */
103
104 #elif defined(HAVE_LIBKSTAT)
105 /* colleague tells me that Sun doesn't sell systems with more than 100 or so CPUs.. */
106 # define MAX_NUMCPU 256
107 extern kstat_ctl_t *kc;
108 static kstat_t *ksp[MAX_NUMCPU];
109 static int numcpu;
110 /* #endif HAVE_LIBKSTAT */
111
112 #elif defined(HAVE_SYSCTLBYNAME)
113 static int numcpu;
114 #endif /* HAVE_SYSCTLBYNAME */
115
116 static int init (void)
117 {
118 #if PROCESSOR_CPU_LOAD_INFO || PROCESSOR_TEMPERATURE
119         kern_return_t status;
120         int collectd_step;
121
122         port_host = mach_host_self ();
123
124         /* FIXME: Free `cpu_list' if it's not NULL */
125         if ((status = host_processors (port_host, &cpu_list, &cpu_list_len)) != KERN_SUCCESS)
126         {
127                 ERROR ("cpu plugin: host_processors returned %i", (int) status);
128                 cpu_list_len = 0;
129                 return (-1);
130         }
131
132         DEBUG ("host_processors returned %i %s", (int) cpu_list_len, cpu_list_len == 1 ? "processor" : "processors");
133         INFO ("cpu plugin: Found %i processor%s.", (int) cpu_list_len, cpu_list_len == 1 ? "" : "s");
134
135         collectd_step = atoi (COLLECTD_STEP);
136         if ((collectd_step > 0) && (collectd_step <= 86400))
137                 cpu_temp_retry_max = 86400 / collectd_step;
138                 
139 /* #endif PROCESSOR_CPU_LOAD_INFO */
140
141 #elif defined(HAVE_LIBKSTAT)
142         kstat_t *ksp_chain;
143
144         numcpu = 0;
145
146         if (kc == NULL)
147                 return (-1);
148
149         /* Solaris doesn't count linear.. *sigh* */
150         for (numcpu = 0, ksp_chain = kc->kc_chain;
151                         (numcpu < MAX_NUMCPU) && (ksp_chain != NULL);
152                         ksp_chain = ksp_chain->ks_next)
153                 if (strncmp (ksp_chain->ks_module, "cpu_stat", 8) == 0)
154                         ksp[numcpu++] = ksp_chain;
155 /* #endif HAVE_LIBKSTAT */
156
157 #elif defined (HAVE_SYSCTLBYNAME)
158         size_t numcpu_size;
159
160         numcpu_size = sizeof (numcpu);
161
162         if (sysctlbyname ("hw.ncpu", &numcpu, &numcpu_size, NULL, 0) < 0)
163         {
164                 char errbuf[1024];
165                 WARNING ("cpu plugin: sysctlbyname: %s",
166                                 sstrerror (errno, errbuf, sizeof (errbuf)));
167                 return (-1);
168         }
169
170         if (numcpu != 1)
171                 NOTICE ("cpu: Only one processor supported when using `sysctlbyname' (found %i)", numcpu);
172 #endif
173
174         return (0);
175 } /* int init */
176
177 static void submit (int cpu_num, const char *type_instance, counter_t value)
178 {
179         value_t values[1];
180         value_list_t vl = VALUE_LIST_INIT;
181
182         values[0].counter = value;
183
184         vl.values = values;
185         vl.values_len = 1;
186         vl.time = time (NULL);
187         strcpy (vl.host, hostname_g);
188         strcpy (vl.plugin, "cpu");
189         snprintf (vl.plugin_instance, sizeof (vl.type_instance),
190                         "%i", cpu_num);
191         vl.plugin_instance[DATA_MAX_NAME_LEN - 1] = '\0';
192         strcpy (vl.type_instance, type_instance);
193
194         plugin_dispatch_values ("cpu", &vl);
195 }
196
197 static int cpu_read (void)
198 {
199 #if PROCESSOR_CPU_LOAD_INFO || PROCESSOR_TEMPERATURE
200         int cpu;
201
202         kern_return_t status;
203         
204 #if PROCESSOR_CPU_LOAD_INFO
205         processor_cpu_load_info_data_t cpu_info;
206         mach_msg_type_number_t         cpu_info_len;
207 #endif
208 #if PROCESSOR_TEMPERATURE
209         processor_info_data_t          cpu_temp;
210         mach_msg_type_number_t         cpu_temp_len;
211 #endif
212
213         host_t cpu_host;
214
215         for (cpu = 0; cpu < cpu_list_len; cpu++)
216         {
217 #if PROCESSOR_CPU_LOAD_INFO
218                 cpu_host = 0;
219                 cpu_info_len = PROCESSOR_BASIC_INFO_COUNT;
220
221                 if ((status = processor_info (cpu_list[cpu],
222                                                 PROCESSOR_CPU_LOAD_INFO, &cpu_host,
223                                                 (processor_info_t) &cpu_info, &cpu_info_len)) != KERN_SUCCESS)
224                 {
225                         ERROR ("cpu plugin: processor_info failed with status %i", (int) status);
226                         continue;
227                 }
228
229                 if (cpu_info_len < CPU_STATE_MAX)
230                 {
231                         ERROR ("cpu plugin: processor_info returned only %i elements..", cpu_info_len);
232                         continue;
233                 }
234
235                 submit (cpu, "user", (counter_t) cpu_info.cpu_ticks[CPU_STATE_USER]);
236                 submit (cpu, "nice", (counter_t) cpu_info.cpu_ticks[CPU_STATE_USER]);
237                 submit (cpu, "system", (counter_t) cpu_info.cpu_ticks[CPU_STATE_USER]);
238                 submit (cpu, "idle", (counter_t) cpu_info.cpu_ticks[CPU_STATE_USER]);
239 #endif /* PROCESSOR_CPU_LOAD_INFO */
240 #if PROCESSOR_TEMPERATURE
241                 /*
242                  * Not all Apple computers do have this ability. To minimize
243                  * the messages sent to the syslog we do an exponential
244                  * stepback if `processor_info' fails. We still try ~once a day
245                  * though..
246                  */
247                 if (cpu_temp_retry_counter > 0)
248                 {
249                         cpu_temp_retry_counter--;
250                         continue;
251                 }
252
253                 cpu_temp_len = PROCESSOR_INFO_MAX;
254
255                 status = processor_info (cpu_list[cpu],
256                                 PROCESSOR_TEMPERATURE,
257                                 &cpu_host,
258                                 cpu_temp, &cpu_temp_len);
259                 if (status != KERN_SUCCESS)
260                 {
261                         ERROR ("cpu plugin: processor_info failed: %s",
262                                         mach_error_string (status));
263
264                         cpu_temp_retry_counter = cpu_temp_retry_step;
265                         cpu_temp_retry_step *= 2;
266                         if (cpu_temp_retry_step > cpu_temp_retry_max)
267                                 cpu_temp_retry_step = cpu_temp_retry_max;
268
269                         continue;
270                 }
271
272                 if (cpu_temp_len != 1)
273                 {
274                         DEBUG ("processor_info (PROCESSOR_TEMPERATURE) returned %i elements..?",
275                                         (int) cpu_temp_len);
276                         continue;
277                 }
278
279                 cpu_temp_retry_counter = 0;
280                 cpu_temp_retry_step    = 1;
281
282                 DEBUG ("cpu_temp = %i", (int) cpu_temp);
283 #endif /* PROCESSOR_TEMPERATURE */
284         }
285 /* #endif PROCESSOR_CPU_LOAD_INFO */
286
287 #elif defined(KERNEL_LINUX)
288         int cpu;
289         counter_t user, nice, syst, idle;
290         counter_t wait, intr, sitr; /* sitr == soft interrupt */
291         FILE *fh;
292         char buf[1024];
293
294         char *fields[9];
295         int numfields;
296
297         static complain_t complain_obj;
298
299         if ((fh = fopen ("/proc/stat", "r")) == NULL)
300         {
301                 char errbuf[1024];
302                 plugin_complain (LOG_ERR, &complain_obj, "cpu plugin: "
303                                 "fopen (/proc/stat) failed: %s",
304                                 sstrerror (errno, errbuf, sizeof (errbuf)));
305                 return (-1);
306         }
307
308         plugin_relief (LOG_NOTICE, &complain_obj, "cpu plugin: "
309                         "fopen (/proc/stat) succeeded.");
310
311         while (fgets (buf, 1024, fh) != NULL)
312         {
313                 if (strncmp (buf, "cpu", 3))
314                         continue;
315                 if ((buf[3] < '0') || (buf[3] > '9'))
316                         continue;
317
318                 numfields = strsplit (buf, fields, 9);
319                 if (numfields < 5)
320                         continue;
321
322                 cpu = atoi (fields[0] + 3);
323                 user = atoll (fields[1]);
324                 nice = atoll (fields[2]);
325                 syst = atoll (fields[3]);
326                 idle = atoll (fields[4]);
327
328                 submit (cpu, "user", user);
329                 submit (cpu, "nice", nice);
330                 submit (cpu, "system", syst);
331                 submit (cpu, "idle", idle);
332
333                 if (numfields >= 8)
334                 {
335                         wait = atoll (fields[5]);
336                         intr = atoll (fields[6]);
337                         sitr = atoll (fields[7]);
338
339                         submit (cpu, "wait", wait);
340                         submit (cpu, "interrupt", intr);
341                         submit (cpu, "softirq", sitr);
342                 }
343         }
344
345         fclose (fh);
346 /* #endif defined(KERNEL_LINUX) */
347
348 #elif defined(HAVE_LIBKSTAT)
349         int cpu;
350         counter_t user, syst, idle, wait;
351         static cpu_stat_t cs;
352
353         if (kc == NULL)
354                 return;
355
356         for (cpu = 0; cpu < numcpu; cpu++)
357         {
358                 if (kstat_read (kc, ksp[cpu], &cs) == -1)
359                         continue; /* error message? */
360
361                 idle = (counter_t) cs.cpu_sysinfo.cpu[CPU_IDLE];
362                 user = (counter_t) cs.cpu_sysinfo.cpu[CPU_USER];
363                 syst = (counter_t) cs.cpu_sysinfo.cpu[CPU_KERNEL];
364                 wait = (counter_t) cs.cpu_sysinfo.cpu[CPU_WAIT];
365
366                 submit (ksp[cpu]->ks_instance, "user", user);
367                 submit (ksp[cpu]->ks_instance, "system", syst);
368                 submit (ksp[cpu]->ks_instance, "idle", idle);
369                 submit (ksp[cpu]->ks_instance, "wait", wait);
370         }
371 /* #endif defined(HAVE_LIBKSTAT) */
372
373 #elif defined(HAVE_SYSCTLBYNAME)
374         long cpuinfo[CPUSTATES];
375         size_t cpuinfo_size;
376
377         static complain_t complain_obj;
378
379         cpuinfo_size = sizeof (cpuinfo);
380
381         if (sysctlbyname("kern.cp_time", &cpuinfo, &cpuinfo_size, NULL, 0) < 0)
382         {
383                 char errbuf[1024];
384                 plugin_complain (LOG_ERR, &complain_obj, "cpu plugin: "
385                                 "sysctlbyname failed: %s.",
386                                 sstrerror (errno, errbuf, sizeof (errbuf)));
387                 return;
388         }
389
390         plugin_relief (LOG_NOTICE, &complain_obj, "cpu plugin: "
391                         "sysctlbyname succeeded.");
392
393         cpuinfo[CP_SYS] += cpuinfo[CP_INTR];
394
395         submit (0, "user", cpuinfo[CP_USER]);
396         submit (0, "nice", cpuinfo[CP_NICE]);
397         submit (0, "system", cpuinfo[CP_SYS]);
398         submit (0, "idle", cpuinfo[CP_IDLE]);
399 #endif
400
401         return (0);
402 }
403 #endif /* CPU_HAVE_READ */
404
405 void module_register (void)
406 {
407         plugin_register_data_set (&ds);
408
409 #if CPU_HAVE_READ
410         plugin_register_init ("cpu", init);
411         plugin_register_read ("cpu", cpu_read);
412 #endif /* CPU_HAVE_READ */
413 }