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