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>
27 #include "utils/common/common.h"
33 #include <sys/types.h>
35 #define FC_RECURSIVE 1
39 struct fc_directory_conf_s {
43 char *files_size_type;
58 /* Helper for the recursive functions */
61 typedef struct fc_directory_conf_s fc_directory_conf_t;
63 static fc_directory_conf_t **directories;
64 static size_t directories_num;
66 static void fc_free_dir(fc_directory_conf_t *dir) {
68 sfree(dir->plugin_name);
70 sfree(dir->files_size_type);
71 sfree(dir->files_num_type);
72 sfree(dir->type_instance);
76 } /* void fc_free_dir */
78 static void fc_submit_dir(const fc_directory_conf_t *dir) {
79 value_list_t vl = VALUE_LIST_INIT;
81 sstrncpy(vl.plugin, dir->plugin_name, sizeof(vl.plugin));
82 if (dir->instance != NULL)
83 sstrncpy(vl.plugin_instance, dir->instance, sizeof(vl.plugin_instance));
84 if (dir->type_instance != NULL)
85 sstrncpy(vl.type_instance, dir->type_instance, sizeof(vl.type_instance));
89 if (dir->files_num_type != NULL) {
90 vl.values = &(value_t){.gauge = (gauge_t)dir->files_num};
91 sstrncpy(vl.type, dir->files_num_type, sizeof(vl.type));
92 plugin_dispatch_values(&vl);
95 if (dir->files_size_type != NULL) {
96 vl.values = &(value_t){.gauge = (gauge_t)dir->files_size};
97 sstrncpy(vl.type, dir->files_size_type, sizeof(vl.type));
98 plugin_dispatch_values(&vl);
100 } /* void fc_submit_dir */
105 * <Directory /path/to/dir>
112 * IncludeHidden false
113 * FilesSizeType "bytes"
114 * FilesCountType "files"
115 * TypeInstance "instance"
124 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++)
136 char *copy = strdup(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_mtime(fc_directory_conf_t *dir,
158 oconfig_item_t *ci) {
159 if ((ci->values_num != 1) || ((ci->values[0].type != OCONFIG_TYPE_STRING) &&
160 (ci->values[0].type != OCONFIG_TYPE_NUMBER))) {
161 WARNING("filecount plugin: The `MTime' config option needs exactly one "
162 "string or numeric argument.");
166 if (ci->values[0].type == OCONFIG_TYPE_NUMBER) {
167 dir->mtime = (int64_t)ci->values[0].value.number;
173 double temp = strtod(ci->values[0].value.string, &endptr);
174 if ((errno != 0) || (endptr == NULL) ||
175 (endptr == ci->values[0].value.string)) {
176 WARNING("filecount plugin: Converting `%s' to a number failed.",
177 ci->values[0].value.string);
209 temp *= 31557600; /* == 365.25 * 86400 */
213 WARNING("filecount plugin: Invalid suffix for `MTime': `%c'", *endptr);
215 } /* switch (*endptr) */
217 dir->mtime = (int64_t)temp;
220 } /* int fc_config_add_dir_mtime */
222 static int fc_config_add_dir_size(fc_directory_conf_t *dir,
223 oconfig_item_t *ci) {
224 if ((ci->values_num != 1) || ((ci->values[0].type != OCONFIG_TYPE_STRING) &&
225 (ci->values[0].type != OCONFIG_TYPE_NUMBER))) {
226 WARNING("filecount plugin: The `Size' config option needs exactly one "
227 "string or numeric argument.");
231 if (ci->values[0].type == OCONFIG_TYPE_NUMBER) {
232 dir->size = (int64_t)ci->values[0].value.number;
238 double temp = strtod(ci->values[0].value.string, &endptr);
239 if ((errno != 0) || (endptr == NULL) ||
240 (endptr == ci->values[0].value.string)) {
241 WARNING("filecount plugin: Converting `%s' to a number failed.",
242 ci->values[0].value.string);
259 temp *= 1000.0 * 1000.0;
264 temp *= 1000.0 * 1000.0 * 1000.0;
269 temp *= 1000.0 * 1000.0 * 1000.0 * 1000.0;
274 temp *= 1000.0 * 1000.0 * 1000.0 * 1000.0 * 1000.0;
278 WARNING("filecount plugin: Invalid suffix for `Size': `%c'", *endptr);
280 } /* switch (*endptr) */
282 dir->size = (int64_t)temp;
285 } /* int fc_config_add_dir_size */
287 static int fc_config_add_dir_option(fc_directory_conf_t *dir,
288 oconfig_item_t *ci, int bit) {
289 if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_BOOLEAN)) {
290 WARNING("filecount plugin: The `Recursive' config options needs exactly "
291 "one boolean argument.");
295 if (ci->values[0].value.boolean)
298 dir->options &= ~bit;
301 } /* int fc_config_add_dir_option */
303 static int fc_config_add_dir(oconfig_item_t *ci) {
304 if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_STRING)) {
305 WARNING("filecount plugin: `Directory' needs exactly one string "
310 /* Initialize `dir' */
311 fc_directory_conf_t *dir = calloc(1, sizeof(*dir));
313 ERROR("filecount plugin: calloc failed.");
317 dir->path = strdup(ci->values[0].value.string);
318 if (dir->path == NULL) {
319 ERROR("filecount plugin: strdup failed.");
324 dir->options = FC_RECURSIVE | FC_REGULAR;
327 dir->plugin_name = strdup("filecount");
328 dir->instance = NULL;
329 dir->type_instance = NULL;
333 dir->files_size_type = strdup("bytes");
334 dir->files_num_type = strdup("files");
336 if (dir->plugin_name == NULL || dir->files_size_type == NULL ||
337 dir->files_num_type == NULL) {
338 ERROR("filecount plugin: strdup failed.");
344 for (int i = 0; i < ci->children_num; i++) {
345 oconfig_item_t *option = ci->children + i;
347 if (strcasecmp("Plugin", option->key) == 0)
348 status = cf_util_get_string(option, &dir->plugin_name);
349 else if (strcasecmp("Instance", option->key) == 0)
350 status = fc_config_add_dir_instance(dir, option);
351 else if (strcasecmp("Name", option->key) == 0)
352 status = cf_util_get_string(option, &dir->name);
353 else if (strcasecmp("MTime", option->key) == 0)
354 status = fc_config_add_dir_mtime(dir, option);
355 else if (strcasecmp("Size", option->key) == 0)
356 status = fc_config_add_dir_size(dir, option);
357 else if (strcasecmp("Recursive", option->key) == 0)
358 status = fc_config_add_dir_option(dir, option, FC_RECURSIVE);
359 else if (strcasecmp("IncludeHidden", option->key) == 0)
360 status = fc_config_add_dir_option(dir, option, FC_HIDDEN);
361 else if (strcasecmp("RegularOnly", option->key) == 0)
362 status = fc_config_add_dir_option(dir, option, FC_REGULAR);
363 else if (strcasecmp("FilesSizeType", option->key) == 0)
364 status = cf_util_get_string(option, &dir->files_size_type);
365 else if (strcasecmp("FilesCountType", option->key) == 0)
366 status = cf_util_get_string(option, &dir->files_num_type);
367 else if (strcasecmp("TypeInstance", option->key) == 0)
368 status = cf_util_get_string(option, &dir->type_instance);
370 WARNING("filecount plugin: fc_config_add_dir: "
371 "Option `%s' not allowed here.",
378 } /* for (ci->children) */
385 /* Set default plugin instance */
386 if (dir->instance == NULL) {
387 fc_config_set_instance(dir, dir->path);
388 if (dir->instance == NULL || strlen(dir->instance) == 0) {
389 ERROR("filecount plugin: failed to build plugin instance name.");
395 /* Handle disabled types */
396 if (strlen(dir->instance) == 0)
397 sfree(dir->instance);
399 if (strlen(dir->files_size_type) == 0)
400 sfree(dir->files_size_type);
402 if (strlen(dir->files_num_type) == 0)
403 sfree(dir->files_num_type);
405 if (dir->files_size_type == NULL && dir->files_num_type == NULL) {
406 WARNING("filecount plugin: Both `FilesSizeType' and `FilesCountType ' "
407 "are disabled for '%s'. There's no types to report.",
413 /* Ready to add it to list */
414 fc_directory_conf_t **temp =
415 realloc(directories, sizeof(*directories) * (directories_num + 1));
417 ERROR("filecount plugin: realloc failed.");
423 directories[directories_num] = dir;
427 } /* int fc_config_add_dir */
429 static int fc_config(oconfig_item_t *ci) {
430 for (int i = 0; i < ci->children_num; i++) {
431 oconfig_item_t *child = ci->children + i;
432 if (strcasecmp("Directory", child->key) == 0)
433 fc_config_add_dir(child);
435 WARNING("filecount plugin: Ignoring unknown config option `%s'.",
438 } /* for (ci->children) */
441 } /* int fc_config */
443 static int fc_init(void) {
444 if (directories_num < 1) {
445 WARNING("filecount plugin: No directories have been configured.");
452 static int fc_read_dir_callback(const char *dirname, const char *filename,
454 fc_directory_conf_t *dir = user_data;
455 char abs_path[PATH_MAX];
461 snprintf(abs_path, sizeof(abs_path), "%s/%s", dirname, filename);
463 int status = lstat(abs_path, &statbuf);
465 ERROR("filecount plugin: stat (%s) failed.", abs_path);
469 if (S_ISDIR(statbuf.st_mode) && (dir->options & FC_RECURSIVE)) {
470 status = walk_directory(
471 abs_path, fc_read_dir_callback, dir,
472 /* include hidden = */ (dir->options & FC_HIDDEN) ? 1 : 0);
474 } else if ((dir->options & FC_REGULAR) && !S_ISREG(statbuf.st_mode)) {
478 if (dir->name != NULL) {
479 status = fnmatch(dir->name, filename, /* flags = */ 0);
484 if (!S_ISREG(statbuf.st_mode)) {
489 if (dir->mtime != 0) {
490 time_t mtime = dir->now;
497 DEBUG("filecount plugin: Only collecting files that were touched %s %u.",
498 (dir->mtime < 0) ? "after" : "before", (unsigned int)mtime);
500 if (((dir->mtime < 0) && (statbuf.st_mtime < mtime)) ||
501 ((dir->mtime > 0) && (statbuf.st_mtime > mtime)))
505 if (dir->size != 0) {
509 size = (off_t)((-1) * dir->size);
511 size = (off_t)dir->size;
513 if (((dir->size < 0) && (statbuf.st_size > size)) ||
514 ((dir->size > 0) && (statbuf.st_size < size)))
519 dir->files_size += (uint64_t)statbuf.st_size;
522 } /* int fc_read_dir_callback */
524 static int fc_read_dir(fc_directory_conf_t *dir) {
529 dir->now = time(NULL);
532 walk_directory(dir->path, fc_read_dir_callback, dir,
533 /* include hidden */ (dir->options & FC_HIDDEN) ? 1 : 0);
535 WARNING("filecount plugin: walk_directory (%s) failed.", dir->path);
542 } /* int fc_read_dir */
544 static int fc_read(void) {
545 for (size_t i = 0; i < directories_num; i++)
546 fc_read_dir(directories[i]);
551 void module_register(void) {
552 plugin_register_complex_config("filecount", fc_config);
553 plugin_register_init("filecount", fc_init);
554 plugin_register_read("filecount", fc_read);
555 } /* void module_register */