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