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)
59 value_list_t vl = VALUE_LIST_INIT;
66 vl.values_len = STATIC_ARRAY_SIZE (values);
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));
72 plugin_dispatch_values (&vl);
73 } /* void traffic_submit */
75 static void load_submit (const char *plugin_instance,
76 gauge_t snum, gauge_t mnum, gauge_t lnum)
78 value_list_t vl = VALUE_LIST_INIT;
86 vl.values_len = STATIC_ARRAY_SIZE (values);
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));
91 plugin_dispatch_values (&vl);
94 static void submit_gauge (const char *plugin_instance, const char *type,
95 const char *type_instance, gauge_t value)
98 value_list_t vl = VALUE_LIST_INIT;
100 vl.values = &(value_t) { .gauge = value };
102 sstrncpy (vl.plugin, "vserver", sizeof (vl.plugin));
103 sstrncpy (vl.plugin_instance, plugin_instance, sizeof (vl.plugin_instance));
104 sstrncpy (vl.type, type, sizeof (vl.type));
105 sstrncpy (vl.type_instance, type_instance, sizeof (vl.type_instance));
107 plugin_dispatch_values (&vl);
108 } /* void submit_gauge */
110 static derive_t vserver_get_sock_bytes(const char *s)
121 status = parse_value (s, &v, DS_TYPE_DERIVE);
127 static int vserver_read (void)
130 # define DIRENT_BUFFER_SIZE (sizeof (struct dirent) + 1024 + 1)
132 # define DIRENT_BUFFER_SIZE (sizeof (struct dirent) + NAME_MAX + 1)
136 struct dirent *dent; /* 42 */
137 char dirent_buffer[DIRENT_BUFFER_SIZE];
140 proc = opendir (PROCDIR);
144 ERROR ("vserver plugin: fopen (%s): %s", PROCDIR,
145 sstrerror (errno, errbuf, sizeof (errbuf)));
155 char buffer[BUFSIZE];
162 status = readdir_r (proc, (struct dirent *) dirent_buffer, &dent);
166 ERROR ("vserver plugin: readdir_r failed: %s",
167 sstrerror (errno, errbuf, sizeof (errbuf)));
171 else if (dent == NULL)
173 /* end of directory */
177 if (dent->d_name[0] == '.')
180 len = ssnprintf (file, sizeof (file), PROCDIR "/%s", dent->d_name);
181 if ((len < 0) || (len >= BUFSIZE))
184 status = stat (file, &statbuf);
188 WARNING ("vserver plugin: stat (%s) failed: %s",
189 file, sstrerror (errno, errbuf, sizeof (errbuf)));
193 if (!S_ISDIR (statbuf.st_mode))
196 /* socket message accounting */
197 len = ssnprintf (file, sizeof (file),
198 PROCDIR "/%s/cacct", dent->d_name);
199 if ((len < 0) || ((size_t) len >= sizeof (file)))
202 if (NULL == (fh = fopen (file, "r")))
205 ERROR ("Cannot open '%s': %s", file,
206 sstrerror (errno, errbuf, sizeof (errbuf)));
209 while ((fh != NULL) && (NULL != fgets (buffer, BUFSIZE, fh)))
213 const char *type_instance;
215 if (strsplit (buffer, cols, 4) < 4)
218 if (0 == strcmp (cols[0], "UNIX:"))
219 type_instance = "unix";
220 else if (0 == strcmp (cols[0], "INET:"))
221 type_instance = "inet";
222 else if (0 == strcmp (cols[0], "INET6:"))
223 type_instance = "inet6";
224 else if (0 == strcmp (cols[0], "OTHER:"))
225 type_instance = "other";
226 else if (0 == strcmp (cols[0], "UNSPEC:"))
227 type_instance = "unspec";
231 rx = vserver_get_sock_bytes (cols[1]);
232 tx = vserver_get_sock_bytes (cols[2]);
233 /* cols[3] == errors */
235 traffic_submit (dent->d_name, type_instance, rx, tx);
236 } /* while (fgets) */
244 /* thread information and load */
245 len = ssnprintf (file, sizeof (file),
246 PROCDIR "/%s/cvirt", dent->d_name);
247 if ((len < 0) || ((size_t) len >= sizeof (file)))
250 if (NULL == (fh = fopen (file, "r")))
253 ERROR ("Cannot open '%s': %s", file,
254 sstrerror (errno, errbuf, sizeof (errbuf)));
257 while ((fh != NULL) && (NULL != fgets (buffer, BUFSIZE, fh)))
259 int n = strsplit (buffer, cols, 4);
263 const char *type_instance;
266 if (0 == strcmp (cols[0], "nr_threads:"))
267 type_instance = "total";
268 else if (0 == strcmp (cols[0], "nr_running:"))
269 type_instance = "running";
270 else if (0 == strcmp (cols[0], "nr_unintr:"))
271 type_instance = "uninterruptable";
272 else if (0 == strcmp (cols[0], "nr_onhold:"))
273 type_instance = "onhold";
277 value = atof (cols[1]);
278 submit_gauge (dent->d_name, "vs_threads", type_instance, value);
281 if (0 == strcmp (cols[0], "loadavg:"))
283 gauge_t snum = atof (cols[1]);
284 gauge_t mnum = atof (cols[2]);
285 gauge_t lnum = atof (cols[3]);
286 load_submit (dent->d_name, snum, mnum, lnum);
289 } /* while (fgets) */
297 /* processes and memory usage */
298 len = ssnprintf (file, sizeof (file),
299 PROCDIR "/%s/limit", dent->d_name);
300 if ((len < 0) || ((size_t) len >= sizeof (file)))
303 if (NULL == (fh = fopen (file, "r")))
306 ERROR ("Cannot open '%s': %s", file,
307 sstrerror (errno, errbuf, sizeof (errbuf)));
310 while ((fh != NULL) && (NULL != fgets (buffer, BUFSIZE, fh)))
312 const char *type = "vs_memory";
313 const char *type_instance;
316 if (strsplit (buffer, cols, 2) < 2)
319 if (0 == strcmp (cols[0], "PROC:"))
321 type = "vs_processes";
323 value = atof (cols[1]);
327 if (0 == strcmp (cols[0], "VM:"))
328 type_instance = "vm";
329 else if (0 == strcmp (cols[0], "VML:"))
330 type_instance = "vml";
331 else if (0 == strcmp (cols[0], "RSS:"))
332 type_instance = "rss";
333 else if (0 == strcmp (cols[0], "ANON:"))
334 type_instance = "anon";
338 value = atof (cols[1]) * pagesize;
341 submit_gauge (dent->d_name, type, type_instance, value);
342 } /* while (fgets) */
349 } /* while (readdir) */
354 } /* int vserver_read */
356 void module_register (void)
358 plugin_register_init ("vserver", vserver_init);
359 plugin_register_read ("vserver", vserver_read);
360 } /* void module_register(void) */
362 /* vim: set ts=4 sw=4 noexpandtab : */