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>
27 #define MODULE_NAME "cpu"
30 # include <sys/sysinfo.h>
31 #endif /* HAVE_LIBKSTAT */
33 #ifdef HAVE_SYSCTLBYNAME
34 # ifdef HAVE_SYS_SYSCTL_H
35 # include <sys/sysctl.h>
38 # ifdef HAVE_SYS_DKSTAT_H
39 # include <sys/dkstat.h>
42 # if !defined(CP_USER) || !defined(CP_NICE) || !defined(CP_SYS) || !defined(CP_INTR) || !defined(CP_IDLE) || !defined(CPUSTATES)
50 #endif /* HAVE_SYSCTLBYNAME */
52 #if defined(KERNEL_LINUX) || defined(HAVE_LIBKSTAT) || defined(HAVE_SYSCTLBYNAME)
53 # define CPU_HAVE_READ 1
55 # define CPU_HAVE_READ 0
59 /* colleague tells me that Sun doesn't sell systems with more than 100 or so CPUs.. */
60 # define MAX_NUMCPU 256
61 extern kstat_ctl_t *kc;
62 static kstat_t *ksp[MAX_NUMCPU];
64 #endif /* HAVE_LIBKSTAT */
66 #ifdef HAVE_SYSCTLBYNAME
68 #endif /* HAVE_SYSCTLBYNAME */
70 static char *cpu_filename = "cpu-%s.rrd";
72 static char *ds_def[] =
74 "DS:user:COUNTER:25:0:U",
75 "DS:nice:COUNTER:25:0:U",
76 "DS:syst:COUNTER:25:0:U",
77 "DS:idle:COUNTER:25:0:U",
78 "DS:wait:COUNTER:25:0:U",
81 static int ds_num = 5;
83 static void cpu_init (void)
93 /* Solaris doesn't count linear.. *sigh* */
94 for (numcpu = 0, ksp_chain = kc->kc_chain;
95 (numcpu < MAX_NUMCPU) && (ksp_chain != NULL);
96 ksp_chain = ksp_chain->ks_next)
97 if (strncmp (ksp_chain->ks_module, "cpu_stat", 8) == 0)
98 ksp[numcpu++] = ksp_chain;
99 /* #endif HAVE_LIBKSTAT */
101 #elif defined (HAVE_SYSCTLBYNAME)
104 numcpu_size = sizeof (numcpu);
106 if (sysctlbyname ("hw.ncpu", &numcpu, &numcpu_size, NULL, 0) < 0)
108 syslog (LOG_WARNING, "cpu: sysctlbyname: %s", strerror (errno));
113 syslog (LOG_NOTICE, "cpu: Only one processor supported when using `sysctlbyname' (found %i)", numcpu);
119 static void cpu_write (char *host, char *inst, char *val)
124 status = snprintf (file, 512, cpu_filename, inst);
127 else if (status >= 512)
130 rrd_update_file (host, file, val, ds_def, ds_num);
135 static void cpu_submit (int cpu_num, unsigned long long user,
136 unsigned long long nice, unsigned long long syst,
137 unsigned long long idle, unsigned long long wait)
142 if (snprintf (buf, BUFSIZE, "%u:%llu:%llu:%llu:%llu:%llu", (unsigned int) curtime,
143 user, nice, syst, idle, wait) >= BUFSIZE)
145 snprintf (cpu, 16, "%i", cpu_num);
147 plugin_submit (MODULE_NAME, cpu, buf);
151 static void cpu_read (void)
156 unsigned long long user, nice, syst, idle;
157 unsigned long long wait, intr, sitr; /* sitr == soft interrupt */
164 if ((fh = fopen ("/proc/stat", "r")) == NULL)
166 syslog (LOG_WARNING, "cpu: fopen: %s", strerror (errno));
170 while (fgets (buf, BUFSIZE, fh) != NULL)
172 if (strncmp (buf, "cpu", 3))
174 if ((buf[3] < '0') || (buf[3] > '9'))
177 numfields = strsplit (buf, fields, 9);
181 cpu = atoi (fields[0] + 3);
182 user = atoll (fields[1]);
183 nice = atoll (fields[2]);
184 syst = atoll (fields[3]);
185 idle = atoll (fields[4]);
189 wait = atoll (fields[5]);
190 intr = atoll (fields[6]);
191 sitr = atoll (fields[7]);
193 /* I doubt anyone cares about the time spent in
194 * interrupt handlers.. */
202 cpu_submit (cpu, user, nice, syst, idle, wait);
207 /* #endif defined(KERNEL_LINUX) */
209 #elif defined(HAVE_LIBKSTAT)
211 unsigned long long user, syst, idle, wait;
212 static cpu_stat_t cs;
217 for (cpu = 0; cpu < numcpu; cpu++)
219 if (kstat_read (kc, ksp[cpu], &cs) == -1)
220 continue; /* error message? */
222 idle = (unsigned long long) cs.cpu_sysinfo.cpu[CPU_IDLE];
223 user = (unsigned long long) cs.cpu_sysinfo.cpu[CPU_USER];
224 syst = (unsigned long long) cs.cpu_sysinfo.cpu[CPU_KERNEL];
225 wait = (unsigned long long) cs.cpu_sysinfo.cpu[CPU_WAIT];
227 cpu_submit (ksp[cpu]->ks_instance,
228 user, 0LL, syst, idle, wait);
230 /* #endif defined(HAVE_LIBKSTAT) */
232 #elif defined(HAVE_SYSCTLBYNAME)
233 long cpuinfo[CPUSTATES];
236 cpuinfo_size = sizeof (cpuinfo);
238 if (sysctlbyname("kern.cp_time", &cpuinfo, &cpuinfo_size, NULL, 0) < 0)
240 syslog (LOG_WARNING, "cpu: sysctlbyname: %s", strerror (errno));
244 cpuinfo[CP_SYS] += cpuinfo[CP_INTR];
246 /* FIXME: Instance is always `0' */
247 cpu_submit (0, cpuinfo[CP_USER], cpuinfo[CP_NICE], cpuinfo[CP_SYS], cpuinfo[CP_IDLE], 0LL);
253 # define cpu_read NULL
254 #endif /* CPU_HAVE_READ */
256 void module_register (void)
258 plugin_register (MODULE_NAME, cpu_init, cpu_read, cpu_write);