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) */
41 static data_source_t octets_dsrc[2] =
43 {"rx", DS_TYPE_COUNTER, 0, 4294967295.0},
44 {"tx", DS_TYPE_COUNTER, 0, 4294967295.0}
47 static data_set_t octets_ds =
49 "if_octets", 2, octets_dsrc
52 static data_source_t load_dsrc[3] =
54 {"shortterm", DS_TYPE_GAUGE, 0.0, 100.0},
55 {"midterm", DS_TYPE_GAUGE, 0.0, 100.0},
56 {"longterm", DS_TYPE_GAUGE, 0.0, 100.0}
59 static data_set_t load_ds =
64 static data_source_t threads_dsrc[1] =
66 {"value", DS_TYPE_GAUGE, 0.0, 65535.0}
69 static data_set_t threads_ds =
71 "vs_threads", 1, threads_dsrc
74 static data_source_t processes_dsrc[1] =
76 {"value", DS_TYPE_GAUGE, 0.0, 65535.0}
79 static data_set_t processes_ds =
81 "vs_processes", 1, processes_dsrc
84 static data_source_t memory_dsrc[1] =
86 {"value", DS_TYPE_GAUGE, 0.0, 9223372036854775807.0}
89 static data_set_t memory_ds =
91 "vs_memory", 1, memory_dsrc
95 static int pagesize = 0;
97 static int vserver_init (void)
99 /* XXX Should we check for getpagesize () in configure?
100 * What's the right thing to do, if there is no getpagesize ()? */
101 pagesize = getpagesize ();
104 } /* static void vserver_init(void) */
106 static void traffic_submit (const char *plugin_instance,
107 const char *type_instance, counter_t rx, counter_t tx)
110 value_list_t vl = VALUE_LIST_INIT;
112 values[0].counter = rx;
113 values[1].counter = tx;
116 vl.values_len = STATIC_ARRAY_SIZE (values);
117 vl.time = time (NULL);
118 strcpy (vl.host, hostname_g);
119 strcpy (vl.plugin, "vserver");
120 strncpy (vl.plugin_instance, plugin_instance, sizeof (vl.plugin_instance));
121 strncpy (vl.type_instance, type_instance, sizeof (vl.type_instance));
123 plugin_dispatch_values ("if_octets", &vl);
124 } /* void traffic_submit */
126 static void load_submit (const char *plugin_instance,
127 gauge_t snum, gauge_t mnum, gauge_t lnum)
130 value_list_t vl = VALUE_LIST_INIT;
132 values[0].gauge = snum;
133 values[1].gauge = mnum;
134 values[2].gauge = lnum;
137 vl.values_len = STATIC_ARRAY_SIZE (values);
138 vl.time = time (NULL);
139 strcpy (vl.host, hostname_g);
140 strcpy (vl.plugin, "vserver");
141 strncpy (vl.plugin_instance, plugin_instance, sizeof (vl.plugin_instance));
143 plugin_dispatch_values ("load", &vl);
146 static void submit_gauge (const char *plugin_instance, const char *type,
147 const char *type_instance, gauge_t value)
151 value_list_t vl = VALUE_LIST_INIT;
153 values[0].gauge = value;
156 vl.values_len = STATIC_ARRAY_SIZE (values);
157 vl.time = time (NULL);
158 strcpy (vl.host, hostname_g);
159 strcpy (vl.plugin, "vserver");
160 strncpy (vl.plugin_instance, plugin_instance, sizeof (vl.plugin_instance));
161 strncpy (vl.type_instance, type_instance, sizeof (vl.type_instance));
163 plugin_dispatch_values (type, &vl);
164 } /* void submit_gauge */
166 static inline long long __get_sock_bytes(const char *s)
176 static int vserver_read (void)
179 struct dirent *dent; /* 42 */
182 if (NULL == (proc = opendir (PROCDIR)))
185 ERROR ("vserver plugin: fopen (%s): %s", PROCDIR,
186 sstrerror (errno, errbuf, sizeof (errbuf)));
190 while (NULL != (dent = readdir (proc)))
196 char buffer[BUFSIZE];
200 if (dent->d_name[0] == '.')
203 /* This is not a directory */
204 if (dent->d_type != DT_DIR)
207 /* socket message accounting */
208 len = snprintf (file, BUFSIZE, PROCDIR "/%s/cacct", dent->d_name);
209 if ((len < 0) || (len >= BUFSIZE))
212 if (NULL == (fh = fopen (file, "r")))
215 ERROR ("Cannot open '%s': %s", file,
216 sstrerror (errno, errbuf, sizeof (errbuf)));
219 while ((fh != NULL) && (NULL != fgets (buffer, BUFSIZE, fh)))
225 if (strsplit (buffer, cols, 4) < 4)
228 if (0 == strcmp (cols[0], "UNIX:"))
229 type_instance = "unix";
230 else if (0 == strcmp (cols[0], "INET:"))
231 type_instance = "inet";
232 else if (0 == strcmp (cols[0], "INET6:"))
233 type_instance = "inet6";
234 else if (0 == strcmp (cols[0], "OTHER:"))
235 type_instance = "other";
236 else if (0 == strcmp (cols[0], "UNSPEC:"))
237 type_instance = "unspec";
241 rx = __get_sock_bytes (cols[1]);
242 tx = __get_sock_bytes (cols[2]);
243 /* cols[3] == errors */
245 traffic_submit (dent->d_name, type_instance, rx, tx);
246 } /* while (fgets) */
254 /* thread information and load */
255 len = snprintf (file, BUFSIZE, PROCDIR "/%s/cvirt", dent->d_name);
256 if ((len < 0) || (len >= BUFSIZE))
259 if (NULL == (fh = fopen (file, "r")))
262 ERROR ("Cannot open '%s': %s", file,
263 sstrerror (errno, errbuf, sizeof (errbuf)));
266 while ((fh != NULL) && (NULL != fgets (buffer, BUFSIZE, fh)))
268 int n = strsplit (buffer, cols, 4);
275 if (0 == strcmp (cols[0], "nr_threads:"))
276 type_instance = "total";
277 else if (0 == strcmp (cols[0], "nr_running:"))
278 type_instance = "running";
279 else if (0 == strcmp (cols[0], "nr_unintr:"))
280 type_instance = "uninterruptable";
281 else if (0 == strcmp (cols[0], "nr_onhold:"))
282 type_instance = "onhold";
286 value = atof (cols[1]);
287 submit_gauge (dent->d_name, "vs_threads", type_instance, value);
290 if (0 == strcmp (cols[0], "loadavg:"))
292 gauge_t snum = atof (cols[1]);
293 gauge_t mnum = atof (cols[2]);
294 gauge_t lnum = atof (cols[3]);
295 load_submit (dent->d_name, snum, mnum, lnum);
298 } /* while (fgets) */
306 /* processes and memory usage */
307 len = snprintf (file, BUFSIZE, PROCDIR "/%s/limit", dent->d_name);
308 if ((len < 0) || (len >= BUFSIZE))
311 if (NULL == (fh = fopen (file, "r")))
314 ERROR ("Cannot open '%s': %s", file,
315 sstrerror (errno, errbuf, sizeof (errbuf)));
318 while ((fh != NULL) && (NULL != fgets (buffer, BUFSIZE, fh)))
320 char *type = "vs_memory";
324 if (strsplit (buffer, cols, 2) < 2)
327 if (0 == strcmp (cols[0], "PROC:"))
329 type = "vs_processes";
331 value = atof (cols[1]);
335 if (0 == strcmp (cols[0], "VM:"))
336 type_instance = "vm";
337 else if (0 == strcmp (cols[0], "VML:"))
338 type_instance = "vml";
339 else if (0 == strcmp (cols[0], "RSS:"))
340 type_instance = "rss";
341 else if (0 == strcmp (cols[0], "ANON:"))
342 type_instance = "anon";
346 value = atof (cols[1]) * pagesize;
349 submit_gauge (dent->d_name, type, type_instance, value);
350 } /* while (fgets) */
357 } /* while (readdir) */
362 } /* int vserver_read */
363 #endif /* VSERVER_HAVE_READ */
365 void module_register (modreg_e load)
367 if (load & MR_DATASETS)
369 plugin_register_data_set (&octets_ds);
370 plugin_register_data_set (&load_ds);
371 plugin_register_data_set (&threads_ds);
372 plugin_register_data_set (&processes_ds);
373 plugin_register_data_set (&memory_ds);
376 #if VSERVER_HAVE_READ
379 plugin_register_init ("vserver", vserver_init);
380 plugin_register_read ("vserver", vserver_read);
382 #endif /* VSERVER_HAVE_READ */
383 } /* void module_register(void) */
385 /* vim: set ts=4 sw=4 noexpandtab : */