62e814578cb688d50bb5a857a34899f65bc520c2
1 /**
2 * collectd - src/cpu.c
3 * Copyright (C) 2005 Florian octo Forster
4 *
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.
9 *
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.
14 *
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
18 *
19 * Authors:
20 * Florian octo Forster <octo at verplant.org>
21 **/
23 #include "collectd.h"
24 #include "common.h"
25 #include "plugin.h"
27 #define MODULE_NAME "cpu"
29 #ifdef HAVE_LIBKSTAT
30 # include <sys/sysinfo.h>
31 #endif /* HAVE_LIBKSTAT */
33 #ifdef HAVE_SYSCTLBYNAME
34 # ifdef HAVE_SYS_SYSCTL_H
35 # include <sys/sysctl.h>
36 # endif
38 # ifdef HAVE_SYS_DKSTAT_H
39 # include <sys/dkstat.h>
40 # endif
42 # if !defined(CP_USER) || !defined(CP_NICE) || !defined(CP_SYS) || !defined(CP_INTR) || !defined(CP_IDLE) || !defined(CPUSTATES)
43 # define CP_USER 0
44 # define CP_NICE 1
45 # define CP_SYS 2
46 # define CP_INTR 3
47 # define CP_IDLE 4
48 # define CPUSTATES 5
49 # endif
50 #endif /* HAVE_SYSCTLBYNAME */
52 #if defined(KERNEL_LINUX) || defined(HAVE_LIBKSTAT) || defined(HAVE_SYSCTLBYNAME)
53 # define CPU_HAVE_READ 1
54 #else
55 # define CPU_HAVE_READ 0
56 #endif
58 #ifdef HAVE_LIBKSTAT
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];
63 static int numcpu;
64 #endif /* HAVE_LIBKSTAT */
66 #ifdef HAVE_SYSCTLBYNAME
67 static int numcpu;
68 #endif /* HAVE_SYSCTLBYNAME */
70 static char *cpu_filename = "cpu-%s.rrd";
72 static char *ds_def[] =
73 {
74 "DS:user:COUNTER:25:0:100",
75 "DS:nice:COUNTER:25:0:100",
76 "DS:syst:COUNTER:25:0:100",
77 "DS:idle:COUNTER:25:0:100",
78 "DS:wait:COUNTER:25:0:100",
79 NULL
80 };
81 static int ds_num = 5;
83 static void cpu_init (void)
84 {
85 #ifdef HAVE_LIBKSTAT
86 kstat_t *ksp_chain;
88 numcpu = 0;
90 if (kc == NULL)
91 return;
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)
102 size_t numcpu_size;
104 numcpu_size = sizeof (numcpu);
106 if (sysctlbyname ("hw.ncpu", &numcpu, &numcpu_size, NULL, 0) < 0)
107 {
108 syslog (LOG_WARNING, "cpu: sysctlbyname: %s", strerror (errno));
109 return;
110 }
112 if (numcpu != 1)
113 syslog (LOG_NOTICE, "cpu: Only one processor supported when using `sysctlbyname' (found %i)", numcpu);
114 #endif
116 return;
117 }
119 static void cpu_write (char *host, char *inst, char *val)
120 {
121 char file[512];
122 int status;
124 status = snprintf (file, 512, cpu_filename, inst);
125 if (status < 1)
126 return;
127 else if (status >= 512)
128 return;
130 rrd_update_file (host, file, val, ds_def, ds_num);
131 }
133 #if CPU_HAVE_READ
134 #define BUFSIZE 512
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)
138 {
139 char buf[BUFSIZE];
140 char cpu[16];
142 if (snprintf (buf, BUFSIZE, "%u:%llu:%llu:%llu:%llu:%llu", (unsigned int) curtime,
143 user, nice, syst, idle, wait) >= BUFSIZE)
144 return;
145 snprintf (cpu, 16, "%i", cpu_num);
147 plugin_submit (MODULE_NAME, cpu, buf);
148 }
149 #undef BUFSIZE
151 static void cpu_read (void)
152 {
153 #ifdef KERNEL_LINUX
154 #define BUFSIZE 1024
155 int cpu;
156 unsigned long long user, nice, syst, idle;
157 unsigned long long wait, intr, sitr; /* sitr == soft interrupt */
158 FILE *fh;
159 char buf[BUFSIZE];
161 char *fields[9];
162 int numfields;
164 if ((fh = fopen ("/proc/stat", "r")) == NULL)
165 {
166 syslog (LOG_WARNING, "cpu: fopen: %s", strerror (errno));
167 return;
168 }
170 while (fgets (buf, BUFSIZE, fh) != NULL)
171 {
172 if (strncmp (buf, "cpu", 3))
173 continue;
174 if ((buf[3] < '0') || (buf[3] > '9'))
175 continue;
177 numfields = strsplit (buf, fields, 9);
178 if (numfields < 5)
179 continue;
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]);
187 if (numfields >= 8)
188 {
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.. */
195 syst += intr + sitr;
196 }
197 else
198 {
199 wait = 0LL;
200 }
202 cpu_submit (cpu, user, nice, syst, idle, wait);
203 }
205 fclose (fh);
206 #undef BUFSIZE
207 /* #endif defined(KERNEL_LINUX) */
209 #elif defined(HAVE_LIBKSTAT)
210 int cpu;
211 unsigned long long user, syst, idle, wait;
212 static cpu_stat_t cs;
214 if (kc == NULL)
215 return;
217 for (cpu = 0; cpu < numcpu; cpu++)
218 {
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);
229 }
230 /* #endif defined(HAVE_LIBKSTAT) */
232 #elif defined(HAVE_SYSCTLBYNAME)
233 long cpuinfo[CPUSTATES];
234 size_t cpuinfo_size;
236 cpuinfo_size = sizeof (cpuinfo);
238 if (sysctlbyname("kern.cp_time", &cpuinfo, &cpuinfo_size, NULL, 0) < 0)
239 {
240 syslog (LOG_WARNING, "cpu: sysctlbyname: %s", strerror (errno));
241 return;
242 }
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);
248 #endif
250 return;
251 }
252 #else
253 # define cpu_read NULL
254 #endif /* CPU_HAVE_READ */
256 void module_register (void)
257 {
258 plugin_register (MODULE_NAME, cpu_init, cpu_read, cpu_write);
259 }
261 #undef MODULE_NAME