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>
33 #include <sys/types.h>
35 #define FC_RECURSIVE 1
38 struct fc_directory_conf_s {
42 char *files_size_type;
57 /* Helper for the recursive functions */
60 typedef struct fc_directory_conf_s fc_directory_conf_t;
62 static fc_directory_conf_t **directories = NULL;
63 static size_t directories_num = 0;
65 void fc_free_dir(fc_directory_conf_t *dir) {
67 sfree(dir->plugin_name);
69 sfree(dir->files_size_type);
70 sfree(dir->files_num_type);
71 sfree(dir->type_instance);
75 } /* void fc_free_dir */
77 static void fc_submit_dir(const fc_directory_conf_t *dir) {
78 value_list_t vl = VALUE_LIST_INIT;
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));
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);
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);
99 } /* void fc_submit_dir */
104 * <Directory /path/to/dir>
111 * IncludeHidden false
112 * FilesSizeType "bytes"
113 * FilesCountType "files"
114 * TypeInstance "instance"
123 static int fc_config_set_instance(fc_directory_conf_t *dir, const char *str) {
128 sstrncpy(buffer, str, sizeof(buffer));
129 for (ptr = buffer; *ptr != 0; ptr++)
133 for (ptr = buffer; *ptr == '_'; ptr++)
140 sfree(dir->instance);
141 dir->instance = copy;
144 } /* int fc_config_set_instance */
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.");
154 return fc_config_set_instance(dir, ci->values[0].value.string);
155 } /* int fc_config_add_dir_instance */
157 static int fc_config_add_dir_name(fc_directory_conf_t *dir,
158 oconfig_item_t *ci) {
161 if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_STRING)) {
162 WARNING("filecount plugin: The `Name' config option needs exactly one "
167 temp = strdup(ci->values[0].value.string);
169 ERROR("filecount plugin: strdup failed.");
177 } /* int fc_config_add_dir_name */
179 static int fc_config_add_dir_mtime(fc_directory_conf_t *dir,
180 oconfig_item_t *ci) {
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.");
191 if (ci->values[0].type == OCONFIG_TYPE_NUMBER) {
192 dir->mtime = (int64_t)ci->values[0].value.number;
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);
234 temp *= 31557600; /* == 365.25 * 86400 */
238 WARNING("filecount plugin: Invalid suffix for `MTime': `%c'", *endptr);
240 } /* switch (*endptr) */
242 dir->mtime = (int64_t)temp;
245 } /* int fc_config_add_dir_mtime */
247 static int fc_config_add_dir_size(fc_directory_conf_t *dir,
248 oconfig_item_t *ci) {
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.");
259 if (ci->values[0].type == OCONFIG_TYPE_NUMBER) {
260 dir->size = (int64_t)ci->values[0].value.number;
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);
287 temp *= 1000.0 * 1000.0;
292 temp *= 1000.0 * 1000.0 * 1000.0;
297 temp *= 1000.0 * 1000.0 * 1000.0 * 1000.0;
302 temp *= 1000.0 * 1000.0 * 1000.0 * 1000.0 * 1000.0;
306 WARNING("filecount plugin: Invalid suffix for `Size': `%c'", *endptr);
308 } /* switch (*endptr) */
310 dir->size = (int64_t)temp;
313 } /* int fc_config_add_dir_size */
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.");
323 if (ci->values[0].value.boolean)
326 dir->options &= ~bit;
329 } /* int fc_config_add_dir_option */
331 static int fc_config_add_dir(oconfig_item_t *ci) {
332 fc_directory_conf_t *dir, **temp;
335 if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_STRING)) {
336 WARNING("filecount plugin: `Directory' needs exactly one string "
341 /* Initialize `dir' */
342 dir = calloc(1, sizeof(*dir));
344 ERROR("filecount plugin: calloc failed.");
348 dir->path = strdup(ci->values[0].value.string);
349 if (dir->path == NULL) {
350 ERROR("filecount plugin: strdup failed.");
355 dir->options = FC_RECURSIVE;
358 dir->plugin_name = strdup("filecount");
359 dir->instance = NULL;
360 dir->type_instance = NULL;
364 dir->files_size_type = strdup("bytes");
365 dir->files_num_type = strdup("files");
367 if (dir->plugin_name == NULL
368 || dir->files_size_type == NULL
369 || dir->files_num_type == NULL) {
370 ERROR("filecount plugin: strdup failed.");
376 for (int i = 0; i < ci->children_num; i++) {
377 oconfig_item_t *option = ci->children + i;
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);
400 WARNING("filecount plugin: fc_config_add_dir: "
401 "Option `%s' not allowed here.",
408 } /* for (ci->children) */
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.");
425 /* Handle disabled types */
426 if (strlen(dir->instance) == 0)
427 sfree(dir->instance);
429 if (strlen(dir->files_size_type) == 0)
430 sfree(dir->files_size_type);
432 if (strlen(dir->files_num_type) == 0)
433 sfree(dir->files_num_type);
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.",
443 /* Ready to add it to list */
444 temp = realloc(directories, sizeof(*directories) * (directories_num + 1));
446 ERROR("filecount plugin: realloc failed.");
452 directories[directories_num] = dir;
456 } /* int fc_config_add_dir */
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);
464 WARNING("filecount plugin: Ignoring unknown config option `%s'.",
467 } /* for (ci->children) */
470 } /* int fc_config */
472 static int fc_init(void) {
473 if (directories_num < 1) {
474 WARNING("filecount plugin: No directories have been configured.");
481 static int fc_read_dir_callback(const char *dirname, const char *filename,
483 fc_directory_conf_t *dir = user_data;
484 char abs_path[PATH_MAX];
491 snprintf(abs_path, sizeof(abs_path), "%s/%s", dirname, filename);
493 status = lstat(abs_path, &statbuf);
495 ERROR("filecount plugin: stat (%s) failed.", abs_path);
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);
504 } else if (!S_ISREG(statbuf.st_mode)) {
508 if (dir->name != NULL) {
509 status = fnmatch(dir->name, filename, /* flags = */ 0);
514 if (dir->mtime != 0) {
515 time_t mtime = dir->now;
522 DEBUG("filecount plugin: Only collecting files that were touched %s %u.",
523 (dir->mtime < 0) ? "after" : "before", (unsigned int)mtime);
525 if (((dir->mtime < 0) && (statbuf.st_mtime < mtime)) ||
526 ((dir->mtime > 0) && (statbuf.st_mtime > mtime)))
530 if (dir->size != 0) {
534 size = (off_t)((-1) * dir->size);
536 size = (off_t)dir->size;
538 if (((dir->size < 0) && (statbuf.st_size > size)) ||
539 ((dir->size > 0) && (statbuf.st_size < size)))
544 dir->files_size += (uint64_t)statbuf.st_size;
547 } /* int fc_read_dir_callback */
549 static int fc_read_dir(fc_directory_conf_t *dir) {
556 dir->now = time(NULL);
559 walk_directory(dir->path, fc_read_dir_callback, dir,
560 /* include hidden */ (dir->options & FC_HIDDEN) ? 1 : 0);
562 WARNING("filecount plugin: walk_directory (%s) failed.", dir->path);
569 } /* int fc_read_dir */
571 static int fc_read(void) {
572 for (size_t i = 0; i < directories_num; i++)
573 fc_read_dir(directories[i]);
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 */