2 * collectd - src/vserver.c
3 * Copyright (C) 2006,2007 Sebastian Harl
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License as published by the
7 * Free Software Foundation; only version 2 of the license is applicable.
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * General Public License for more details.
14 * You should have received a copy of the GNU General Public License along
15 * with this program; if not, write to the Free Software Foundation, Inc.,
16 * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19 * Sebastian Harl <sh at tokkee.org>
27 #include <sys/types.h>
31 #define PROCDIR "/proc/virtual"
34 # error "No applicable input method."
37 static int pagesize = 0;
39 static int vserver_init (void)
41 /* XXX Should we check for getpagesize () in configure?
42 * What's the right thing to do, if there is no getpagesize ()? */
43 pagesize = getpagesize ();
46 } /* static void vserver_init(void) */
48 static void traffic_submit (const char *plugin_instance,
49 const char *type_instance, counter_t rx, counter_t tx)
52 value_list_t vl = VALUE_LIST_INIT;
54 values[0].counter = rx;
55 values[1].counter = tx;
58 vl.values_len = STATIC_ARRAY_SIZE (values);
59 vl.time = time (NULL);
60 sstrncpy (vl.host, hostname_g, sizeof (vl.host));
61 sstrncpy (vl.plugin, "vserver", sizeof (vl.plugin));
62 sstrncpy (vl.plugin_instance, plugin_instance, sizeof (vl.plugin_instance));
63 sstrncpy (vl.type, "if_octets", sizeof (vl.type));
64 sstrncpy (vl.type_instance, type_instance, sizeof (vl.type_instance));
66 plugin_dispatch_values (&vl);
67 } /* void traffic_submit */
69 static void load_submit (const char *plugin_instance,
70 gauge_t snum, gauge_t mnum, gauge_t lnum)
73 value_list_t vl = VALUE_LIST_INIT;
75 values[0].gauge = snum;
76 values[1].gauge = mnum;
77 values[2].gauge = lnum;
80 vl.values_len = STATIC_ARRAY_SIZE (values);
81 vl.time = time (NULL);
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 vl.time = time (NULL);
102 sstrncpy (vl.host, hostname_g, sizeof (vl.host));
103 sstrncpy (vl.plugin, "vserver", sizeof (vl.plugin));
104 sstrncpy (vl.plugin_instance, plugin_instance, sizeof (vl.plugin_instance));
105 sstrncpy (vl.type, type, sizeof (vl.type));
106 sstrncpy (vl.type_instance, type_instance, sizeof (vl.type_instance));
108 plugin_dispatch_values (&vl);
109 } /* void submit_gauge */
111 static inline long long __get_sock_bytes(const char *s)
121 static int vserver_read (void)
124 # define DIRENT_BUFFER_SIZE (sizeof (struct dirent) + 1024 + 1)
126 # define DIRENT_BUFFER_SIZE (sizeof (struct dirent) + NAME_MAX + 1)
130 struct dirent *dent; /* 42 */
131 char dirent_buffer[DIRENT_BUFFER_SIZE];
134 proc = opendir (PROCDIR);
138 ERROR ("vserver plugin: fopen (%s): %s", PROCDIR,
139 sstrerror (errno, errbuf, sizeof (errbuf)));
149 char buffer[BUFSIZE];
156 status = readdir_r (proc, (struct dirent *) dirent_buffer, &dent);
160 ERROR ("vserver plugin: readdir_r failed: %s",
161 sstrerror (errno, errbuf, sizeof (errbuf)));
165 else if (dent == NULL)
167 /* end of directory */
171 if (dent->d_name[0] == '.')
174 len = snprintf (file, sizeof (file), PROCDIR "/%s", dent->d_name);
175 if ((len < 0) || (len >= BUFSIZE))
178 status = stat (file, &statbuf);
182 WARNING ("vserver plugin: stat (%s) failed: %s",
183 file, sstrerror (errno, errbuf, sizeof (errbuf)));
187 if (!S_ISDIR (statbuf.st_mode))
190 /* socket message accounting */
191 len = ssnprintf (file, sizeof (file),
192 PROCDIR "/%s/cacct", dent->d_name);
193 if ((len < 0) || (len >= sizeof (file)))
196 if (NULL == (fh = fopen (file, "r")))
199 ERROR ("Cannot open '%s': %s", file,
200 sstrerror (errno, errbuf, sizeof (errbuf)));
203 while ((fh != NULL) && (NULL != fgets (buffer, BUFSIZE, fh)))
209 if (strsplit (buffer, cols, 4) < 4)
212 if (0 == strcmp (cols[0], "UNIX:"))
213 type_instance = "unix";
214 else if (0 == strcmp (cols[0], "INET:"))
215 type_instance = "inet";
216 else if (0 == strcmp (cols[0], "INET6:"))
217 type_instance = "inet6";
218 else if (0 == strcmp (cols[0], "OTHER:"))
219 type_instance = "other";
220 else if (0 == strcmp (cols[0], "UNSPEC:"))
221 type_instance = "unspec";
225 rx = __get_sock_bytes (cols[1]);
226 tx = __get_sock_bytes (cols[2]);
227 /* cols[3] == errors */
229 traffic_submit (dent->d_name, type_instance, rx, tx);
230 } /* while (fgets) */
238 /* thread information and load */
239 len = ssnprintf (file, sizeof (file),
240 PROCDIR "/%s/cvirt", dent->d_name);
241 if ((len < 0) || (len >= sizeof (file)))
244 if (NULL == (fh = fopen (file, "r")))
247 ERROR ("Cannot open '%s': %s", file,
248 sstrerror (errno, errbuf, sizeof (errbuf)));
251 while ((fh != NULL) && (NULL != fgets (buffer, BUFSIZE, fh)))
253 int n = strsplit (buffer, cols, 4);
260 if (0 == strcmp (cols[0], "nr_threads:"))
261 type_instance = "total";
262 else if (0 == strcmp (cols[0], "nr_running:"))
263 type_instance = "running";
264 else if (0 == strcmp (cols[0], "nr_unintr:"))
265 type_instance = "uninterruptable";
266 else if (0 == strcmp (cols[0], "nr_onhold:"))
267 type_instance = "onhold";
271 value = atof (cols[1]);
272 submit_gauge (dent->d_name, "vs_threads", type_instance, value);
275 if (0 == strcmp (cols[0], "loadavg:"))
277 gauge_t snum = atof (cols[1]);
278 gauge_t mnum = atof (cols[2]);
279 gauge_t lnum = atof (cols[3]);
280 load_submit (dent->d_name, snum, mnum, lnum);
283 } /* while (fgets) */
291 /* processes and memory usage */
292 len = ssnprintf (file, sizeof (file),
293 PROCDIR "/%s/limit", dent->d_name);
294 if ((len < 0) || (len >= sizeof (file)))
297 if (NULL == (fh = fopen (file, "r")))
300 ERROR ("Cannot open '%s': %s", file,
301 sstrerror (errno, errbuf, sizeof (errbuf)));
304 while ((fh != NULL) && (NULL != fgets (buffer, BUFSIZE, fh)))
306 char *type = "vs_memory";
310 if (strsplit (buffer, cols, 2) < 2)
313 if (0 == strcmp (cols[0], "PROC:"))
315 type = "vs_processes";
317 value = atof (cols[1]);
321 if (0 == strcmp (cols[0], "VM:"))
322 type_instance = "vm";
323 else if (0 == strcmp (cols[0], "VML:"))
324 type_instance = "vml";
325 else if (0 == strcmp (cols[0], "RSS:"))
326 type_instance = "rss";
327 else if (0 == strcmp (cols[0], "ANON:"))
328 type_instance = "anon";
332 value = atof (cols[1]) * pagesize;
335 submit_gauge (dent->d_name, type, type_instance, value);
336 } /* while (fgets) */
343 } /* while (readdir) */
348 } /* int vserver_read */
350 void module_register (void)
352 plugin_register_init ("vserver", vserver_init);
353 plugin_register_read ("vserver", vserver_read);
354 } /* void module_register(void) */
356 /* vim: set ts=4 sw=4 noexpandtab : */