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[5] =
80 {"user", DS_TYPE_COUNTER, 0, 4294967295.0},
81 {"nice", DS_TYPE_COUNTER, 0, 4294967295.0},
82 {"syst", DS_TYPE_COUNTER, 0, 4294967295.0},
83 {"idle", DS_TYPE_COUNTER, 0, 4294967295.0},
84 {"wait", DS_TYPE_COUNTER, 0, 4294967295.0}
87 static data_set_t ds =
93 #ifdef PROCESSOR_CPU_LOAD_INFO
94 static mach_port_t port_host;
95 static processor_port_array_t cpu_list;
96 static mach_msg_type_number_t cpu_list_len;
98 #if PROCESSOR_TEMPERATURE
99 static int cpu_temp_retry_counter = 0;
100 static int cpu_temp_retry_step = 1;
101 static int cpu_temp_retry_max = 1;
102 #endif /* PROCESSOR_TEMPERATURE */
103 /* #endif PROCESSOR_CPU_LOAD_INFO */
105 #elif defined(KERNEL_LINUX)
106 /* no variables needed */
107 /* #endif KERNEL_LINUX */
109 #elif defined(HAVE_LIBKSTAT)
110 /* colleague tells me that Sun doesn't sell systems with more than 100 or so CPUs.. */
111 # define MAX_NUMCPU 256
112 extern kstat_ctl_t *kc;
113 static kstat_t *ksp[MAX_NUMCPU];
115 /* #endif HAVE_LIBKSTAT */
117 #elif defined(HAVE_SYSCTLBYNAME)
119 #endif /* HAVE_SYSCTLBYNAME */
121 static int init (void)
123 #if PROCESSOR_CPU_LOAD_INFO || PROCESSOR_TEMPERATURE
124 kern_return_t status;
127 port_host = mach_host_self ();
129 /* FIXME: Free `cpu_list' if it's not NULL */
130 if ((status = host_processors (port_host, &cpu_list, &cpu_list_len)) != KERN_SUCCESS)
132 syslog (LOG_ERR, "cpu plugin: host_processors returned %i", (int) status);
137 DBG ("host_processors returned %i %s", (int) cpu_list_len, cpu_list_len == 1 ? "processor" : "processors");
138 syslog (LOG_INFO, "cpu plugin: Found %i processor%s.", (int) cpu_list_len, cpu_list_len == 1 ? "" : "s");
140 collectd_step = atoi (COLLECTD_STEP);
141 if ((collectd_step > 0) && (collectd_step <= 86400))
142 cpu_temp_retry_max = 86400 / collectd_step;
144 /* #endif PROCESSOR_CPU_LOAD_INFO */
146 #elif defined(HAVE_LIBKSTAT)
154 /* Solaris doesn't count linear.. *sigh* */
155 for (numcpu = 0, ksp_chain = kc->kc_chain;
156 (numcpu < MAX_NUMCPU) && (ksp_chain != NULL);
157 ksp_chain = ksp_chain->ks_next)
158 if (strncmp (ksp_chain->ks_module, "cpu_stat", 8) == 0)
159 ksp[numcpu++] = ksp_chain;
160 /* #endif HAVE_LIBKSTAT */
162 #elif defined (HAVE_SYSCTLBYNAME)
165 numcpu_size = sizeof (numcpu);
167 if (sysctlbyname ("hw.ncpu", &numcpu, &numcpu_size, NULL, 0) < 0)
169 syslog (LOG_WARNING, "cpu: sysctlbyname: %s", strerror (errno));
174 syslog (LOG_NOTICE, "cpu: Only one processor supported when using `sysctlbyname' (found %i)", numcpu);
180 static void submit (int cpu_num, unsigned long long user,
181 unsigned long long nice, unsigned long long syst,
182 unsigned long long idle, unsigned long long wait)
185 value_list_t vl = VALUE_LIST_INIT;
187 values[0].counter = user;
188 values[1].counter = nice;
189 values[2].counter = syst;
190 values[3].counter = idle;
191 values[4].counter = wait;
195 vl.time = time (NULL);
196 strcpy (vl.host, hostname);
197 strcpy (vl.plugin, "cpu");
198 strcpy (vl.plugin_instance, "");
199 snprintf (vl.type_instance, sizeof (vl.type_instance),
201 vl.type_instance[DATA_MAX_NAME_LEN - 1] = '\0';
203 plugin_dispatch_values ("cpu", &vl);
206 static int cpu_read (void)
208 #if PROCESSOR_CPU_LOAD_INFO || PROCESSOR_TEMPERATURE
211 kern_return_t status;
213 #if PROCESSOR_CPU_LOAD_INFO
214 processor_cpu_load_info_data_t cpu_info;
215 mach_msg_type_number_t cpu_info_len;
217 #if PROCESSOR_TEMPERATURE
218 processor_info_data_t cpu_temp;
219 mach_msg_type_number_t cpu_temp_len;
224 for (cpu = 0; cpu < cpu_list_len; cpu++)
226 #if PROCESSOR_CPU_LOAD_INFO
228 cpu_info_len = PROCESSOR_BASIC_INFO_COUNT;
230 if ((status = processor_info (cpu_list[cpu],
231 PROCESSOR_CPU_LOAD_INFO, &cpu_host,
232 (processor_info_t) &cpu_info, &cpu_info_len)) != KERN_SUCCESS)
234 syslog (LOG_ERR, "cpu plugin: processor_info failed with status %i", (int) status);
238 if (cpu_info_len < CPU_STATE_MAX)
240 syslog (LOG_ERR, "cpu plugin: processor_info returned only %i elements..", cpu_info_len);
244 submit (cpu, cpu_info.cpu_ticks[CPU_STATE_USER],
245 cpu_info.cpu_ticks[CPU_STATE_NICE],
246 cpu_info.cpu_ticks[CPU_STATE_SYSTEM],
247 cpu_info.cpu_ticks[CPU_STATE_IDLE],
249 #endif /* PROCESSOR_CPU_LOAD_INFO */
250 #if PROCESSOR_TEMPERATURE
252 * Not all Apple computers do have this ability. To minimize
253 * the messages sent to the syslog we do an exponential
254 * stepback if `processor_info' fails. We still try ~once a day
257 if (cpu_temp_retry_counter > 0)
259 cpu_temp_retry_counter--;
263 cpu_temp_len = PROCESSOR_INFO_MAX;
265 status = processor_info (cpu_list[cpu],
266 PROCESSOR_TEMPERATURE,
268 cpu_temp, &cpu_temp_len);
269 if (status != KERN_SUCCESS)
271 syslog (LOG_ERR, "cpu plugin: processor_info failed: %s",
272 mach_error_string (status));
274 cpu_temp_retry_counter = cpu_temp_retry_step;
275 cpu_temp_retry_step *= 2;
276 if (cpu_temp_retry_step > cpu_temp_retry_max)
277 cpu_temp_retry_step = cpu_temp_retry_max;
282 if (cpu_temp_len != 1)
284 DBG ("processor_info (PROCESSOR_TEMPERATURE) returned %i elements..?",
289 cpu_temp_retry_counter = 0;
290 cpu_temp_retry_step = 1;
292 DBG ("cpu_temp = %i", (int) cpu_temp);
293 #endif /* PROCESSOR_TEMPERATURE */
295 /* #endif PROCESSOR_CPU_LOAD_INFO */
297 #elif defined(KERNEL_LINUX)
299 unsigned long long user, nice, syst, idle;
300 unsigned long long wait, intr, sitr; /* sitr == soft interrupt */
307 static complain_t complain_obj;
309 if ((fh = fopen ("/proc/stat", "r")) == NULL)
311 plugin_complain (LOG_ERR, &complain_obj, "cpu plugin: "
312 "fopen (/proc/stat) failed: %s",
317 plugin_relief (LOG_NOTICE, &complain_obj, "cpu plugin: "
318 "fopen (/proc/stat) succeeded.");
320 while (fgets (buf, 1024, fh) != NULL)
322 if (strncmp (buf, "cpu", 3))
324 if ((buf[3] < '0') || (buf[3] > '9'))
327 numfields = strsplit (buf, fields, 9);
331 cpu = atoi (fields[0] + 3);
332 user = atoll (fields[1]);
333 nice = atoll (fields[2]);
334 syst = atoll (fields[3]);
335 idle = atoll (fields[4]);
339 wait = atoll (fields[5]);
340 intr = atoll (fields[6]);
341 sitr = atoll (fields[7]);
343 /* I doubt anyone cares about the time spent in
344 * interrupt handlers.. */
352 submit (cpu, user, nice, syst, idle, wait);
356 /* #endif defined(KERNEL_LINUX) */
358 #elif defined(HAVE_LIBKSTAT)
360 unsigned long long user, syst, idle, wait;
361 static cpu_stat_t cs;
366 for (cpu = 0; cpu < numcpu; cpu++)
368 if (kstat_read (kc, ksp[cpu], &cs) == -1)
369 continue; /* error message? */
371 idle = (unsigned long long) cs.cpu_sysinfo.cpu[CPU_IDLE];
372 user = (unsigned long long) cs.cpu_sysinfo.cpu[CPU_USER];
373 syst = (unsigned long long) cs.cpu_sysinfo.cpu[CPU_KERNEL];
374 wait = (unsigned long long) cs.cpu_sysinfo.cpu[CPU_WAIT];
376 submit (ksp[cpu]->ks_instance,
377 user, 0LL, syst, idle, wait);
379 /* #endif defined(HAVE_LIBKSTAT) */
381 #elif defined(HAVE_SYSCTLBYNAME)
382 long cpuinfo[CPUSTATES];
385 static complain_t complain_obj;
387 cpuinfo_size = sizeof (cpuinfo);
389 if (sysctlbyname("kern.cp_time", &cpuinfo, &cpuinfo_size, NULL, 0) < 0)
391 plugin_complain (LOG_ERR, &complain_obj, "cpu plugin: "
392 "sysctlbyname failed: %s.",
397 plugin_relief (LOG_NOTICE, &complain_obj, "cpu plugin: "
398 "sysctlbyname succeeded.");
400 cpuinfo[CP_SYS] += cpuinfo[CP_INTR];
402 /* FIXME: Instance is always `0' */
403 submit (0, cpuinfo[CP_USER], cpuinfo[CP_NICE], cpuinfo[CP_SYS], cpuinfo[CP_IDLE], 0LL);
408 #endif /* CPU_HAVE_READ */
410 void module_register (void)
412 plugin_register_data_set (&ds);
415 plugin_register_init ("cpu", init);
416 plugin_register_read ("cpu", cpu_read);
417 #endif /* CPU_HAVE_READ */