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>
29 #include <sys/types.h>
35 #define FC_RECURSIVE 1
38 struct fc_directory_conf_s
54 /* Helper for the recursive functions */
57 typedef struct fc_directory_conf_s fc_directory_conf_t;
59 static fc_directory_conf_t **directories = NULL;
60 static size_t directories_num = 0;
62 static void fc_submit_dir (const fc_directory_conf_t *dir)
65 value_list_t vl = VALUE_LIST_INIT;
67 values[0].gauge = (gauge_t) dir->files_num;
70 vl.values_len = STATIC_ARRAY_SIZE (values);
71 sstrncpy (vl.host, hostname_g, sizeof (vl.host));
72 sstrncpy (vl.plugin, "filecount", sizeof (vl.plugin));
73 sstrncpy (vl.plugin_instance, dir->instance, sizeof (vl.plugin_instance));
74 sstrncpy (vl.type, "files", sizeof (vl.type));
76 plugin_dispatch_values (&vl);
78 values[0].gauge = (gauge_t) dir->files_size;
79 sstrncpy (vl.type, "bytes", sizeof (vl.type));
81 plugin_dispatch_values (&vl);
82 } /* void fc_submit_dir */
87 * <Directory /path/to/dir>
100 static int fc_config_set_instance (fc_directory_conf_t *dir, const char *str)
106 sstrncpy (buffer, str, sizeof (buffer));
107 for (ptr = buffer; *ptr != 0; ptr++)
111 for (ptr = buffer; *ptr == '_'; ptr++)
121 sfree (dir->instance);
122 dir->instance = copy;
125 } /* int fc_config_set_instance */
127 static int fc_config_add_dir_instance (fc_directory_conf_t *dir,
130 if ((ci->values_num != 1)
131 || (ci->values[0].type != OCONFIG_TYPE_STRING))
133 WARNING ("filecount plugin: The `Instance' config option needs exactly "
134 "one string argument.");
138 return (fc_config_set_instance (dir, ci->values[0].value.string));
139 } /* int fc_config_add_dir_instance */
141 static int fc_config_add_dir_name (fc_directory_conf_t *dir,
146 if ((ci->values_num != 1)
147 || (ci->values[0].type != OCONFIG_TYPE_STRING))
149 WARNING ("filecount plugin: The `Name' config option needs exactly one "
154 temp = strdup (ci->values[0].value.string);
157 ERROR ("filecount plugin: strdup failed.");
165 } /* int fc_config_add_dir_name */
167 static int fc_config_add_dir_mtime (fc_directory_conf_t *dir,
173 if ((ci->values_num != 1)
174 || ((ci->values[0].type != OCONFIG_TYPE_STRING)
175 && (ci->values[0].type != OCONFIG_TYPE_NUMBER)))
177 WARNING ("filecount plugin: The `MTime' config option needs exactly one "
178 "string or numeric argument.");
182 if (ci->values[0].type == OCONFIG_TYPE_NUMBER)
184 dir->mtime = (int64_t) ci->values[0].value.number;
190 temp = strtod (ci->values[0].value.string, &endptr);
191 if ((errno != 0) || (endptr == NULL)
192 || (endptr == ci->values[0].value.string))
194 WARNING ("filecount plugin: Converting `%s' to a number failed.",
195 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,
247 if ((ci->values_num != 1)
248 || ((ci->values[0].type != OCONFIG_TYPE_STRING)
249 && (ci->values[0].type != OCONFIG_TYPE_NUMBER)))
251 WARNING ("filecount plugin: The `Size' config option needs exactly one "
252 "string or numeric argument.");
256 if (ci->values[0].type == OCONFIG_TYPE_NUMBER)
258 dir->size = (int64_t) ci->values[0].value.number;
264 temp = strtod (ci->values[0].value.string, &endptr);
265 if ((errno != 0) || (endptr == NULL)
266 || (endptr == ci->values[0].value.string))
268 WARNING ("filecount plugin: Converting `%s' to a number failed.",
269 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)
318 if ((ci->values_num != 1)
319 || (ci->values[0].type != OCONFIG_TYPE_BOOLEAN))
321 WARNING ("filecount plugin: The `Recursive' config options needs exactly "
322 "one boolean argument.");
326 if (ci->values[0].value.boolean)
329 dir->options &= ~bit;
332 } /* int fc_config_add_dir_option */
334 static int fc_config_add_dir (oconfig_item_t *ci)
336 fc_directory_conf_t *dir;
340 if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_STRING))
342 WARNING ("filecount plugin: `Directory' needs exactly one string "
347 /* Initialize `dir' */
348 dir = calloc (1, sizeof (*dir));
351 ERROR ("filecount plugin: calloc failed.");
355 dir->path = strdup (ci->values[0].value.string);
356 if (dir->path == NULL)
358 ERROR ("filecount plugin: strdup failed.");
363 fc_config_set_instance (dir, dir->path);
365 dir->options = FC_RECURSIVE;
372 for (i = 0; i < ci->children_num; i++)
374 oconfig_item_t *option = ci->children + i;
376 if (strcasecmp ("Instance", option->key) == 0)
377 status = fc_config_add_dir_instance (dir, option);
378 else if (strcasecmp ("Name", option->key) == 0)
379 status = fc_config_add_dir_name (dir, option);
380 else if (strcasecmp ("MTime", option->key) == 0)
381 status = fc_config_add_dir_mtime (dir, option);
382 else if (strcasecmp ("Size", option->key) == 0)
383 status = fc_config_add_dir_size (dir, option);
384 else if (strcasecmp ("Recursive", option->key) == 0)
385 status = fc_config_add_dir_option (dir, option, FC_RECURSIVE);
386 else if (strcasecmp ("IncludeHidden", option->key) == 0)
387 status = fc_config_add_dir_option (dir, option, FC_HIDDEN);
390 WARNING ("filecount plugin: fc_config_add_dir: "
391 "Option `%s' not allowed here.", option->key);
397 } /* for (ci->children) */
401 fc_directory_conf_t **temp;
403 temp = realloc (directories,
404 sizeof (*directories) * (directories_num + 1));
407 ERROR ("filecount plugin: realloc failed.");
413 directories[directories_num] = dir;
421 sfree (dir->instance);
428 } /* int fc_config_add_dir */
430 static int fc_config (oconfig_item_t *ci)
434 for (i = 0; i < ci->children_num; i++)
436 oconfig_item_t *child = ci->children + i;
437 if (strcasecmp ("Directory", child->key) == 0)
438 fc_config_add_dir (child);
441 WARNING ("filecount plugin: Ignoring unknown config option `%s'.",
444 } /* for (ci->children) */
447 } /* int fc_config */
449 static int fc_init (void)
451 if (directories_num < 1)
453 WARNING ("filecount plugin: No directories have been configured.");
460 static int fc_read_dir_callback (const char *dirname, const char *filename,
463 fc_directory_conf_t *dir = user_data;
464 char abs_path[PATH_MAX];
471 ssnprintf (abs_path, sizeof (abs_path), "%s/%s", dirname, filename);
473 status = lstat (abs_path, &statbuf);
476 ERROR ("filecount plugin: stat (%s) failed.", abs_path);
480 if (S_ISDIR (statbuf.st_mode) && (dir->options & FC_RECURSIVE))
482 status = walk_directory (abs_path, fc_read_dir_callback, dir,
483 /* include hidden = */ (dir->options & FC_HIDDEN) ? 1 : 0);
486 else if (!S_ISREG (statbuf.st_mode))
491 if (dir->name != NULL)
493 status = fnmatch (dir->name, filename, /* flags = */ 0);
500 time_t mtime = dir->now;
507 DEBUG ("filecount plugin: Only collecting files that were touched %s %u.",
508 (dir->mtime < 0) ? "after" : "before",
509 (unsigned int) mtime);
511 if (((dir->mtime < 0) && (statbuf.st_mtime < mtime))
512 || ((dir->mtime > 0) && (statbuf.st_mtime > mtime)))
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)
544 dir->now = time (NULL);
546 status = walk_directory (dir->path, fc_read_dir_callback, dir,
547 /* include hidden */ (dir->options & FC_HIDDEN) ? 1 : 0);
550 WARNING ("filecount plugin: walk_directory (%s) failed.", dir->path);
557 } /* int fc_read_dir */
559 static int fc_read (void)
563 for (i = 0; i < directories_num; i++)
564 fc_read_dir (directories[i]);
569 void module_register (void)
571 plugin_register_complex_config ("filecount", fc_config);
572 plugin_register_init ("filecount", fc_init);
573 plugin_register_read ("filecount", fc_read);
574 } /* void module_register */
577 * vim: set sw=2 sts=2 et :