2 * collectd - src/vserver.c
3 * Copyright (C) 2006,2007 Sebastian Harl
4 * Copyright (C) 2007-2010 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, derive_t rx, derive_t tx)
54 value_list_t vl = VALUE_LIST_INIT;
56 values[0].derive = rx;
57 values[1].derive = tx;
60 vl.values_len = STATIC_ARRAY_SIZE (values);
61 sstrncpy (vl.host, hostname_g, sizeof (vl.host));
62 sstrncpy (vl.plugin, "vserver", sizeof (vl.plugin));
63 sstrncpy (vl.plugin_instance, plugin_instance, sizeof (vl.plugin_instance));
64 sstrncpy (vl.type, "if_octets", sizeof (vl.type));
65 sstrncpy (vl.type_instance, type_instance, sizeof (vl.type_instance));
67 plugin_dispatch_values (&vl);
68 } /* void traffic_submit */
70 static void load_submit (const char *plugin_instance,
71 gauge_t snum, gauge_t mnum, gauge_t lnum)
74 value_list_t vl = VALUE_LIST_INIT;
76 values[0].gauge = snum;
77 values[1].gauge = mnum;
78 values[2].gauge = lnum;
81 vl.values_len = STATIC_ARRAY_SIZE (values);
82 sstrncpy (vl.host, hostname_g, sizeof (vl.host));
83 sstrncpy (vl.plugin, "vserver", sizeof (vl.plugin));
84 sstrncpy (vl.plugin_instance, plugin_instance, sizeof (vl.plugin_instance));
85 sstrncpy (vl.type, "load", sizeof (vl.type));
87 plugin_dispatch_values (&vl);
90 static void submit_gauge (const char *plugin_instance, const char *type,
91 const char *type_instance, gauge_t value)
95 value_list_t vl = VALUE_LIST_INIT;
97 values[0].gauge = value;
100 vl.values_len = STATIC_ARRAY_SIZE (values);
101 sstrncpy (vl.host, hostname_g, sizeof (vl.host));
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)))
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);
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 char *type = "vs_memory";
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 : */