2 * collectd - src/filecount.c
3 * Copyright (C) 2008 Alessandro Iurlano
4 * Copyright (C) 2008 Florian octo Forster
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.
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.
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
20 * Alessandro Iurlano <alessandro.iurlano at gmail.com>
21 * Florian octo Forster <octo at collectd.org>
29 #include <sys/types.h>
35 #define FC_RECURSIVE 1
38 struct fc_directory_conf_s
54 /* Helper for the recursive functions */
57 typedef struct fc_directory_conf_s fc_directory_conf_t;
59 static fc_directory_conf_t **directories = NULL;
60 static size_t directories_num = 0;
62 static void fc_submit_dir (const fc_directory_conf_t *dir)
64 value_list_t vl = VALUE_LIST_INIT;
66 vl.values = &(value_t) { .gauge = (gauge_t) dir->files_num };
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));
72 plugin_dispatch_values (&vl);
74 vl.values = &(value_t) { .gauge = (gauge_t) dir->files_size };
75 sstrncpy (vl.type, "bytes", sizeof (vl.type));
77 plugin_dispatch_values (&vl);
78 } /* void fc_submit_dir */
83 * <Directory /path/to/dir>
96 static int fc_config_set_instance (fc_directory_conf_t *dir, const char *str)
102 sstrncpy (buffer, str, sizeof (buffer));
103 for (ptr = buffer; *ptr != 0; ptr++)
107 for (ptr = buffer; *ptr == '_'; ptr++)
117 sfree (dir->instance);
118 dir->instance = copy;
121 } /* int fc_config_set_instance */
123 static int fc_config_add_dir_instance (fc_directory_conf_t *dir,
126 if ((ci->values_num != 1)
127 || (ci->values[0].type != OCONFIG_TYPE_STRING))
129 WARNING ("filecount plugin: The `Instance' config option needs exactly "
130 "one string argument.");
134 return (fc_config_set_instance (dir, ci->values[0].value.string));
135 } /* int fc_config_add_dir_instance */
137 static int fc_config_add_dir_name (fc_directory_conf_t *dir,
142 if ((ci->values_num != 1)
143 || (ci->values[0].type != OCONFIG_TYPE_STRING))
145 WARNING ("filecount plugin: The `Name' config option needs exactly one "
150 temp = strdup (ci->values[0].value.string);
153 ERROR ("filecount plugin: strdup failed.");
161 } /* int fc_config_add_dir_name */
163 static int fc_config_add_dir_mtime (fc_directory_conf_t *dir,
169 if ((ci->values_num != 1)
170 || ((ci->values[0].type != OCONFIG_TYPE_STRING)
171 && (ci->values[0].type != OCONFIG_TYPE_NUMBER)))
173 WARNING ("filecount plugin: The `MTime' config option needs exactly one "
174 "string or numeric argument.");
178 if (ci->values[0].type == OCONFIG_TYPE_NUMBER)
180 dir->mtime = (int64_t) ci->values[0].value.number;
186 temp = strtod (ci->values[0].value.string, &endptr);
187 if ((errno != 0) || (endptr == NULL)
188 || (endptr == ci->values[0].value.string))
190 WARNING ("filecount plugin: Converting `%s' to a number failed.",
191 ci->values[0].value.string);
224 temp *= 31557600; /* == 365.25 * 86400 */
228 WARNING ("filecount plugin: Invalid suffix for `MTime': `%c'", *endptr);
230 } /* switch (*endptr) */
232 dir->mtime = (int64_t) temp;
235 } /* int fc_config_add_dir_mtime */
237 static int fc_config_add_dir_size (fc_directory_conf_t *dir,
243 if ((ci->values_num != 1)
244 || ((ci->values[0].type != OCONFIG_TYPE_STRING)
245 && (ci->values[0].type != OCONFIG_TYPE_NUMBER)))
247 WARNING ("filecount plugin: The `Size' config option needs exactly one "
248 "string or numeric argument.");
252 if (ci->values[0].type == OCONFIG_TYPE_NUMBER)
254 dir->size = (int64_t) ci->values[0].value.number;
260 temp = strtod (ci->values[0].value.string, &endptr);
261 if ((errno != 0) || (endptr == NULL)
262 || (endptr == ci->values[0].value.string))
264 WARNING ("filecount plugin: Converting `%s' to a number failed.",
265 ci->values[0].value.string);
283 temp *= 1000.0 * 1000.0;
288 temp *= 1000.0 * 1000.0 * 1000.0;
293 temp *= 1000.0 * 1000.0 * 1000.0 * 1000.0;
298 temp *= 1000.0 * 1000.0 * 1000.0 * 1000.0 * 1000.0;
302 WARNING ("filecount plugin: Invalid suffix for `Size': `%c'", *endptr);
304 } /* switch (*endptr) */
306 dir->size = (int64_t) temp;
309 } /* int fc_config_add_dir_size */
311 static int fc_config_add_dir_option (fc_directory_conf_t *dir,
312 oconfig_item_t *ci, int bit)
314 if ((ci->values_num != 1)
315 || (ci->values[0].type != OCONFIG_TYPE_BOOLEAN))
317 WARNING ("filecount plugin: The `Recursive' config options needs exactly "
318 "one boolean argument.");
322 if (ci->values[0].value.boolean)
325 dir->options &= ~bit;
328 } /* int fc_config_add_dir_option */
330 static int fc_config_add_dir (oconfig_item_t *ci)
332 fc_directory_conf_t *dir;
335 if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_STRING))
337 WARNING ("filecount plugin: `Directory' needs exactly one string "
342 /* Initialize `dir' */
343 dir = calloc (1, sizeof (*dir));
346 ERROR ("filecount plugin: calloc failed.");
350 dir->path = strdup (ci->values[0].value.string);
351 if (dir->path == NULL)
353 ERROR ("filecount plugin: strdup failed.");
358 fc_config_set_instance (dir, dir->path);
360 dir->options = FC_RECURSIVE;
367 for (int i = 0; i < ci->children_num; i++)
369 oconfig_item_t *option = ci->children + i;
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);
385 WARNING ("filecount plugin: fc_config_add_dir: "
386 "Option `%s' not allowed here.", option->key);
392 } /* for (ci->children) */
396 fc_directory_conf_t **temp;
398 temp = realloc (directories,
399 sizeof (*directories) * (directories_num + 1));
402 ERROR ("filecount plugin: realloc failed.");
408 directories[directories_num] = dir;
416 sfree (dir->instance);
423 } /* int fc_config_add_dir */
425 static int fc_config (oconfig_item_t *ci)
427 for (int i = 0; i < ci->children_num; i++)
429 oconfig_item_t *child = ci->children + i;
430 if (strcasecmp ("Directory", child->key) == 0)
431 fc_config_add_dir (child);
434 WARNING ("filecount plugin: Ignoring unknown config option `%s'.",
437 } /* for (ci->children) */
440 } /* int fc_config */
442 static int fc_init (void)
444 if (directories_num < 1)
446 WARNING ("filecount plugin: No directories have been configured.");
453 static int fc_read_dir_callback (const char *dirname, const char *filename,
456 fc_directory_conf_t *dir = user_data;
457 char abs_path[PATH_MAX];
464 ssnprintf (abs_path, sizeof (abs_path), "%s/%s", dirname, filename);
466 status = lstat (abs_path, &statbuf);
469 ERROR ("filecount plugin: stat (%s) failed.", abs_path);
473 if (S_ISDIR (statbuf.st_mode) && (dir->options & FC_RECURSIVE))
475 status = walk_directory (abs_path, fc_read_dir_callback, dir,
476 /* include hidden = */ (dir->options & FC_HIDDEN) ? 1 : 0);
479 else if (!S_ISREG (statbuf.st_mode))
484 if (dir->name != NULL)
486 status = fnmatch (dir->name, filename, /* flags = */ 0);
493 time_t mtime = dir->now;
500 DEBUG ("filecount plugin: Only collecting files that were touched %s %u.",
501 (dir->mtime < 0) ? "after" : "before",
502 (unsigned int) mtime);
504 if (((dir->mtime < 0) && (statbuf.st_mtime < mtime))
505 || ((dir->mtime > 0) && (statbuf.st_mtime > mtime)))
514 size = (off_t) ((-1) * dir->size);
516 size = (off_t) dir->size;
518 if (((dir->size < 0) && (statbuf.st_size > size))
519 || ((dir->size > 0) && (statbuf.st_size < size)))
524 dir->files_size += (uint64_t) statbuf.st_size;
527 } /* int fc_read_dir_callback */
529 static int fc_read_dir (fc_directory_conf_t *dir)
537 dir->now = time (NULL);
539 status = walk_directory (dir->path, fc_read_dir_callback, dir,
540 /* include hidden */ (dir->options & FC_HIDDEN) ? 1 : 0);
543 WARNING ("filecount plugin: walk_directory (%s) failed.", dir->path);
550 } /* int fc_read_dir */
552 static int fc_read (void)
554 for (size_t i = 0; i < directories_num; i++)
555 fc_read_dir (directories[i]);
560 void module_register (void)
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 */
568 * vim: set sw=2 sts=2 et :