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