Tree wide: Don't set vl.host to hostname_g in plugin code.
[collectd.git] / src / filecount.c
1 /**
2  * collectd - src/filecount.c
3  * Copyright (C) 2008  Alessandro Iurlano
4  * Copyright (C) 2008  Florian octo Forster
5  *
6  * This program is free software; you can redistribute it and/or modify it
7  * under the terms of the GNU General Public License as published by the
8  * Free Software Foundation; only version 2 of the License is applicable.
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  *   Alessandro Iurlano <alessandro.iurlano at gmail.com>
21  *   Florian octo Forster <octo at collectd.org>
22  **/
23
24 #include "collectd.h"
25
26 #include "common.h"
27 #include "plugin.h"
28
29 #include <sys/types.h>
30 #include <sys/stat.h>
31 #include <fcntl.h>
32 #include <dirent.h>
33 #include <fnmatch.h>
34
35 #define FC_RECURSIVE 1
36 #define FC_HIDDEN 2
37
38 struct fc_directory_conf_s
39 {
40   char *path;
41   char *instance;
42
43   int options;
44
45   /* Data counters */
46   uint64_t files_num;
47   uint64_t files_size;
48
49   /* Selectors */
50   char *name;
51   int64_t mtime;
52   int64_t size;
53
54   /* Helper for the recursive functions */
55   time_t now;
56 };
57 typedef struct fc_directory_conf_s fc_directory_conf_t;
58
59 static fc_directory_conf_t **directories = NULL;
60 static size_t directories_num = 0;
61
62 static void fc_submit_dir (const fc_directory_conf_t *dir)
63 {
64   value_list_t vl = VALUE_LIST_INIT;
65
66   vl.values = &(value_t) { .gauge = (gauge_t) dir->files_num };
67   vl.values_len = 1;
68   sstrncpy (vl.plugin, "filecount", sizeof (vl.plugin));
69   sstrncpy (vl.plugin_instance, dir->instance, sizeof (vl.plugin_instance));
70   sstrncpy (vl.type, "files", sizeof (vl.type));
71
72   plugin_dispatch_values (&vl);
73
74   vl.values = &(value_t) { .gauge = (gauge_t) dir->files_size };
75   sstrncpy (vl.type, "bytes", sizeof (vl.type));
76
77   plugin_dispatch_values (&vl);
78 } /* void fc_submit_dir */
79
80 /*
81  * Config:
82  * <Plugin filecount>
83  *   <Directory /path/to/dir>
84  *     Instance "foobar"
85  *     Name "*.conf"
86  *     MTime -3600
87  *     Size "+10M"
88  *   </Directory>
89  * </Plugin>
90  *
91  * Collect:
92  * - Number of files
93  * - Total size
94  */
95
96 static int fc_config_set_instance (fc_directory_conf_t *dir, const char *str)
97 {
98   char buffer[1024];
99   char *ptr;
100   char *copy;
101
102   sstrncpy (buffer, str, sizeof (buffer));
103   for (ptr = buffer; *ptr != 0; ptr++)
104     if (*ptr == '/')
105       *ptr = '_';
106
107   for (ptr = buffer; *ptr == '_'; ptr++)
108     /* do nothing */;
109
110   if (*ptr == 0)
111     return (-1);
112
113   copy = strdup (ptr);
114   if (copy == NULL)
115     return (-1);
116
117   sfree (dir->instance);
118   dir->instance = copy;
119
120   return (0);
121 } /* int fc_config_set_instance */
122
123 static int fc_config_add_dir_instance (fc_directory_conf_t *dir,
124     oconfig_item_t *ci)
125 {
126   if ((ci->values_num != 1)
127       || (ci->values[0].type != OCONFIG_TYPE_STRING))
128   {
129     WARNING ("filecount plugin: The `Instance' config option needs exactly "
130         "one string argument.");
131     return (-1);
132   }
133
134   return (fc_config_set_instance (dir, ci->values[0].value.string));
135 } /* int fc_config_add_dir_instance */
136
137 static int fc_config_add_dir_name (fc_directory_conf_t *dir,
138     oconfig_item_t *ci)
139 {
140   char *temp;
141
142   if ((ci->values_num != 1)
143       || (ci->values[0].type != OCONFIG_TYPE_STRING))
144   {
145     WARNING ("filecount plugin: The `Name' config option needs exactly one "
146         "string argument.");
147     return (-1);
148   }
149
150   temp = strdup (ci->values[0].value.string);
151   if (temp == NULL)
152   {
153     ERROR ("filecount plugin: strdup failed.");
154     return (-1);
155   }
156
157   sfree (dir->name);
158   dir->name = temp;
159
160   return (0);
161 } /* int fc_config_add_dir_name */
162
163 static int fc_config_add_dir_mtime (fc_directory_conf_t *dir,
164     oconfig_item_t *ci)
165 {
166   char *endptr;
167   double temp;
168
169   if ((ci->values_num != 1)
170       || ((ci->values[0].type != OCONFIG_TYPE_STRING)
171         && (ci->values[0].type != OCONFIG_TYPE_NUMBER)))
172   {
173     WARNING ("filecount plugin: The `MTime' config option needs exactly one "
174         "string or numeric argument.");
175     return (-1);
176   }
177
178   if (ci->values[0].type == OCONFIG_TYPE_NUMBER)
179   {
180     dir->mtime = (int64_t) ci->values[0].value.number;
181     return (0);
182   }
183
184   errno = 0;
185   endptr = NULL;
186   temp = strtod (ci->values[0].value.string, &endptr);
187   if ((errno != 0) || (endptr == NULL)
188       || (endptr == ci->values[0].value.string))
189   {
190     WARNING ("filecount plugin: Converting `%s' to a number failed.",
191         ci->values[0].value.string);
192     return (-1);
193   }
194
195   switch (*endptr)
196   {
197     case 0:
198     case 's':
199     case 'S':
200       break;
201
202     case 'm':
203     case 'M':
204       temp *= 60;
205       break;
206
207     case 'h':
208     case 'H':
209       temp *= 3600;
210       break;
211
212     case 'd':
213     case 'D':
214       temp *= 86400;
215       break;
216
217     case 'w':
218     case 'W':
219       temp *= 7 * 86400;
220       break;
221
222     case 'y':
223     case 'Y':
224       temp *= 31557600; /* == 365.25 * 86400 */
225       break;
226
227     default:
228       WARNING ("filecount plugin: Invalid suffix for `MTime': `%c'", *endptr);
229       return (-1);
230   } /* switch (*endptr) */
231
232   dir->mtime = (int64_t) temp;
233
234   return (0);
235 } /* int fc_config_add_dir_mtime */
236
237 static int fc_config_add_dir_size (fc_directory_conf_t *dir,
238     oconfig_item_t *ci)
239 {
240   char *endptr;
241   double temp;
242
243   if ((ci->values_num != 1)
244       || ((ci->values[0].type != OCONFIG_TYPE_STRING)
245         && (ci->values[0].type != OCONFIG_TYPE_NUMBER)))
246   {
247     WARNING ("filecount plugin: The `Size' config option needs exactly one "
248         "string or numeric argument.");
249     return (-1);
250   }
251
252   if (ci->values[0].type == OCONFIG_TYPE_NUMBER)
253   {
254     dir->size = (int64_t) ci->values[0].value.number;
255     return (0);
256   }
257
258   errno = 0;
259   endptr = NULL;
260   temp = strtod (ci->values[0].value.string, &endptr);
261   if ((errno != 0) || (endptr == NULL)
262       || (endptr == ci->values[0].value.string))
263   {
264     WARNING ("filecount plugin: Converting `%s' to a number failed.",
265         ci->values[0].value.string);
266     return (-1);
267   }
268
269   switch (*endptr)
270   {
271     case 0:
272     case 'b':
273     case 'B':
274       break;
275
276     case 'k':
277     case 'K':
278       temp *= 1000.0;
279       break;
280
281     case 'm':
282     case 'M':
283       temp *= 1000.0 * 1000.0;
284       break;
285
286     case 'g':
287     case 'G':
288       temp *= 1000.0 * 1000.0 * 1000.0;
289       break;
290
291     case 't':
292     case 'T':
293       temp *= 1000.0 * 1000.0 * 1000.0 * 1000.0;
294       break;
295
296     case 'p':
297     case 'P':
298       temp *= 1000.0 * 1000.0 * 1000.0 * 1000.0 * 1000.0;
299       break;
300
301     default:
302       WARNING ("filecount plugin: Invalid suffix for `Size': `%c'", *endptr);
303       return (-1);
304   } /* switch (*endptr) */
305
306   dir->size = (int64_t) temp;
307
308   return (0);
309 } /* int fc_config_add_dir_size */
310
311 static int fc_config_add_dir_option (fc_directory_conf_t *dir,
312     oconfig_item_t *ci, int bit)
313 {
314   if ((ci->values_num != 1)
315       || (ci->values[0].type != OCONFIG_TYPE_BOOLEAN))
316   {
317     WARNING ("filecount plugin: The `Recursive' config options needs exactly "
318         "one boolean argument.");
319     return (-1);
320   }
321
322   if (ci->values[0].value.boolean)
323     dir->options |= bit;
324   else
325     dir->options &= ~bit;
326
327   return (0);
328 } /* int fc_config_add_dir_option */
329
330 static int fc_config_add_dir (oconfig_item_t *ci)
331 {
332   fc_directory_conf_t *dir;
333   int status;
334
335   if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_STRING))
336   {
337     WARNING ("filecount plugin: `Directory' needs exactly one string "
338         "argument.");
339     return (-1);
340   }
341
342   /* Initialize `dir' */
343   dir = calloc (1, sizeof (*dir));
344   if (dir == NULL)
345   {
346     ERROR ("filecount plugin: calloc failed.");
347     return (-1);
348   }
349
350   dir->path = strdup (ci->values[0].value.string);
351   if (dir->path == NULL)
352   {
353     ERROR ("filecount plugin: strdup failed.");
354     sfree (dir);
355     return (-1);
356   }
357
358   fc_config_set_instance (dir, dir->path);
359
360   dir->options = FC_RECURSIVE;
361
362   dir->name = NULL;
363   dir->mtime = 0;
364   dir->size = 0;
365
366   status = 0;
367   for (int i = 0; i < ci->children_num; i++)
368   {
369     oconfig_item_t *option = ci->children + i;
370
371     if (strcasecmp ("Instance", option->key) == 0)
372       status = fc_config_add_dir_instance (dir, option);
373     else if (strcasecmp ("Name", option->key) == 0)
374       status = fc_config_add_dir_name (dir, option);
375     else if (strcasecmp ("MTime", option->key) == 0)
376       status = fc_config_add_dir_mtime (dir, option);
377     else if (strcasecmp ("Size", option->key) == 0)
378       status = fc_config_add_dir_size (dir, option);
379     else if (strcasecmp ("Recursive", option->key) == 0)
380       status = fc_config_add_dir_option (dir, option, FC_RECURSIVE);
381     else if (strcasecmp ("IncludeHidden", option->key) == 0)
382       status = fc_config_add_dir_option (dir, option, FC_HIDDEN);
383     else
384     {
385       WARNING ("filecount plugin: fc_config_add_dir: "
386           "Option `%s' not allowed here.", option->key);
387       status = -1;
388     }
389
390     if (status != 0)
391       break;
392   } /* for (ci->children) */
393
394   if (status == 0)
395   {
396     fc_directory_conf_t **temp;
397
398     temp = realloc (directories,
399         sizeof (*directories) * (directories_num + 1));
400     if (temp == NULL)
401     {
402       ERROR ("filecount plugin: realloc failed.");
403       status = -1;
404     }
405     else
406     {
407       directories = temp;
408       directories[directories_num] = dir;
409       directories_num++;
410     }
411   }
412
413   if (status != 0)
414   {
415     sfree (dir->name);
416     sfree (dir->instance);
417     sfree (dir->path);
418     sfree (dir);
419     return (-1);
420   }
421
422   return (0);
423 } /* int fc_config_add_dir */
424
425 static int fc_config (oconfig_item_t *ci)
426 {
427   for (int i = 0; i < ci->children_num; i++)
428   {
429     oconfig_item_t *child = ci->children + i;
430     if (strcasecmp ("Directory", child->key) == 0)
431       fc_config_add_dir (child);
432     else
433     {
434       WARNING ("filecount plugin: Ignoring unknown config option `%s'.",
435           child->key);
436     }
437   } /* for (ci->children) */
438
439   return (0);
440 } /* int fc_config */
441
442 static int fc_init (void)
443 {
444   if (directories_num < 1)
445   {
446     WARNING ("filecount plugin: No directories have been configured.");
447     return (-1);
448   }
449
450   return (0);
451 } /* int fc_init */
452
453 static int fc_read_dir_callback (const char *dirname, const char *filename,
454     void *user_data)
455 {
456   fc_directory_conf_t *dir = user_data;
457   char abs_path[PATH_MAX];
458   struct stat statbuf;
459   int status;
460
461   if (dir == NULL)
462     return (-1);
463
464   ssnprintf (abs_path, sizeof (abs_path), "%s/%s", dirname, filename);
465
466   status = lstat (abs_path, &statbuf);
467   if (status != 0)
468   {
469     ERROR ("filecount plugin: stat (%s) failed.", abs_path);
470     return (-1);
471   }
472
473   if (S_ISDIR (statbuf.st_mode) && (dir->options & FC_RECURSIVE))
474   {
475     status = walk_directory (abs_path, fc_read_dir_callback, dir,
476         /* include hidden = */ (dir->options & FC_HIDDEN) ? 1 : 0);
477     return (status);
478   }
479   else if (!S_ISREG (statbuf.st_mode))
480   {
481     return (0);
482   }
483
484   if (dir->name != NULL)
485   {
486     status = fnmatch (dir->name, filename, /* flags = */ 0);
487     if (status != 0)
488       return (0);
489   }
490
491   if (dir->mtime != 0)
492   {
493     time_t mtime = dir->now;
494
495     if (dir->mtime < 0)
496       mtime += dir->mtime;
497     else
498       mtime -= dir->mtime;
499
500     DEBUG ("filecount plugin: Only collecting files that were touched %s %u.",
501         (dir->mtime < 0) ? "after" : "before",
502         (unsigned int) mtime);
503
504     if (((dir->mtime < 0) && (statbuf.st_mtime < mtime))
505         || ((dir->mtime > 0) && (statbuf.st_mtime > mtime)))
506       return (0);
507   }
508
509   if (dir->size != 0)
510   {
511     off_t size;
512
513     if (dir->size < 0)
514       size = (off_t) ((-1) * dir->size);
515     else
516       size = (off_t) dir->size;
517
518     if (((dir->size < 0) && (statbuf.st_size > size))
519         || ((dir->size > 0) && (statbuf.st_size < size)))
520       return (0);
521   }
522
523   dir->files_num++;
524   dir->files_size += (uint64_t) statbuf.st_size;
525
526   return (0);
527 } /* int fc_read_dir_callback */
528
529 static int fc_read_dir (fc_directory_conf_t *dir)
530 {
531   int status;
532
533   dir->files_num = 0;
534   dir->files_size = 0;
535
536   if (dir->mtime != 0)
537     dir->now = time (NULL);
538
539   status = walk_directory (dir->path, fc_read_dir_callback, dir,
540       /* include hidden */ (dir->options & FC_HIDDEN) ? 1 : 0);
541   if (status != 0)
542   {
543     WARNING ("filecount plugin: walk_directory (%s) failed.", dir->path);
544     return (-1);
545   }
546
547   fc_submit_dir (dir);
548
549   return (0);
550 } /* int fc_read_dir */
551
552 static int fc_read (void)
553 {
554   for (size_t i = 0; i < directories_num; i++)
555     fc_read_dir (directories[i]);
556
557   return (0);
558 } /* int fc_read */
559
560 void module_register (void)
561 {
562   plugin_register_complex_config ("filecount", fc_config);
563   plugin_register_init ("filecount", fc_init);
564   plugin_register_read ("filecount", fc_read);
565 } /* void module_register */
566
567 /*
568  * vim: set sw=2 sts=2 et :
569  */