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