2 * collectd - src/vserver.c
3 * Copyright (C) 2006,2007 Sebastian Harl
4 * Copyright (C) 2007-2010 Florian octo Forster
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:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
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.
25 * Sebastian Harl <sh at tokkee.org>
26 * Florian octo Forster <octo at collectd.org>
32 #include "utils/common/common.h"
35 #include <sys/types.h>
39 #define PROCDIR "/proc/virtual"
42 #error "No applicable input method."
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();
53 } /* static void vserver_init(void) */
55 static void traffic_submit(const char *plugin_instance,
56 const char *type_instance, derive_t rx,
58 value_list_t vl = VALUE_LIST_INIT;
65 vl.values_len = STATIC_ARRAY_SIZE(values);
66 sstrncpy(vl.plugin, "vserver", sizeof(vl.plugin));
67 sstrncpy(vl.plugin_instance, plugin_instance, sizeof(vl.plugin_instance));
68 sstrncpy(vl.type, "if_octets", sizeof(vl.type));
69 sstrncpy(vl.type_instance, type_instance, sizeof(vl.type_instance));
71 plugin_dispatch_values(&vl);
72 } /* void traffic_submit */
74 static void load_submit(const char *plugin_instance, gauge_t snum, gauge_t mnum,
76 value_list_t vl = VALUE_LIST_INIT;
84 vl.values_len = STATIC_ARRAY_SIZE(values);
85 sstrncpy(vl.plugin, "vserver", sizeof(vl.plugin));
86 sstrncpy(vl.plugin_instance, plugin_instance, sizeof(vl.plugin_instance));
87 sstrncpy(vl.type, "load", sizeof(vl.type));
89 plugin_dispatch_values(&vl);
92 static void submit_gauge(const char *plugin_instance, const char *type,
93 const char *type_instance, gauge_t value)
96 value_list_t vl = VALUE_LIST_INIT;
98 vl.values = &(value_t){.gauge = value};
100 sstrncpy(vl.plugin, "vserver", sizeof(vl.plugin));
101 sstrncpy(vl.plugin_instance, plugin_instance, sizeof(vl.plugin_instance));
102 sstrncpy(vl.type, type, sizeof(vl.type));
103 sstrncpy(vl.type_instance, type_instance, sizeof(vl.type_instance));
105 plugin_dispatch_values(&vl);
106 } /* void submit_gauge */
108 static derive_t vserver_get_sock_bytes(const char *s) {
118 status = parse_value(s, &v, DS_TYPE_DERIVE);
124 static int vserver_read(void) {
128 proc = opendir(PROCDIR);
130 ERROR("vserver plugin: fopen (%s): %s", PROCDIR, STRERRNO);
140 char buffer[BUFSIZE];
148 dent = readdir(proc);
150 if (errno == 0) /* end of directory */
153 ERROR("vserver plugin: failed to read directory %s: %s", PROCDIR,
159 if (dent->d_name[0] == '.')
162 len = snprintf(file, sizeof(file), PROCDIR "/%s", dent->d_name);
163 if ((len < 0) || (len >= BUFSIZE))
166 status = stat(file, &statbuf);
168 WARNING("vserver plugin: stat (%s) failed: %s", file, STRERRNO);
172 if (!S_ISDIR(statbuf.st_mode))
175 /* socket message accounting */
176 len = snprintf(file, sizeof(file), PROCDIR "/%s/cacct", dent->d_name);
177 if ((len < 0) || ((size_t)len >= sizeof(file)))
180 if (NULL == (fh = fopen(file, "r"))) {
181 ERROR("Cannot open '%s': %s", file, STRERRNO);
184 while ((fh != NULL) && (NULL != fgets(buffer, BUFSIZE, fh))) {
187 const char *type_instance;
189 if (strsplit(buffer, cols, 4) < 4)
192 if (0 == strcmp(cols[0], "UNIX:"))
193 type_instance = "unix";
194 else if (0 == strcmp(cols[0], "INET:"))
195 type_instance = "inet";
196 else if (0 == strcmp(cols[0], "INET6:"))
197 type_instance = "inet6";
198 else if (0 == strcmp(cols[0], "OTHER:"))
199 type_instance = "other";
200 else if (0 == strcmp(cols[0], "UNSPEC:"))
201 type_instance = "unspec";
205 rx = vserver_get_sock_bytes(cols[1]);
206 tx = vserver_get_sock_bytes(cols[2]);
207 /* cols[3] == errors */
209 traffic_submit(dent->d_name, type_instance, rx, tx);
210 } /* while (fgets) */
217 /* thread information and load */
218 len = snprintf(file, sizeof(file), PROCDIR "/%s/cvirt", dent->d_name);
219 if ((len < 0) || ((size_t)len >= sizeof(file)))
222 if (NULL == (fh = fopen(file, "r"))) {
223 ERROR("Cannot open '%s': %s", file, STRERRNO);
226 while ((fh != NULL) && (NULL != fgets(buffer, BUFSIZE, fh))) {
227 int n = strsplit(buffer, cols, 4);
230 const char *type_instance;
233 if (0 == strcmp(cols[0], "nr_threads:"))
234 type_instance = "total";
235 else if (0 == strcmp(cols[0], "nr_running:"))
236 type_instance = "running";
237 else if (0 == strcmp(cols[0], "nr_unintr:"))
238 type_instance = "uninterruptable";
239 else if (0 == strcmp(cols[0], "nr_onhold:"))
240 type_instance = "onhold";
244 value = atof(cols[1]);
245 submit_gauge(dent->d_name, "vs_threads", type_instance, value);
247 if (0 == strcmp(cols[0], "loadavg:")) {
248 gauge_t snum = atof(cols[1]);
249 gauge_t mnum = atof(cols[2]);
250 gauge_t lnum = atof(cols[3]);
251 load_submit(dent->d_name, snum, mnum, lnum);
254 } /* while (fgets) */
261 /* processes and memory usage */
262 len = snprintf(file, sizeof(file), PROCDIR "/%s/limit", dent->d_name);
263 if ((len < 0) || ((size_t)len >= sizeof(file)))
266 if (NULL == (fh = fopen(file, "r"))) {
267 ERROR("Cannot open '%s': %s", file, STRERRNO);
270 while ((fh != NULL) && (NULL != fgets(buffer, BUFSIZE, fh))) {
271 const char *type = "vs_memory";
272 const char *type_instance;
275 if (strsplit(buffer, cols, 2) < 2)
278 if (0 == strcmp(cols[0], "PROC:")) {
279 type = "vs_processes";
281 value = atof(cols[1]);
283 if (0 == strcmp(cols[0], "VM:"))
284 type_instance = "vm";
285 else if (0 == strcmp(cols[0], "VML:"))
286 type_instance = "vml";
287 else if (0 == strcmp(cols[0], "RSS:"))
288 type_instance = "rss";
289 else if (0 == strcmp(cols[0], "ANON:"))
290 type_instance = "anon";
294 value = atof(cols[1]) * pagesize;
297 submit_gauge(dent->d_name, type, type_instance, value);
298 } /* while (fgets) */
304 } /* while (readdir) */
309 } /* int vserver_read */
311 void module_register(void) {
312 plugin_register_init("vserver", vserver_init);
313 plugin_register_read("vserver", vserver_read);
314 } /* void module_register(void) */