Tree wide: Don't set vl.host to hostname_g in plugin code.
[collectd.git] / src / vserver.c
1 /**
2  * collectd - src/vserver.c
3  * Copyright (C) 2006,2007  Sebastian Harl
4  * Copyright (C) 2007-2010  Florian octo Forster
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining a
7  * copy of this software and associated documentation files (the "Software"),
8  * to deal in the Software without restriction, including without limitation
9  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10  * and/or sell copies of the Software, and to permit persons to whom the
11  * Software is furnished to do so, subject to the following conditions:
12  *
13  * The above copyright notice and this permission notice shall be included in
14  * all copies or substantial portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
22  * DEALINGS IN THE SOFTWARE.
23  *
24  * Authors:
25  *   Sebastian Harl <sh at tokkee.org>
26  *   Florian octo Forster <octo at collectd.org>
27  **/
28
29 #include "collectd.h"
30
31 #include "common.h"
32 #include "plugin.h"
33
34 #include <dirent.h>
35 #include <sys/types.h>
36
37 #define BUFSIZE 512
38
39 #define PROCDIR "/proc/virtual"
40
41 #if !KERNEL_LINUX
42 # error "No applicable input method."
43 #endif
44
45 static int pagesize = 0;
46
47 static int vserver_init (void)
48 {
49         /* XXX Should we check for getpagesize () in configure?
50          * What's the right thing to do, if there is no getpagesize ()? */
51         pagesize = getpagesize ();
52
53         return (0);
54 } /* static void vserver_init(void) */
55
56 static void traffic_submit (const char *plugin_instance,
57                 const char *type_instance, derive_t rx, derive_t tx)
58 {
59         value_list_t vl = VALUE_LIST_INIT;
60         value_t values[] = {
61                 { .derive = rx },
62                 { .derive = tx },
63         };
64
65         vl.values = values;
66         vl.values_len = STATIC_ARRAY_SIZE (values);
67         sstrncpy (vl.plugin, "vserver", sizeof (vl.plugin));
68         sstrncpy (vl.plugin_instance, plugin_instance, sizeof (vl.plugin_instance));
69         sstrncpy (vl.type, "if_octets", sizeof (vl.type));
70         sstrncpy (vl.type_instance, type_instance, sizeof (vl.type_instance));
71
72         plugin_dispatch_values (&vl);
73 } /* void traffic_submit */
74
75 static void load_submit (const char *plugin_instance,
76                 gauge_t snum, gauge_t mnum, gauge_t lnum)
77 {
78         value_list_t vl = VALUE_LIST_INIT;
79         value_t values[] = {
80                 { .gauge = snum },
81                 { .gauge = mnum },
82                 { .gauge = lnum },
83         };
84
85         vl.values = values;
86         vl.values_len = STATIC_ARRAY_SIZE (values);
87         sstrncpy (vl.plugin, "vserver", sizeof (vl.plugin));
88         sstrncpy (vl.plugin_instance, plugin_instance, sizeof (vl.plugin_instance));
89         sstrncpy (vl.type, "load", sizeof (vl.type));
90
91         plugin_dispatch_values (&vl);
92 }
93
94 static void submit_gauge (const char *plugin_instance, const char *type,
95                 const char *type_instance, gauge_t value)
96
97 {
98         value_list_t vl = VALUE_LIST_INIT;
99
100         vl.values = &(value_t) { .gauge = value };
101         vl.values_len = 1;
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));
106
107         plugin_dispatch_values (&vl);
108 } /* void submit_gauge */
109
110 static derive_t vserver_get_sock_bytes(const char *s)
111 {
112         value_t v;
113         int status;
114
115         while (s[0] != '/')
116                 ++s;
117
118         /* Remove '/' */
119         ++s;
120
121         status = parse_value (s, &v, DS_TYPE_DERIVE);
122         if (status != 0)
123                 return (-1);
124         return (v.derive);
125 }
126
127 static int vserver_read (void)
128 {
129 #if NAME_MAX < 1024
130 # define DIRENT_BUFFER_SIZE (sizeof (struct dirent) + 1024 + 1)
131 #else
132 # define DIRENT_BUFFER_SIZE (sizeof (struct dirent) + NAME_MAX + 1)
133 #endif
134
135         DIR                     *proc;
136         struct dirent   *dent; /* 42 */
137         char dirent_buffer[DIRENT_BUFFER_SIZE];
138
139         errno = 0;
140         proc = opendir (PROCDIR);
141         if (proc == NULL)
142         {
143                 char errbuf[1024];
144                 ERROR ("vserver plugin: fopen (%s): %s", PROCDIR,
145                                 sstrerror (errno, errbuf, sizeof (errbuf)));
146                 return (-1);
147         }
148
149         while (42)
150         {
151                 int len;
152                 char file[BUFSIZE];
153
154                 FILE *fh;
155                 char buffer[BUFSIZE];
156
157                 struct stat statbuf;
158                 char *cols[4];
159
160                 int status;
161
162                 status = readdir_r (proc, (struct dirent *) dirent_buffer, &dent);
163                 if (status != 0)
164                 {
165                         char errbuf[4096];
166                         ERROR ("vserver plugin: readdir_r failed: %s",
167                                         sstrerror (errno, errbuf, sizeof (errbuf)));
168                         closedir (proc);
169                         return (-1);
170                 }
171                 else if (dent == NULL)
172                 {
173                         /* end of directory */
174                         break;
175                 }
176
177                 if (dent->d_name[0] == '.')
178                         continue;
179
180                 len = ssnprintf (file, sizeof (file), PROCDIR "/%s", dent->d_name);
181                 if ((len < 0) || (len >= BUFSIZE))
182                         continue;
183
184                 status = stat (file, &statbuf);
185                 if (status != 0)
186                 {
187                         char errbuf[4096];
188                         WARNING ("vserver plugin: stat (%s) failed: %s",
189                                         file, sstrerror (errno, errbuf, sizeof (errbuf)));
190                         continue;
191                 }
192
193                 if (!S_ISDIR (statbuf.st_mode))
194                         continue;
195
196                 /* socket message accounting */
197                 len = ssnprintf (file, sizeof (file),
198                                 PROCDIR "/%s/cacct", dent->d_name);
199                 if ((len < 0) || ((size_t) len >= sizeof (file)))
200                         continue;
201
202                 if (NULL == (fh = fopen (file, "r")))
203                 {
204                         char errbuf[1024];
205                         ERROR ("Cannot open '%s': %s", file,
206                                         sstrerror (errno, errbuf, sizeof (errbuf)));
207                 }
208
209                 while ((fh != NULL) && (NULL != fgets (buffer, BUFSIZE, fh)))
210                 {
211                         derive_t rx;
212                         derive_t tx;
213                         const char *type_instance;
214
215                         if (strsplit (buffer, cols, 4) < 4)
216                                 continue;
217
218                         if (0 == strcmp (cols[0], "UNIX:"))
219                                 type_instance = "unix";
220                         else if (0 == strcmp (cols[0], "INET:"))
221                                 type_instance = "inet";
222                         else if (0 == strcmp (cols[0], "INET6:"))
223                                 type_instance = "inet6";
224                         else if (0 == strcmp (cols[0], "OTHER:"))
225                                 type_instance = "other";
226                         else if (0 == strcmp (cols[0], "UNSPEC:"))
227                                 type_instance = "unspec";
228                         else
229                                 continue;
230
231                         rx = vserver_get_sock_bytes (cols[1]);
232                         tx = vserver_get_sock_bytes (cols[2]);
233                         /* cols[3] == errors */
234
235                         traffic_submit (dent->d_name, type_instance, rx, tx);
236                 } /* while (fgets) */
237
238                 if (fh != NULL)
239                 {
240                         fclose (fh);
241                         fh = NULL;
242                 }
243
244                 /* thread information and load */
245                 len = ssnprintf (file, sizeof (file),
246                                 PROCDIR "/%s/cvirt", dent->d_name);
247                 if ((len < 0) || ((size_t) len >= sizeof (file)))
248                         continue;
249
250                 if (NULL == (fh = fopen (file, "r")))
251                 {
252                         char errbuf[1024];
253                         ERROR ("Cannot open '%s': %s", file,
254                                         sstrerror (errno, errbuf, sizeof (errbuf)));
255                 }
256
257                 while ((fh != NULL) && (NULL != fgets (buffer, BUFSIZE, fh)))
258                 {
259                         int n = strsplit (buffer, cols, 4);
260
261                         if (2 == n)
262                         {
263                                 const char *type_instance;
264                                 gauge_t value;
265
266                                 if (0 == strcmp (cols[0], "nr_threads:"))
267                                         type_instance = "total";
268                                 else if (0 == strcmp (cols[0], "nr_running:"))
269                                         type_instance = "running";
270                                 else if (0 == strcmp (cols[0], "nr_unintr:"))
271                                         type_instance = "uninterruptable";
272                                 else if (0 == strcmp (cols[0], "nr_onhold:"))
273                                         type_instance = "onhold";
274                                 else
275                                         continue;
276
277                                 value = atof (cols[1]);
278                                 submit_gauge (dent->d_name, "vs_threads", type_instance, value);
279                         }
280                         else if (4 == n) {
281                                 if (0 == strcmp (cols[0], "loadavg:"))
282                                 {
283                                         gauge_t snum = atof (cols[1]);
284                                         gauge_t mnum = atof (cols[2]);
285                                         gauge_t lnum = atof (cols[3]);
286                                         load_submit (dent->d_name, snum, mnum, lnum);
287                                 }
288                         }
289                 } /* while (fgets) */
290
291                 if (fh != NULL)
292                 {
293                         fclose (fh);
294                         fh = NULL;
295                 }
296
297                 /* processes and memory usage */
298                 len = ssnprintf (file, sizeof (file),
299                                 PROCDIR "/%s/limit", dent->d_name);
300                 if ((len < 0) || ((size_t) len >= sizeof (file)))
301                         continue;
302
303                 if (NULL == (fh = fopen (file, "r")))
304                 {
305                         char errbuf[1024];
306                         ERROR ("Cannot open '%s': %s", file,
307                                         sstrerror (errno, errbuf, sizeof (errbuf)));
308                 }
309
310                 while ((fh != NULL) && (NULL != fgets (buffer, BUFSIZE, fh)))
311                 {
312                         const char *type = "vs_memory";
313                         const char *type_instance;
314                         gauge_t value;
315
316                         if (strsplit (buffer, cols, 2) < 2)
317                                 continue;
318
319                         if (0 == strcmp (cols[0], "PROC:"))
320                         {
321                                 type = "vs_processes";
322                                 type_instance = "";
323                                 value = atof (cols[1]);
324                         }
325                         else
326                         {
327                                 if (0 == strcmp (cols[0], "VM:"))
328                                         type_instance = "vm";
329                                 else if (0 == strcmp (cols[0], "VML:"))
330                                         type_instance = "vml";
331                                 else if (0 == strcmp (cols[0], "RSS:"))
332                                         type_instance = "rss";
333                                 else if (0 == strcmp (cols[0], "ANON:"))
334                                         type_instance = "anon";
335                                 else
336                                         continue;
337
338                                 value = atof (cols[1]) * pagesize;
339                         }
340
341                         submit_gauge (dent->d_name, type, type_instance, value);
342                 } /* while (fgets) */
343
344                 if (fh != NULL)
345                 {
346                         fclose (fh);
347                         fh = NULL;
348                 }
349         } /* while (readdir) */
350
351         closedir (proc);
352
353         return (0);
354 } /* int vserver_read */
355
356 void module_register (void)
357 {
358         plugin_register_init ("vserver", vserver_init);
359         plugin_register_read ("vserver", vserver_read);
360 } /* void module_register(void) */
361
362 /* vim: set ts=4 sw=4 noexpandtab : */