Code

830f8acec97402e1fb2f3962514e3a5d4d63a984
[collectd.git] / src / cpu.c
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 "cpu.h"
25 #if COLLECT_CPU
26 #define MODULE_NAME "cpu"
28 #ifdef HAVE_LIBKSTAT
29 #include <sys/sysinfo.h>
30 #endif /* HAVE_LIBKSTAT */
32 #ifdef HAVE_SYSCTLBYNAME
33 #ifdef HAVE_SYS_SYSCTL_H
34 #include <sys/sysctl.h>
35 #endif
37 #ifdef HAVE_SYS_DKSTAT_H
38 #include <sys/dkstat.h>
39 #endif
41 #if !defined(CP_USER) || !defined(CP_NICE) || !defined(CP_SYS) || !defined(CP_INTR) || !defined(CP_IDLE) || !defined(CPUSTATES)
42 #define CP_USER   0
43 #define CP_NICE   1
44 #define CP_SYS    2
45 #define CP_INTR   3
46 #define CP_IDLE   4
47 #define CPUSTATES 5
48 #endif
49 #endif /* HAVE_SYSCTLBYNAME */
51 #include "plugin.h"
52 #include "common.h"
54 #ifdef HAVE_LIBKSTAT
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];
59 static int numcpu;
60 #endif /* HAVE_LIBKSTAT */
62 #ifdef HAVE_SYSCTLBYNAME
63 static int numcpu;
64 #endif /* HAVE_SYSCTLBYNAME */
66 static char *cpu_filename = "cpu-%s.rrd";
68 static char *ds_def[] =
69 {
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",
75         NULL
76 };
77 static int ds_num = 5;
79 extern time_t curtime;
81 void cpu_init (void)
82 {
83 #ifdef HAVE_LIBKSTAT
84         kstat_t *ksp_chain;
86         numcpu = 0;
88         if (kc == NULL)
89                 return;
91         /* Solaris doesn't count linear.. *sigh* */
92         for (numcpu = 0, ksp_chain = kc->kc_chain;
93                         (numcpu < MAX_NUMCPU) && (ksp_chain != NULL);
94                         ksp_chain = ksp_chain->ks_next)
95                 if (strncmp (ksp_chain->ks_module, "cpu_stat", 8) == 0)
96                         ksp[numcpu++] = ksp_chain;
97 /* #endif HAVE_LIBKSTAT */
99 #elif defined (HAVE_SYSCTLBYNAME)
100         size_t numcpu_size;
102         numcpu_size = sizeof (numcpu);
104         if (sysctlbyname ("hw.ncpu", &numcpu, &numcpu_size, NULL, 0) < 0)
105         {
106                 syslog (LOG_WARNING, "cpu: sysctlbyname: %s", strerror (errno));
107                 return;
108         }
110         if (numcpu != 1)
111                 syslog (LOG_NOTICE, "cpu: Only one processor supported when using `sysctlbyname' (found %i)", numcpu);
112 #endif
114         return;
117 void cpu_write (char *host, char *inst, char *val)
119         char file[512];
120         int status;
122         status = snprintf (file, 512, cpu_filename, inst);
123         if (status < 1)
124                 return;
125         else if (status >= 512)
126                 return;
128         rrd_update_file (host, file, val, ds_def, ds_num);
131 #define BUFSIZE 512
132 void cpu_submit (int cpu_num, unsigned long long user, unsigned long long nice, unsigned long long syst,
133                 unsigned long long idle, unsigned long long wait)
135         char buf[BUFSIZE];
136         char cpu[16];
138         if (snprintf (buf, BUFSIZE, "%u:%llu:%llu:%llu:%llu:%llu", (unsigned int) curtime,
139                                 user, nice, syst, idle, wait) >= BUFSIZE)
140                 return;
141         snprintf (cpu, 16, "%i", cpu_num);
143         plugin_submit (MODULE_NAME, cpu, buf);
145 #undef BUFSIZE
147 void cpu_read (void)
149 #ifdef KERNEL_LINUX
150 #define BUFSIZE 1024
151         int cpu;
152         unsigned long long user, nice, syst, idle;
153         unsigned long long wait, intr, sitr; /* sitr == soft interrupt */
154         FILE *fh;
155         char buf[BUFSIZE];
157         char *fields[9];
158         int numfields;
160         if ((fh = fopen ("/proc/stat", "r")) == NULL)
161         {
162                 syslog (LOG_WARNING, "cpu: fopen: %s", strerror (errno));
163                 return;
164         }
166         while (fgets (buf, BUFSIZE, fh) != NULL)
167         {
168                 if (strncmp (buf, "cpu", 3))
169                         continue;
170                 if ((buf[3] < '0') || (buf[3] > '9'))
171                         continue;
173                 numfields = strsplit (buf, fields, 9);
174                 if (numfields < 5)
175                         continue;
177                 cpu = atoi (fields[0] + 3);
178                 user = atoll (fields[1]);
179                 nice = atoll (fields[2]);
180                 syst = atoll (fields[3]);
181                 idle = atoll (fields[4]);
183                 if (numfields >= 8)
184                 {
185                         wait = atoll (fields[5]);
186                         intr = atoll (fields[6]);
187                         sitr = atoll (fields[7]);
189                         /* I doubt anyone cares about the time spent in
190                          * interrupt handlers.. */
191                         syst += intr + sitr;
192                 }
193                 else
194                 {
195                         wait = 0LL;
196                 }
198                 cpu_submit (cpu, user, nice, syst, idle, wait);
199         }
201         fclose (fh);
202 #undef BUFSIZE
203 /* #endif defined(KERNEL_LINUX) */
205 #elif defined(HAVE_LIBKSTAT)
206         int cpu;
207         unsigned long long user, syst, idle, wait;
208         static cpu_stat_t cs;
210         if (kc == NULL)
211                 return;
213         for (cpu = 0; cpu < numcpu; cpu++)
214         {
215                 if (kstat_read (kc, ksp[cpu], &cs) == -1)
216                         continue; /* error message? */
218                 idle = (unsigned long long) cs.cpu_sysinfo.cpu[CPU_IDLE];
219                 user = (unsigned long long) cs.cpu_sysinfo.cpu[CPU_USER];
220                 syst = (unsigned long long) cs.cpu_sysinfo.cpu[CPU_KERNEL];
221                 wait = (unsigned long long) cs.cpu_sysinfo.cpu[CPU_WAIT];
223                 cpu_submit (ksp[cpu]->ks_instance,
224                                 user, 0LL, syst, idle, wait);
225         }
226 /* #endif defined(HAVE_LIBKSTAT) */
228 #elif defined (HAVE_SYSCTLBYNAME)
229         long cpuinfo[CPUSTATES];
230         size_t cpuinfo_size;
232         cpuinfo_size = sizeof (cpuinfo);
234         if (sysctlbyname("kern.cp_time", &cpuinfo, &cpuinfo_size, NULL, 0) < 0)
235         {
236                 syslog (LOG_WARNING, "cpu: sysctlbyname: %s", strerror (errno));
237                 return;
238         }
240         cpuinfo[CP_SYS] += cpuinfo[CP_INTR];
242         /* FIXME: Instance is always `0' */
243         cpu_submit (0, cpuinfo[CP_USER], cpuinfo[CP_NICE], cpuinfo[CP_SYS], cpuinfo[CP_IDLE], 0LL);
244 #endif
247 void module_register (void)
249         plugin_register (MODULE_NAME, cpu_init, cpu_read, cpu_write);
252 #undef MODULE_NAME
253 #endif /* COLLECT_CPU */