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
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_name(fc_directory_conf_t *dir,
158 oconfig_item_t *ci) {
159 if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_STRING)) {
160 WARNING("filecount plugin: The `Name' config option needs exactly one "
165 char *temp = strdup(ci->values[0].value.string);
167 ERROR("filecount plugin: strdup failed.");
175 } /* int fc_config_add_dir_name */
177 static int fc_config_add_dir_mtime(fc_directory_conf_t *dir,
178 oconfig_item_t *ci) {
179 if ((ci->values_num != 1) || ((ci->values[0].type != OCONFIG_TYPE_STRING) &&
180 (ci->values[0].type != OCONFIG_TYPE_NUMBER))) {
181 WARNING("filecount plugin: The `MTime' config option needs exactly one "
182 "string or numeric argument.");
186 if (ci->values[0].type == OCONFIG_TYPE_NUMBER) {
187 dir->mtime = (int64_t)ci->values[0].value.number;
193 double temp = strtod(ci->values[0].value.string, &endptr);
194 if ((errno != 0) || (endptr == NULL) ||
195 (endptr == ci->values[0].value.string)) {
196 WARNING("filecount plugin: Converting `%s' to a number failed.",
197 ci->values[0].value.string);
229 temp *= 31557600; /* == 365.25 * 86400 */
233 WARNING("filecount plugin: Invalid suffix for `MTime': `%c'", *endptr);
235 } /* switch (*endptr) */
237 dir->mtime = (int64_t)temp;
240 } /* int fc_config_add_dir_mtime */
242 static int fc_config_add_dir_size(fc_directory_conf_t *dir,
243 oconfig_item_t *ci) {
244 if ((ci->values_num != 1) || ((ci->values[0].type != OCONFIG_TYPE_STRING) &&
245 (ci->values[0].type != OCONFIG_TYPE_NUMBER))) {
246 WARNING("filecount plugin: The `Size' config option needs exactly one "
247 "string or numeric argument.");
251 if (ci->values[0].type == OCONFIG_TYPE_NUMBER) {
252 dir->size = (int64_t)ci->values[0].value.number;
258 double temp = strtod(ci->values[0].value.string, &endptr);
259 if ((errno != 0) || (endptr == NULL) ||
260 (endptr == ci->values[0].value.string)) {
261 WARNING("filecount plugin: Converting `%s' to a number failed.",
262 ci->values[0].value.string);
279 temp *= 1000.0 * 1000.0;
284 temp *= 1000.0 * 1000.0 * 1000.0;
289 temp *= 1000.0 * 1000.0 * 1000.0 * 1000.0;
294 temp *= 1000.0 * 1000.0 * 1000.0 * 1000.0 * 1000.0;
298 WARNING("filecount plugin: Invalid suffix for `Size': `%c'", *endptr);
300 } /* switch (*endptr) */
302 dir->size = (int64_t)temp;
305 } /* int fc_config_add_dir_size */
307 static int fc_config_add_dir_option(fc_directory_conf_t *dir,
308 oconfig_item_t *ci, int bit) {
309 if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_BOOLEAN)) {
310 WARNING("filecount plugin: The `Recursive' config options needs exactly "
311 "one boolean argument.");
315 if (ci->values[0].value.boolean)
318 dir->options &= ~bit;
321 } /* int fc_config_add_dir_option */
323 static int fc_config_add_dir(oconfig_item_t *ci) {
324 if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_STRING)) {
325 WARNING("filecount plugin: `Directory' needs exactly one string "
330 /* Initialize `dir' */
331 fc_directory_conf_t *dir = calloc(1, sizeof(*dir));
333 ERROR("filecount plugin: calloc failed.");
337 dir->path = strdup(ci->values[0].value.string);
338 if (dir->path == NULL) {
339 ERROR("filecount plugin: strdup failed.");
344 dir->options = FC_RECURSIVE | FC_REGULAR;
347 dir->plugin_name = strdup("filecount");
348 dir->instance = NULL;
349 dir->type_instance = NULL;
353 dir->files_size_type = strdup("bytes");
354 dir->files_num_type = strdup("files");
356 if (dir->plugin_name == NULL || dir->files_size_type == NULL ||
357 dir->files_num_type == NULL) {
358 ERROR("filecount plugin: strdup failed.");
364 for (int i = 0; i < ci->children_num; i++) {
365 oconfig_item_t *option = ci->children + i;
367 if (strcasecmp("Plugin", option->key) == 0)
368 status = cf_util_get_string(option, &dir->plugin_name);
369 else if (strcasecmp("Instance", option->key) == 0)
370 status = fc_config_add_dir_instance(dir, option);
371 else if (strcasecmp("Name", option->key) == 0)
372 status = fc_config_add_dir_name(dir, option);
373 else if (strcasecmp("MTime", option->key) == 0)
374 status = fc_config_add_dir_mtime(dir, option);
375 else if (strcasecmp("Size", option->key) == 0)
376 status = fc_config_add_dir_size(dir, option);
377 else if (strcasecmp("Recursive", option->key) == 0)
378 status = fc_config_add_dir_option(dir, option, FC_RECURSIVE);
379 else if (strcasecmp("IncludeHidden", option->key) == 0)
380 status = fc_config_add_dir_option(dir, option, FC_HIDDEN);
381 else if (strcasecmp("RegularOnly", option->key) == 0)
382 status = fc_config_add_dir_option(dir, option, FC_REGULAR);
383 else if (strcasecmp("FilesSizeType", option->key) == 0)
384 status = cf_util_get_string(option, &dir->files_size_type);
385 else if (strcasecmp("FilesCountType", option->key) == 0)
386 status = cf_util_get_string(option, &dir->files_num_type);
387 else if (strcasecmp("TypeInstance", option->key) == 0)
388 status = cf_util_get_string(option, &dir->type_instance);
390 WARNING("filecount plugin: fc_config_add_dir: "
391 "Option `%s' not allowed here.",
398 } /* for (ci->children) */
405 /* Set default plugin instance */
406 if (dir->instance == NULL) {
407 fc_config_set_instance(dir, dir->path);
408 if (dir->instance == NULL || strlen(dir->instance) == 0) {
409 ERROR("filecount plugin: failed to build plugin instance name.");
415 /* Handle disabled types */
416 if (strlen(dir->instance) == 0)
417 sfree(dir->instance);
419 if (strlen(dir->files_size_type) == 0)
420 sfree(dir->files_size_type);
422 if (strlen(dir->files_num_type) == 0)
423 sfree(dir->files_num_type);
425 if (dir->files_size_type == NULL && dir->files_num_type == NULL) {
426 WARNING("filecount plugin: Both `FilesSizeType' and `FilesCountType ' "
427 "are disabled for '%s'. There's no types to report.",
433 /* Ready to add it to list */
434 fc_directory_conf_t **temp =
435 realloc(directories, sizeof(*directories) * (directories_num + 1));
437 ERROR("filecount plugin: realloc failed.");
443 directories[directories_num] = dir;
447 } /* int fc_config_add_dir */
449 static int fc_config(oconfig_item_t *ci) {
450 for (int i = 0; i < ci->children_num; i++) {
451 oconfig_item_t *child = ci->children + i;
452 if (strcasecmp("Directory", child->key) == 0)
453 fc_config_add_dir(child);
455 WARNING("filecount plugin: Ignoring unknown config option `%s'.",
458 } /* for (ci->children) */
461 } /* int fc_config */
463 static int fc_init(void) {
464 if (directories_num < 1) {
465 WARNING("filecount plugin: No directories have been configured.");
472 static int fc_read_dir_callback(const char *dirname, const char *filename,
474 fc_directory_conf_t *dir = user_data;
475 char abs_path[PATH_MAX];
481 snprintf(abs_path, sizeof(abs_path), "%s/%s", dirname, filename);
483 int status = lstat(abs_path, &statbuf);
485 ERROR("filecount plugin: stat (%s) failed.", abs_path);
489 if (S_ISDIR(statbuf.st_mode) && (dir->options & FC_RECURSIVE)) {
490 status = walk_directory(
491 abs_path, fc_read_dir_callback, dir,
492 /* include hidden = */ (dir->options & FC_HIDDEN) ? 1 : 0);
494 } else if ((dir->options & FC_REGULAR) && !S_ISREG(statbuf.st_mode)) {
498 if (dir->name != NULL) {
499 status = fnmatch(dir->name, filename, /* flags = */ 0);
504 if (!S_ISREG(statbuf.st_mode)) {
509 if (dir->mtime != 0) {
510 time_t mtime = dir->now;
517 DEBUG("filecount plugin: Only collecting files that were touched %s %u.",
518 (dir->mtime < 0) ? "after" : "before", (unsigned int)mtime);
520 if (((dir->mtime < 0) && (statbuf.st_mtime < mtime)) ||
521 ((dir->mtime > 0) && (statbuf.st_mtime > mtime)))
525 if (dir->size != 0) {
529 size = (off_t)((-1) * dir->size);
531 size = (off_t)dir->size;
533 if (((dir->size < 0) && (statbuf.st_size > size)) ||
534 ((dir->size > 0) && (statbuf.st_size < size)))
539 dir->files_size += (uint64_t)statbuf.st_size;
542 } /* int fc_read_dir_callback */
544 static int fc_read_dir(fc_directory_conf_t *dir) {
549 dir->now = time(NULL);
552 walk_directory(dir->path, fc_read_dir_callback, dir,
553 /* include hidden */ (dir->options & FC_HIDDEN) ? 1 : 0);
555 WARNING("filecount plugin: walk_directory (%s) failed.", dir->path);
562 } /* int fc_read_dir */
564 static int fc_read(void) {
565 for (size_t i = 0; i < directories_num; i++)
566 fc_read_dir(directories[i]);
571 void module_register(void) {
572 plugin_register_complex_config("filecount", fc_config);
573 plugin_register_init("filecount", fc_init);
574 plugin_register_read("filecount", fc_read);
575 } /* void module_register */