Code

- s/monitor_/collect_/g
[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  **/
24 #include "collectd.h"
25 #include "common.h"
26 #include "plugin.h"
28 #include <dirent.h>
29 #include <sys/types.h>
31 #define BUFSIZE 512
33 #define PROCDIR "/proc/virtual"
35 #if !KERNEL_LINUX
36 # error "No applicable input method."
37 #endif
39 static int pagesize = 0;
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 ();
47         return (0);
48 } /* static void vserver_init(void) */
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;
56         values[0].counter = rx;
57         values[1].counter = tx;
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));
67         plugin_dispatch_values (&vl);
68 } /* void traffic_submit */
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;
76         values[0].gauge = snum;
77         values[1].gauge = mnum;
78         values[2].gauge = lnum;
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));
87         plugin_dispatch_values (&vl);
88 }
90 static void submit_gauge (const char *plugin_instance, const char *type,
91                 const char *type_instance, gauge_t value)
93 {
94         value_t values[1];
95         value_list_t vl = VALUE_LIST_INIT;
97         values[0].gauge = value;
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));
107         plugin_dispatch_values (&vl);
108 } /* void submit_gauge */
110 static inline long long __get_sock_bytes(const char *s)
112         while (s[0] != '/')
113                 ++s;
115         /* Remove '/' */
116         ++s;
117         return atoll(s);
120 static int vserver_read (void)
122 #if NAME_MAX < 1024
123 # define DIRENT_BUFFER_SIZE (sizeof (struct dirent) + 1024 + 1)
124 #else
125 # define DIRENT_BUFFER_SIZE (sizeof (struct dirent) + NAME_MAX + 1)
126 #endif
128         DIR                     *proc;
129         struct dirent   *dent; /* 42 */
130         char dirent_buffer[DIRENT_BUFFER_SIZE];
132         errno = 0;
133         proc = opendir (PROCDIR);
134         if (proc == NULL)
135         {
136                 char errbuf[1024];
137                 ERROR ("vserver plugin: fopen (%s): %s", PROCDIR, 
138                                 sstrerror (errno, errbuf, sizeof (errbuf)));
139                 return (-1);
140         }
142         while (42)
143         {
144                 int len;
145                 char file[BUFSIZE];
147                 FILE *fh;
148                 char buffer[BUFSIZE];
150                 struct stat statbuf;
151                 char *cols[4];
153                 int status;
155                 status = readdir_r (proc, (struct dirent *) dirent_buffer, &dent);
156                 if (status != 0)
157                 {
158                         char errbuf[4096];
159                         ERROR ("vserver plugin: readdir_r failed: %s",
160                                         sstrerror (errno, errbuf, sizeof (errbuf)));
161                         closedir (proc);
162                         return (-1);
163                 }
164                 else if (dent == NULL)
165                 {
166                         /* end of directory */
167                         break;
168                 }
170                 if (dent->d_name[0] == '.')
171                         continue;
173                 len = ssnprintf (file, sizeof (file), PROCDIR "/%s", dent->d_name);
174                 if ((len < 0) || (len >= BUFSIZE))
175                         continue;
176                 
177                 status = stat (file, &statbuf);
178                 if (status != 0)
179                 {
180                         char errbuf[4096];
181                         WARNING ("vserver plugin: stat (%s) failed: %s",
182                                         file, sstrerror (errno, errbuf, sizeof (errbuf)));
183                         continue;
184                 }
185                 
186                 if (!S_ISDIR (statbuf.st_mode))
187                         continue;
189                 /* socket message accounting */
190                 len = ssnprintf (file, sizeof (file),
191                                 PROCDIR "/%s/cacct", dent->d_name);
192                 if ((len < 0) || ((size_t) len >= sizeof (file)))
193                         continue;
195                 if (NULL == (fh = fopen (file, "r")))
196                 {
197                         char errbuf[1024];
198                         ERROR ("Cannot open '%s': %s", file,
199                                         sstrerror (errno, errbuf, sizeof (errbuf)));
200                 }
202                 while ((fh != NULL) && (NULL != fgets (buffer, BUFSIZE, fh)))
203                 {
204                         counter_t rx;
205                         counter_t tx;
206                         char *type_instance;
208                         if (strsplit (buffer, cols, 4) < 4)
209                                 continue;
211                         if (0 == strcmp (cols[0], "UNIX:"))
212                                 type_instance = "unix";
213                         else if (0 == strcmp (cols[0], "INET:"))
214                                 type_instance = "inet";
215                         else if (0 == strcmp (cols[0], "INET6:"))
216                                 type_instance = "inet6";
217                         else if (0 == strcmp (cols[0], "OTHER:"))
218                                 type_instance = "other";
219                         else if (0 == strcmp (cols[0], "UNSPEC:"))
220                                 type_instance = "unspec";
221                         else
222                                 continue;
224                         rx = __get_sock_bytes (cols[1]);
225                         tx = __get_sock_bytes (cols[2]);
226                         /* cols[3] == errors */
228                         traffic_submit (dent->d_name, type_instance, rx, tx);
229                 } /* while (fgets) */
231                 if (fh != NULL)
232                 {
233                         fclose (fh);
234                         fh = NULL;
235                 }
237                 /* thread information and load */
238                 len = ssnprintf (file, sizeof (file),
239                                 PROCDIR "/%s/cvirt", dent->d_name);
240                 if ((len < 0) || ((size_t) len >= sizeof (file)))
241                         continue;
243                 if (NULL == (fh = fopen (file, "r")))
244                 {
245                         char errbuf[1024];
246                         ERROR ("Cannot open '%s': %s", file,
247                                         sstrerror (errno, errbuf, sizeof (errbuf)));
248                 }
250                 while ((fh != NULL) && (NULL != fgets (buffer, BUFSIZE, fh)))
251                 {
252                         int n = strsplit (buffer, cols, 4);
254                         if (2 == n)
255                         {
256                                 char   *type_instance;
257                                 gauge_t value;
259                                 if (0 == strcmp (cols[0], "nr_threads:"))
260                                         type_instance = "total";
261                                 else if (0 == strcmp (cols[0], "nr_running:"))
262                                         type_instance = "running";
263                                 else if (0 == strcmp (cols[0], "nr_unintr:"))
264                                         type_instance = "uninterruptable";
265                                 else if (0 == strcmp (cols[0], "nr_onhold:"))
266                                         type_instance = "onhold";
267                                 else
268                                         continue;
270                                 value = atof (cols[1]);
271                                 submit_gauge (dent->d_name, "vs_threads", type_instance, value);
272                         }
273                         else if (4 == n) {
274                                 if (0 == strcmp (cols[0], "loadavg:"))
275                                 {
276                                         gauge_t snum = atof (cols[1]);
277                                         gauge_t mnum = atof (cols[2]);
278                                         gauge_t lnum = atof (cols[3]);
279                                         load_submit (dent->d_name, snum, mnum, lnum);
280                                 }
281                         }
282                 } /* while (fgets) */
284                 if (fh != NULL)
285                 {
286                         fclose (fh);
287                         fh = NULL;
288                 }
290                 /* processes and memory usage */
291                 len = ssnprintf (file, sizeof (file),
292                                 PROCDIR "/%s/limit", dent->d_name);
293                 if ((len < 0) || ((size_t) len >= sizeof (file)))
294                         continue;
296                 if (NULL == (fh = fopen (file, "r")))
297                 {
298                         char errbuf[1024];
299                         ERROR ("Cannot open '%s': %s", file,
300                                         sstrerror (errno, errbuf, sizeof (errbuf)));
301                 }
303                 while ((fh != NULL) && (NULL != fgets (buffer, BUFSIZE, fh)))
304                 {
305                         char *type = "vs_memory";
306                         char *type_instance;
307                         gauge_t value;
309                         if (strsplit (buffer, cols, 2) < 2)
310                                 continue;
312                         if (0 == strcmp (cols[0], "PROC:"))
313                         {
314                                 type = "vs_processes";
315                                 type_instance = "";
316                                 value = atof (cols[1]);
317                         }
318                         else
319                         {
320                                 if (0 == strcmp (cols[0], "VM:"))
321                                         type_instance = "vm";
322                                 else if (0 == strcmp (cols[0], "VML:"))
323                                         type_instance = "vml";
324                                 else if (0 == strcmp (cols[0], "RSS:"))
325                                         type_instance = "rss";
326                                 else if (0 == strcmp (cols[0], "ANON:"))
327                                         type_instance = "anon";
328                                 else
329                                         continue;
331                                 value = atof (cols[1]) * pagesize;
332                         }
334                         submit_gauge (dent->d_name, type, type_instance, value);
335                 } /* while (fgets) */
337                 if (fh != NULL)
338                 {
339                         fclose (fh);
340                         fh = NULL;
341                 }
342         } /* while (readdir) */
344         closedir (proc);
346         return (0);
347 } /* int vserver_read */
349 void module_register (void)
351         plugin_register_init ("vserver", vserver_init);
352         plugin_register_read ("vserver", vserver_read);
353 } /* void module_register(void) */
355 /* vim: set ts=4 sw=4 noexpandtab : */