{GPL, other}: Relicense to MIT license.
[collectd.git] / src / vserver.c
1 /**
2  * collectd - src/vserver.c
3  * Copyright (C) 2006,2007  Sebastian Harl
4  * Copyright (C) 2007-2010  Florian octo Forster
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining a
7  * copy of this software and associated documentation files (the "Software"),
8  * to deal in the Software without restriction, including without limitation
9  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10  * and/or sell copies of the Software, and to permit persons to whom the
11  * Software is furnished to do so, subject to the following conditions:
12  *
13  * The above copyright notice and this permission notice shall be included in
14  * all copies or substantial portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
22  * DEALINGS IN THE SOFTWARE.
23  *
24  * Authors:
25  *   Sebastian Harl <sh at tokkee.org>
26  *   Florian octo Forster <octo at collectd.org>
27  **/
28
29 #include "collectd.h"
30 #include "common.h"
31 #include "plugin.h"
32
33 #include <dirent.h>
34 #include <sys/types.h>
35
36 #define BUFSIZE 512
37
38 #define PROCDIR "/proc/virtual"
39
40 #if !KERNEL_LINUX
41 # error "No applicable input method."
42 #endif
43
44 static int pagesize = 0;
45
46 static int vserver_init (void)
47 {
48         /* XXX Should we check for getpagesize () in configure?
49          * What's the right thing to do, if there is no getpagesize ()? */
50         pagesize = getpagesize ();
51
52         return (0);
53 } /* static void vserver_init(void) */
54
55 static void traffic_submit (const char *plugin_instance,
56                 const char *type_instance, derive_t rx, derive_t tx)
57 {
58         value_t values[2];
59         value_list_t vl = VALUE_LIST_INIT;
60
61         values[0].derive = rx;
62         values[1].derive = tx;
63
64         vl.values = values;
65         vl.values_len = STATIC_ARRAY_SIZE (values);
66         sstrncpy (vl.host, hostname_g, sizeof (vl.host));
67         sstrncpy (vl.plugin, "vserver", sizeof (vl.plugin));
68         sstrncpy (vl.plugin_instance, plugin_instance, sizeof (vl.plugin_instance));
69         sstrncpy (vl.type, "if_octets", sizeof (vl.type));
70         sstrncpy (vl.type_instance, type_instance, sizeof (vl.type_instance));
71
72         plugin_dispatch_values (&vl);
73 } /* void traffic_submit */
74
75 static void load_submit (const char *plugin_instance,
76                 gauge_t snum, gauge_t mnum, gauge_t lnum)
77 {
78         value_t values[3];
79         value_list_t vl = VALUE_LIST_INIT;
80
81         values[0].gauge = snum;
82         values[1].gauge = mnum;
83         values[2].gauge = lnum;
84
85         vl.values = values;
86         vl.values_len = STATIC_ARRAY_SIZE (values);
87         sstrncpy (vl.host, hostname_g, sizeof (vl.host));
88         sstrncpy (vl.plugin, "vserver", sizeof (vl.plugin));
89         sstrncpy (vl.plugin_instance, plugin_instance, sizeof (vl.plugin_instance));
90         sstrncpy (vl.type, "load", sizeof (vl.type));
91
92         plugin_dispatch_values (&vl);
93 }
94
95 static void submit_gauge (const char *plugin_instance, const char *type,
96                 const char *type_instance, gauge_t value)
97
98 {
99         value_t values[1];
100         value_list_t vl = VALUE_LIST_INIT;
101
102         values[0].gauge = value;
103
104         vl.values = values;
105         vl.values_len = STATIC_ARRAY_SIZE (values);
106         sstrncpy (vl.host, hostname_g, sizeof (vl.host));
107         sstrncpy (vl.plugin, "vserver", sizeof (vl.plugin));
108         sstrncpy (vl.plugin_instance, plugin_instance, sizeof (vl.plugin_instance));
109         sstrncpy (vl.type, type, sizeof (vl.type));
110         sstrncpy (vl.type_instance, type_instance, sizeof (vl.type_instance));
111
112         plugin_dispatch_values (&vl);
113 } /* void submit_gauge */
114
115 static derive_t vserver_get_sock_bytes(const char *s)
116 {
117         value_t v;
118         int status;
119
120         while (s[0] != '/')
121                 ++s;
122
123         /* Remove '/' */
124         ++s;
125
126         status = parse_value (s, &v, DS_TYPE_DERIVE);
127         if (status != 0)
128                 return (-1);
129         return (v.derive);
130 }
131
132 static int vserver_read (void)
133 {
134 #if NAME_MAX < 1024
135 # define DIRENT_BUFFER_SIZE (sizeof (struct dirent) + 1024 + 1)
136 #else
137 # define DIRENT_BUFFER_SIZE (sizeof (struct dirent) + NAME_MAX + 1)
138 #endif
139
140         DIR                     *proc;
141         struct dirent   *dent; /* 42 */
142         char dirent_buffer[DIRENT_BUFFER_SIZE];
143
144         errno = 0;
145         proc = opendir (PROCDIR);
146         if (proc == NULL)
147         {
148                 char errbuf[1024];
149                 ERROR ("vserver plugin: fopen (%s): %s", PROCDIR, 
150                                 sstrerror (errno, errbuf, sizeof (errbuf)));
151                 return (-1);
152         }
153
154         while (42)
155         {
156                 int len;
157                 char file[BUFSIZE];
158
159                 FILE *fh;
160                 char buffer[BUFSIZE];
161
162                 struct stat statbuf;
163                 char *cols[4];
164
165                 int status;
166
167                 status = readdir_r (proc, (struct dirent *) dirent_buffer, &dent);
168                 if (status != 0)
169                 {
170                         char errbuf[4096];
171                         ERROR ("vserver plugin: readdir_r failed: %s",
172                                         sstrerror (errno, errbuf, sizeof (errbuf)));
173                         closedir (proc);
174                         return (-1);
175                 }
176                 else if (dent == NULL)
177                 {
178                         /* end of directory */
179                         break;
180                 }
181
182                 if (dent->d_name[0] == '.')
183                         continue;
184
185                 len = ssnprintf (file, sizeof (file), PROCDIR "/%s", dent->d_name);
186                 if ((len < 0) || (len >= BUFSIZE))
187                         continue;
188                 
189                 status = stat (file, &statbuf);
190                 if (status != 0)
191                 {
192                         char errbuf[4096];
193                         WARNING ("vserver plugin: stat (%s) failed: %s",
194                                         file, sstrerror (errno, errbuf, sizeof (errbuf)));
195                         continue;
196                 }
197                 
198                 if (!S_ISDIR (statbuf.st_mode))
199                         continue;
200
201                 /* socket message accounting */
202                 len = ssnprintf (file, sizeof (file),
203                                 PROCDIR "/%s/cacct", dent->d_name);
204                 if ((len < 0) || ((size_t) len >= sizeof (file)))
205                         continue;
206
207                 if (NULL == (fh = fopen (file, "r")))
208                 {
209                         char errbuf[1024];
210                         ERROR ("Cannot open '%s': %s", file,
211                                         sstrerror (errno, errbuf, sizeof (errbuf)));
212                 }
213
214                 while ((fh != NULL) && (NULL != fgets (buffer, BUFSIZE, fh)))
215                 {
216                         derive_t rx;
217                         derive_t tx;
218                         char *type_instance;
219
220                         if (strsplit (buffer, cols, 4) < 4)
221                                 continue;
222
223                         if (0 == strcmp (cols[0], "UNIX:"))
224                                 type_instance = "unix";
225                         else if (0 == strcmp (cols[0], "INET:"))
226                                 type_instance = "inet";
227                         else if (0 == strcmp (cols[0], "INET6:"))
228                                 type_instance = "inet6";
229                         else if (0 == strcmp (cols[0], "OTHER:"))
230                                 type_instance = "other";
231                         else if (0 == strcmp (cols[0], "UNSPEC:"))
232                                 type_instance = "unspec";
233                         else
234                                 continue;
235
236                         rx = vserver_get_sock_bytes (cols[1]);
237                         tx = vserver_get_sock_bytes (cols[2]);
238                         /* cols[3] == errors */
239
240                         traffic_submit (dent->d_name, type_instance, rx, tx);
241                 } /* while (fgets) */
242
243                 if (fh != NULL)
244                 {
245                         fclose (fh);
246                         fh = NULL;
247                 }
248
249                 /* thread information and load */
250                 len = ssnprintf (file, sizeof (file),
251                                 PROCDIR "/%s/cvirt", dent->d_name);
252                 if ((len < 0) || ((size_t) len >= sizeof (file)))
253                         continue;
254
255                 if (NULL == (fh = fopen (file, "r")))
256                 {
257                         char errbuf[1024];
258                         ERROR ("Cannot open '%s': %s", file,
259                                         sstrerror (errno, errbuf, sizeof (errbuf)));
260                 }
261
262                 while ((fh != NULL) && (NULL != fgets (buffer, BUFSIZE, fh)))
263                 {
264                         int n = strsplit (buffer, cols, 4);
265
266                         if (2 == n)
267                         {
268                                 char   *type_instance;
269                                 gauge_t value;
270
271                                 if (0 == strcmp (cols[0], "nr_threads:"))
272                                         type_instance = "total";
273                                 else if (0 == strcmp (cols[0], "nr_running:"))
274                                         type_instance = "running";
275                                 else if (0 == strcmp (cols[0], "nr_unintr:"))
276                                         type_instance = "uninterruptable";
277                                 else if (0 == strcmp (cols[0], "nr_onhold:"))
278                                         type_instance = "onhold";
279                                 else
280                                         continue;
281
282                                 value = atof (cols[1]);
283                                 submit_gauge (dent->d_name, "vs_threads", type_instance, value);
284                         }
285                         else if (4 == n) {
286                                 if (0 == strcmp (cols[0], "loadavg:"))
287                                 {
288                                         gauge_t snum = atof (cols[1]);
289                                         gauge_t mnum = atof (cols[2]);
290                                         gauge_t lnum = atof (cols[3]);
291                                         load_submit (dent->d_name, snum, mnum, lnum);
292                                 }
293                         }
294                 } /* while (fgets) */
295
296                 if (fh != NULL)
297                 {
298                         fclose (fh);
299                         fh = NULL;
300                 }
301
302                 /* processes and memory usage */
303                 len = ssnprintf (file, sizeof (file),
304                                 PROCDIR "/%s/limit", dent->d_name);
305                 if ((len < 0) || ((size_t) len >= sizeof (file)))
306                         continue;
307
308                 if (NULL == (fh = fopen (file, "r")))
309                 {
310                         char errbuf[1024];
311                         ERROR ("Cannot open '%s': %s", file,
312                                         sstrerror (errno, errbuf, sizeof (errbuf)));
313                 }
314
315                 while ((fh != NULL) && (NULL != fgets (buffer, BUFSIZE, fh)))
316                 {
317                         char *type = "vs_memory";
318                         char *type_instance;
319                         gauge_t value;
320
321                         if (strsplit (buffer, cols, 2) < 2)
322                                 continue;
323
324                         if (0 == strcmp (cols[0], "PROC:"))
325                         {
326                                 type = "vs_processes";
327                                 type_instance = "";
328                                 value = atof (cols[1]);
329                         }
330                         else
331                         {
332                                 if (0 == strcmp (cols[0], "VM:"))
333                                         type_instance = "vm";
334                                 else if (0 == strcmp (cols[0], "VML:"))
335                                         type_instance = "vml";
336                                 else if (0 == strcmp (cols[0], "RSS:"))
337                                         type_instance = "rss";
338                                 else if (0 == strcmp (cols[0], "ANON:"))
339                                         type_instance = "anon";
340                                 else
341                                         continue;
342
343                                 value = atof (cols[1]) * pagesize;
344                         }
345
346                         submit_gauge (dent->d_name, type, type_instance, value);
347                 } /* while (fgets) */
348
349                 if (fh != NULL)
350                 {
351                         fclose (fh);
352                         fh = NULL;
353                 }
354         } /* while (readdir) */
355
356         closedir (proc);
357
358         return (0);
359 } /* int vserver_read */
360
361 void module_register (void)
362 {
363         plugin_register_init ("vserver", vserver_init);
364         plugin_register_read ("vserver", vserver_read);
365 } /* void module_register(void) */
366
367 /* vim: set ts=4 sw=4 noexpandtab : */