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; either version 2 of the License, or (at your
8 * option) any later version.
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>
28 #include <sys/types.h>
32 #define MODULE_NAME "vserver"
33 #define PROCDIR "/proc/virtual"
35 #if defined(KERNEL_LINUX)
36 # define VSERVER_HAVE_READ 1
38 # define VSERVER_HAVE_READ 0
39 #endif /* defined(KERNEL_LINUX) */
42 static int pagesize = 0;
44 static int vserver_init (void)
46 /* XXX Should we check for getpagesize () in configure?
47 * What's the right thing to do, if there is no getpagesize ()? */
48 pagesize = getpagesize ();
51 } /* static void vserver_init(void) */
53 static void traffic_submit (const char *plugin_instance,
54 const char *type_instance, counter_t rx, counter_t tx)
57 value_list_t vl = VALUE_LIST_INIT;
59 values[0].counter = rx;
60 values[1].counter = tx;
63 vl.values_len = STATIC_ARRAY_SIZE (values);
64 vl.time = time (NULL);
65 strcpy (vl.host, hostname_g);
66 strcpy (vl.plugin, "vserver");
67 strncpy (vl.plugin_instance, plugin_instance, sizeof (vl.plugin_instance));
68 strncpy (vl.type_instance, type_instance, sizeof (vl.type_instance));
70 plugin_dispatch_values ("if_octets", &vl);
71 } /* void traffic_submit */
73 static void load_submit (const char *plugin_instance,
74 gauge_t snum, gauge_t mnum, gauge_t lnum)
77 value_list_t vl = VALUE_LIST_INIT;
79 values[0].gauge = snum;
80 values[1].gauge = mnum;
81 values[2].gauge = lnum;
84 vl.values_len = STATIC_ARRAY_SIZE (values);
85 vl.time = time (NULL);
86 strcpy (vl.host, hostname_g);
87 strcpy (vl.plugin, "vserver");
88 strncpy (vl.plugin_instance, plugin_instance, sizeof (vl.plugin_instance));
90 plugin_dispatch_values ("load", &vl);
93 static void submit_gauge (const char *plugin_instance, const char *type,
94 const char *type_instance, gauge_t value)
98 value_list_t vl = VALUE_LIST_INIT;
100 values[0].gauge = value;
103 vl.values_len = STATIC_ARRAY_SIZE (values);
104 vl.time = time (NULL);
105 strcpy (vl.host, hostname_g);
106 strcpy (vl.plugin, "vserver");
107 strncpy (vl.plugin_instance, plugin_instance, sizeof (vl.plugin_instance));
108 strncpy (vl.type_instance, type_instance, sizeof (vl.type_instance));
110 plugin_dispatch_values (type, &vl);
111 } /* void submit_gauge */
113 static inline long long __get_sock_bytes(const char *s)
123 static int vserver_read (void)
126 struct dirent *dent; /* 42 */
129 if (NULL == (proc = opendir (PROCDIR)))
132 ERROR ("vserver plugin: fopen (%s): %s", PROCDIR,
133 sstrerror (errno, errbuf, sizeof (errbuf)));
137 while (NULL != (dent = readdir (proc)))
143 char buffer[BUFSIZE];
147 if (dent->d_name[0] == '.')
150 /* This is not a directory */
151 if (dent->d_type != DT_DIR)
154 /* socket message accounting */
155 len = snprintf (file, BUFSIZE, PROCDIR "/%s/cacct", dent->d_name);
156 if ((len < 0) || (len >= BUFSIZE))
159 if (NULL == (fh = fopen (file, "r")))
162 ERROR ("Cannot open '%s': %s", file,
163 sstrerror (errno, errbuf, sizeof (errbuf)));
166 while ((fh != NULL) && (NULL != fgets (buffer, BUFSIZE, fh)))
172 if (strsplit (buffer, cols, 4) < 4)
175 if (0 == strcmp (cols[0], "UNIX:"))
176 type_instance = "unix";
177 else if (0 == strcmp (cols[0], "INET:"))
178 type_instance = "inet";
179 else if (0 == strcmp (cols[0], "INET6:"))
180 type_instance = "inet6";
181 else if (0 == strcmp (cols[0], "OTHER:"))
182 type_instance = "other";
183 else if (0 == strcmp (cols[0], "UNSPEC:"))
184 type_instance = "unspec";
188 rx = __get_sock_bytes (cols[1]);
189 tx = __get_sock_bytes (cols[2]);
190 /* cols[3] == errors */
192 traffic_submit (dent->d_name, type_instance, rx, tx);
193 } /* while (fgets) */
201 /* thread information and load */
202 len = snprintf (file, BUFSIZE, PROCDIR "/%s/cvirt", dent->d_name);
203 if ((len < 0) || (len >= BUFSIZE))
206 if (NULL == (fh = fopen (file, "r")))
209 ERROR ("Cannot open '%s': %s", file,
210 sstrerror (errno, errbuf, sizeof (errbuf)));
213 while ((fh != NULL) && (NULL != fgets (buffer, BUFSIZE, fh)))
215 int n = strsplit (buffer, cols, 4);
222 if (0 == strcmp (cols[0], "nr_threads:"))
223 type_instance = "total";
224 else if (0 == strcmp (cols[0], "nr_running:"))
225 type_instance = "running";
226 else if (0 == strcmp (cols[0], "nr_unintr:"))
227 type_instance = "uninterruptable";
228 else if (0 == strcmp (cols[0], "nr_onhold:"))
229 type_instance = "onhold";
233 value = atof (cols[1]);
234 submit_gauge (dent->d_name, "vs_threads", type_instance, value);
237 if (0 == strcmp (cols[0], "loadavg:"))
239 gauge_t snum = atof (cols[1]);
240 gauge_t mnum = atof (cols[2]);
241 gauge_t lnum = atof (cols[3]);
242 load_submit (dent->d_name, snum, mnum, lnum);
245 } /* while (fgets) */
253 /* processes and memory usage */
254 len = snprintf (file, BUFSIZE, PROCDIR "/%s/limit", dent->d_name);
255 if ((len < 0) || (len >= BUFSIZE))
258 if (NULL == (fh = fopen (file, "r")))
261 ERROR ("Cannot open '%s': %s", file,
262 sstrerror (errno, errbuf, sizeof (errbuf)));
265 while ((fh != NULL) && (NULL != fgets (buffer, BUFSIZE, fh)))
267 char *type = "vs_memory";
271 if (strsplit (buffer, cols, 2) < 2)
274 if (0 == strcmp (cols[0], "PROC:"))
276 type = "vs_processes";
278 value = atof (cols[1]);
282 if (0 == strcmp (cols[0], "VM:"))
283 type_instance = "vm";
284 else if (0 == strcmp (cols[0], "VML:"))
285 type_instance = "vml";
286 else if (0 == strcmp (cols[0], "RSS:"))
287 type_instance = "rss";
288 else if (0 == strcmp (cols[0], "ANON:"))
289 type_instance = "anon";
293 value = atof (cols[1]) * pagesize;
296 submit_gauge (dent->d_name, type, type_instance, value);
297 } /* while (fgets) */
304 } /* while (readdir) */
309 } /* int vserver_read */
310 #endif /* VSERVER_HAVE_READ */
312 void module_register (void)
314 #if VSERVER_HAVE_READ
315 plugin_register_init ("vserver", vserver_init);
316 plugin_register_read ("vserver", vserver_read);
317 #endif /* VSERVER_HAVE_READ */
318 } /* void module_register(void) */
320 /* vim: set ts=4 sw=4 noexpandtab : */