Code

Use readdir() instead of the deprecated readdir_r().
[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  **/
29 #include "collectd.h"
30 #include "common.h"
31 #include "plugin.h"
33 #include <dirent.h>
34 #include <sys/types.h>
36 #define BUFSIZE 512
38 #define PROCDIR "/proc/virtual"
40 #if !KERNEL_LINUX
41 # error "No applicable input method."
42 #endif
44 static int pagesize = 0;
46 static int vserver_init (void)
47 {
48         /* XXX Should we check for getpagesize () in configure?
49          * What's the right thing to do, if there is no getpagesize ()? */
50         pagesize = getpagesize ();
52         return (0);
53 } /* static void vserver_init(void) */
55 static void traffic_submit (const char *plugin_instance,
56                 const char *type_instance, derive_t rx, derive_t tx)
57 {
58         value_t values[2];
59         value_list_t vl = VALUE_LIST_INIT;
61         values[0].derive = rx;
62         values[1].derive = tx;
64         vl.values = values;
65         vl.values_len = STATIC_ARRAY_SIZE (values);
66         sstrncpy (vl.host, hostname_g, sizeof (vl.host));
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));
72         plugin_dispatch_values (&vl);
73 } /* void traffic_submit */
75 static void load_submit (const char *plugin_instance,
76                 gauge_t snum, gauge_t mnum, gauge_t lnum)
77 {
78         value_t values[3];
79         value_list_t vl = VALUE_LIST_INIT;
81         values[0].gauge = snum;
82         values[1].gauge = mnum;
83         values[2].gauge = lnum;
85         vl.values = values;
86         vl.values_len = STATIC_ARRAY_SIZE (values);
87         sstrncpy (vl.host, hostname_g, sizeof (vl.host));
88         sstrncpy (vl.plugin, "vserver", sizeof (vl.plugin));
89         sstrncpy (vl.plugin_instance, plugin_instance, sizeof (vl.plugin_instance));
90         sstrncpy (vl.type, "load", sizeof (vl.type));
92         plugin_dispatch_values (&vl);
93 }
95 static void submit_gauge (const char *plugin_instance, const char *type,
96                 const char *type_instance, gauge_t value)
98 {
99         value_t values[1];
100         value_list_t vl = VALUE_LIST_INIT;
102         values[0].gauge = value;
104         vl.values = values;
105         vl.values_len = STATIC_ARRAY_SIZE (values);
106         sstrncpy (vl.host, hostname_g, sizeof (vl.host));
107         sstrncpy (vl.plugin, "vserver", sizeof (vl.plugin));
108         sstrncpy (vl.plugin_instance, plugin_instance, sizeof (vl.plugin_instance));
109         sstrncpy (vl.type, type, sizeof (vl.type));
110         sstrncpy (vl.type_instance, type_instance, sizeof (vl.type_instance));
112         plugin_dispatch_values (&vl);
113 } /* void submit_gauge */
115 static derive_t vserver_get_sock_bytes(const char *s)
117         value_t v;
118         int status;
120         while (s[0] != '/')
121                 ++s;
123         /* Remove '/' */
124         ++s;
126         status = parse_value (s, &v, DS_TYPE_DERIVE);
127         if (status != 0)
128                 return (-1);
129         return (v.derive);
132 static int vserver_read (void)
134         DIR *proc;
136         errno = 0;
137         proc = opendir (PROCDIR);
138         if (proc == NULL)
139         {
140                 char errbuf[1024];
141                 ERROR ("vserver plugin: fopen (%s): %s", PROCDIR, 
142                                 sstrerror (errno, errbuf, sizeof (errbuf)));
143                 return (-1);
144         }
146         while (42)
147         {
148                 struct dirent *dent;
149                 int len;
150                 char file[BUFSIZE];
152                 FILE *fh;
153                 char buffer[BUFSIZE];
155                 struct stat statbuf;
156                 char *cols[4];
158                 int status;
160                 errno = 0;
161                 dent = readdir (proc);
162                 if (dent == NULL)
163                 {
164                         char errbuf[4096];
166                         if (errno == 0) /* end of directory */
167                                 break;
169                         ERROR ("vserver plugin: failed to read directory %s: %s",
170                                         PROCDIR, sstrerror (errno, errbuf, sizeof (errbuf)));
171                         closedir (proc);
172                         return (-1);
173                 }
175                 if (dent->d_name[0] == '.')
176                         continue;
178                 len = ssnprintf (file, sizeof (file), PROCDIR "/%s", dent->d_name);
179                 if ((len < 0) || (len >= BUFSIZE))
180                         continue;
181                 
182                 status = stat (file, &statbuf);
183                 if (status != 0)
184                 {
185                         char errbuf[4096];
186                         WARNING ("vserver plugin: stat (%s) failed: %s",
187                                         file, sstrerror (errno, errbuf, sizeof (errbuf)));
188                         continue;
189                 }
190                 
191                 if (!S_ISDIR (statbuf.st_mode))
192                         continue;
194                 /* socket message accounting */
195                 len = ssnprintf (file, sizeof (file),
196                                 PROCDIR "/%s/cacct", dent->d_name);
197                 if ((len < 0) || ((size_t) len >= sizeof (file)))
198                         continue;
200                 if (NULL == (fh = fopen (file, "r")))
201                 {
202                         char errbuf[1024];
203                         ERROR ("Cannot open '%s': %s", file,
204                                         sstrerror (errno, errbuf, sizeof (errbuf)));
205                 }
207                 while ((fh != NULL) && (NULL != fgets (buffer, BUFSIZE, fh)))
208                 {
209                         derive_t rx;
210                         derive_t tx;
211                         char *type_instance;
213                         if (strsplit (buffer, cols, 4) < 4)
214                                 continue;
216                         if (0 == strcmp (cols[0], "UNIX:"))
217                                 type_instance = "unix";
218                         else if (0 == strcmp (cols[0], "INET:"))
219                                 type_instance = "inet";
220                         else if (0 == strcmp (cols[0], "INET6:"))
221                                 type_instance = "inet6";
222                         else if (0 == strcmp (cols[0], "OTHER:"))
223                                 type_instance = "other";
224                         else if (0 == strcmp (cols[0], "UNSPEC:"))
225                                 type_instance = "unspec";
226                         else
227                                 continue;
229                         rx = vserver_get_sock_bytes (cols[1]);
230                         tx = vserver_get_sock_bytes (cols[2]);
231                         /* cols[3] == errors */
233                         traffic_submit (dent->d_name, type_instance, rx, tx);
234                 } /* while (fgets) */
236                 if (fh != NULL)
237                 {
238                         fclose (fh);
239                         fh = NULL;
240                 }
242                 /* thread information and load */
243                 len = ssnprintf (file, sizeof (file),
244                                 PROCDIR "/%s/cvirt", dent->d_name);
245                 if ((len < 0) || ((size_t) len >= sizeof (file)))
246                         continue;
248                 if (NULL == (fh = fopen (file, "r")))
249                 {
250                         char errbuf[1024];
251                         ERROR ("Cannot open '%s': %s", file,
252                                         sstrerror (errno, errbuf, sizeof (errbuf)));
253                 }
255                 while ((fh != NULL) && (NULL != fgets (buffer, BUFSIZE, fh)))
256                 {
257                         int n = strsplit (buffer, cols, 4);
259                         if (2 == n)
260                         {
261                                 char   *type_instance;
262                                 gauge_t value;
264                                 if (0 == strcmp (cols[0], "nr_threads:"))
265                                         type_instance = "total";
266                                 else if (0 == strcmp (cols[0], "nr_running:"))
267                                         type_instance = "running";
268                                 else if (0 == strcmp (cols[0], "nr_unintr:"))
269                                         type_instance = "uninterruptable";
270                                 else if (0 == strcmp (cols[0], "nr_onhold:"))
271                                         type_instance = "onhold";
272                                 else
273                                         continue;
275                                 value = atof (cols[1]);
276                                 submit_gauge (dent->d_name, "vs_threads", type_instance, value);
277                         }
278                         else if (4 == n) {
279                                 if (0 == strcmp (cols[0], "loadavg:"))
280                                 {
281                                         gauge_t snum = atof (cols[1]);
282                                         gauge_t mnum = atof (cols[2]);
283                                         gauge_t lnum = atof (cols[3]);
284                                         load_submit (dent->d_name, snum, mnum, lnum);
285                                 }
286                         }
287                 } /* while (fgets) */
289                 if (fh != NULL)
290                 {
291                         fclose (fh);
292                         fh = NULL;
293                 }
295                 /* processes and memory usage */
296                 len = ssnprintf (file, sizeof (file),
297                                 PROCDIR "/%s/limit", dent->d_name);
298                 if ((len < 0) || ((size_t) len >= sizeof (file)))
299                         continue;
301                 if (NULL == (fh = fopen (file, "r")))
302                 {
303                         char errbuf[1024];
304                         ERROR ("Cannot open '%s': %s", file,
305                                         sstrerror (errno, errbuf, sizeof (errbuf)));
306                 }
308                 while ((fh != NULL) && (NULL != fgets (buffer, BUFSIZE, fh)))
309                 {
310                         char *type = "vs_memory";
311                         char *type_instance;
312                         gauge_t value;
314                         if (strsplit (buffer, cols, 2) < 2)
315                                 continue;
317                         if (0 == strcmp (cols[0], "PROC:"))
318                         {
319                                 type = "vs_processes";
320                                 type_instance = "";
321                                 value = atof (cols[1]);
322                         }
323                         else
324                         {
325                                 if (0 == strcmp (cols[0], "VM:"))
326                                         type_instance = "vm";
327                                 else if (0 == strcmp (cols[0], "VML:"))
328                                         type_instance = "vml";
329                                 else if (0 == strcmp (cols[0], "RSS:"))
330                                         type_instance = "rss";
331                                 else if (0 == strcmp (cols[0], "ANON:"))
332                                         type_instance = "anon";
333                                 else
334                                         continue;
336                                 value = atof (cols[1]) * pagesize;
337                         }
339                         submit_gauge (dent->d_name, type, type_instance, value);
340                 } /* while (fgets) */
342                 if (fh != NULL)
343                 {
344                         fclose (fh);
345                         fh = NULL;
346                 }
347         } /* while (readdir) */
349         closedir (proc);
351         return (0);
352 } /* int vserver_read */
354 void module_register (void)
356         plugin_register_init ("vserver", vserver_init);
357         plugin_register_read ("vserver", vserver_read);
358 } /* void module_register(void) */
360 /* vim: set ts=4 sw=4 noexpandtab : */