2e3906097a1882bf1d0d1bc2acf910aed9534965
[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; either version 2 of the License, or (at your
8  * option) any later version.
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  **/
22
23 #include "collectd.h"
24 #include "common.h"
25 #include "plugin.h"
26
27 #include <dirent.h>
28 #include <sys/types.h>
29
30 #define BUFSIZE 512
31
32 #define MODULE_NAME "vserver"
33 #define PROCDIR "/proc/virtual"
34
35 #if defined(KERNEL_LINUX)
36 # define VSERVER_HAVE_READ 1
37 #else
38 # define VSERVER_HAVE_READ 0
39 #endif /* defined(KERNEL_LINUX) */
40
41 static data_source_t octets_dsrc[2] =
42 {
43         {"rx", DS_TYPE_COUNTER, 0, 4294967295.0},
44         {"tx", DS_TYPE_COUNTER, 0, 4294967295.0}
45 };
46
47 static data_set_t octets_ds =
48 {
49         "if_octets", 2, octets_dsrc
50 };
51
52 static data_source_t load_dsrc[3] =
53 {
54         {"shortterm", DS_TYPE_GAUGE, 0.0, 100.0},
55         {"midterm",   DS_TYPE_GAUGE, 0.0, 100.0},
56         {"longterm",  DS_TYPE_GAUGE, 0.0, 100.0}
57 };
58
59 static data_set_t load_ds =
60 {
61         "load", 3, load_dsrc
62 };
63
64 static data_source_t threads_dsrc[1] =
65 {
66         {"value", DS_TYPE_GAUGE, 0.0, 65535.0}
67 };
68
69 static data_set_t threads_ds =
70 {
71         "vs_threads", 1, threads_dsrc
72 };
73
74 static data_source_t processes_dsrc[1] =
75 {
76         {"value", DS_TYPE_GAUGE, 0.0, 65535.0}
77 };
78
79 static data_set_t processes_ds =
80 {
81         "vs_processes", 1, processes_dsrc
82 };
83
84 static data_source_t memory_dsrc[1] =
85 {
86         {"value", DS_TYPE_GAUGE, 0.0, 9223372036854775807.0}
87 };
88
89 static data_set_t memory_ds =
90 {
91         "vs_memory", 1, memory_dsrc
92 };
93
94 #if VSERVER_HAVE_READ
95 static int pagesize = 0;
96
97 static int vserver_init (void)
98 {
99         /* XXX Should we check for getpagesize () in configure?
100          * What's the right thing to do, if there is no getpagesize ()? */
101         pagesize = getpagesize ();
102
103         return (0);
104 } /* static void vserver_init(void) */
105
106 static void traffic_submit (const char *plugin_instance,
107                 const char *type_instance, counter_t rx, counter_t tx)
108 {
109         value_t values[2];
110         value_list_t vl = VALUE_LIST_INIT;
111
112         values[0].counter = rx;
113         values[1].counter = tx;
114
115         vl.values = values;
116         vl.values_len = STATIC_ARRAY_SIZE (values);
117         vl.time = time (NULL);
118         strcpy (vl.host, hostname_g);
119         strcpy (vl.plugin, "vserver");
120         strncpy (vl.plugin_instance, plugin_instance, sizeof (vl.plugin_instance));
121         strncpy (vl.type_instance, type_instance, sizeof (vl.type_instance));
122
123         plugin_dispatch_values ("if_octets", &vl);
124 } /* void traffic_submit */
125
126 static void load_submit (const char *plugin_instance,
127                 gauge_t snum, gauge_t mnum, gauge_t lnum)
128 {
129         value_t values[3];
130         value_list_t vl = VALUE_LIST_INIT;
131
132         values[0].gauge = snum;
133         values[1].gauge = mnum;
134         values[2].gauge = lnum;
135
136         vl.values = values;
137         vl.values_len = STATIC_ARRAY_SIZE (values);
138         vl.time = time (NULL);
139         strcpy (vl.host, hostname_g);
140         strcpy (vl.plugin, "vserver");
141         strncpy (vl.plugin_instance, plugin_instance, sizeof (vl.plugin_instance));
142
143         plugin_dispatch_values ("load", &vl);
144 }
145
146 static void submit_gauge (const char *plugin_instance, const char *type,
147                 const char *type_instance, gauge_t value)
148
149 {
150         value_t values[1];
151         value_list_t vl = VALUE_LIST_INIT;
152
153         values[0].gauge = value;
154
155         vl.values = values;
156         vl.values_len = STATIC_ARRAY_SIZE (values);
157         vl.time = time (NULL);
158         strcpy (vl.host, hostname_g);
159         strcpy (vl.plugin, "vserver");
160         strncpy (vl.plugin_instance, plugin_instance, sizeof (vl.plugin_instance));
161         strncpy (vl.type_instance, type_instance, sizeof (vl.type_instance));
162
163         plugin_dispatch_values (type, &vl);
164 } /* void submit_gauge */
165
166 static inline long long __get_sock_bytes(const char *s)
167 {
168         while (s[0] != '/')
169                 ++s;
170
171         /* Remove '/' */
172         ++s;
173         return atoll(s);
174 }
175
176 static int vserver_read (void)
177 {
178         DIR                     *proc;
179         struct dirent   *dent; /* 42 */
180
181         errno = 0;
182         if (NULL == (proc = opendir (PROCDIR)))
183         {
184                 char errbuf[1024];
185                 ERROR ("vserver plugin: fopen (%s): %s", PROCDIR, 
186                                 sstrerror (errno, errbuf, sizeof (errbuf)));
187                 return (-1);
188         }
189
190         while (NULL != (dent = readdir (proc)))
191         {
192                 int  len;
193                 char file[BUFSIZE];
194
195                 FILE *fh;
196                 char buffer[BUFSIZE];
197
198                 char *cols[4];
199
200                 if (dent->d_name[0] == '.')
201                         continue;
202
203                 /* This is not a directory */
204                 if (dent->d_type != DT_DIR)
205                         continue;
206
207                 /* socket message accounting */
208                 len = snprintf (file, BUFSIZE, PROCDIR "/%s/cacct", dent->d_name);
209                 if ((len < 0) || (len >= BUFSIZE))
210                         continue;
211
212                 if (NULL == (fh = fopen (file, "r")))
213                 {
214                         char errbuf[1024];
215                         ERROR ("Cannot open '%s': %s", file,
216                                         sstrerror (errno, errbuf, sizeof (errbuf)));
217                 }
218
219                 while ((fh != NULL) && (NULL != fgets (buffer, BUFSIZE, fh)))
220                 {
221                         counter_t rx;
222                         counter_t tx;
223                         char *type_instance;
224
225                         if (strsplit (buffer, cols, 4) < 4)
226                                 continue;
227
228                         if (0 == strcmp (cols[0], "UNIX:"))
229                                 type_instance = "unix";
230                         else if (0 == strcmp (cols[0], "INET:"))
231                                 type_instance = "inet";
232                         else if (0 == strcmp (cols[0], "INET6:"))
233                                 type_instance = "inet6";
234                         else if (0 == strcmp (cols[0], "OTHER:"))
235                                 type_instance = "other";
236                         else if (0 == strcmp (cols[0], "UNSPEC:"))
237                                 type_instance = "unspec";
238                         else
239                                 continue;
240
241                         rx = __get_sock_bytes (cols[1]);
242                         tx = __get_sock_bytes (cols[2]);
243                         /* cols[3] == errors */
244
245                         traffic_submit (dent->d_name, type_instance, rx, tx);
246                 } /* while (fgets) */
247
248                 if (fh != NULL)
249                 {
250                         fclose (fh);
251                         fh = NULL;
252                 }
253
254                 /* thread information and load */
255                 len = snprintf (file, BUFSIZE, PROCDIR "/%s/cvirt", dent->d_name);
256                 if ((len < 0) || (len >= BUFSIZE))
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                         int n = strsplit (buffer, cols, 4);
269
270                         if (2 == n)
271                         {
272                                 char   *type_instance;
273                                 gauge_t value;
274
275                                 if (0 == strcmp (cols[0], "nr_threads:"))
276                                         type_instance = "total";
277                                 else if (0 == strcmp (cols[0], "nr_running:"))
278                                         type_instance = "running";
279                                 else if (0 == strcmp (cols[0], "nr_unintr:"))
280                                         type_instance = "uninterruptable";
281                                 else if (0 == strcmp (cols[0], "nr_onhold:"))
282                                         type_instance = "onhold";
283                                 else
284                                         continue;
285
286                                 value = atof (cols[1]);
287                                 submit_gauge (dent->d_name, "vs_threads", type_instance, value);
288                         }
289                         else if (4 == n) {
290                                 if (0 == strcmp (cols[0], "loadavg:"))
291                                 {
292                                         gauge_t snum = atof (cols[1]);
293                                         gauge_t mnum = atof (cols[2]);
294                                         gauge_t lnum = atof (cols[3]);
295                                         load_submit (dent->d_name, snum, mnum, lnum);
296                                 }
297                         }
298                 } /* while (fgets) */
299
300                 if (fh != NULL)
301                 {
302                         fclose (fh);
303                         fh = NULL;
304                 }
305
306                 /* processes and memory usage */
307                 len = snprintf (file, BUFSIZE, PROCDIR "/%s/limit", dent->d_name);
308                 if ((len < 0) || (len >= BUFSIZE))
309                         continue;
310
311                 if (NULL == (fh = fopen (file, "r")))
312                 {
313                         char errbuf[1024];
314                         ERROR ("Cannot open '%s': %s", file,
315                                         sstrerror (errno, errbuf, sizeof (errbuf)));
316                 }
317
318                 while ((fh != NULL) && (NULL != fgets (buffer, BUFSIZE, fh)))
319                 {
320                         char *type = "vs_memory";
321                         char *type_instance;
322                         gauge_t value;
323
324                         if (strsplit (buffer, cols, 2) < 2)
325                                 continue;
326
327                         if (0 == strcmp (cols[0], "PROC:"))
328                         {
329                                 type = "vs_processes";
330                                 type_instance = "";
331                                 value = atof (cols[1]);
332                         }
333                         else
334                         {
335                                 if (0 == strcmp (cols[0], "VM:"))
336                                         type_instance = "vm";
337                                 else if (0 == strcmp (cols[0], "VML:"))
338                                         type_instance = "vml";
339                                 else if (0 == strcmp (cols[0], "RSS:"))
340                                         type_instance = "rss";
341                                 else if (0 == strcmp (cols[0], "ANON:"))
342                                         type_instance = "anon";
343                                 else
344                                         continue;
345
346                                 value = atof (cols[1]) * pagesize;
347                         }
348
349                         submit_gauge (dent->d_name, type, type_instance, value);
350                 } /* while (fgets) */
351
352                 if (fh != NULL)
353                 {
354                         fclose (fh);
355                         fh = NULL;
356                 }
357         } /* while (readdir) */
358
359         closedir (proc);
360
361         return (0);
362 } /* int vserver_read */
363 #endif /* VSERVER_HAVE_READ */
364
365 void module_register (modreg_e load)
366 {
367         if (load & MR_DATASETS)
368         {
369                 plugin_register_data_set (&octets_ds);
370                 plugin_register_data_set (&load_ds);
371                 plugin_register_data_set (&threads_ds);
372                 plugin_register_data_set (&processes_ds);
373                 plugin_register_data_set (&memory_ds);
374         }
375
376 #if VSERVER_HAVE_READ
377         if (load & MR_READ)
378         {
379                 plugin_register_init ("vserver", vserver_init);
380                 plugin_register_read ("vserver", vserver_read);
381         }
382 #endif /* VSERVER_HAVE_READ */
383 } /* void module_register(void) */
384
385 /* vim: set ts=4 sw=4 noexpandtab : */