4 #define MODULE_NAME "cpu"
7 #include <sys/sysinfo.h>
8 #endif /* HAVE_LIBKSTAT */
10 #ifdef HAVE_SYSCTLBYNAME
11 #ifdef HAVE_SYS_SYSCTL_H
12 #include <sys/sysctl.h>
15 #ifdef HAVE_SYS_DKSTAT_H
16 #include <sys/dkstat.h>
19 #if !defined(CP_USER) || !defined(CP_NICE) || !defined(CP_SYS) || !defined(CP_INTR) || !defined(CP_IDLE) || !defined(CPUSTATES)
27 #endif /* HAVE_SYSCTLBYNAME */
33 /* colleague tells me that Sun doesn't sell systems with more than 100 or so CPUs.. */
34 #define MAX_NUMCPU 256
35 extern kstat_ctl_t *kc;
36 static kstat_t *ksp[MAX_NUMCPU];
38 #endif /* HAVE_LIBKSTAT */
40 #ifdef HAVE_SYSCTLBYNAME
42 #endif /* HAVE_SYSCTLBYNAME */
44 static char *cpu_filename = "cpu-%s.rrd";
46 static char *ds_def[] =
48 "DS:user:COUNTER:25:0:100",
49 "DS:nice:COUNTER:25:0:100",
50 "DS:syst:COUNTER:25:0:100",
51 "DS:idle:COUNTER:25:0:100",
52 "DS:wait:COUNTER:25:0:100",
55 static int ds_num = 5;
57 extern time_t curtime;
69 /* Solaris doesn't count linear.. *sigh* */
70 for (numcpu = 0, ksp_chain = kc->kc_chain;
71 (numcpu < MAX_NUMCPU) && (ksp_chain != NULL);
72 ksp_chain = ksp_chain->ks_next)
73 if (strncmp (ksp_chain->ks_module, "cpu_stat", 8) == 0)
74 ksp[numcpu++] = ksp_chain;
75 /* #endif HAVE_LIBKSTAT */
77 #elif defined (HAVE_SYSCTLBYNAME)
80 numcpu_size = sizeof (numcpu);
82 if (sysctlbyname ("hw.ncpu", &numcpu, &numcpu_size, NULL, 0) < 0)
84 syslog (LOG_WARNING, "cpu: sysctlbyname: %s", strerror (errno));
89 syslog (LOG_NOTICE, "cpu: Only one processor supported when using `sysctlbyname' (found %i)", numcpu);
95 void cpu_write (char *host, char *inst, char *val)
100 status = snprintf (file, 512, cpu_filename, inst);
103 else if (status >= 512)
106 rrd_update_file (host, file, val, ds_def, ds_num);
110 void cpu_submit (int cpu_num, unsigned long long user, unsigned long long nice, unsigned long long syst,
111 unsigned long long idle, unsigned long long wait)
116 if (snprintf (buf, BUFSIZE, "%u:%llu:%llu:%llu:%llu:%llu", (unsigned int) curtime,
117 user, nice, syst, idle, wait) >= BUFSIZE)
119 snprintf (cpu, 16, "%i", cpu_num);
121 plugin_submit (MODULE_NAME, cpu, buf);
130 unsigned long long user, nice, syst, idle;
131 unsigned long long wait, intr, sitr; /* sitr == soft interrupt */
138 if ((fh = fopen ("/proc/stat", "r")) == NULL)
140 syslog (LOG_WARNING, "cpu: fopen: %s", strerror (errno));
144 while (fgets (buf, BUFSIZE, fh) != NULL)
146 if (strncmp (buf, "cpu", 3))
148 if ((buf[3] < '0') || (buf[3] > '9'))
151 numfields = strsplit (buf, fields, 9);
155 cpu = atoi (fields[0] + 3);
156 user = atoll (fields[1]);
157 nice = atoll (fields[2]);
158 syst = atoll (fields[3]);
159 idle = atoll (fields[4]);
163 wait = atoll (fields[5]);
164 intr = atoll (fields[6]);
165 sitr = atoll (fields[7]);
167 /* I doubt anyone cares about the time spent in
168 * interrupt handlers.. */
176 cpu_submit (cpu, user, nice, syst, idle, wait);
181 /* #endif defined(KERNEL_LINUX) */
183 #elif defined(HAVE_LIBKSTAT)
185 unsigned long long user, syst, idle, wait;
186 static cpu_stat_t cs;
191 for (cpu = 0; cpu < numcpu; cpu++)
193 if (kstat_read (kc, ksp[cpu], &cs) == -1)
194 continue; /* error message? */
196 idle = (unsigned long long) cs.cpu_sysinfo.cpu[CPU_IDLE];
197 user = (unsigned long long) cs.cpu_sysinfo.cpu[CPU_USER];
198 syst = (unsigned long long) cs.cpu_sysinfo.cpu[CPU_KERNEL];
199 wait = (unsigned long long) cs.cpu_sysinfo.cpu[CPU_WAIT];
201 cpu_submit (ksp[cpu]->ks_instance,
202 user, 0LL, syst, idle, wait);
204 /* #endif defined(HAVE_LIBKSTAT) */
206 #elif defined (HAVE_SYSCTLBYNAME)
207 long cpuinfo[CPUSTATES];
210 cpuinfo_size = sizeof (cpuinfo);
212 if (sysctlbyname("kern.cp_time", &cpuinfo, &cpuinfo_size, NULL, 0) < 0)
214 syslog (LOG_WARNING, "cpu: sysctlbyname: %s", strerror (errno));
218 cpuinfo[CP_SYS] += cpuinfo[CP_INTR];
220 /* FIXME: Instance is always `0' */
221 cpu_submit (0, cpuinfo[CP_USER], cpuinfo[CP_NICE], cpuinfo[CP_SYS], cpuinfo[CP_IDLE], 0LL);
225 void module_register (void)
227 plugin_register (MODULE_NAME, cpu_init, cpu_read, cpu_write);
231 #endif /* COLLECT_CPU */