Merge branch 'collectd-3.11'
[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 MODULE_NAME "vserver"
32 #define PROCDIR "/proc/virtual"
33
34 #if defined(KERNEL_LINUX)
35 # define VSERVER_HAVE_READ 1
36 #else
37 # define VSERVER_HAVE_READ 0
38 #endif /* defined(KERNEL_LINUX) */
39
40 #if VSERVER_HAVE_READ
41 static int pagesize = 0;
42
43 static int vserver_init (void)
44 {
45         /* XXX Should we check for getpagesize () in configure?
46          * What's the right thing to do, if there is no getpagesize ()? */
47         pagesize = getpagesize ();
48
49         return (0);
50 } /* static void vserver_init(void) */
51
52 static void traffic_submit (const char *plugin_instance,
53                 const char *type_instance, counter_t rx, counter_t tx)
54 {
55         value_t values[2];
56         value_list_t vl = VALUE_LIST_INIT;
57
58         values[0].counter = rx;
59         values[1].counter = tx;
60
61         vl.values = values;
62         vl.values_len = STATIC_ARRAY_SIZE (values);
63         vl.time = time (NULL);
64         strcpy (vl.host, hostname_g);
65         strcpy (vl.plugin, "vserver");
66         strncpy (vl.plugin_instance, plugin_instance, sizeof (vl.plugin_instance));
67         strncpy (vl.type_instance, type_instance, sizeof (vl.type_instance));
68
69         plugin_dispatch_values ("if_octets", &vl);
70 } /* void traffic_submit */
71
72 static void load_submit (const char *plugin_instance,
73                 gauge_t snum, gauge_t mnum, gauge_t lnum)
74 {
75         value_t values[3];
76         value_list_t vl = VALUE_LIST_INIT;
77
78         values[0].gauge = snum;
79         values[1].gauge = mnum;
80         values[2].gauge = lnum;
81
82         vl.values = values;
83         vl.values_len = STATIC_ARRAY_SIZE (values);
84         vl.time = time (NULL);
85         strcpy (vl.host, hostname_g);
86         strcpy (vl.plugin, "vserver");
87         strncpy (vl.plugin_instance, plugin_instance, sizeof (vl.plugin_instance));
88
89         plugin_dispatch_values ("load", &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         strcpy (vl.host, hostname_g);
105         strcpy (vl.plugin, "vserver");
106         strncpy (vl.plugin_instance, plugin_instance, sizeof (vl.plugin_instance));
107         strncpy (vl.type_instance, type_instance, sizeof (vl.type_instance));
108
109         plugin_dispatch_values (type, &vl);
110 } /* void submit_gauge */
111
112 static inline long long __get_sock_bytes(const char *s)
113 {
114         while (s[0] != '/')
115                 ++s;
116
117         /* Remove '/' */
118         ++s;
119         return atoll(s);
120 }
121
122 static int vserver_read (void)
123 {
124         DIR                     *proc;
125         struct dirent   *dent; /* 42 */
126
127         errno = 0;
128         if (NULL == (proc = opendir (PROCDIR)))
129         {
130                 char errbuf[1024];
131                 ERROR ("vserver plugin: fopen (%s): %s", PROCDIR, 
132                                 sstrerror (errno, errbuf, sizeof (errbuf)));
133                 return (-1);
134         }
135
136         while (NULL != (dent = readdir (proc)))
137         {
138                 int  len;
139                 char file[BUFSIZE];
140
141                 FILE *fh;
142                 char buffer[BUFSIZE];
143
144                 char *cols[4];
145
146                 if (dent->d_name[0] == '.')
147                         continue;
148
149                 /* This is not a directory */
150                 if (dent->d_type != DT_DIR)
151                         continue;
152
153                 /* socket message accounting */
154                 len = snprintf (file, BUFSIZE, PROCDIR "/%s/cacct", dent->d_name);
155                 if ((len < 0) || (len >= BUFSIZE))
156                         continue;
157
158                 if (NULL == (fh = fopen (file, "r")))
159                 {
160                         char errbuf[1024];
161                         ERROR ("Cannot open '%s': %s", file,
162                                         sstrerror (errno, errbuf, sizeof (errbuf)));
163                 }
164
165                 while ((fh != NULL) && (NULL != fgets (buffer, BUFSIZE, fh)))
166                 {
167                         counter_t rx;
168                         counter_t tx;
169                         char *type_instance;
170
171                         if (strsplit (buffer, cols, 4) < 4)
172                                 continue;
173
174                         if (0 == strcmp (cols[0], "UNIX:"))
175                                 type_instance = "unix";
176                         else if (0 == strcmp (cols[0], "INET:"))
177                                 type_instance = "inet";
178                         else if (0 == strcmp (cols[0], "INET6:"))
179                                 type_instance = "inet6";
180                         else if (0 == strcmp (cols[0], "OTHER:"))
181                                 type_instance = "other";
182                         else if (0 == strcmp (cols[0], "UNSPEC:"))
183                                 type_instance = "unspec";
184                         else
185                                 continue;
186
187                         rx = __get_sock_bytes (cols[1]);
188                         tx = __get_sock_bytes (cols[2]);
189                         /* cols[3] == errors */
190
191                         traffic_submit (dent->d_name, type_instance, rx, tx);
192                 } /* while (fgets) */
193
194                 if (fh != NULL)
195                 {
196                         fclose (fh);
197                         fh = NULL;
198                 }
199
200                 /* thread information and load */
201                 len = snprintf (file, BUFSIZE, PROCDIR "/%s/cvirt", dent->d_name);
202                 if ((len < 0) || (len >= BUFSIZE))
203                         continue;
204
205                 if (NULL == (fh = fopen (file, "r")))
206                 {
207                         char errbuf[1024];
208                         ERROR ("Cannot open '%s': %s", file,
209                                         sstrerror (errno, errbuf, sizeof (errbuf)));
210                 }
211
212                 while ((fh != NULL) && (NULL != fgets (buffer, BUFSIZE, fh)))
213                 {
214                         int n = strsplit (buffer, cols, 4);
215
216                         if (2 == n)
217                         {
218                                 char   *type_instance;
219                                 gauge_t value;
220
221                                 if (0 == strcmp (cols[0], "nr_threads:"))
222                                         type_instance = "total";
223                                 else if (0 == strcmp (cols[0], "nr_running:"))
224                                         type_instance = "running";
225                                 else if (0 == strcmp (cols[0], "nr_unintr:"))
226                                         type_instance = "uninterruptable";
227                                 else if (0 == strcmp (cols[0], "nr_onhold:"))
228                                         type_instance = "onhold";
229                                 else
230                                         continue;
231
232                                 value = atof (cols[1]);
233                                 submit_gauge (dent->d_name, "vs_threads", type_instance, value);
234                         }
235                         else if (4 == n) {
236                                 if (0 == strcmp (cols[0], "loadavg:"))
237                                 {
238                                         gauge_t snum = atof (cols[1]);
239                                         gauge_t mnum = atof (cols[2]);
240                                         gauge_t lnum = atof (cols[3]);
241                                         load_submit (dent->d_name, snum, mnum, lnum);
242                                 }
243                         }
244                 } /* while (fgets) */
245
246                 if (fh != NULL)
247                 {
248                         fclose (fh);
249                         fh = NULL;
250                 }
251
252                 /* processes and memory usage */
253                 len = snprintf (file, BUFSIZE, PROCDIR "/%s/limit", dent->d_name);
254                 if ((len < 0) || (len >= BUFSIZE))
255                         continue;
256
257                 if (NULL == (fh = fopen (file, "r")))
258                 {
259                         char errbuf[1024];
260                         ERROR ("Cannot open '%s': %s", file,
261                                         sstrerror (errno, errbuf, sizeof (errbuf)));
262                 }
263
264                 while ((fh != NULL) && (NULL != fgets (buffer, BUFSIZE, fh)))
265                 {
266                         char *type = "vs_memory";
267                         char *type_instance;
268                         gauge_t value;
269
270                         if (strsplit (buffer, cols, 2) < 2)
271                                 continue;
272
273                         if (0 == strcmp (cols[0], "PROC:"))
274                         {
275                                 type = "vs_processes";
276                                 type_instance = "";
277                                 value = atof (cols[1]);
278                         }
279                         else
280                         {
281                                 if (0 == strcmp (cols[0], "VM:"))
282                                         type_instance = "vm";
283                                 else if (0 == strcmp (cols[0], "VML:"))
284                                         type_instance = "vml";
285                                 else if (0 == strcmp (cols[0], "RSS:"))
286                                         type_instance = "rss";
287                                 else if (0 == strcmp (cols[0], "ANON:"))
288                                         type_instance = "anon";
289                                 else
290                                         continue;
291
292                                 value = atof (cols[1]) * pagesize;
293                         }
294
295                         submit_gauge (dent->d_name, type, type_instance, value);
296                 } /* while (fgets) */
297
298                 if (fh != NULL)
299                 {
300                         fclose (fh);
301                         fh = NULL;
302                 }
303         } /* while (readdir) */
304
305         closedir (proc);
306
307         return (0);
308 } /* int vserver_read */
309 #endif /* VSERVER_HAVE_READ */
310
311 void module_register (void)
312 {
313 #if VSERVER_HAVE_READ
314         plugin_register_init ("vserver", vserver_init);
315         plugin_register_read ("vserver", vserver_read);
316 #endif /* VSERVER_HAVE_READ */
317 } /* void module_register(void) */
318
319 /* vim: set ts=4 sw=4 noexpandtab : */