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 */
181 static complain_t complain_obj;
184 if (NULL == (proc = opendir (PROCDIR)))
186 plugin_complain (LOG_ERR, &complain_obj, "vserver plugin: "
187 "fopen (%s) failed: %s", PROCDIR, strerror (errno));
190 plugin_relief (LOG_NOTICE, &complain_obj, "vserver plugin: "
191 "fopen (%s) succeeded.", PROCDIR);
193 while (NULL != (dent = readdir (proc)))
199 char buffer[BUFSIZE];
203 if (dent->d_name[0] == '.')
206 /* This is not a directory */
207 if (dent->d_type != DT_DIR)
210 /* socket message accounting */
211 len = snprintf (file, BUFSIZE, PROCDIR "/%s/cacct", dent->d_name);
212 if ((len < 0) || (len >= BUFSIZE))
215 if (NULL == (fh = fopen (file, "r")))
216 ERROR ("Cannot open '%s': %s", file, strerror (errno));
218 while ((fh != NULL) && (NULL != fgets (buffer, BUFSIZE, fh)))
224 if (strsplit (buffer, cols, 4) < 4)
227 if (0 == strcmp (cols[0], "UNIX:"))
228 type_instance = "unix";
229 else if (0 == strcmp (cols[0], "INET:"))
230 type_instance = "inet";
231 else if (0 == strcmp (cols[0], "INET6:"))
232 type_instance = "inet6";
233 else if (0 == strcmp (cols[0], "OTHER:"))
234 type_instance = "other";
235 else if (0 == strcmp (cols[0], "UNSPEC:"))
236 type_instance = "unspec";
240 rx = __get_sock_bytes (cols[1]);
241 tx = __get_sock_bytes (cols[2]);
242 /* cols[3] == errors */
244 traffic_submit (dent->d_name, type_instance, rx, tx);
245 } /* while (fgets) */
253 /* thread information and load */
254 len = snprintf (file, BUFSIZE, PROCDIR "/%s/cvirt", dent->d_name);
255 if ((len < 0) || (len >= BUFSIZE))
258 if (NULL == (fh = fopen (file, "r")))
259 ERROR ("Cannot open '%s': %s", file, strerror (errno));
261 while ((fh != NULL) && (NULL != fgets (buffer, BUFSIZE, fh)))
263 int n = strsplit (buffer, cols, 4);
270 if (0 == strcmp (cols[0], "nr_threads:"))
271 type_instance = "total";
272 else if (0 == strcmp (cols[0], "nr_running:"))
273 type_instance = "running";
274 else if (0 == strcmp (cols[0], "nr_unintr:"))
275 type_instance = "uninterruptable";
276 else if (0 == strcmp (cols[0], "nr_onhold:"))
277 type_instance = "onhold";
281 value = atof (cols[1]);
282 submit_gauge (dent->d_name, "vs_threads", type_instance, value);
285 if (0 == strcmp (cols[0], "loadavg:"))
287 gauge_t snum = atof (cols[1]);
288 gauge_t mnum = atof (cols[2]);
289 gauge_t lnum = atof (cols[3]);
290 load_submit (dent->d_name, snum, mnum, lnum);
293 } /* while (fgets) */
301 /* processes and memory usage */
302 len = snprintf (file, BUFSIZE, PROCDIR "/%s/limit", dent->d_name);
303 if ((len < 0) || (len >= BUFSIZE))
306 if (NULL == (fh = fopen (file, "r")))
307 ERROR ("Cannot open '%s': %s", file, strerror (errno));
309 while ((fh != NULL) && (NULL != fgets (buffer, BUFSIZE, fh)))
311 char *type = "vs_memory";
315 if (strsplit (buffer, cols, 2) < 2)
318 if (0 == strcmp (cols[0], "PROC:"))
320 type = "vs_processes";
322 value = atof (cols[1]);
326 if (0 == strcmp (cols[0], "VM:"))
327 type_instance = "vm";
328 else if (0 == strcmp (cols[0], "VML:"))
329 type_instance = "vml";
330 else if (0 == strcmp (cols[0], "RSS:"))
331 type_instance = "rss";
332 else if (0 == strcmp (cols[0], "ANON:"))
333 type_instance = "anon";
337 value = atof (cols[1]) * pagesize;
340 submit_gauge (dent->d_name, type, type_instance, value);
341 } /* while (fgets) */
348 } /* while (readdir) */
353 } /* int vserver_read */
354 #endif /* VSERVER_HAVE_READ */
356 void module_register (void)
358 plugin_register_data_set (&octets_ds);
359 plugin_register_data_set (&load_ds);
360 plugin_register_data_set (&threads_ds);
361 plugin_register_data_set (&processes_ds);
362 plugin_register_data_set (&memory_ds);
364 #if VSERVER_HAVE_READ
365 plugin_register_init ("vserver", vserver_init);
366 plugin_register_read ("vserver", vserver_read);
367 #endif /* VSERVER_HAVE_READ */
368 } /* void module_register(void) */
370 /* vim: set ts=4 sw=4 noexpandtab : */