1 /**
2 * collectd - src/vserver.c
3 * Copyright (C) 2006,2007 Sebastian Harl
4 *
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.
9 *
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.
14 *
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
18 *
19 * Authors:
20 * Sebastian Harl <sh at tokkee.org>
21 **/
23 #include "collectd.h"
24 #include "common.h"
25 #include "plugin.h"
27 #include <dirent.h>
28 #include <sys/types.h>
30 #define BUFSIZE 512
32 #define MODULE_NAME "vserver"
33 #define PROCDIR "/proc/virtual"
35 #if defined(KERNEL_LINUX)
36 # define VSERVER_HAVE_READ 1
37 #else
38 # define VSERVER_HAVE_READ 0
39 #endif /* defined(KERNEL_LINUX) */
41 static data_source_t octets_dsrc[2] =
42 {
43 {"rx", DS_TYPE_COUNTER, 0, 4294967295.0},
44 {"tx", DS_TYPE_COUNTER, 0, 4294967295.0}
45 };
47 static data_set_t octets_ds =
48 {
49 "if_octets", 2, octets_dsrc
50 };
52 static data_source_t load_dsrc[3] =
53 {
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}
57 };
59 static data_set_t load_ds =
60 {
61 "load", 3, load_dsrc
62 };
64 static data_source_t threads_dsrc[1] =
65 {
66 {"value", DS_TYPE_GAUGE, 0.0, 65535.0}
67 };
69 static data_set_t threads_ds =
70 {
71 "vs_threads", 1, threads_dsrc
72 };
74 static data_source_t processes_dsrc[1] =
75 {
76 {"value", DS_TYPE_GAUGE, 0.0, 65535.0}
77 };
79 static data_set_t processes_ds =
80 {
81 "vs_processes", 1, processes_dsrc
82 };
84 static data_source_t memory_dsrc[1] =
85 {
86 {"value", DS_TYPE_GAUGE, 0.0, 9223372036854775807.0}
87 };
89 static data_set_t memory_ds =
90 {
91 "vs_memory", 1, memory_dsrc
92 };
94 #if VSERVER_HAVE_READ
95 static int pagesize = 0;
97 static int vserver_init (void)
98 {
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 ();
103 return (0);
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)
108 {
109 value_t values[2];
110 value_list_t vl = VALUE_LIST_INIT;
112 values[0].counter = rx;
113 values[1].counter = tx;
115 vl.values = values;
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)
128 {
129 value_t values[3];
130 value_list_t vl = VALUE_LIST_INIT;
132 values[0].gauge = snum;
133 values[1].gauge = mnum;
134 values[2].gauge = lnum;
136 vl.values = values;
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);
144 }
146 static void submit_gauge (const char *plugin_instance, const char *type,
147 const char *type_instance, gauge_t value)
149 {
150 value_t values[1];
151 value_list_t vl = VALUE_LIST_INIT;
153 values[0].gauge = value;
155 vl.values = values;
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)
167 {
168 while (s[0] != '/')
169 ++s;
171 /* Remove '/' */
172 ++s;
173 return atoll(s);
174 }
176 static int vserver_read (void)
177 {
178 DIR *proc;
179 struct dirent *dent; /* 42 */
181 static complain_t complain_obj;
183 errno = 0;
184 if (NULL == (proc = opendir (PROCDIR)))
185 {
186 plugin_complain (LOG_ERR, &complain_obj, "vserver plugin: "
187 "fopen (%s) failed: %s", PROCDIR, strerror (errno));
188 return (-1);
189 }
190 plugin_relief (LOG_NOTICE, &complain_obj, "vserver plugin: "
191 "fopen (%s) succeeded.", PROCDIR);
193 while (NULL != (dent = readdir (proc)))
194 {
195 int len;
196 char file[BUFSIZE];
198 FILE *fh;
199 char buffer[BUFSIZE];
201 char *cols[4];
203 if (dent->d_name[0] == '.')
204 continue;
206 /* This is not a directory */
207 if (dent->d_type != DT_DIR)
208 continue;
210 /* socket message accounting */
211 len = snprintf (file, BUFSIZE, PROCDIR "/%s/cacct", dent->d_name);
212 if ((len < 0) || (len >= BUFSIZE))
213 continue;
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)))
219 {
220 counter_t rx;
221 counter_t tx;
222 char *type_instance;
224 if (strsplit (buffer, cols, 4) < 4)
225 continue;
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";
237 else
238 continue;
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) */
247 if (fh != NULL)
248 {
249 fclose (fh);
250 fh = NULL;
251 }
253 /* thread information and load */
254 len = snprintf (file, BUFSIZE, PROCDIR "/%s/cvirt", dent->d_name);
255 if ((len < 0) || (len >= BUFSIZE))
256 continue;
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)))
262 {
263 int n = strsplit (buffer, cols, 4);
265 if (2 == n)
266 {
267 char *type_instance;
268 gauge_t value;
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";
278 else
279 continue;
281 value = atof (cols[1]);
282 submit_gauge (dent->d_name, "vs_threads", type_instance, value);
283 }
284 else if (4 == n) {
285 if (0 == strcmp (cols[0], "loadavg:"))
286 {
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);
291 }
292 }
293 } /* while (fgets) */
295 if (fh != NULL)
296 {
297 fclose (fh);
298 fh = NULL;
299 }
301 /* processes and memory usage */
302 len = snprintf (file, BUFSIZE, PROCDIR "/%s/limit", dent->d_name);
303 if ((len < 0) || (len >= BUFSIZE))
304 continue;
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)))
310 {
311 char *type = "vs_memory";
312 char *type_instance;
313 gauge_t value;
315 if (strsplit (buffer, cols, 2) < 2)
316 continue;
318 if (0 == strcmp (cols[0], "PROC:"))
319 {
320 type = "vs_processes";
321 type_instance = "";
322 value = atof (cols[1]);
323 }
324 else
325 {
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";
334 else
335 continue;
337 value = atof (cols[1]) * pagesize;
338 }
340 submit_gauge (dent->d_name, type, type_instance, value);
341 } /* while (fgets) */
343 if (fh != NULL)
344 {
345 fclose (fh);
346 fh = NULL;
347 }
348 } /* while (readdir) */
350 closedir (proc);
352 return (0);
353 } /* int vserver_read */
354 #endif /* VSERVER_HAVE_READ */
356 void module_register (void)
357 {
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 : */