3 * Copyright (C) 2005-2007 Florian octo Forster
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.
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.
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
19 * Florian octo Forster <octo at verplant.org>
25 #include "utils_debug.h"
27 #ifdef HAVE_MACH_KERN_RETURN_H
28 # include <mach/kern_return.h>
30 #ifdef HAVE_MACH_MACH_INIT_H
31 # include <mach/mach_init.h>
33 #ifdef HAVE_MACH_HOST_PRIV_H
34 # include <mach/host_priv.h>
36 #if HAVE_MACH_MACH_ERROR_H
37 # include <mach/mach_error.h>
39 #ifdef HAVE_MACH_PROCESSOR_INFO_H
40 # include <mach/processor_info.h>
42 #ifdef HAVE_MACH_PROCESSOR_H
43 # include <mach/processor.h>
45 #ifdef HAVE_MACH_VM_MAP_H
46 # include <mach/vm_map.h>
50 # include <sys/sysinfo.h>
51 #endif /* HAVE_LIBKSTAT */
53 #ifdef HAVE_SYSCTLBYNAME
54 # ifdef HAVE_SYS_SYSCTL_H
55 # include <sys/sysctl.h>
58 # ifdef HAVE_SYS_DKSTAT_H
59 # include <sys/dkstat.h>
62 # if !defined(CP_USER) || !defined(CP_NICE) || !defined(CP_SYS) || !defined(CP_INTR) || !defined(CP_IDLE) || !defined(CPUSTATES)
70 #endif /* HAVE_SYSCTLBYNAME */
72 #if defined(PROCESSOR_CPU_LOAD_INFO) || defined(KERNEL_LINUX) || defined(HAVE_LIBKSTAT) || defined(HAVE_SYSCTLBYNAME)
73 # define CPU_HAVE_READ 1
75 # define CPU_HAVE_READ 0
78 static data_source_t dsrc[1] =
80 {"value", DS_TYPE_COUNTER, 0, 4294967295.0}
83 static data_set_t ds =
89 #ifdef PROCESSOR_CPU_LOAD_INFO
90 static mach_port_t port_host;
91 static processor_port_array_t cpu_list;
92 static mach_msg_type_number_t cpu_list_len;
94 #if PROCESSOR_TEMPERATURE
95 static int cpu_temp_retry_counter = 0;
96 static int cpu_temp_retry_step = 1;
97 static int cpu_temp_retry_max = 1;
98 #endif /* PROCESSOR_TEMPERATURE */
99 /* #endif PROCESSOR_CPU_LOAD_INFO */
101 #elif defined(KERNEL_LINUX)
102 /* no variables needed */
103 /* #endif KERNEL_LINUX */
105 #elif defined(HAVE_LIBKSTAT)
106 /* colleague tells me that Sun doesn't sell systems with more than 100 or so CPUs.. */
107 # define MAX_NUMCPU 256
108 extern kstat_ctl_t *kc;
109 static kstat_t *ksp[MAX_NUMCPU];
111 /* #endif HAVE_LIBKSTAT */
113 #elif defined(HAVE_SYSCTLBYNAME)
115 #endif /* HAVE_SYSCTLBYNAME */
117 static int init (void)
119 #if PROCESSOR_CPU_LOAD_INFO || PROCESSOR_TEMPERATURE
120 kern_return_t status;
123 port_host = mach_host_self ();
125 /* FIXME: Free `cpu_list' if it's not NULL */
126 if ((status = host_processors (port_host, &cpu_list, &cpu_list_len)) != KERN_SUCCESS)
128 syslog (LOG_ERR, "cpu plugin: host_processors returned %i\n", (int) status);
133 DBG ("host_processors returned %i %s", (int) cpu_list_len, cpu_list_len == 1 ? "processor" : "processors");
134 syslog (LOG_INFO, "cpu plugin: Found %i processor%s.", (int) cpu_list_len, cpu_list_len == 1 ? "" : "s");
136 collectd_step = atoi (COLLECTD_STEP);
137 if ((collectd_step > 0) && (collectd_step <= 86400))
138 cpu_temp_retry_max = 86400 / collectd_step;
140 /* #endif PROCESSOR_CPU_LOAD_INFO */
142 #elif defined(HAVE_LIBKSTAT)
150 /* Solaris doesn't count linear.. *sigh* */
151 for (numcpu = 0, ksp_chain = kc->kc_chain;
152 (numcpu < MAX_NUMCPU) && (ksp_chain != NULL);
153 ksp_chain = ksp_chain->ks_next)
154 if (strncmp (ksp_chain->ks_module, "cpu_stat", 8) == 0)
155 ksp[numcpu++] = ksp_chain;
156 /* #endif HAVE_LIBKSTAT */
158 #elif defined (HAVE_SYSCTLBYNAME)
161 numcpu_size = sizeof (numcpu);
163 if (sysctlbyname ("hw.ncpu", &numcpu, &numcpu_size, NULL, 0) < 0)
165 syslog (LOG_WARNING, "cpu: sysctlbyname: %s", strerror (errno));
170 syslog (LOG_NOTICE, "cpu: Only one processor supported when using `sysctlbyname' (found %i)", numcpu);
176 static void submit (int cpu_num, const char *type_instance, counter_t value)
179 value_list_t vl = VALUE_LIST_INIT;
181 values[0].counter = value;
185 vl.time = time (NULL);
186 strcpy (vl.host, hostname);
187 strcpy (vl.plugin, "cpu");
188 snprintf (vl.plugin_instance, sizeof (vl.type_instance),
190 vl.plugin_instance[DATA_MAX_NAME_LEN - 1] = '\0';
191 strcpy (vl.type_instance, type_instance);
193 plugin_dispatch_values ("cpu", &vl);
196 static int cpu_read (void)
198 #if PROCESSOR_CPU_LOAD_INFO || PROCESSOR_TEMPERATURE
201 kern_return_t status;
203 #if PROCESSOR_CPU_LOAD_INFO
204 processor_cpu_load_info_data_t cpu_info;
205 mach_msg_type_number_t cpu_info_len;
207 #if PROCESSOR_TEMPERATURE
208 processor_info_data_t cpu_temp;
209 mach_msg_type_number_t cpu_temp_len;
214 for (cpu = 0; cpu < cpu_list_len; cpu++)
216 #if PROCESSOR_CPU_LOAD_INFO
218 cpu_info_len = PROCESSOR_BASIC_INFO_COUNT;
220 if ((status = processor_info (cpu_list[cpu],
221 PROCESSOR_CPU_LOAD_INFO, &cpu_host,
222 (processor_info_t) &cpu_info, &cpu_info_len)) != KERN_SUCCESS)
224 syslog (LOG_ERR, "cpu plugin: processor_info failed with status %i", (int) status);
228 if (cpu_info_len < CPU_STATE_MAX)
230 syslog (LOG_ERR, "cpu plugin: processor_info returned only %i elements..", cpu_info_len);
234 submit (cpu, "user", (counter_t) cpu_info.cpu_ticks[CPU_STATE_USER]);
235 submit (cpu, "nice", (counter_t) cpu_info.cpu_ticks[CPU_STATE_USER]);
236 submit (cpu, "system", (counter_t) cpu_info.cpu_ticks[CPU_STATE_USER]);
237 submit (cpu, "idle", (counter_t) cpu_info.cpu_ticks[CPU_STATE_USER]);
238 #endif /* PROCESSOR_CPU_LOAD_INFO */
239 #if PROCESSOR_TEMPERATURE
241 * Not all Apple computers do have this ability. To minimize
242 * the messages sent to the syslog we do an exponential
243 * stepback if `processor_info' fails. We still try ~once a day
246 if (cpu_temp_retry_counter > 0)
248 cpu_temp_retry_counter--;
252 cpu_temp_len = PROCESSOR_INFO_MAX;
254 status = processor_info (cpu_list[cpu],
255 PROCESSOR_TEMPERATURE,
257 cpu_temp, &cpu_temp_len);
258 if (status != KERN_SUCCESS)
260 syslog (LOG_ERR, "cpu plugin: processor_info failed: %s",
261 mach_error_string (status));
263 cpu_temp_retry_counter = cpu_temp_retry_step;
264 cpu_temp_retry_step *= 2;
265 if (cpu_temp_retry_step > cpu_temp_retry_max)
266 cpu_temp_retry_step = cpu_temp_retry_max;
271 if (cpu_temp_len != 1)
273 DBG ("processor_info (PROCESSOR_TEMPERATURE) returned %i elements..?",
278 cpu_temp_retry_counter = 0;
279 cpu_temp_retry_step = 1;
281 DBG ("cpu_temp = %i", (int) cpu_temp);
282 #endif /* PROCESSOR_TEMPERATURE */
284 /* #endif PROCESSOR_CPU_LOAD_INFO */
286 #elif defined(KERNEL_LINUX)
288 counter_t user, nice, syst, idle;
289 counter_t wait, intr, sitr; /* sitr == soft interrupt */
296 static complain_t complain_obj;
298 if ((fh = fopen ("/proc/stat", "r")) == NULL)
300 plugin_complain (LOG_ERR, &complain_obj, "cpu plugin: "
301 "fopen (/proc/stat) failed: %s",
306 plugin_relief (LOG_NOTICE, &complain_obj, "cpu plugin: "
307 "fopen (/proc/stat) succeeded.");
309 while (fgets (buf, 1024, fh) != NULL)
311 if (strncmp (buf, "cpu", 3))
313 if ((buf[3] < '0') || (buf[3] > '9'))
316 numfields = strsplit (buf, fields, 9);
320 cpu = atoi (fields[0] + 3);
321 user = atoll (fields[1]);
322 nice = atoll (fields[2]);
323 syst = atoll (fields[3]);
324 idle = atoll (fields[4]);
326 submit (cpu, "user", user);
327 submit (cpu, "nice", nice);
328 submit (cpu, "system", syst);
329 submit (cpu, "idle", idle);
333 wait = atoll (fields[5]);
334 intr = atoll (fields[6]);
335 sitr = atoll (fields[7]);
337 submit (cpu, "wait", wait);
338 submit (cpu, "interrupt", intr);
339 submit (cpu, "softirq", sitr);
344 /* #endif defined(KERNEL_LINUX) */
346 #elif defined(HAVE_LIBKSTAT)
348 counter_t user, syst, idle, wait;
349 static cpu_stat_t cs;
354 for (cpu = 0; cpu < numcpu; cpu++)
356 if (kstat_read (kc, ksp[cpu], &cs) == -1)
357 continue; /* error message? */
359 idle = (counter_t) cs.cpu_sysinfo.cpu[CPU_IDLE];
360 user = (counter_t) cs.cpu_sysinfo.cpu[CPU_USER];
361 syst = (counter_t) cs.cpu_sysinfo.cpu[CPU_KERNEL];
362 wait = (counter_t) cs.cpu_sysinfo.cpu[CPU_WAIT];
364 submit (ksp[cpu]->ks_instance, "user", user);
365 submit (ksp[cpu]->ks_instance, "system", syst);
366 submit (ksp[cpu]->ks_instance, "idle", idle);
367 submit (ksp[cpu]->ks_instance, "wait", wait);
369 /* #endif defined(HAVE_LIBKSTAT) */
371 #elif defined(HAVE_SYSCTLBYNAME)
372 long cpuinfo[CPUSTATES];
375 static complain_t complain_obj;
377 cpuinfo_size = sizeof (cpuinfo);
379 if (sysctlbyname("kern.cp_time", &cpuinfo, &cpuinfo_size, NULL, 0) < 0)
381 plugin_complain (LOG_ERR, &complain_obj, "cpu plugin: "
382 "sysctlbyname failed: %s.",
387 plugin_relief (LOG_NOTICE, &complain_obj, "cpu plugin: "
388 "sysctlbyname succeeded.");
390 cpuinfo[CP_SYS] += cpuinfo[CP_INTR];
392 submit (0, "user", cpuinfo[CP_USER]);
393 submit (0, "nice", cpuinfo[CP_NICE]);
394 submit (0, "system", cpuinfo[CP_SYS]);
395 submit (0, "idle", cpuinfo[CP_IDLE]);
400 #endif /* CPU_HAVE_READ */
402 void module_register (void)
404 plugin_register_data_set (&ds);
407 plugin_register_init ("cpu", init);
408 plugin_register_read ("cpu", cpu_read);
409 #endif /* CPU_HAVE_READ */