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)
49 /* XXX Should we check for getpagesize () in configure?
50 * What's the right thing to do, if there is no getpagesize ()? */
51 pagesize = getpagesize ();
54 } /* static void vserver_init(void) */
56 static void traffic_submit (const char *plugin_instance,
57 const char *type_instance, derive_t rx, derive_t tx)
60 value_list_t vl = VALUE_LIST_INIT;
62 values[0].derive = rx;
63 values[1].derive = tx;
66 vl.values_len = STATIC_ARRAY_SIZE (values);
67 sstrncpy (vl.host, hostname_g, sizeof (vl.host));
68 sstrncpy (vl.plugin, "vserver", sizeof (vl.plugin));
69 sstrncpy (vl.plugin_instance, plugin_instance, sizeof (vl.plugin_instance));
70 sstrncpy (vl.type, "if_octets", sizeof (vl.type));
71 sstrncpy (vl.type_instance, type_instance, sizeof (vl.type_instance));
73 plugin_dispatch_values (&vl);
74 } /* void traffic_submit */
76 static void load_submit (const char *plugin_instance,
77 gauge_t snum, gauge_t mnum, gauge_t lnum)
80 value_list_t vl = VALUE_LIST_INIT;
82 values[0].gauge = snum;
83 values[1].gauge = mnum;
84 values[2].gauge = lnum;
87 vl.values_len = STATIC_ARRAY_SIZE (values);
88 sstrncpy (vl.host, hostname_g, sizeof (vl.host));
89 sstrncpy (vl.plugin, "vserver", sizeof (vl.plugin));
90 sstrncpy (vl.plugin_instance, plugin_instance, sizeof (vl.plugin_instance));
91 sstrncpy (vl.type, "load", sizeof (vl.type));
93 plugin_dispatch_values (&vl);
96 static void submit_gauge (const char *plugin_instance, const char *type,
97 const char *type_instance, gauge_t value)
101 value_list_t vl = VALUE_LIST_INIT;
103 values[0].gauge = value;
106 vl.values_len = STATIC_ARRAY_SIZE (values);
107 sstrncpy (vl.host, hostname_g, sizeof (vl.host));
108 sstrncpy (vl.plugin, "vserver", sizeof (vl.plugin));
109 sstrncpy (vl.plugin_instance, plugin_instance, sizeof (vl.plugin_instance));
110 sstrncpy (vl.type, type, sizeof (vl.type));
111 sstrncpy (vl.type_instance, type_instance, sizeof (vl.type_instance));
113 plugin_dispatch_values (&vl);
114 } /* void submit_gauge */
116 static derive_t vserver_get_sock_bytes(const char *s)
127 status = parse_value (s, &v, DS_TYPE_DERIVE);
133 static int vserver_read (void)
138 proc = opendir (PROCDIR);
142 ERROR ("vserver plugin: fopen (%s): %s", PROCDIR,
143 sstrerror (errno, errbuf, sizeof (errbuf)));
154 char buffer[BUFSIZE];
162 dent = readdir (proc);
167 if (errno == 0) /* end of directory */
170 ERROR ("vserver plugin: failed to read directory %s: %s",
171 PROCDIR, sstrerror (errno, errbuf, sizeof (errbuf)));
176 if (dent->d_name[0] == '.')
179 len = ssnprintf (file, sizeof (file), PROCDIR "/%s", dent->d_name);
180 if ((len < 0) || (len >= BUFSIZE))
183 status = stat (file, &statbuf);
187 WARNING ("vserver plugin: stat (%s) failed: %s",
188 file, sstrerror (errno, errbuf, sizeof (errbuf)));
192 if (!S_ISDIR (statbuf.st_mode))
195 /* socket message accounting */
196 len = ssnprintf (file, sizeof (file),
197 PROCDIR "/%s/cacct", dent->d_name);
198 if ((len < 0) || ((size_t) len >= sizeof (file)))
201 if (NULL == (fh = fopen (file, "r")))
204 ERROR ("Cannot open '%s': %s", file,
205 sstrerror (errno, errbuf, sizeof (errbuf)));
208 while ((fh != NULL) && (NULL != fgets (buffer, BUFSIZE, fh)))
212 const char *type_instance;
214 if (strsplit (buffer, cols, 4) < 4)
217 if (0 == strcmp (cols[0], "UNIX:"))
218 type_instance = "unix";
219 else if (0 == strcmp (cols[0], "INET:"))
220 type_instance = "inet";
221 else if (0 == strcmp (cols[0], "INET6:"))
222 type_instance = "inet6";
223 else if (0 == strcmp (cols[0], "OTHER:"))
224 type_instance = "other";
225 else if (0 == strcmp (cols[0], "UNSPEC:"))
226 type_instance = "unspec";
230 rx = vserver_get_sock_bytes (cols[1]);
231 tx = vserver_get_sock_bytes (cols[2]);
232 /* cols[3] == errors */
234 traffic_submit (dent->d_name, type_instance, rx, tx);
235 } /* while (fgets) */
243 /* thread information and load */
244 len = ssnprintf (file, sizeof (file),
245 PROCDIR "/%s/cvirt", dent->d_name);
246 if ((len < 0) || ((size_t) len >= sizeof (file)))
249 if (NULL == (fh = fopen (file, "r")))
252 ERROR ("Cannot open '%s': %s", file,
253 sstrerror (errno, errbuf, sizeof (errbuf)));
256 while ((fh != NULL) && (NULL != fgets (buffer, BUFSIZE, fh)))
258 int n = strsplit (buffer, cols, 4);
262 const char *type_instance;
265 if (0 == strcmp (cols[0], "nr_threads:"))
266 type_instance = "total";
267 else if (0 == strcmp (cols[0], "nr_running:"))
268 type_instance = "running";
269 else if (0 == strcmp (cols[0], "nr_unintr:"))
270 type_instance = "uninterruptable";
271 else if (0 == strcmp (cols[0], "nr_onhold:"))
272 type_instance = "onhold";
276 value = atof (cols[1]);
277 submit_gauge (dent->d_name, "vs_threads", type_instance, value);
280 if (0 == strcmp (cols[0], "loadavg:"))
282 gauge_t snum = atof (cols[1]);
283 gauge_t mnum = atof (cols[2]);
284 gauge_t lnum = atof (cols[3]);
285 load_submit (dent->d_name, snum, mnum, lnum);
288 } /* while (fgets) */
296 /* processes and memory usage */
297 len = ssnprintf (file, sizeof (file),
298 PROCDIR "/%s/limit", dent->d_name);
299 if ((len < 0) || ((size_t) len >= sizeof (file)))
302 if (NULL == (fh = fopen (file, "r")))
305 ERROR ("Cannot open '%s': %s", file,
306 sstrerror (errno, errbuf, sizeof (errbuf)));
309 while ((fh != NULL) && (NULL != fgets (buffer, BUFSIZE, fh)))
311 const char *type = "vs_memory";
312 const char *type_instance;
315 if (strsplit (buffer, cols, 2) < 2)
318 if (0 == strcmp (cols[0], "PROC:"))
320 type = "vs_processes";
322 value = atof (cols[1]);
326 if (0 == strcmp (cols[0], "VM:"))
327 type_instance = "vm";
328 else if (0 == strcmp (cols[0], "VML:"))
329 type_instance = "vml";
330 else if (0 == strcmp (cols[0], "RSS:"))
331 type_instance = "rss";
332 else if (0 == strcmp (cols[0], "ANON:"))
333 type_instance = "anon";
337 value = atof (cols[1]) * pagesize;
340 submit_gauge (dent->d_name, type, type_instance, value);
341 } /* while (fgets) */
348 } /* while (readdir) */
353 } /* int vserver_read */
355 void module_register (void)
357 plugin_register_init ("vserver", vserver_init);
358 plugin_register_read ("vserver", vserver_read);
359 } /* void module_register(void) */
361 /* vim: set ts=4 sw=4 noexpandtab : */