Merge branch 'collectd-4.4'
[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         DIR                     *proc;
124         struct dirent   *dent; /* 42 */
125
126         errno = 0;
127         if (NULL == (proc = opendir (PROCDIR)))
128         {
129                 char errbuf[1024];
130                 ERROR ("vserver plugin: fopen (%s): %s", PROCDIR, 
131                                 sstrerror (errno, errbuf, sizeof (errbuf)));
132                 return (-1);
133         }
134
135         while (NULL != (dent = readdir (proc)))
136         {
137                 int  len;
138                 char file[BUFSIZE];
139
140                 FILE *fh;
141                 char buffer[BUFSIZE];
142
143                 char *cols[4];
144
145                 if (dent->d_name[0] == '.')
146                         continue;
147
148                 /* This is not a directory */
149                 if (dent->d_type != DT_DIR)
150                         continue;
151
152                 /* socket message accounting */
153                 len = ssnprintf (file, sizeof (file),
154                                 PROCDIR "/%s/cacct", dent->d_name);
155                 if ((len < 0) || (len >= sizeof (file)))
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 = ssnprintf (file, sizeof (file),
202                                 PROCDIR "/%s/cvirt", dent->d_name);
203                 if ((len < 0) || (len >= sizeof (file)))
204                         continue;
205
206                 if (NULL == (fh = fopen (file, "r")))
207                 {
208                         char errbuf[1024];
209                         ERROR ("Cannot open '%s': %s", file,
210                                         sstrerror (errno, errbuf, sizeof (errbuf)));
211                 }
212
213                 while ((fh != NULL) && (NULL != fgets (buffer, BUFSIZE, fh)))
214                 {
215                         int n = strsplit (buffer, cols, 4);
216
217                         if (2 == n)
218                         {
219                                 char   *type_instance;
220                                 gauge_t value;
221
222                                 if (0 == strcmp (cols[0], "nr_threads:"))
223                                         type_instance = "total";
224                                 else if (0 == strcmp (cols[0], "nr_running:"))
225                                         type_instance = "running";
226                                 else if (0 == strcmp (cols[0], "nr_unintr:"))
227                                         type_instance = "uninterruptable";
228                                 else if (0 == strcmp (cols[0], "nr_onhold:"))
229                                         type_instance = "onhold";
230                                 else
231                                         continue;
232
233                                 value = atof (cols[1]);
234                                 submit_gauge (dent->d_name, "vs_threads", type_instance, value);
235                         }
236                         else if (4 == n) {
237                                 if (0 == strcmp (cols[0], "loadavg:"))
238                                 {
239                                         gauge_t snum = atof (cols[1]);
240                                         gauge_t mnum = atof (cols[2]);
241                                         gauge_t lnum = atof (cols[3]);
242                                         load_submit (dent->d_name, snum, mnum, lnum);
243                                 }
244                         }
245                 } /* while (fgets) */
246
247                 if (fh != NULL)
248                 {
249                         fclose (fh);
250                         fh = NULL;
251                 }
252
253                 /* processes and memory usage */
254                 len = ssnprintf (file, sizeof (file),
255                                 PROCDIR "/%s/limit", dent->d_name);
256                 if ((len < 0) || (len >= sizeof (file)))
257                         continue;
258
259                 if (NULL == (fh = fopen (file, "r")))
260                 {
261                         char errbuf[1024];
262                         ERROR ("Cannot open '%s': %s", file,
263                                         sstrerror (errno, errbuf, sizeof (errbuf)));
264                 }
265
266                 while ((fh != NULL) && (NULL != fgets (buffer, BUFSIZE, fh)))
267                 {
268                         char *type = "vs_memory";
269                         char *type_instance;
270                         gauge_t value;
271
272                         if (strsplit (buffer, cols, 2) < 2)
273                                 continue;
274
275                         if (0 == strcmp (cols[0], "PROC:"))
276                         {
277                                 type = "vs_processes";
278                                 type_instance = "";
279                                 value = atof (cols[1]);
280                         }
281                         else
282                         {
283                                 if (0 == strcmp (cols[0], "VM:"))
284                                         type_instance = "vm";
285                                 else if (0 == strcmp (cols[0], "VML:"))
286                                         type_instance = "vml";
287                                 else if (0 == strcmp (cols[0], "RSS:"))
288                                         type_instance = "rss";
289                                 else if (0 == strcmp (cols[0], "ANON:"))
290                                         type_instance = "anon";
291                                 else
292                                         continue;
293
294                                 value = atof (cols[1]) * pagesize;
295                         }
296
297                         submit_gauge (dent->d_name, type, type_instance, value);
298                 } /* while (fgets) */
299
300                 if (fh != NULL)
301                 {
302                         fclose (fh);
303                         fh = NULL;
304                 }
305         } /* while (readdir) */
306
307         closedir (proc);
308
309         return (0);
310 } /* int vserver_read */
311
312 void module_register (void)
313 {
314         plugin_register_init ("vserver", vserver_init);
315         plugin_register_read ("vserver", vserver_read);
316 } /* void module_register(void) */
317
318 /* vim: set ts=4 sw=4 noexpandtab : */