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