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