2 * collectd - src/vserver.c
3 * Copyright (C) 2006,2007 Sebastian Harl
4 * Copyright (C) 2007,2008 Florian octo Forster
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published by the
8 * Free Software Foundation; only version 2 of the license is applicable.
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License for more details.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
20 * Sebastian Harl <sh at tokkee.org>
21 * Florian octo Forster <octo at verplant.org>
29 #include <sys/types.h>
33 #define PROCDIR "/proc/virtual"
36 # error "No applicable input method."
39 static int pagesize = 0;
41 static int vserver_init (void)
43 /* XXX Should we check for getpagesize () in configure?
44 * What's the right thing to do, if there is no getpagesize ()? */
45 pagesize = getpagesize ();
48 } /* static void vserver_init(void) */
50 static void traffic_submit (const char *plugin_instance,
51 const char *type_instance, counter_t rx, counter_t tx)
54 value_list_t vl = VALUE_LIST_INIT;
56 values[0].counter = rx;
57 values[1].counter = tx;
60 vl.values_len = STATIC_ARRAY_SIZE (values);
61 vl.time = time (NULL);
62 sstrncpy (vl.host, hostname_g, sizeof (vl.host));
63 sstrncpy (vl.plugin, "vserver", sizeof (vl.plugin));
64 sstrncpy (vl.plugin_instance, plugin_instance, sizeof (vl.plugin_instance));
65 sstrncpy (vl.type, "if_octets", sizeof (vl.type));
66 sstrncpy (vl.type_instance, type_instance, sizeof (vl.type_instance));
68 plugin_dispatch_values (&vl);
69 } /* void traffic_submit */
71 static void load_submit (const char *plugin_instance,
72 gauge_t snum, gauge_t mnum, gauge_t lnum)
75 value_list_t vl = VALUE_LIST_INIT;
77 values[0].gauge = snum;
78 values[1].gauge = mnum;
79 values[2].gauge = lnum;
82 vl.values_len = STATIC_ARRAY_SIZE (values);
83 vl.time = time (NULL);
84 sstrncpy (vl.host, hostname_g, sizeof (vl.host));
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)
97 value_list_t vl = VALUE_LIST_INIT;
99 values[0].gauge = value;
102 vl.values_len = STATIC_ARRAY_SIZE (values);
103 vl.time = time (NULL);
104 sstrncpy (vl.host, hostname_g, sizeof (vl.host));
105 sstrncpy (vl.plugin, "vserver", sizeof (vl.plugin));
106 sstrncpy (vl.plugin_instance, plugin_instance, sizeof (vl.plugin_instance));
107 sstrncpy (vl.type, type, sizeof (vl.type));
108 sstrncpy (vl.type_instance, type_instance, sizeof (vl.type_instance));
110 plugin_dispatch_values (&vl);
111 } /* void submit_gauge */
113 static inline long long __get_sock_bytes(const char *s)
123 static int vserver_read (void)
126 # define DIRENT_BUFFER_SIZE (sizeof (struct dirent) + 1024 + 1)
128 # define DIRENT_BUFFER_SIZE (sizeof (struct dirent) + NAME_MAX + 1)
132 struct dirent *dent; /* 42 */
133 char dirent_buffer[DIRENT_BUFFER_SIZE];
136 proc = opendir (PROCDIR);
140 ERROR ("vserver plugin: fopen (%s): %s", PROCDIR,
141 sstrerror (errno, errbuf, sizeof (errbuf)));
151 char buffer[BUFSIZE];
158 status = readdir_r (proc, (struct dirent *) dirent_buffer, &dent);
162 ERROR ("vserver plugin: readdir_r failed: %s",
163 sstrerror (errno, errbuf, sizeof (errbuf)));
167 else if (dent == NULL)
169 /* end of directory */
173 if (dent->d_name[0] == '.')
176 len = snprintf (file, sizeof (file), PROCDIR "/%s", dent->d_name);
177 if ((len < 0) || (len >= BUFSIZE))
180 status = stat (file, &statbuf);
184 WARNING ("vserver plugin: stat (%s) failed: %s",
185 file, sstrerror (errno, errbuf, sizeof (errbuf)));
189 if (!S_ISDIR (statbuf.st_mode))
192 /* socket message accounting */
193 len = ssnprintf (file, sizeof (file),
194 PROCDIR "/%s/cacct", dent->d_name);
195 if ((len < 0) || (len >= sizeof (file)))
198 if (NULL == (fh = fopen (file, "r")))
201 ERROR ("Cannot open '%s': %s", file,
202 sstrerror (errno, errbuf, sizeof (errbuf)));
205 while ((fh != NULL) && (NULL != fgets (buffer, BUFSIZE, fh)))
211 if (strsplit (buffer, cols, 4) < 4)
214 if (0 == strcmp (cols[0], "UNIX:"))
215 type_instance = "unix";
216 else if (0 == strcmp (cols[0], "INET:"))
217 type_instance = "inet";
218 else if (0 == strcmp (cols[0], "INET6:"))
219 type_instance = "inet6";
220 else if (0 == strcmp (cols[0], "OTHER:"))
221 type_instance = "other";
222 else if (0 == strcmp (cols[0], "UNSPEC:"))
223 type_instance = "unspec";
227 rx = __get_sock_bytes (cols[1]);
228 tx = __get_sock_bytes (cols[2]);
229 /* cols[3] == errors */
231 traffic_submit (dent->d_name, type_instance, rx, tx);
232 } /* while (fgets) */
240 /* thread information and load */
241 len = ssnprintf (file, sizeof (file),
242 PROCDIR "/%s/cvirt", dent->d_name);
243 if ((len < 0) || (len >= sizeof (file)))
246 if (NULL == (fh = fopen (file, "r")))
249 ERROR ("Cannot open '%s': %s", file,
250 sstrerror (errno, errbuf, sizeof (errbuf)));
253 while ((fh != NULL) && (NULL != fgets (buffer, BUFSIZE, fh)))
255 int n = strsplit (buffer, cols, 4);
262 if (0 == strcmp (cols[0], "nr_threads:"))
263 type_instance = "total";
264 else if (0 == strcmp (cols[0], "nr_running:"))
265 type_instance = "running";
266 else if (0 == strcmp (cols[0], "nr_unintr:"))
267 type_instance = "uninterruptable";
268 else if (0 == strcmp (cols[0], "nr_onhold:"))
269 type_instance = "onhold";
273 value = atof (cols[1]);
274 submit_gauge (dent->d_name, "vs_threads", type_instance, value);
277 if (0 == strcmp (cols[0], "loadavg:"))
279 gauge_t snum = atof (cols[1]);
280 gauge_t mnum = atof (cols[2]);
281 gauge_t lnum = atof (cols[3]);
282 load_submit (dent->d_name, snum, mnum, lnum);
285 } /* while (fgets) */
293 /* processes and memory usage */
294 len = ssnprintf (file, sizeof (file),
295 PROCDIR "/%s/limit", dent->d_name);
296 if ((len < 0) || (len >= sizeof (file)))
299 if (NULL == (fh = fopen (file, "r")))
302 ERROR ("Cannot open '%s': %s", file,
303 sstrerror (errno, errbuf, sizeof (errbuf)));
306 while ((fh != NULL) && (NULL != fgets (buffer, BUFSIZE, fh)))
308 char *type = "vs_memory";
312 if (strsplit (buffer, cols, 2) < 2)
315 if (0 == strcmp (cols[0], "PROC:"))
317 type = "vs_processes";
319 value = atof (cols[1]);
323 if (0 == strcmp (cols[0], "VM:"))
324 type_instance = "vm";
325 else if (0 == strcmp (cols[0], "VML:"))
326 type_instance = "vml";
327 else if (0 == strcmp (cols[0], "RSS:"))
328 type_instance = "rss";
329 else if (0 == strcmp (cols[0], "ANON:"))
330 type_instance = "anon";
334 value = atof (cols[1]) * pagesize;
337 submit_gauge (dent->d_name, type, type_instance, value);
338 } /* while (fgets) */
345 } /* while (readdir) */
350 } /* int vserver_read */
352 void module_register (void)
354 plugin_register_init ("vserver", vserver_init);
355 plugin_register_read ("vserver", vserver_read);
356 } /* void module_register(void) */
358 /* vim: set ts=4 sw=4 noexpandtab : */