3 * Copyright (C) 2005 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; either version 2 of the License, or (at your
8 * option) any later version.
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.
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
20 * Florian octo Forster <octo at verplant.org>
26 #define MODULE_NAME "cpu"
29 #include <sys/sysinfo.h>
30 #endif /* HAVE_LIBKSTAT */
32 #ifdef HAVE_SYSCTLBYNAME
33 #ifdef HAVE_SYS_SYSCTL_H
34 #include <sys/sysctl.h>
37 #ifdef HAVE_SYS_DKSTAT_H
38 #include <sys/dkstat.h>
41 #if !defined(CP_USER) || !defined(CP_NICE) || !defined(CP_SYS) || !defined(CP_INTR) || !defined(CP_IDLE) || !defined(CPUSTATES)
49 #endif /* HAVE_SYSCTLBYNAME */
55 /* colleague tells me that Sun doesn't sell systems with more than 100 or so CPUs.. */
56 #define MAX_NUMCPU 256
57 extern kstat_ctl_t *kc;
58 static kstat_t *ksp[MAX_NUMCPU];
60 #endif /* HAVE_LIBKSTAT */
62 #ifdef HAVE_SYSCTLBYNAME
64 #endif /* HAVE_SYSCTLBYNAME */
66 static char *cpu_filename = "cpu-%s.rrd";
68 static char *ds_def[] =
70 "DS:user:COUNTER:25:0:100",
71 "DS:nice:COUNTER:25:0:100",
72 "DS:syst:COUNTER:25:0:100",
73 "DS:idle:COUNTER:25:0:100",
74 "DS:wait:COUNTER:25:0:100",
77 static int ds_num = 5;
89 /* Solaris doesn't count linear.. *sigh* */
90 for (numcpu = 0, ksp_chain = kc->kc_chain;
91 (numcpu < MAX_NUMCPU) && (ksp_chain != NULL);
92 ksp_chain = ksp_chain->ks_next)
93 if (strncmp (ksp_chain->ks_module, "cpu_stat", 8) == 0)
94 ksp[numcpu++] = ksp_chain;
95 /* #endif HAVE_LIBKSTAT */
97 #elif defined (HAVE_SYSCTLBYNAME)
100 numcpu_size = sizeof (numcpu);
102 if (sysctlbyname ("hw.ncpu", &numcpu, &numcpu_size, NULL, 0) < 0)
104 syslog (LOG_WARNING, "cpu: sysctlbyname: %s", strerror (errno));
109 syslog (LOG_NOTICE, "cpu: Only one processor supported when using `sysctlbyname' (found %i)", numcpu);
115 void cpu_write (char *host, char *inst, char *val)
120 status = snprintf (file, 512, cpu_filename, inst);
123 else if (status >= 512)
126 rrd_update_file (host, file, val, ds_def, ds_num);
130 void cpu_submit (int cpu_num, unsigned long long user, unsigned long long nice, unsigned long long syst,
131 unsigned long long idle, unsigned long long wait)
136 if (snprintf (buf, BUFSIZE, "%u:%llu:%llu:%llu:%llu:%llu", (unsigned int) curtime,
137 user, nice, syst, idle, wait) >= BUFSIZE)
139 snprintf (cpu, 16, "%i", cpu_num);
141 plugin_submit (MODULE_NAME, cpu, buf);
150 unsigned long long user, nice, syst, idle;
151 unsigned long long wait, intr, sitr; /* sitr == soft interrupt */
158 if ((fh = fopen ("/proc/stat", "r")) == NULL)
160 syslog (LOG_WARNING, "cpu: fopen: %s", strerror (errno));
164 while (fgets (buf, BUFSIZE, fh) != NULL)
166 if (strncmp (buf, "cpu", 3))
168 if ((buf[3] < '0') || (buf[3] > '9'))
171 numfields = strsplit (buf, fields, 9);
175 cpu = atoi (fields[0] + 3);
176 user = atoll (fields[1]);
177 nice = atoll (fields[2]);
178 syst = atoll (fields[3]);
179 idle = atoll (fields[4]);
183 wait = atoll (fields[5]);
184 intr = atoll (fields[6]);
185 sitr = atoll (fields[7]);
187 /* I doubt anyone cares about the time spent in
188 * interrupt handlers.. */
196 cpu_submit (cpu, user, nice, syst, idle, wait);
201 /* #endif defined(KERNEL_LINUX) */
203 #elif defined(HAVE_LIBKSTAT)
205 unsigned long long user, syst, idle, wait;
206 static cpu_stat_t cs;
211 for (cpu = 0; cpu < numcpu; cpu++)
213 if (kstat_read (kc, ksp[cpu], &cs) == -1)
214 continue; /* error message? */
216 idle = (unsigned long long) cs.cpu_sysinfo.cpu[CPU_IDLE];
217 user = (unsigned long long) cs.cpu_sysinfo.cpu[CPU_USER];
218 syst = (unsigned long long) cs.cpu_sysinfo.cpu[CPU_KERNEL];
219 wait = (unsigned long long) cs.cpu_sysinfo.cpu[CPU_WAIT];
221 cpu_submit (ksp[cpu]->ks_instance,
222 user, 0LL, syst, idle, wait);
224 /* #endif defined(HAVE_LIBKSTAT) */
226 #elif defined (HAVE_SYSCTLBYNAME)
227 long cpuinfo[CPUSTATES];
230 cpuinfo_size = sizeof (cpuinfo);
232 if (sysctlbyname("kern.cp_time", &cpuinfo, &cpuinfo_size, NULL, 0) < 0)
234 syslog (LOG_WARNING, "cpu: sysctlbyname: %s", strerror (errno));
238 cpuinfo[CP_SYS] += cpuinfo[CP_INTR];
240 /* FIXME: Instance is always `0' */
241 cpu_submit (0, cpuinfo[CP_USER], cpuinfo[CP_NICE], cpuinfo[CP_SYS], cpuinfo[CP_IDLE], 0LL);
245 void module_register (void)
247 plugin_register (MODULE_NAME, cpu_init, cpu_read, cpu_write);
251 #endif /* COLLECT_CPU */