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);
128 ERROR("vserver plugin: fopen (%s): %s", PROCDIR,
129 sstrerror(errno, errbuf, sizeof(errbuf)));
139 char buffer[BUFSIZE];
147 dent = readdir(proc);
151 if (errno == 0) /* end of directory */
154 ERROR("vserver plugin: failed to read directory %s: %s", PROCDIR,
155 sstrerror(errno, errbuf, sizeof(errbuf)));
160 if (dent->d_name[0] == '.')
163 len = ssnprintf(file, sizeof(file), PROCDIR "/%s", dent->d_name);
164 if ((len < 0) || (len >= BUFSIZE))
167 status = stat(file, &statbuf);
170 WARNING("vserver plugin: stat (%s) failed: %s", file,
171 sstrerror(errno, errbuf, sizeof(errbuf)));
175 if (!S_ISDIR(statbuf.st_mode))
178 /* socket message accounting */
179 len = ssnprintf(file, sizeof(file), PROCDIR "/%s/cacct", dent->d_name);
180 if ((len < 0) || ((size_t)len >= sizeof(file)))
183 if (NULL == (fh = fopen(file, "r"))) {
185 ERROR("Cannot open '%s': %s", file,
186 sstrerror(errno, errbuf, sizeof(errbuf)));
189 while ((fh != NULL) && (NULL != fgets(buffer, BUFSIZE, fh))) {
192 const char *type_instance;
194 if (strsplit(buffer, cols, 4) < 4)
197 if (0 == strcmp(cols[0], "UNIX:"))
198 type_instance = "unix";
199 else if (0 == strcmp(cols[0], "INET:"))
200 type_instance = "inet";
201 else if (0 == strcmp(cols[0], "INET6:"))
202 type_instance = "inet6";
203 else if (0 == strcmp(cols[0], "OTHER:"))
204 type_instance = "other";
205 else if (0 == strcmp(cols[0], "UNSPEC:"))
206 type_instance = "unspec";
210 rx = vserver_get_sock_bytes(cols[1]);
211 tx = vserver_get_sock_bytes(cols[2]);
212 /* cols[3] == errors */
214 traffic_submit(dent->d_name, type_instance, rx, tx);
215 } /* while (fgets) */
222 /* thread information and load */
223 len = ssnprintf(file, sizeof(file), PROCDIR "/%s/cvirt", dent->d_name);
224 if ((len < 0) || ((size_t)len >= sizeof(file)))
227 if (NULL == (fh = fopen(file, "r"))) {
229 ERROR("Cannot open '%s': %s", file,
230 sstrerror(errno, errbuf, sizeof(errbuf)));
233 while ((fh != NULL) && (NULL != fgets(buffer, BUFSIZE, fh))) {
234 int n = strsplit(buffer, cols, 4);
237 const char *type_instance;
240 if (0 == strcmp(cols[0], "nr_threads:"))
241 type_instance = "total";
242 else if (0 == strcmp(cols[0], "nr_running:"))
243 type_instance = "running";
244 else if (0 == strcmp(cols[0], "nr_unintr:"))
245 type_instance = "uninterruptable";
246 else if (0 == strcmp(cols[0], "nr_onhold:"))
247 type_instance = "onhold";
251 value = atof(cols[1]);
252 submit_gauge(dent->d_name, "vs_threads", type_instance, value);
254 if (0 == strcmp(cols[0], "loadavg:")) {
255 gauge_t snum = atof(cols[1]);
256 gauge_t mnum = atof(cols[2]);
257 gauge_t lnum = atof(cols[3]);
258 load_submit(dent->d_name, snum, mnum, lnum);
261 } /* while (fgets) */
268 /* processes and memory usage */
269 len = ssnprintf(file, sizeof(file), PROCDIR "/%s/limit", dent->d_name);
270 if ((len < 0) || ((size_t)len >= sizeof(file)))
273 if (NULL == (fh = fopen(file, "r"))) {
275 ERROR("Cannot open '%s': %s", file,
276 sstrerror(errno, errbuf, sizeof(errbuf)));
279 while ((fh != NULL) && (NULL != fgets(buffer, BUFSIZE, fh))) {
280 const char *type = "vs_memory";
281 const char *type_instance;
284 if (strsplit(buffer, cols, 2) < 2)
287 if (0 == strcmp(cols[0], "PROC:")) {
288 type = "vs_processes";
290 value = atof(cols[1]);
292 if (0 == strcmp(cols[0], "VM:"))
293 type_instance = "vm";
294 else if (0 == strcmp(cols[0], "VML:"))
295 type_instance = "vml";
296 else if (0 == strcmp(cols[0], "RSS:"))
297 type_instance = "rss";
298 else if (0 == strcmp(cols[0], "ANON:"))
299 type_instance = "anon";
303 value = atof(cols[1]) * pagesize;
306 submit_gauge(dent->d_name, type, type_instance, value);
307 } /* while (fgets) */
313 } /* while (readdir) */
318 } /* int vserver_read */
320 void module_register(void) {
321 plugin_register_init("vserver", vserver_init);
322 plugin_register_read("vserver", vserver_read);
323 } /* void module_register(void) */