f295e49a9daf46f81e63c62f734a74bd6ca0ce8e
1 /**
2 * collectd - src/vserver.c
3 * Copyright (C) 2006,2007 Sebastian Harl
4 * Copyright (C) 2007,2008 Florian octo Forster
5 *
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published by the
8 * Free Software Foundation; only version 2 of the license is applicable.
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 * Florian octo Forster <octo at verplant.org>
22 **/
24 #include "collectd.h"
25 #include "common.h"
26 #include "plugin.h"
28 #include <dirent.h>
29 #include <sys/types.h>
31 #define BUFSIZE 512
33 #define PROCDIR "/proc/virtual"
35 #if !KERNEL_LINUX
36 # error "No applicable input method."
37 #endif
39 static int pagesize = 0;
41 static int vserver_init (void)
42 {
43 /* XXX Should we check for getpagesize () in configure?
44 * What's the right thing to do, if there is no getpagesize ()? */
45 pagesize = getpagesize ();
47 return (0);
48 } /* static void vserver_init(void) */
50 static void traffic_submit (const char *plugin_instance,
51 const char *type_instance, counter_t rx, counter_t tx)
52 {
53 value_t values[2];
54 value_list_t vl = VALUE_LIST_INIT;
56 values[0].counter = rx;
57 values[1].counter = tx;
59 vl.values = values;
60 vl.values_len = STATIC_ARRAY_SIZE (values);
61 vl.time = time (NULL);
62 sstrncpy (vl.host, hostname_g, sizeof (vl.host));
63 sstrncpy (vl.plugin, "vserver", sizeof (vl.plugin));
64 sstrncpy (vl.plugin_instance, plugin_instance, sizeof (vl.plugin_instance));
65 sstrncpy (vl.type, "if_octets", sizeof (vl.type));
66 sstrncpy (vl.type_instance, type_instance, sizeof (vl.type_instance));
68 plugin_dispatch_values (&vl);
69 } /* void traffic_submit */
71 static void load_submit (const char *plugin_instance,
72 gauge_t snum, gauge_t mnum, gauge_t lnum)
73 {
74 value_t values[3];
75 value_list_t vl = VALUE_LIST_INIT;
77 values[0].gauge = snum;
78 values[1].gauge = mnum;
79 values[2].gauge = lnum;
81 vl.values = values;
82 vl.values_len = STATIC_ARRAY_SIZE (values);
83 vl.time = time (NULL);
84 sstrncpy (vl.host, hostname_g, sizeof (vl.host));
85 sstrncpy (vl.plugin, "vserver", sizeof (vl.plugin));
86 sstrncpy (vl.plugin_instance, plugin_instance, sizeof (vl.plugin_instance));
87 sstrncpy (vl.type, "load", sizeof (vl.type));
89 plugin_dispatch_values (&vl);
90 }
92 static void submit_gauge (const char *plugin_instance, const char *type,
93 const char *type_instance, gauge_t value)
95 {
96 value_t values[1];
97 value_list_t vl = VALUE_LIST_INIT;
99 values[0].gauge = value;
101 vl.values = values;
102 vl.values_len = STATIC_ARRAY_SIZE (values);
103 vl.time = time (NULL);
104 sstrncpy (vl.host, hostname_g, sizeof (vl.host));
105 sstrncpy (vl.plugin, "vserver", sizeof (vl.plugin));
106 sstrncpy (vl.plugin_instance, plugin_instance, sizeof (vl.plugin_instance));
107 sstrncpy (vl.type, type, sizeof (vl.type));
108 sstrncpy (vl.type_instance, type_instance, sizeof (vl.type_instance));
110 plugin_dispatch_values (&vl);
111 } /* void submit_gauge */
113 static inline long long __get_sock_bytes(const char *s)
114 {
115 while (s[0] != '/')
116 ++s;
118 /* Remove '/' */
119 ++s;
120 return atoll(s);
121 }
123 static int vserver_read (void)
124 {
125 #if NAME_MAX < 1024
126 # define DIRENT_BUFFER_SIZE (sizeof (struct dirent) + 1024 + 1)
127 #else
128 # define DIRENT_BUFFER_SIZE (sizeof (struct dirent) + NAME_MAX + 1)
129 #endif
131 DIR *proc;
132 struct dirent *dent; /* 42 */
133 char dirent_buffer[DIRENT_BUFFER_SIZE];
135 errno = 0;
136 proc = opendir (PROCDIR);
137 if (proc == NULL)
138 {
139 char errbuf[1024];
140 ERROR ("vserver plugin: fopen (%s): %s", PROCDIR,
141 sstrerror (errno, errbuf, sizeof (errbuf)));
142 return (-1);
143 }
145 while (42)
146 {
147 size_t len;
148 char file[BUFSIZE];
150 FILE *fh;
151 char buffer[BUFSIZE];
153 struct stat statbuf;
154 char *cols[4];
156 int status;
158 status = readdir_r (proc, (struct dirent *) dirent_buffer, &dent);
159 if (status != 0)
160 {
161 char errbuf[4096];
162 ERROR ("vserver plugin: readdir_r failed: %s",
163 sstrerror (errno, errbuf, sizeof (errbuf)));
164 closedir (proc);
165 return (-1);
166 }
167 else if (dent == NULL)
168 {
169 /* end of directory */
170 break;
171 }
173 if (dent->d_name[0] == '.')
174 continue;
176 len = snprintf (file, sizeof (file), PROCDIR "/%s", dent->d_name);
177 if ((len < 0) || (len >= BUFSIZE))
178 continue;
180 status = stat (file, &statbuf);
181 if (status != 0)
182 {
183 char errbuf[4096];
184 WARNING ("vserver plugin: stat (%s) failed: %s",
185 file, sstrerror (errno, errbuf, sizeof (errbuf)));
186 continue;
187 }
189 if (!S_ISDIR (statbuf.st_mode))
190 continue;
192 /* socket message accounting */
193 len = ssnprintf (file, sizeof (file),
194 PROCDIR "/%s/cacct", dent->d_name);
195 if ((len < 0) || (len >= sizeof (file)))
196 continue;
198 if (NULL == (fh = fopen (file, "r")))
199 {
200 char errbuf[1024];
201 ERROR ("Cannot open '%s': %s", file,
202 sstrerror (errno, errbuf, sizeof (errbuf)));
203 }
205 while ((fh != NULL) && (NULL != fgets (buffer, BUFSIZE, fh)))
206 {
207 counter_t rx;
208 counter_t tx;
209 char *type_instance;
211 if (strsplit (buffer, cols, 4) < 4)
212 continue;
214 if (0 == strcmp (cols[0], "UNIX:"))
215 type_instance = "unix";
216 else if (0 == strcmp (cols[0], "INET:"))
217 type_instance = "inet";
218 else if (0 == strcmp (cols[0], "INET6:"))
219 type_instance = "inet6";
220 else if (0 == strcmp (cols[0], "OTHER:"))
221 type_instance = "other";
222 else if (0 == strcmp (cols[0], "UNSPEC:"))
223 type_instance = "unspec";
224 else
225 continue;
227 rx = __get_sock_bytes (cols[1]);
228 tx = __get_sock_bytes (cols[2]);
229 /* cols[3] == errors */
231 traffic_submit (dent->d_name, type_instance, rx, tx);
232 } /* while (fgets) */
234 if (fh != NULL)
235 {
236 fclose (fh);
237 fh = NULL;
238 }
240 /* thread information and load */
241 len = ssnprintf (file, sizeof (file),
242 PROCDIR "/%s/cvirt", dent->d_name);
243 if ((len < 0) || (len >= sizeof (file)))
244 continue;
246 if (NULL == (fh = fopen (file, "r")))
247 {
248 char errbuf[1024];
249 ERROR ("Cannot open '%s': %s", file,
250 sstrerror (errno, errbuf, sizeof (errbuf)));
251 }
253 while ((fh != NULL) && (NULL != fgets (buffer, BUFSIZE, fh)))
254 {
255 int n = strsplit (buffer, cols, 4);
257 if (2 == n)
258 {
259 char *type_instance;
260 gauge_t value;
262 if (0 == strcmp (cols[0], "nr_threads:"))
263 type_instance = "total";
264 else if (0 == strcmp (cols[0], "nr_running:"))
265 type_instance = "running";
266 else if (0 == strcmp (cols[0], "nr_unintr:"))
267 type_instance = "uninterruptable";
268 else if (0 == strcmp (cols[0], "nr_onhold:"))
269 type_instance = "onhold";
270 else
271 continue;
273 value = atof (cols[1]);
274 submit_gauge (dent->d_name, "vs_threads", type_instance, value);
275 }
276 else if (4 == n) {
277 if (0 == strcmp (cols[0], "loadavg:"))
278 {
279 gauge_t snum = atof (cols[1]);
280 gauge_t mnum = atof (cols[2]);
281 gauge_t lnum = atof (cols[3]);
282 load_submit (dent->d_name, snum, mnum, lnum);
283 }
284 }
285 } /* while (fgets) */
287 if (fh != NULL)
288 {
289 fclose (fh);
290 fh = NULL;
291 }
293 /* processes and memory usage */
294 len = ssnprintf (file, sizeof (file),
295 PROCDIR "/%s/limit", dent->d_name);
296 if ((len < 0) || (len >= sizeof (file)))
297 continue;
299 if (NULL == (fh = fopen (file, "r")))
300 {
301 char errbuf[1024];
302 ERROR ("Cannot open '%s': %s", file,
303 sstrerror (errno, errbuf, sizeof (errbuf)));
304 }
306 while ((fh != NULL) && (NULL != fgets (buffer, BUFSIZE, fh)))
307 {
308 char *type = "vs_memory";
309 char *type_instance;
310 gauge_t value;
312 if (strsplit (buffer, cols, 2) < 2)
313 continue;
315 if (0 == strcmp (cols[0], "PROC:"))
316 {
317 type = "vs_processes";
318 type_instance = "";
319 value = atof (cols[1]);
320 }
321 else
322 {
323 if (0 == strcmp (cols[0], "VM:"))
324 type_instance = "vm";
325 else if (0 == strcmp (cols[0], "VML:"))
326 type_instance = "vml";
327 else if (0 == strcmp (cols[0], "RSS:"))
328 type_instance = "rss";
329 else if (0 == strcmp (cols[0], "ANON:"))
330 type_instance = "anon";
331 else
332 continue;
334 value = atof (cols[1]) * pagesize;
335 }
337 submit_gauge (dent->d_name, type, type_instance, value);
338 } /* while (fgets) */
340 if (fh != NULL)
341 {
342 fclose (fh);
343 fh = NULL;
344 }
345 } /* while (readdir) */
347 closedir (proc);
349 return (0);
350 } /* int vserver_read */
352 void module_register (void)
353 {
354 plugin_register_init ("vserver", vserver_init);
355 plugin_register_read ("vserver", vserver_read);
356 } /* void module_register(void) */
358 /* vim: set ts=4 sw=4 noexpandtab : */