removed
[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  **/
22
23 #include "cpu.h"
24
25 #if COLLECT_CPU
26 #define MODULE_NAME "cpu"
27
28 #ifdef HAVE_LIBKSTAT
29 #include <sys/sysinfo.h>
30 #endif /* HAVE_LIBKSTAT */
31
32 #ifdef HAVE_SYSCTLBYNAME
33 #ifdef HAVE_SYS_SYSCTL_H
34 #include <sys/sysctl.h>
35 #endif
36
37 #ifdef HAVE_SYS_DKSTAT_H
38 #include <sys/dkstat.h>
39 #endif
40
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 */
50
51 #include "plugin.h"
52 #include "common.h"
53
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 */
61
62 #ifdef HAVE_SYSCTLBYNAME
63 static int numcpu;
64 #endif /* HAVE_SYSCTLBYNAME */
65
66 static char *cpu_filename = "cpu-%s.rrd";
67
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;
78
79 void cpu_init (void)
80 {
81 #ifdef HAVE_LIBKSTAT
82         kstat_t *ksp_chain;
83
84         numcpu = 0;
85
86         if (kc == NULL)
87                 return;
88
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 */
96
97 #elif defined (HAVE_SYSCTLBYNAME)
98         size_t numcpu_size;
99
100         numcpu_size = sizeof (numcpu);
101
102         if (sysctlbyname ("hw.ncpu", &numcpu, &numcpu_size, NULL, 0) < 0)
103         {
104                 syslog (LOG_WARNING, "cpu: sysctlbyname: %s", strerror (errno));
105                 return;
106         }
107
108         if (numcpu != 1)
109                 syslog (LOG_NOTICE, "cpu: Only one processor supported when using `sysctlbyname' (found %i)", numcpu);
110 #endif
111
112         return;
113 }
114
115 void cpu_write (char *host, char *inst, char *val)
116 {
117         char file[512];
118         int status;
119
120         status = snprintf (file, 512, cpu_filename, inst);
121         if (status < 1)
122                 return;
123         else if (status >= 512)
124                 return;
125
126         rrd_update_file (host, file, val, ds_def, ds_num);
127 }
128
129 #define BUFSIZE 512
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)
132 {
133         char buf[BUFSIZE];
134         char cpu[16];
135
136         if (snprintf (buf, BUFSIZE, "%u:%llu:%llu:%llu:%llu:%llu", (unsigned int) curtime,
137                                 user, nice, syst, idle, wait) >= BUFSIZE)
138                 return;
139         snprintf (cpu, 16, "%i", cpu_num);
140
141         plugin_submit (MODULE_NAME, cpu, buf);
142 }
143 #undef BUFSIZE
144
145 void cpu_read (void)
146 {
147 #ifdef KERNEL_LINUX
148 #define BUFSIZE 1024
149         int cpu;
150         unsigned long long user, nice, syst, idle;
151         unsigned long long wait, intr, sitr; /* sitr == soft interrupt */
152         FILE *fh;
153         char buf[BUFSIZE];
154
155         char *fields[9];
156         int numfields;
157
158         if ((fh = fopen ("/proc/stat", "r")) == NULL)
159         {
160                 syslog (LOG_WARNING, "cpu: fopen: %s", strerror (errno));
161                 return;
162         }
163
164         while (fgets (buf, BUFSIZE, fh) != NULL)
165         {
166                 if (strncmp (buf, "cpu", 3))
167                         continue;
168                 if ((buf[3] < '0') || (buf[3] > '9'))
169                         continue;
170
171                 numfields = strsplit (buf, fields, 9);
172                 if (numfields < 5)
173                         continue;
174
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]);
180
181                 if (numfields >= 8)
182                 {
183                         wait = atoll (fields[5]);
184                         intr = atoll (fields[6]);
185                         sitr = atoll (fields[7]);
186
187                         /* I doubt anyone cares about the time spent in
188                          * interrupt handlers.. */
189                         syst += intr + sitr;
190                 }
191                 else
192                 {
193                         wait = 0LL;
194                 }
195
196                 cpu_submit (cpu, user, nice, syst, idle, wait);
197         }
198
199         fclose (fh);
200 #undef BUFSIZE
201 /* #endif defined(KERNEL_LINUX) */
202
203 #elif defined(HAVE_LIBKSTAT)
204         int cpu;
205         unsigned long long user, syst, idle, wait;
206         static cpu_stat_t cs;
207
208         if (kc == NULL)
209                 return;
210
211         for (cpu = 0; cpu < numcpu; cpu++)
212         {
213                 if (kstat_read (kc, ksp[cpu], &cs) == -1)
214                         continue; /* error message? */
215
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];
220
221                 cpu_submit (ksp[cpu]->ks_instance,
222                                 user, 0LL, syst, idle, wait);
223         }
224 /* #endif defined(HAVE_LIBKSTAT) */
225
226 #elif defined (HAVE_SYSCTLBYNAME)
227         long cpuinfo[CPUSTATES];
228         size_t cpuinfo_size;
229
230         cpuinfo_size = sizeof (cpuinfo);
231
232         if (sysctlbyname("kern.cp_time", &cpuinfo, &cpuinfo_size, NULL, 0) < 0)
233         {
234                 syslog (LOG_WARNING, "cpu: sysctlbyname: %s", strerror (errno));
235                 return;
236         }
237
238         cpuinfo[CP_SYS] += cpuinfo[CP_INTR];
239
240         /* FIXME: Instance is always `0' */
241         cpu_submit (0, cpuinfo[CP_USER], cpuinfo[CP_NICE], cpuinfo[CP_SYS], cpuinfo[CP_IDLE], 0LL);
242 #endif
243 }
244
245 void module_register (void)
246 {
247         plugin_register (MODULE_NAME, cpu_init, cpu_read, cpu_write);
248 }
249
250 #undef MODULE_NAME
251 #endif /* COLLECT_CPU */