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