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) {
127 sstrncpy(buffer, str, sizeof(buffer));
128 for (ptr = buffer; *ptr != 0; ptr++)
132 for (ptr = buffer; *ptr == '_'; ptr++)
135 char *copy = strdup(ptr);
139 sfree(dir->instance);
140 dir->instance = copy;
143 } /* int fc_config_set_instance */
145 static int fc_config_add_dir_instance(fc_directory_conf_t *dir,
146 oconfig_item_t *ci) {
147 if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_STRING)) {
148 WARNING("filecount plugin: The `Instance' config option needs exactly "
149 "one string argument.");
153 return fc_config_set_instance(dir, ci->values[0].value.string);
154 } /* int fc_config_add_dir_instance */
156 static int fc_config_add_dir_name(fc_directory_conf_t *dir,
157 oconfig_item_t *ci) {
158 if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_STRING)) {
159 WARNING("filecount plugin: The `Name' config option needs exactly one "
164 char *temp = strdup(ci->values[0].value.string);
166 ERROR("filecount plugin: strdup failed.");
174 } /* int fc_config_add_dir_name */
176 static int fc_config_add_dir_mtime(fc_directory_conf_t *dir,
177 oconfig_item_t *ci) {
178 if ((ci->values_num != 1) || ((ci->values[0].type != OCONFIG_TYPE_STRING) &&
179 (ci->values[0].type != OCONFIG_TYPE_NUMBER))) {
180 WARNING("filecount plugin: The `MTime' config option needs exactly one "
181 "string or numeric argument.");
185 if (ci->values[0].type == OCONFIG_TYPE_NUMBER) {
186 dir->mtime = (int64_t)ci->values[0].value.number;
192 double temp = strtod(ci->values[0].value.string, &endptr);
193 if ((errno != 0) || (endptr == NULL) ||
194 (endptr == ci->values[0].value.string)) {
195 WARNING("filecount plugin: Converting `%s' to a number failed.",
196 ci->values[0].value.string);
228 temp *= 31557600; /* == 365.25 * 86400 */
232 WARNING("filecount plugin: Invalid suffix for `MTime': `%c'", *endptr);
234 } /* switch (*endptr) */
236 dir->mtime = (int64_t)temp;
239 } /* int fc_config_add_dir_mtime */
241 static int fc_config_add_dir_size(fc_directory_conf_t *dir,
242 oconfig_item_t *ci) {
243 if ((ci->values_num != 1) || ((ci->values[0].type != OCONFIG_TYPE_STRING) &&
244 (ci->values[0].type != OCONFIG_TYPE_NUMBER))) {
245 WARNING("filecount plugin: The `Size' config option needs exactly one "
246 "string or numeric argument.");
250 if (ci->values[0].type == OCONFIG_TYPE_NUMBER) {
251 dir->size = (int64_t)ci->values[0].value.number;
257 double temp = strtod(ci->values[0].value.string, &endptr);
258 if ((errno != 0) || (endptr == NULL) ||
259 (endptr == ci->values[0].value.string)) {
260 WARNING("filecount plugin: Converting `%s' to a number failed.",
261 ci->values[0].value.string);
278 temp *= 1000.0 * 1000.0;
283 temp *= 1000.0 * 1000.0 * 1000.0;
288 temp *= 1000.0 * 1000.0 * 1000.0 * 1000.0;
293 temp *= 1000.0 * 1000.0 * 1000.0 * 1000.0 * 1000.0;
297 WARNING("filecount plugin: Invalid suffix for `Size': `%c'", *endptr);
299 } /* switch (*endptr) */
301 dir->size = (int64_t)temp;
304 } /* int fc_config_add_dir_size */
306 static int fc_config_add_dir_option(fc_directory_conf_t *dir,
307 oconfig_item_t *ci, int bit) {
308 if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_BOOLEAN)) {
309 WARNING("filecount plugin: The `Recursive' config options needs exactly "
310 "one boolean argument.");
314 if (ci->values[0].value.boolean)
317 dir->options &= ~bit;
320 } /* int fc_config_add_dir_option */
322 static int fc_config_add_dir(oconfig_item_t *ci) {
323 if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_STRING)) {
324 WARNING("filecount plugin: `Directory' needs exactly one string "
329 /* Initialize `dir' */
330 fc_directory_conf_t *dir = calloc(1, sizeof(*dir));
332 ERROR("filecount plugin: calloc failed.");
336 dir->path = strdup(ci->values[0].value.string);
337 if (dir->path == NULL) {
338 ERROR("filecount plugin: strdup failed.");
343 dir->options = FC_RECURSIVE;
346 dir->plugin_name = strdup("filecount");
347 dir->instance = NULL;
348 dir->type_instance = NULL;
352 dir->files_size_type = strdup("bytes");
353 dir->files_num_type = strdup("files");
355 if (dir->plugin_name == NULL || dir->files_size_type == NULL ||
356 dir->files_num_type == NULL) {
357 ERROR("filecount plugin: strdup failed.");
363 for (int i = 0; i < ci->children_num; i++) {
364 oconfig_item_t *option = ci->children + i;
366 if (strcasecmp("Plugin", option->key) == 0)
367 status = cf_util_get_string(option, &dir->plugin_name);
368 else if (strcasecmp("Instance", option->key) == 0)
369 status = fc_config_add_dir_instance(dir, option);
370 else if (strcasecmp("Name", option->key) == 0)
371 status = fc_config_add_dir_name(dir, option);
372 else if (strcasecmp("MTime", option->key) == 0)
373 status = fc_config_add_dir_mtime(dir, option);
374 else if (strcasecmp("Size", option->key) == 0)
375 status = fc_config_add_dir_size(dir, option);
376 else if (strcasecmp("Recursive", option->key) == 0)
377 status = fc_config_add_dir_option(dir, option, FC_RECURSIVE);
378 else if (strcasecmp("IncludeHidden", option->key) == 0)
379 status = fc_config_add_dir_option(dir, option, FC_HIDDEN);
380 else if (strcasecmp("FilesSizeType", option->key) == 0)
381 status = cf_util_get_string(option, &dir->files_size_type);
382 else if (strcasecmp("FilesCountType", option->key) == 0)
383 status = cf_util_get_string(option, &dir->files_num_type);
384 else if (strcasecmp("TypeInstance", option->key) == 0)
385 status = cf_util_get_string(option, &dir->type_instance);
387 WARNING("filecount plugin: fc_config_add_dir: "
388 "Option `%s' not allowed here.",
395 } /* for (ci->children) */
402 /* Set default plugin instance */
403 if (dir->instance == NULL) {
404 fc_config_set_instance(dir, dir->path);
405 if (dir->instance == NULL || strlen(dir->instance) == 0) {
406 ERROR("filecount plugin: failed to build plugin instance name.");
412 /* Handle disabled types */
413 if (strlen(dir->instance) == 0)
414 sfree(dir->instance);
416 if (strlen(dir->files_size_type) == 0)
417 sfree(dir->files_size_type);
419 if (strlen(dir->files_num_type) == 0)
420 sfree(dir->files_num_type);
422 if (dir->files_size_type == NULL && dir->files_num_type == NULL) {
423 WARNING("filecount plugin: Both `FilesSizeType' and `FilesCountType ' "
424 "are disabled for '%s'. There's no types to report.",
430 /* Ready to add it to list */
431 fc_directory_conf_t **temp =
432 realloc(directories, sizeof(*directories) * (directories_num + 1));
434 ERROR("filecount plugin: realloc failed.");
440 directories[directories_num] = dir;
444 } /* int fc_config_add_dir */
446 static int fc_config(oconfig_item_t *ci) {
447 for (int i = 0; i < ci->children_num; i++) {
448 oconfig_item_t *child = ci->children + i;
449 if (strcasecmp("Directory", child->key) == 0)
450 fc_config_add_dir(child);
452 WARNING("filecount plugin: Ignoring unknown config option `%s'.",
455 } /* for (ci->children) */
458 } /* int fc_config */
460 static int fc_init(void) {
461 if (directories_num < 1) {
462 WARNING("filecount plugin: No directories have been configured.");
469 static int fc_read_dir_callback(const char *dirname, const char *filename,
471 fc_directory_conf_t *dir = user_data;
472 char abs_path[PATH_MAX];
478 snprintf(abs_path, sizeof(abs_path), "%s/%s", dirname, filename);
480 int status = lstat(abs_path, &statbuf);
482 ERROR("filecount plugin: stat (%s) failed.", abs_path);
486 if (S_ISDIR(statbuf.st_mode) && (dir->options & FC_RECURSIVE)) {
487 status = walk_directory(
488 abs_path, fc_read_dir_callback, dir,
489 /* include hidden = */ (dir->options & FC_HIDDEN) ? 1 : 0);
491 } else if (!S_ISREG(statbuf.st_mode)) {
495 if (dir->name != NULL) {
496 status = fnmatch(dir->name, filename, /* flags = */ 0);
501 if (dir->mtime != 0) {
502 time_t mtime = dir->now;
509 DEBUG("filecount plugin: Only collecting files that were touched %s %u.",
510 (dir->mtime < 0) ? "after" : "before", (unsigned int)mtime);
512 if (((dir->mtime < 0) && (statbuf.st_mtime < mtime)) ||
513 ((dir->mtime > 0) && (statbuf.st_mtime > mtime)))
517 if (dir->size != 0) {
521 size = (off_t)((-1) * dir->size);
523 size = (off_t)dir->size;
525 if (((dir->size < 0) && (statbuf.st_size > size)) ||
526 ((dir->size > 0) && (statbuf.st_size < size)))
531 dir->files_size += (uint64_t)statbuf.st_size;
534 } /* int fc_read_dir_callback */
536 static int fc_read_dir(fc_directory_conf_t *dir) {
541 dir->now = time(NULL);
544 walk_directory(dir->path, fc_read_dir_callback, dir,
545 /* include hidden */ (dir->options & FC_HIDDEN) ? 1 : 0);
547 WARNING("filecount plugin: walk_directory (%s) failed.", dir->path);
554 } /* int fc_read_dir */
556 static int fc_read(void) {
557 for (size_t i = 0; i < directories_num; i++)
558 fc_read_dir(directories[i]);
563 void module_register(void) {
564 plugin_register_complex_config("filecount", fc_config);
565 plugin_register_init("filecount", fc_init);
566 plugin_register_read("filecount", fc_read);
567 } /* void module_register */