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 strcpy (vl.host, hostname_g);
61 strcpy (vl.plugin, "vserver");
62 strncpy (vl.plugin_instance, plugin_instance, sizeof (vl.plugin_instance));
63 strcpy (vl.type, "if_octets");
64 strncpy (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 strcpy (vl.host, hostname_g);
83 strcpy (vl.plugin, "vserver");
84 strncpy (vl.plugin_instance, plugin_instance, sizeof (vl.plugin_instance));
85 strcpy (vl.type, "load");
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 strcpy (vl.host, hostname_g);
103 strcpy (vl.plugin, "vserver");
104 strncpy (vl.plugin_instance, plugin_instance, sizeof (vl.plugin_instance));
105 strncpy (vl.type, type, sizeof (vl.type));
106 strncpy (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 struct dirent *dent; /* 42 */
127 if (NULL == (proc = opendir (PROCDIR)))
130 ERROR ("vserver plugin: fopen (%s): %s", PROCDIR,
131 sstrerror (errno, errbuf, sizeof (errbuf)));
135 while (NULL != (dent = readdir (proc)))
141 char buffer[BUFSIZE];
145 if (dent->d_name[0] == '.')
148 /* This is not a directory */
149 if (dent->d_type != DT_DIR)
152 /* socket message accounting */
153 len = snprintf (file, BUFSIZE, PROCDIR "/%s/cacct", dent->d_name);
154 if ((len < 0) || (len >= BUFSIZE))
157 if (NULL == (fh = fopen (file, "r")))
160 ERROR ("Cannot open '%s': %s", file,
161 sstrerror (errno, errbuf, sizeof (errbuf)));
164 while ((fh != NULL) && (NULL != fgets (buffer, BUFSIZE, fh)))
170 if (strsplit (buffer, cols, 4) < 4)
173 if (0 == strcmp (cols[0], "UNIX:"))
174 type_instance = "unix";
175 else if (0 == strcmp (cols[0], "INET:"))
176 type_instance = "inet";
177 else if (0 == strcmp (cols[0], "INET6:"))
178 type_instance = "inet6";
179 else if (0 == strcmp (cols[0], "OTHER:"))
180 type_instance = "other";
181 else if (0 == strcmp (cols[0], "UNSPEC:"))
182 type_instance = "unspec";
186 rx = __get_sock_bytes (cols[1]);
187 tx = __get_sock_bytes (cols[2]);
188 /* cols[3] == errors */
190 traffic_submit (dent->d_name, type_instance, rx, tx);
191 } /* while (fgets) */
199 /* thread information and load */
200 len = snprintf (file, BUFSIZE, PROCDIR "/%s/cvirt", dent->d_name);
201 if ((len < 0) || (len >= BUFSIZE))
204 if (NULL == (fh = fopen (file, "r")))
207 ERROR ("Cannot open '%s': %s", file,
208 sstrerror (errno, errbuf, sizeof (errbuf)));
211 while ((fh != NULL) && (NULL != fgets (buffer, BUFSIZE, fh)))
213 int n = strsplit (buffer, cols, 4);
220 if (0 == strcmp (cols[0], "nr_threads:"))
221 type_instance = "total";
222 else if (0 == strcmp (cols[0], "nr_running:"))
223 type_instance = "running";
224 else if (0 == strcmp (cols[0], "nr_unintr:"))
225 type_instance = "uninterruptable";
226 else if (0 == strcmp (cols[0], "nr_onhold:"))
227 type_instance = "onhold";
231 value = atof (cols[1]);
232 submit_gauge (dent->d_name, "vs_threads", type_instance, value);
235 if (0 == strcmp (cols[0], "loadavg:"))
237 gauge_t snum = atof (cols[1]);
238 gauge_t mnum = atof (cols[2]);
239 gauge_t lnum = atof (cols[3]);
240 load_submit (dent->d_name, snum, mnum, lnum);
243 } /* while (fgets) */
251 /* processes and memory usage */
252 len = snprintf (file, BUFSIZE, PROCDIR "/%s/limit", dent->d_name);
253 if ((len < 0) || (len >= BUFSIZE))
256 if (NULL == (fh = fopen (file, "r")))
259 ERROR ("Cannot open '%s': %s", file,
260 sstrerror (errno, errbuf, sizeof (errbuf)));
263 while ((fh != NULL) && (NULL != fgets (buffer, BUFSIZE, fh)))
265 char *type = "vs_memory";
269 if (strsplit (buffer, cols, 2) < 2)
272 if (0 == strcmp (cols[0], "PROC:"))
274 type = "vs_processes";
276 value = atof (cols[1]);
280 if (0 == strcmp (cols[0], "VM:"))
281 type_instance = "vm";
282 else if (0 == strcmp (cols[0], "VML:"))
283 type_instance = "vml";
284 else if (0 == strcmp (cols[0], "RSS:"))
285 type_instance = "rss";
286 else if (0 == strcmp (cols[0], "ANON:"))
287 type_instance = "anon";
291 value = atof (cols[1]) * pagesize;
294 submit_gauge (dent->d_name, type, type_instance, value);
295 } /* while (fgets) */
302 } /* while (readdir) */
307 } /* int vserver_read */
309 void module_register (void)
311 plugin_register_init ("vserver", vserver_init);
312 plugin_register_read ("vserver", vserver_read);
313 } /* void module_register(void) */
315 /* vim: set ts=4 sw=4 noexpandtab : */