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 strncpy (vl.plugin_instance, plugin_instance, sizeof (vl.plugin_instance));
63 strncpy (vl.type_instance, type_instance, sizeof (vl.type_instance));
65 plugin_dispatch_values ("if_octets", &vl);
66 } /* void traffic_submit */
68 static void load_submit (const char *plugin_instance,
69 gauge_t snum, gauge_t mnum, gauge_t lnum)
72 value_list_t vl = VALUE_LIST_INIT;
74 values[0].gauge = snum;
75 values[1].gauge = mnum;
76 values[2].gauge = lnum;
79 vl.values_len = STATIC_ARRAY_SIZE (values);
80 vl.time = time (NULL);
81 sstrncpy (vl.host, hostname_g, sizeof (vl.host));
82 sstrncpy (vl.plugin, "vserver", sizeof (vl.plugin));
83 strncpy (vl.plugin_instance, plugin_instance, sizeof (vl.plugin_instance));
85 plugin_dispatch_values ("load", &vl);
88 static void submit_gauge (const char *plugin_instance, const char *type,
89 const char *type_instance, gauge_t value)
93 value_list_t vl = VALUE_LIST_INIT;
95 values[0].gauge = value;
98 vl.values_len = STATIC_ARRAY_SIZE (values);
99 vl.time = time (NULL);
100 sstrncpy (vl.host, hostname_g, sizeof (vl.host));
101 sstrncpy (vl.plugin, "vserver", sizeof (vl.plugin));
102 strncpy (vl.plugin_instance, plugin_instance, sizeof (vl.plugin_instance));
103 strncpy (vl.type_instance, type_instance, sizeof (vl.type_instance));
105 plugin_dispatch_values (type, &vl);
106 } /* void submit_gauge */
108 static inline long long __get_sock_bytes(const char *s)
118 static int vserver_read (void)
121 # define DIRENT_BUFFER_SIZE (sizeof (struct dirent) + 1024 + 1)
123 # define DIRENT_BUFFER_SIZE (sizeof (struct dirent) + NAME_MAX + 1)
127 struct dirent *dent; /* 42 */
128 char dirent_buffer[DIRENT_BUFFER_SIZE];
131 proc = opendir (PROCDIR);
135 ERROR ("vserver plugin: fopen (%s): %s", PROCDIR,
136 sstrerror (errno, errbuf, sizeof (errbuf)));
146 char buffer[BUFSIZE];
153 status = readdir_r (proc, (struct dirent *) dirent_buffer, &dent);
157 ERROR ("vserver plugin: readdir_r failed: %s",
158 sstrerror (errno, errbuf, sizeof (errbuf)));
162 else if (dent == NULL)
164 /* end of directory */
168 if (dent->d_name[0] == '.')
171 len = snprintf (file, sizeof (file), PROCDIR "/%s", dent->d_name);
172 if ((len < 0) || (len >= BUFSIZE))
175 status = stat (file, &statbuf);
179 WARNING ("vserver plugin: stat (%s) failed: %s",
180 file, sstrerror (errno, errbuf, sizeof (errbuf)));
184 if (!S_ISDIR (statbuf.st_mode))
187 /* socket message accounting */
188 len = snprintf (file, BUFSIZE, PROCDIR "/%s/cacct", dent->d_name);
189 if ((len < 0) || (len >= BUFSIZE))
192 if (NULL == (fh = fopen (file, "r")))
195 ERROR ("Cannot open '%s': %s", file,
196 sstrerror (errno, errbuf, sizeof (errbuf)));
199 while ((fh != NULL) && (NULL != fgets (buffer, BUFSIZE, fh)))
205 if (strsplit (buffer, cols, 4) < 4)
208 if (0 == strcmp (cols[0], "UNIX:"))
209 type_instance = "unix";
210 else if (0 == strcmp (cols[0], "INET:"))
211 type_instance = "inet";
212 else if (0 == strcmp (cols[0], "INET6:"))
213 type_instance = "inet6";
214 else if (0 == strcmp (cols[0], "OTHER:"))
215 type_instance = "other";
216 else if (0 == strcmp (cols[0], "UNSPEC:"))
217 type_instance = "unspec";
221 rx = __get_sock_bytes (cols[1]);
222 tx = __get_sock_bytes (cols[2]);
223 /* cols[3] == errors */
225 traffic_submit (dent->d_name, type_instance, rx, tx);
226 } /* while (fgets) */
234 /* thread information and load */
235 len = snprintf (file, BUFSIZE, PROCDIR "/%s/cvirt", dent->d_name);
236 if ((len < 0) || (len >= BUFSIZE))
239 if (NULL == (fh = fopen (file, "r")))
242 ERROR ("Cannot open '%s': %s", file,
243 sstrerror (errno, errbuf, sizeof (errbuf)));
246 while ((fh != NULL) && (NULL != fgets (buffer, BUFSIZE, fh)))
248 int n = strsplit (buffer, cols, 4);
255 if (0 == strcmp (cols[0], "nr_threads:"))
256 type_instance = "total";
257 else if (0 == strcmp (cols[0], "nr_running:"))
258 type_instance = "running";
259 else if (0 == strcmp (cols[0], "nr_unintr:"))
260 type_instance = "uninterruptable";
261 else if (0 == strcmp (cols[0], "nr_onhold:"))
262 type_instance = "onhold";
266 value = atof (cols[1]);
267 submit_gauge (dent->d_name, "vs_threads", type_instance, value);
270 if (0 == strcmp (cols[0], "loadavg:"))
272 gauge_t snum = atof (cols[1]);
273 gauge_t mnum = atof (cols[2]);
274 gauge_t lnum = atof (cols[3]);
275 load_submit (dent->d_name, snum, mnum, lnum);
278 } /* while (fgets) */
286 /* processes and memory usage */
287 len = snprintf (file, BUFSIZE, PROCDIR "/%s/limit", dent->d_name);
288 if ((len < 0) || (len >= BUFSIZE))
291 if (NULL == (fh = fopen (file, "r")))
294 ERROR ("Cannot open '%s': %s", file,
295 sstrerror (errno, errbuf, sizeof (errbuf)));
298 while ((fh != NULL) && (NULL != fgets (buffer, BUFSIZE, fh)))
300 char *type = "vs_memory";
304 if (strsplit (buffer, cols, 2) < 2)
307 if (0 == strcmp (cols[0], "PROC:"))
309 type = "vs_processes";
311 value = atof (cols[1]);
315 if (0 == strcmp (cols[0], "VM:"))
316 type_instance = "vm";
317 else if (0 == strcmp (cols[0], "VML:"))
318 type_instance = "vml";
319 else if (0 == strcmp (cols[0], "RSS:"))
320 type_instance = "rss";
321 else if (0 == strcmp (cols[0], "ANON:"))
322 type_instance = "anon";
326 value = atof (cols[1]) * pagesize;
329 submit_gauge (dent->d_name, type, type_instance, value);
330 } /* while (fgets) */
337 } /* while (readdir) */
342 } /* int vserver_read */
344 void module_register (void)
346 plugin_register_init ("vserver", vserver_init);
347 plugin_register_read ("vserver", vserver_read);
348 } /* void module_register(void) */
350 /* vim: set ts=4 sw=4 noexpandtab : */