Merge branch 'collectd-5.6'
[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
31 #include "common.h"
32 #include "plugin.h"
33
34 #include <dirent.h>
35 #include <sys/types.h>
36
37 #define BUFSIZE 512
38
39 #define PROCDIR "/proc/virtual"
40
41 #if !KERNEL_LINUX
42 # error "No applicable input method."
43 #endif
44
45 static int pagesize = 0;
46
47 static int vserver_init (void)
48 {
49         /* XXX Should we check for getpagesize () in configure?
50          * What's the right thing to do, if there is no getpagesize ()? */
51         pagesize = getpagesize ();
52
53         return (0);
54 } /* static void vserver_init(void) */
55
56 static void traffic_submit (const char *plugin_instance,
57                 const char *type_instance, derive_t rx, derive_t tx)
58 {
59         value_list_t vl = VALUE_LIST_INIT;
60         value_t values[] = {
61                 { .derive = rx },
62                 { .derive = tx },
63         };
64
65         vl.values = values;
66         vl.values_len = STATIC_ARRAY_SIZE (values);
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_list_t vl = VALUE_LIST_INIT;
79         value_t values[] = {
80                 { .gauge = snum },
81                 { .gauge = mnum },
82                 { .gauge = lnum },
83         };
84
85         vl.values = values;
86         vl.values_len = STATIC_ARRAY_SIZE (values);
87         sstrncpy (vl.plugin, "vserver", sizeof (vl.plugin));
88         sstrncpy (vl.plugin_instance, plugin_instance, sizeof (vl.plugin_instance));
89         sstrncpy (vl.type, "load", sizeof (vl.type));
90
91         plugin_dispatch_values (&vl);
92 }
93
94 static void submit_gauge (const char *plugin_instance, const char *type,
95                 const char *type_instance, gauge_t value)
96
97 {
98         value_list_t vl = VALUE_LIST_INIT;
99
100         vl.values = &(value_t) { .gauge = value };
101         vl.values_len = 1;
102         sstrncpy (vl.plugin, "vserver", sizeof (vl.plugin));
103         sstrncpy (vl.plugin_instance, plugin_instance, sizeof (vl.plugin_instance));
104         sstrncpy (vl.type, type, sizeof (vl.type));
105         sstrncpy (vl.type_instance, type_instance, sizeof (vl.type_instance));
106
107         plugin_dispatch_values (&vl);
108 } /* void submit_gauge */
109
110 static derive_t vserver_get_sock_bytes(const char *s)
111 {
112         value_t v;
113         int status;
114
115         while (s[0] != '/')
116                 ++s;
117
118         /* Remove '/' */
119         ++s;
120
121         status = parse_value (s, &v, DS_TYPE_DERIVE);
122         if (status != 0)
123                 return (-1);
124         return (v.derive);
125 }
126
127 static int vserver_read (void)
128 {
129         DIR *proc;
130
131         errno = 0;
132         proc = opendir (PROCDIR);
133         if (proc == NULL)
134         {
135                 char errbuf[1024];
136                 ERROR ("vserver plugin: fopen (%s): %s", PROCDIR,
137                                 sstrerror (errno, errbuf, sizeof (errbuf)));
138                 return (-1);
139         }
140
141         while (42)
142         {
143                 struct dirent *dent;
144                 int len;
145                 char file[BUFSIZE];
146
147                 FILE *fh;
148                 char buffer[BUFSIZE];
149
150                 struct stat statbuf;
151                 char *cols[4];
152
153                 int status;
154
155                 errno = 0;
156                 dent = readdir (proc);
157                 if (dent == NULL)
158                 {
159                         char errbuf[4096];
160
161                         if (errno == 0) /* end of directory */
162                                 break;
163
164                         ERROR ("vserver plugin: failed to read directory %s: %s",
165                                         PROCDIR, sstrerror (errno, errbuf, sizeof (errbuf)));
166                         closedir (proc);
167                         return (-1);
168                 }
169
170                 if (dent->d_name[0] == '.')
171                         continue;
172
173                 len = ssnprintf (file, sizeof (file), PROCDIR "/%s", dent->d_name);
174                 if ((len < 0) || (len >= BUFSIZE))
175                         continue;
176
177                 status = stat (file, &statbuf);
178                 if (status != 0)
179                 {
180                         char errbuf[4096];
181                         WARNING ("vserver plugin: stat (%s) failed: %s",
182                                         file, sstrerror (errno, errbuf, sizeof (errbuf)));
183                         continue;
184                 }
185
186                 if (!S_ISDIR (statbuf.st_mode))
187                         continue;
188
189                 /* socket message accounting */
190                 len = ssnprintf (file, sizeof (file),
191                                 PROCDIR "/%s/cacct", dent->d_name);
192                 if ((len < 0) || ((size_t) len >= sizeof (file)))
193                         continue;
194
195                 if (NULL == (fh = fopen (file, "r")))
196                 {
197                         char errbuf[1024];
198                         ERROR ("Cannot open '%s': %s", file,
199                                         sstrerror (errno, errbuf, sizeof (errbuf)));
200                 }
201
202                 while ((fh != NULL) && (NULL != fgets (buffer, BUFSIZE, fh)))
203                 {
204                         derive_t rx;
205                         derive_t tx;
206                         const char *type_instance;
207
208                         if (strsplit (buffer, cols, 4) < 4)
209                                 continue;
210
211                         if (0 == strcmp (cols[0], "UNIX:"))
212                                 type_instance = "unix";
213                         else if (0 == strcmp (cols[0], "INET:"))
214                                 type_instance = "inet";
215                         else if (0 == strcmp (cols[0], "INET6:"))
216                                 type_instance = "inet6";
217                         else if (0 == strcmp (cols[0], "OTHER:"))
218                                 type_instance = "other";
219                         else if (0 == strcmp (cols[0], "UNSPEC:"))
220                                 type_instance = "unspec";
221                         else
222                                 continue;
223
224                         rx = vserver_get_sock_bytes (cols[1]);
225                         tx = vserver_get_sock_bytes (cols[2]);
226                         /* cols[3] == errors */
227
228                         traffic_submit (dent->d_name, type_instance, rx, tx);
229                 } /* while (fgets) */
230
231                 if (fh != NULL)
232                 {
233                         fclose (fh);
234                         fh = NULL;
235                 }
236
237                 /* thread information and load */
238                 len = ssnprintf (file, sizeof (file),
239                                 PROCDIR "/%s/cvirt", dent->d_name);
240                 if ((len < 0) || ((size_t) len >= sizeof (file)))
241                         continue;
242
243                 if (NULL == (fh = fopen (file, "r")))
244                 {
245                         char errbuf[1024];
246                         ERROR ("Cannot open '%s': %s", file,
247                                         sstrerror (errno, errbuf, sizeof (errbuf)));
248                 }
249
250                 while ((fh != NULL) && (NULL != fgets (buffer, BUFSIZE, fh)))
251                 {
252                         int n = strsplit (buffer, cols, 4);
253
254                         if (2 == n)
255                         {
256                                 const char *type_instance;
257                                 gauge_t value;
258
259                                 if (0 == strcmp (cols[0], "nr_threads:"))
260                                         type_instance = "total";
261                                 else if (0 == strcmp (cols[0], "nr_running:"))
262                                         type_instance = "running";
263                                 else if (0 == strcmp (cols[0], "nr_unintr:"))
264                                         type_instance = "uninterruptable";
265                                 else if (0 == strcmp (cols[0], "nr_onhold:"))
266                                         type_instance = "onhold";
267                                 else
268                                         continue;
269
270                                 value = atof (cols[1]);
271                                 submit_gauge (dent->d_name, "vs_threads", type_instance, value);
272                         }
273                         else if (4 == n) {
274                                 if (0 == strcmp (cols[0], "loadavg:"))
275                                 {
276                                         gauge_t snum = atof (cols[1]);
277                                         gauge_t mnum = atof (cols[2]);
278                                         gauge_t lnum = atof (cols[3]);
279                                         load_submit (dent->d_name, snum, mnum, lnum);
280                                 }
281                         }
282                 } /* while (fgets) */
283
284                 if (fh != NULL)
285                 {
286                         fclose (fh);
287                         fh = NULL;
288                 }
289
290                 /* processes and memory usage */
291                 len = ssnprintf (file, sizeof (file),
292                                 PROCDIR "/%s/limit", dent->d_name);
293                 if ((len < 0) || ((size_t) len >= sizeof (file)))
294                         continue;
295
296                 if (NULL == (fh = fopen (file, "r")))
297                 {
298                         char errbuf[1024];
299                         ERROR ("Cannot open '%s': %s", file,
300                                         sstrerror (errno, errbuf, sizeof (errbuf)));
301                 }
302
303                 while ((fh != NULL) && (NULL != fgets (buffer, BUFSIZE, fh)))
304                 {
305                         const char *type = "vs_memory";
306                         const char *type_instance;
307                         gauge_t value;
308
309                         if (strsplit (buffer, cols, 2) < 2)
310                                 continue;
311
312                         if (0 == strcmp (cols[0], "PROC:"))
313                         {
314                                 type = "vs_processes";
315                                 type_instance = "";
316                                 value = atof (cols[1]);
317                         }
318                         else
319                         {
320                                 if (0 == strcmp (cols[0], "VM:"))
321                                         type_instance = "vm";
322                                 else if (0 == strcmp (cols[0], "VML:"))
323                                         type_instance = "vml";
324                                 else if (0 == strcmp (cols[0], "RSS:"))
325                                         type_instance = "rss";
326                                 else if (0 == strcmp (cols[0], "ANON:"))
327                                         type_instance = "anon";
328                                 else
329                                         continue;
330
331                                 value = atof (cols[1]) * pagesize;
332                         }
333
334                         submit_gauge (dent->d_name, type, type_instance, value);
335                 } /* while (fgets) */
336
337                 if (fh != NULL)
338                 {
339                         fclose (fh);
340                         fh = NULL;
341                 }
342         } /* while (readdir) */
343
344         closedir (proc);
345
346         return (0);
347 } /* int vserver_read */
348
349 void module_register (void)
350 {
351         plugin_register_init ("vserver", vserver_init);
352         plugin_register_read ("vserver", vserver_read);
353 } /* void module_register(void) */
354
355 /* vim: set ts=4 sw=4 noexpandtab : */