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 verplant.org>
28 #include <sys/types.h>
34 #define FC_RECURSIVE 1
37 struct fc_directory_conf_s
53 /* Helper for the recursive functions */
56 typedef struct fc_directory_conf_s fc_directory_conf_t;
58 static fc_directory_conf_t **directories = NULL;
59 static size_t directories_num = 0;
61 static void fc_submit_dir (const fc_directory_conf_t *dir)
64 value_list_t vl = VALUE_LIST_INIT;
66 values[0].gauge = (gauge_t) dir->files_num;
69 vl.values_len = STATIC_ARRAY_SIZE (values);
70 sstrncpy (vl.host, hostname_g, sizeof (vl.host));
71 sstrncpy (vl.plugin, "filecount", sizeof (vl.plugin));
72 sstrncpy (vl.plugin_instance, dir->instance, sizeof (vl.plugin_instance));
73 sstrncpy (vl.type, "files", sizeof (vl.type));
75 plugin_dispatch_values (&vl);
77 values[0].gauge = (gauge_t) dir->files_size;
78 sstrncpy (vl.type, "bytes", sizeof (vl.type));
80 plugin_dispatch_values (&vl);
81 } /* void fc_submit_dir */
86 * <Directory /path/to/dir>
99 static int fc_config_set_instance (fc_directory_conf_t *dir, const char *str)
105 sstrncpy (buffer, str, sizeof (buffer));
106 for (ptr = buffer; *ptr != 0; ptr++)
110 for (ptr = buffer; *ptr == '_'; ptr++)
120 sfree (dir->instance);
121 dir->instance = copy;
124 } /* int fc_config_set_instance */
126 static int fc_config_add_dir_instance (fc_directory_conf_t *dir,
129 if ((ci->values_num != 1)
130 || (ci->values[0].type != OCONFIG_TYPE_STRING))
132 WARNING ("filecount plugin: The `Instance' config option needs exactly "
133 "one string argument.");
137 return (fc_config_set_instance (dir, ci->values[0].value.string));
138 } /* int fc_config_add_dir_instance */
140 static int fc_config_add_dir_name (fc_directory_conf_t *dir,
145 if ((ci->values_num != 1)
146 || (ci->values[0].type != OCONFIG_TYPE_STRING))
148 WARNING ("filecount plugin: The `Name' config option needs exactly one "
153 temp = strdup (ci->values[0].value.string);
156 ERROR ("filecount plugin: strdup failed.");
164 } /* int fc_config_add_dir_name */
166 static int fc_config_add_dir_mtime (fc_directory_conf_t *dir,
172 if ((ci->values_num != 1)
173 || ((ci->values[0].type != OCONFIG_TYPE_STRING)
174 && (ci->values[0].type != OCONFIG_TYPE_NUMBER)))
176 WARNING ("filecount plugin: The `MTime' config option needs exactly one "
177 "string or numeric argument.");
181 if (ci->values[0].type == OCONFIG_TYPE_NUMBER)
183 dir->mtime = (int64_t) ci->values[0].value.number;
189 temp = strtod (ci->values[0].value.string, &endptr);
190 if ((errno != 0) || (endptr == NULL)
191 || (endptr == ci->values[0].value.string))
193 WARNING ("filecount plugin: Converting `%s' to a number failed.",
194 ci->values[0].value.string);
227 temp *= 31557600; /* == 365.25 * 86400 */
231 WARNING ("filecount plugin: Invalid suffix for `MTime': `%c'", *endptr);
233 } /* switch (*endptr) */
235 dir->mtime = (int64_t) temp;
238 } /* int fc_config_add_dir_mtime */
240 static int fc_config_add_dir_size (fc_directory_conf_t *dir,
246 if ((ci->values_num != 1)
247 || ((ci->values[0].type != OCONFIG_TYPE_STRING)
248 && (ci->values[0].type != OCONFIG_TYPE_NUMBER)))
250 WARNING ("filecount plugin: The `Size' config option needs exactly one "
251 "string or numeric argument.");
255 if (ci->values[0].type == OCONFIG_TYPE_NUMBER)
257 dir->size = (int64_t) ci->values[0].value.number;
263 temp = strtod (ci->values[0].value.string, &endptr);
264 if ((errno != 0) || (endptr == NULL)
265 || (endptr == ci->values[0].value.string))
267 WARNING ("filecount plugin: Converting `%s' to a number failed.",
268 ci->values[0].value.string);
286 temp *= 1000.0 * 1000.0;
291 temp *= 1000.0 * 1000.0 * 1000.0;
296 temp *= 1000.0 * 1000.0 * 1000.0 * 1000.0;
301 temp *= 1000.0 * 1000.0 * 1000.0 * 1000.0 * 1000.0;
305 WARNING ("filecount plugin: Invalid suffix for `Size': `%c'", *endptr);
307 } /* switch (*endptr) */
309 dir->size = (int64_t) temp;
312 } /* int fc_config_add_dir_size */
314 static int fc_config_add_dir_option (fc_directory_conf_t *dir,
315 oconfig_item_t *ci, int bit)
317 if ((ci->values_num != 1)
318 || (ci->values[0].type != OCONFIG_TYPE_BOOLEAN))
320 WARNING ("filecount plugin: The `Recursive' config options needs exactly "
321 "one boolean argument.");
325 if (ci->values[0].value.boolean)
328 dir->options &= ~bit;
331 } /* int fc_config_add_dir_option */
333 static int fc_config_add_dir (oconfig_item_t *ci)
335 fc_directory_conf_t *dir;
339 if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_STRING))
341 WARNING ("filecount plugin: `Directory' needs exactly one string "
346 /* Initialize `dir' */
347 dir = (fc_directory_conf_t *) malloc (sizeof (*dir));
350 ERROR ("filecount plugin: malloc failed.");
353 memset (dir, 0, sizeof (*dir));
355 dir->path = strdup (ci->values[0].value.string);
356 if (dir->path == NULL)
358 ERROR ("filecount plugin: strdup failed.");
362 fc_config_set_instance (dir, dir->path);
364 dir->options = FC_RECURSIVE;
371 for (i = 0; i < ci->children_num; i++)
373 oconfig_item_t *option = ci->children + i;
375 if (strcasecmp ("Instance", option->key) == 0)
376 status = fc_config_add_dir_instance (dir, option);
377 else if (strcasecmp ("Name", option->key) == 0)
378 status = fc_config_add_dir_name (dir, option);
379 else if (strcasecmp ("MTime", option->key) == 0)
380 status = fc_config_add_dir_mtime (dir, option);
381 else if (strcasecmp ("Size", option->key) == 0)
382 status = fc_config_add_dir_size (dir, option);
383 else if (strcasecmp ("Recursive", option->key) == 0)
384 status = fc_config_add_dir_option (dir, option, FC_RECURSIVE);
385 else if (strcasecmp ("IncludeHidden", option->key) == 0)
386 status = fc_config_add_dir_option (dir, option, FC_HIDDEN);
389 WARNING ("filecount plugin: fc_config_add_dir: "
390 "Option `%s' not allowed here.", option->key);
396 } /* for (ci->children) */
400 fc_directory_conf_t **temp;
402 temp = (fc_directory_conf_t **) realloc (directories,
403 sizeof (*directories) * (directories_num + 1));
406 ERROR ("filecount plugin: realloc failed.");
412 directories[directories_num] = dir;
420 sfree (dir->instance);
427 } /* int fc_config_add_dir */
429 static int fc_config (oconfig_item_t *ci)
433 for (i = 0; i < ci->children_num; i++)
435 oconfig_item_t *child = ci->children + i;
436 if (strcasecmp ("Directory", child->key) == 0)
437 fc_config_add_dir (child);
440 WARNING ("filecount plugin: Ignoring unknown config option `%s'.",
443 } /* for (ci->children) */
446 } /* int fc_config */
448 static int fc_init (void)
450 if (directories_num < 1)
452 WARNING ("filecount plugin: No directories have been configured.");
459 static int fc_read_dir_callback (const char *dirname, const char *filename,
462 fc_directory_conf_t *dir = user_data;
463 char abs_path[PATH_MAX];
470 ssnprintf (abs_path, sizeof (abs_path), "%s/%s", dirname, filename);
472 status = lstat (abs_path, &statbuf);
475 ERROR ("filecount plugin: stat (%s) failed.", abs_path);
479 if (S_ISDIR (statbuf.st_mode) && (dir->options & FC_RECURSIVE))
481 status = walk_directory (abs_path, fc_read_dir_callback, dir,
482 /* include hidden = */ (dir->options & FC_HIDDEN) ? 1 : 0);
485 else if (!S_ISREG (statbuf.st_mode))
490 if (dir->name != NULL)
492 status = fnmatch (dir->name, filename, /* flags = */ 0);
499 time_t mtime = dir->now;
506 DEBUG ("filecount plugin: Only collecting files that were touched %s %u.",
507 (dir->mtime < 0) ? "after" : "before",
508 (unsigned int) mtime);
510 if (((dir->mtime < 0) && (statbuf.st_mtime < mtime))
511 || ((dir->mtime > 0) && (statbuf.st_mtime > mtime)))
520 size = (off_t) ((-1) * dir->size);
522 size = (off_t) dir->size;
524 if (((dir->size < 0) && (statbuf.st_size > size))
525 || ((dir->size > 0) && (statbuf.st_size < size)))
530 dir->files_size += (uint64_t) statbuf.st_size;
533 } /* int fc_read_dir_callback */
535 static int fc_read_dir (fc_directory_conf_t *dir)
543 dir->now = time (NULL);
545 status = walk_directory (dir->path, fc_read_dir_callback, dir,
546 /* include hidden */ (dir->options & FC_HIDDEN) ? 1 : 0);
549 WARNING ("filecount plugin: walk_directory (%s) failed.", dir->path);
556 } /* int fc_read_dir */
558 static int fc_read (void)
562 for (i = 0; i < directories_num; i++)
563 fc_read_dir (directories[i]);
568 void module_register (void)
570 plugin_register_complex_config ("filecount", fc_config);
571 plugin_register_init ("filecount", fc_init);
572 plugin_register_read ("filecount", fc_read);
573 } /* void module_register */
576 * vim: set sw=2 sts=2 et :