Tree wide: Reformat with clang-format.
[collectd.git] / src / vserver.c
1 /**
2  * collectd - src/vserver.c
3  * Copyright (C) 2006,2007  Sebastian Harl
4  * Copyright (C) 2007-2010  Florian octo Forster
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining a
7  * copy of this software and associated documentation files (the "Software"),
8  * to deal in the Software without restriction, including without limitation
9  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10  * and/or sell copies of the Software, and to permit persons to whom the
11  * Software is furnished to do so, subject to the following conditions:
12  *
13  * The above copyright notice and this permission notice shall be included in
14  * all copies or substantial portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
22  * DEALINGS IN THE SOFTWARE.
23  *
24  * Authors:
25  *   Sebastian Harl <sh at tokkee.org>
26  *   Florian octo Forster <octo at collectd.org>
27  **/
28
29 #include "collectd.h"
30
31 #include "common.h"
32 #include "plugin.h"
33
34 #include <dirent.h>
35 #include <sys/types.h>
36
37 #define BUFSIZE 512
38
39 #define PROCDIR "/proc/virtual"
40
41 #if !KERNEL_LINUX
42 #error "No applicable input method."
43 #endif
44
45 static int pagesize = 0;
46
47 static int vserver_init(void) {
48   /* XXX Should we check for getpagesize () in configure?
49    * What's the right thing to do, if there is no getpagesize ()? */
50   pagesize = getpagesize();
51
52   return (0);
53 } /* static void vserver_init(void) */
54
55 static void traffic_submit(const char *plugin_instance,
56                            const char *type_instance, derive_t rx,
57                            derive_t tx) {
58   value_t values[2];
59   value_list_t vl = VALUE_LIST_INIT;
60
61   values[0].derive = rx;
62   values[1].derive = tx;
63
64   vl.values = values;
65   vl.values_len = STATIC_ARRAY_SIZE(values);
66   sstrncpy(vl.host, hostname_g, sizeof(vl.host));
67   sstrncpy(vl.plugin, "vserver", sizeof(vl.plugin));
68   sstrncpy(vl.plugin_instance, plugin_instance, sizeof(vl.plugin_instance));
69   sstrncpy(vl.type, "if_octets", sizeof(vl.type));
70   sstrncpy(vl.type_instance, type_instance, sizeof(vl.type_instance));
71
72   plugin_dispatch_values(&vl);
73 } /* void traffic_submit */
74
75 static void load_submit(const char *plugin_instance, gauge_t snum, gauge_t mnum,
76                         gauge_t lnum) {
77   value_t values[3];
78   value_list_t vl = VALUE_LIST_INIT;
79
80   values[0].gauge = snum;
81   values[1].gauge = mnum;
82   values[2].gauge = lnum;
83
84   vl.values = values;
85   vl.values_len = STATIC_ARRAY_SIZE(values);
86   sstrncpy(vl.host, hostname_g, sizeof(vl.host));
87   sstrncpy(vl.plugin, "vserver", sizeof(vl.plugin));
88   sstrncpy(vl.plugin_instance, plugin_instance, sizeof(vl.plugin_instance));
89   sstrncpy(vl.type, "load", sizeof(vl.type));
90
91   plugin_dispatch_values(&vl);
92 }
93
94 static void submit_gauge(const char *plugin_instance, const char *type,
95                          const char *type_instance, gauge_t value)
96
97 {
98   value_t values[1];
99   value_list_t vl = VALUE_LIST_INIT;
100
101   values[0].gauge = value;
102
103   vl.values = values;
104   vl.values_len = STATIC_ARRAY_SIZE(values);
105   sstrncpy(vl.host, hostname_g, sizeof(vl.host));
106   sstrncpy(vl.plugin, "vserver", sizeof(vl.plugin));
107   sstrncpy(vl.plugin_instance, plugin_instance, sizeof(vl.plugin_instance));
108   sstrncpy(vl.type, type, sizeof(vl.type));
109   sstrncpy(vl.type_instance, type_instance, sizeof(vl.type_instance));
110
111   plugin_dispatch_values(&vl);
112 } /* void submit_gauge */
113
114 static derive_t vserver_get_sock_bytes(const char *s) {
115   value_t v;
116   int status;
117
118   while (s[0] != '/')
119     ++s;
120
121   /* Remove '/' */
122   ++s;
123
124   status = parse_value(s, &v, DS_TYPE_DERIVE);
125   if (status != 0)
126     return (-1);
127   return (v.derive);
128 }
129
130 static int vserver_read(void) {
131   DIR *proc;
132
133   errno = 0;
134   proc = opendir(PROCDIR);
135   if (proc == NULL) {
136     char errbuf[1024];
137     ERROR("vserver plugin: fopen (%s): %s", PROCDIR,
138           sstrerror(errno, errbuf, sizeof(errbuf)));
139     return (-1);
140   }
141
142   while (42) {
143     struct dirent *dent;
144     int len;
145     char file[BUFSIZE];
146
147     FILE *fh;
148     char buffer[BUFSIZE];
149
150     struct stat statbuf;
151     char *cols[4];
152
153     int status;
154
155     errno = 0;
156     dent = readdir(proc);
157     if (dent == NULL) {
158       char errbuf[4096];
159
160       if (errno == 0) /* end of directory */
161         break;
162
163       ERROR("vserver plugin: failed to read directory %s: %s", PROCDIR,
164             sstrerror(errno, errbuf, sizeof(errbuf)));
165       closedir(proc);
166       return (-1);
167     }
168
169     if (dent->d_name[0] == '.')
170       continue;
171
172     len = ssnprintf(file, sizeof(file), PROCDIR "/%s", dent->d_name);
173     if ((len < 0) || (len >= BUFSIZE))
174       continue;
175
176     status = stat(file, &statbuf);
177     if (status != 0) {
178       char errbuf[4096];
179       WARNING("vserver plugin: stat (%s) failed: %s", file,
180               sstrerror(errno, errbuf, sizeof(errbuf)));
181       continue;
182     }
183
184     if (!S_ISDIR(statbuf.st_mode))
185       continue;
186
187     /* socket message accounting */
188     len = ssnprintf(file, sizeof(file), PROCDIR "/%s/cacct", dent->d_name);
189     if ((len < 0) || ((size_t)len >= sizeof(file)))
190       continue;
191
192     if (NULL == (fh = fopen(file, "r"))) {
193       char errbuf[1024];
194       ERROR("Cannot open '%s': %s", file,
195             sstrerror(errno, errbuf, sizeof(errbuf)));
196     }
197
198     while ((fh != NULL) && (NULL != fgets(buffer, BUFSIZE, fh))) {
199       derive_t rx;
200       derive_t tx;
201       const char *type_instance;
202
203       if (strsplit(buffer, cols, 4) < 4)
204         continue;
205
206       if (0 == strcmp(cols[0], "UNIX:"))
207         type_instance = "unix";
208       else if (0 == strcmp(cols[0], "INET:"))
209         type_instance = "inet";
210       else if (0 == strcmp(cols[0], "INET6:"))
211         type_instance = "inet6";
212       else if (0 == strcmp(cols[0], "OTHER:"))
213         type_instance = "other";
214       else if (0 == strcmp(cols[0], "UNSPEC:"))
215         type_instance = "unspec";
216       else
217         continue;
218
219       rx = vserver_get_sock_bytes(cols[1]);
220       tx = vserver_get_sock_bytes(cols[2]);
221       /* cols[3] == errors */
222
223       traffic_submit(dent->d_name, type_instance, rx, tx);
224     } /* while (fgets) */
225
226     if (fh != NULL) {
227       fclose(fh);
228       fh = NULL;
229     }
230
231     /* thread information and load */
232     len = ssnprintf(file, sizeof(file), PROCDIR "/%s/cvirt", dent->d_name);
233     if ((len < 0) || ((size_t)len >= sizeof(file)))
234       continue;
235
236     if (NULL == (fh = fopen(file, "r"))) {
237       char errbuf[1024];
238       ERROR("Cannot open '%s': %s", file,
239             sstrerror(errno, errbuf, sizeof(errbuf)));
240     }
241
242     while ((fh != NULL) && (NULL != fgets(buffer, BUFSIZE, fh))) {
243       int n = strsplit(buffer, cols, 4);
244
245       if (2 == n) {
246         const char *type_instance;
247         gauge_t value;
248
249         if (0 == strcmp(cols[0], "nr_threads:"))
250           type_instance = "total";
251         else if (0 == strcmp(cols[0], "nr_running:"))
252           type_instance = "running";
253         else if (0 == strcmp(cols[0], "nr_unintr:"))
254           type_instance = "uninterruptable";
255         else if (0 == strcmp(cols[0], "nr_onhold:"))
256           type_instance = "onhold";
257         else
258           continue;
259
260         value = atof(cols[1]);
261         submit_gauge(dent->d_name, "vs_threads", type_instance, value);
262       } else if (4 == n) {
263         if (0 == strcmp(cols[0], "loadavg:")) {
264           gauge_t snum = atof(cols[1]);
265           gauge_t mnum = atof(cols[2]);
266           gauge_t lnum = atof(cols[3]);
267           load_submit(dent->d_name, snum, mnum, lnum);
268         }
269       }
270     } /* while (fgets) */
271
272     if (fh != NULL) {
273       fclose(fh);
274       fh = NULL;
275     }
276
277     /* processes and memory usage */
278     len = ssnprintf(file, sizeof(file), PROCDIR "/%s/limit", dent->d_name);
279     if ((len < 0) || ((size_t)len >= sizeof(file)))
280       continue;
281
282     if (NULL == (fh = fopen(file, "r"))) {
283       char errbuf[1024];
284       ERROR("Cannot open '%s': %s", file,
285             sstrerror(errno, errbuf, sizeof(errbuf)));
286     }
287
288     while ((fh != NULL) && (NULL != fgets(buffer, BUFSIZE, fh))) {
289       const char *type = "vs_memory";
290       const char *type_instance;
291       gauge_t value;
292
293       if (strsplit(buffer, cols, 2) < 2)
294         continue;
295
296       if (0 == strcmp(cols[0], "PROC:")) {
297         type = "vs_processes";
298         type_instance = "";
299         value = atof(cols[1]);
300       } else {
301         if (0 == strcmp(cols[0], "VM:"))
302           type_instance = "vm";
303         else if (0 == strcmp(cols[0], "VML:"))
304           type_instance = "vml";
305         else if (0 == strcmp(cols[0], "RSS:"))
306           type_instance = "rss";
307         else if (0 == strcmp(cols[0], "ANON:"))
308           type_instance = "anon";
309         else
310           continue;
311
312         value = atof(cols[1]) * pagesize;
313       }
314
315       submit_gauge(dent->d_name, type, type_instance, value);
316     } /* while (fgets) */
317
318     if (fh != NULL) {
319       fclose(fh);
320       fh = NULL;
321     }
322   } /* while (readdir) */
323
324   closedir(proc);
325
326   return (0);
327 } /* int vserver_read */
328
329 void module_register(void) {
330   plugin_register_init("vserver", vserver_init);
331   plugin_register_read("vserver", vserver_read);
332 } /* void module_register(void) */
333
334 /* vim: set ts=4 sw=4 noexpandtab : */