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>
35 #include <sys/types.h>
39 #define PROCDIR "/proc/virtual"
42 #error "No applicable input method."
45 static int pagesize = 0;
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;
60 {.derive = rx}, {.derive = tx},
64 vl.values_len = STATIC_ARRAY_SIZE(values);
65 sstrncpy(vl.plugin, "vserver", sizeof(vl.plugin));
66 sstrncpy(vl.plugin_instance, plugin_instance, sizeof(vl.plugin_instance));
67 sstrncpy(vl.type, "if_octets", sizeof(vl.type));
68 sstrncpy(vl.type_instance, type_instance, sizeof(vl.type_instance));
70 plugin_dispatch_values(&vl);
71 } /* void traffic_submit */
73 static void load_submit(const char *plugin_instance, gauge_t snum, gauge_t mnum,
75 value_list_t vl = VALUE_LIST_INIT;
77 {.gauge = snum}, {.gauge = mnum}, {.gauge = lnum},
81 vl.values_len = STATIC_ARRAY_SIZE(values);
82 sstrncpy(vl.plugin, "vserver", sizeof(vl.plugin));
83 sstrncpy(vl.plugin_instance, plugin_instance, sizeof(vl.plugin_instance));
84 sstrncpy(vl.type, "load", sizeof(vl.type));
86 plugin_dispatch_values(&vl);
89 static void submit_gauge(const char *plugin_instance, const char *type,
90 const char *type_instance, gauge_t value)
93 value_list_t vl = VALUE_LIST_INIT;
95 vl.values = &(value_t){.gauge = value};
97 sstrncpy(vl.plugin, "vserver", sizeof(vl.plugin));
98 sstrncpy(vl.plugin_instance, plugin_instance, sizeof(vl.plugin_instance));
99 sstrncpy(vl.type, type, sizeof(vl.type));
100 sstrncpy(vl.type_instance, type_instance, sizeof(vl.type_instance));
102 plugin_dispatch_values(&vl);
103 } /* void submit_gauge */
105 static derive_t vserver_get_sock_bytes(const char *s) {
115 status = parse_value(s, &v, DS_TYPE_DERIVE);
121 static int vserver_read(void) {
125 proc = opendir(PROCDIR);
127 ERROR("vserver plugin: fopen (%s): %s", PROCDIR, STRERRNO);
137 char buffer[BUFSIZE];
145 dent = readdir(proc);
147 if (errno == 0) /* end of directory */
150 ERROR("vserver plugin: failed to read directory %s: %s", PROCDIR,
156 if (dent->d_name[0] == '.')
159 len = snprintf(file, sizeof(file), PROCDIR "/%s", dent->d_name);
160 if ((len < 0) || (len >= BUFSIZE))
163 status = stat(file, &statbuf);
165 WARNING("vserver plugin: stat (%s) failed: %s", file, STRERRNO);
169 if (!S_ISDIR(statbuf.st_mode))
172 /* socket message accounting */
173 len = snprintf(file, sizeof(file), PROCDIR "/%s/cacct", dent->d_name);
174 if ((len < 0) || ((size_t)len >= sizeof(file)))
177 if (NULL == (fh = fopen(file, "r"))) {
178 ERROR("Cannot open '%s': %s", file, STRERRNO);
181 while ((fh != NULL) && (NULL != fgets(buffer, BUFSIZE, fh))) {
184 const char *type_instance;
186 if (strsplit(buffer, cols, 4) < 4)
189 if (0 == strcmp(cols[0], "UNIX:"))
190 type_instance = "unix";
191 else if (0 == strcmp(cols[0], "INET:"))
192 type_instance = "inet";
193 else if (0 == strcmp(cols[0], "INET6:"))
194 type_instance = "inet6";
195 else if (0 == strcmp(cols[0], "OTHER:"))
196 type_instance = "other";
197 else if (0 == strcmp(cols[0], "UNSPEC:"))
198 type_instance = "unspec";
202 rx = vserver_get_sock_bytes(cols[1]);
203 tx = vserver_get_sock_bytes(cols[2]);
204 /* cols[3] == errors */
206 traffic_submit(dent->d_name, type_instance, rx, tx);
207 } /* while (fgets) */
214 /* thread information and load */
215 len = snprintf(file, sizeof(file), PROCDIR "/%s/cvirt", dent->d_name);
216 if ((len < 0) || ((size_t)len >= sizeof(file)))
219 if (NULL == (fh = fopen(file, "r"))) {
220 ERROR("Cannot open '%s': %s", file, STRERRNO);
223 while ((fh != NULL) && (NULL != fgets(buffer, BUFSIZE, fh))) {
224 int n = strsplit(buffer, cols, 4);
227 const char *type_instance;
230 if (0 == strcmp(cols[0], "nr_threads:"))
231 type_instance = "total";
232 else if (0 == strcmp(cols[0], "nr_running:"))
233 type_instance = "running";
234 else if (0 == strcmp(cols[0], "nr_unintr:"))
235 type_instance = "uninterruptable";
236 else if (0 == strcmp(cols[0], "nr_onhold:"))
237 type_instance = "onhold";
241 value = atof(cols[1]);
242 submit_gauge(dent->d_name, "vs_threads", type_instance, value);
244 if (0 == strcmp(cols[0], "loadavg:")) {
245 gauge_t snum = atof(cols[1]);
246 gauge_t mnum = atof(cols[2]);
247 gauge_t lnum = atof(cols[3]);
248 load_submit(dent->d_name, snum, mnum, lnum);
251 } /* while (fgets) */
258 /* processes and memory usage */
259 len = snprintf(file, sizeof(file), PROCDIR "/%s/limit", dent->d_name);
260 if ((len < 0) || ((size_t)len >= sizeof(file)))
263 if (NULL == (fh = fopen(file, "r"))) {
264 ERROR("Cannot open '%s': %s", file, STRERRNO);
267 while ((fh != NULL) && (NULL != fgets(buffer, BUFSIZE, fh))) {
268 const char *type = "vs_memory";
269 const char *type_instance;
272 if (strsplit(buffer, cols, 2) < 2)
275 if (0 == strcmp(cols[0], "PROC:")) {
276 type = "vs_processes";
278 value = atof(cols[1]);
280 if (0 == strcmp(cols[0], "VM:"))
281 type_instance = "vm";
282 else if (0 == strcmp(cols[0], "VML:"))
283 type_instance = "vml";
284 else if (0 == strcmp(cols[0], "RSS:"))
285 type_instance = "rss";
286 else if (0 == strcmp(cols[0], "ANON:"))
287 type_instance = "anon";
291 value = atof(cols[1]) * pagesize;
294 submit_gauge(dent->d_name, type, type_instance, value);
295 } /* while (fgets) */
301 } /* while (readdir) */
306 } /* int vserver_read */
308 void module_register(void) {
309 plugin_register_init("vserver", vserver_init);
310 plugin_register_read("vserver", vserver_read);
311 } /* void module_register(void) */