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 struct fc_directory_conf_s
48 /* Helper for the recursive functions */
51 typedef struct fc_directory_conf_s fc_directory_conf_t;
53 static fc_directory_conf_t **directories = NULL;
54 static size_t directories_num = 0;
56 static void fc_submit_dir (const fc_directory_conf_t *dir)
59 value_list_t vl = VALUE_LIST_INIT;
61 values[0].gauge = (gauge_t) dir->files_num;
64 vl.values_len = STATIC_ARRAY_SIZE (values);
65 vl.time = time (NULL);
66 sstrncpy (vl.host, hostname_g, sizeof (vl.host));
67 sstrncpy (vl.plugin, "filecount", sizeof (vl.plugin));
68 sstrncpy (vl.plugin_instance, dir->instance, sizeof (vl.plugin_instance));
69 sstrncpy (vl.type, "files", sizeof (vl.type));
71 plugin_dispatch_values (&vl);
73 values[0].gauge = (gauge_t) dir->files_size;
74 sstrncpy (vl.type, "bytes", sizeof (vl.type));
76 plugin_dispatch_values (&vl);
77 } /* void fc_submit_dir */
82 * <Directory /path/to/dir>
95 static int fc_config_set_instance (fc_directory_conf_t *dir, const char *str)
101 sstrncpy (buffer, str, sizeof (buffer));
102 for (ptr = buffer; *ptr != 0; ptr++)
106 for (ptr = buffer; *ptr == '_'; ptr++)
116 sfree (dir->instance);
117 dir->instance = copy;
120 } /* int fc_config_set_instance */
122 static int fc_config_add_dir_instance (fc_directory_conf_t *dir,
125 if ((ci->values_num != 1)
126 || (ci->values[0].type != OCONFIG_TYPE_STRING))
128 WARNING ("filecount plugin: The `Instance' config option needs exactly "
129 "one string argument.");
133 return (fc_config_set_instance (dir, ci->values[0].value.string));
134 } /* int fc_config_add_dir_instance */
136 static int fc_config_add_dir_name (fc_directory_conf_t *dir,
141 if ((ci->values_num != 1)
142 || (ci->values[0].type != OCONFIG_TYPE_STRING))
144 WARNING ("filecount plugin: The `Name' config option needs exactly one "
149 temp = strdup (ci->values[0].value.string);
152 ERROR ("filecount plugin: strdup failed.");
160 } /* int fc_config_add_dir_name */
162 static int fc_config_add_dir_mtime (fc_directory_conf_t *dir,
168 if ((ci->values_num != 1)
169 || ((ci->values[0].type != OCONFIG_TYPE_STRING)
170 && (ci->values[0].type != OCONFIG_TYPE_NUMBER)))
172 WARNING ("filecount plugin: The `MTime' config option needs exactly one "
173 "string or numeric argument.");
177 if (ci->values[0].type == OCONFIG_TYPE_NUMBER)
179 dir->mtime = (int64_t) ci->values[0].value.number;
185 temp = strtod (ci->values[0].value.string, &endptr);
186 if ((errno != 0) || (endptr == NULL)
187 || (endptr == ci->values[0].value.string))
189 WARNING ("filecount plugin: Converting `%s' to a number failed.",
190 ci->values[0].value.string);
223 temp *= 31557600; /* == 365.25 * 86400 */
227 WARNING ("filecount plugin: Invalid suffix for `MTime': `%c'", *endptr);
229 } /* switch (*endptr) */
231 dir->mtime = (int64_t) temp;
234 } /* int fc_config_add_dir_mtime */
236 static int fc_config_add_dir_size (fc_directory_conf_t *dir,
242 if ((ci->values_num != 1)
243 || ((ci->values[0].type != OCONFIG_TYPE_STRING)
244 && (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)
253 dir->size = (int64_t) ci->values[0].value.number;
259 temp = strtod (ci->values[0].value.string, &endptr);
260 if ((errno != 0) || (endptr == NULL)
261 || (endptr == ci->values[0].value.string))
263 WARNING ("filecount plugin: Converting `%s' to a number failed.",
264 ci->values[0].value.string);
282 temp *= 1000.0 * 1000.0;
287 temp *= 1000.0 * 1000.0 * 1000.0;
292 temp *= 1000.0 * 1000.0 * 1000.0 * 1000.0;
297 temp *= 1000.0 * 1000.0 * 1000.0 * 1000.0 * 1000.0;
301 WARNING ("filecount plugin: Invalid suffix for `Size': `%c'", *endptr);
303 } /* switch (*endptr) */
305 dir->size = (int64_t) temp;
308 } /* int fc_config_add_dir_size */
310 static int fc_config_add_dir (oconfig_item_t *ci)
312 fc_directory_conf_t *dir;
316 if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_STRING))
318 WARNING ("filecount plugin: `Directory' needs exactly one string "
323 /* Initialize `dir' */
324 dir = (fc_directory_conf_t *) malloc (sizeof (*dir));
327 ERROR ("filecount plugin: malloc failed.");
330 memset (dir, 0, sizeof (*dir));
332 dir->path = strdup (ci->values[0].value.string);
333 if (dir->path == NULL)
335 ERROR ("filecount plugin: strdup failed.");
339 fc_config_set_instance (dir, dir->path);
346 for (i = 0; i < ci->children_num; i++)
348 oconfig_item_t *option = ci->children + i;
350 if (strcasecmp ("Instance", option->key) == 0)
351 status = fc_config_add_dir_instance (dir, option);
352 else if (strcasecmp ("Name", option->key) == 0)
353 status = fc_config_add_dir_name (dir, option);
354 else if (strcasecmp ("MTime", option->key) == 0)
355 status = fc_config_add_dir_mtime (dir, option);
356 else if (strcasecmp ("Size", option->key) == 0)
357 status = fc_config_add_dir_size (dir, option);
360 WARNING ("filecount plugin: fc_config_add_dir: "
361 "Option `%s' not allowed here.", option->key);
367 } /* for (ci->children) */
371 fc_directory_conf_t **temp;
373 temp = (fc_directory_conf_t **) realloc (directories,
374 sizeof (*directories) * directories_num);
377 ERROR ("filecount plugin: realloc failed.");
383 directories[directories_num] = dir;
391 sfree (dir->instance);
398 } /* int fc_config_add_dir */
400 static int fc_config (oconfig_item_t *ci)
404 for (i = 0; i < ci->children_num; i++)
406 oconfig_item_t *child = ci->children + i;
407 if (strcasecmp ("Directory", child->key) == 0)
408 fc_config_add_dir (child);
411 WARNING ("filecount plugin: Ignoring unknown config option `%s'.",
414 } /* for (ci->children) */
417 } /* int fc_config */
419 static int fc_init (void)
421 if (directories_num < 1)
423 WARNING ("filecount plugin: No directories have been configured.");
430 static int fc_read_dir_callback (const char *dirname, const char *filename,
433 fc_directory_conf_t *dir = user_data;
434 char abs_path[PATH_MAX];
441 ssnprintf (abs_path, sizeof (abs_path), "%s/%s", dirname, filename);
443 status = lstat (abs_path, &statbuf);
446 ERROR ("filecount plugin: stat (%s) failed.", abs_path);
450 if (S_ISDIR (statbuf.st_mode))
452 status = walk_directory (abs_path, fc_read_dir_callback, dir);
455 else if (!S_ISREG (statbuf.st_mode))
460 if (dir->name != NULL)
462 status = fnmatch (dir->name, filename, /* flags = */ 0);
469 time_t mtime = dir->now;
476 DEBUG ("filecount plugin: Only collecting files that were touched %s %u.",
477 (dir->mtime < 0) ? "after" : "before",
478 (unsigned int) mtime);
480 if (((dir->mtime < 0) && (statbuf.st_mtime < mtime))
481 || ((dir->mtime > 0) && (statbuf.st_mtime > mtime)))
490 size = (off_t) ((-1) * dir->size);
492 size = (off_t) dir->size;
494 if (((dir->size < 0) && (statbuf.st_size > size))
495 || ((dir->size > 0) && (statbuf.st_size < size)))
500 dir->files_size += (uint64_t) statbuf.st_size;
503 } /* int fc_read_dir_callback */
505 static int fc_read_dir (fc_directory_conf_t *dir)
513 dir->now = time (NULL);
515 status = walk_directory (dir->path, fc_read_dir_callback, dir);
518 WARNING ("filecount plugin: walk_directory (%s) failed.", dir->path);
525 } /* int fc_read_dir */
527 static int fc_read (void)
531 for (i = 0; i < directories_num; i++)
532 fc_read_dir (directories[i]);
537 void module_register (void)
539 plugin_register_complex_config ("filecount", fc_config);
540 plugin_register_init ("filecount", fc_init);
541 plugin_register_read ("filecount", fc_read);
542 } /* void module_register */
545 * vim: set sw=2 sts=2 et :