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