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