169e9d0e65cb5c95e6228d90a5ad41c84ddf7f82
1 /**
2 * collectd - src/filecount.c
3 * Copyright (C) 2008 Alessandro Iurlano
4 * Copyright (C) 2008 Florian octo Forster
5 *
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.
9 *
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.
14 *
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
18 *
19 * Authors:
20 * Alessandro Iurlano <alessandro.iurlano at gmail.com>
21 * Florian octo Forster <octo at collectd.org>
22 **/
24 #include "collectd.h"
26 #include "common.h"
27 #include "plugin.h"
29 #include <sys/types.h>
30 #include <sys/stat.h>
31 #include <fcntl.h>
32 #include <dirent.h>
33 #include <fnmatch.h>
35 #define FC_RECURSIVE 1
36 #define FC_HIDDEN 2
38 struct fc_directory_conf_s
39 {
40 char *path;
41 char *instance;
43 int options;
45 /* Data counters */
46 uint64_t files_num;
47 uint64_t files_size;
49 /* Selectors */
50 char *name;
51 int64_t mtime;
52 int64_t size;
54 /* Helper for the recursive functions */
55 time_t now;
56 };
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)
63 {
64 value_t values[1];
65 value_list_t vl = VALUE_LIST_INIT;
67 values[0].gauge = (gauge_t) dir->files_num;
69 vl.values = values;
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 */
84 /*
85 * Config:
86 * <Plugin filecount>
87 * <Directory /path/to/dir>
88 * Instance "foobar"
89 * Name "*.conf"
90 * MTime -3600
91 * Size "+10M"
92 * </Directory>
93 * </Plugin>
94 *
95 * Collect:
96 * - Number of files
97 * - Total size
98 */
100 static int fc_config_set_instance (fc_directory_conf_t *dir, const char *str)
101 {
102 char buffer[1024];
103 char *ptr;
104 char *copy;
106 sstrncpy (buffer, str, sizeof (buffer));
107 for (ptr = buffer; *ptr != 0; ptr++)
108 if (*ptr == '/')
109 *ptr = '_';
111 for (ptr = buffer; *ptr == '_'; ptr++)
112 /* do nothing */;
114 if (*ptr == 0)
115 return (-1);
117 copy = strdup (ptr);
118 if (copy == NULL)
119 return (-1);
121 sfree (dir->instance);
122 dir->instance = copy;
124 return (0);
125 } /* int fc_config_set_instance */
127 static int fc_config_add_dir_instance (fc_directory_conf_t *dir,
128 oconfig_item_t *ci)
129 {
130 if ((ci->values_num != 1)
131 || (ci->values[0].type != OCONFIG_TYPE_STRING))
132 {
133 WARNING ("filecount plugin: The `Instance' config option needs exactly "
134 "one string argument.");
135 return (-1);
136 }
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,
142 oconfig_item_t *ci)
143 {
144 char *temp;
146 if ((ci->values_num != 1)
147 || (ci->values[0].type != OCONFIG_TYPE_STRING))
148 {
149 WARNING ("filecount plugin: The `Name' config option needs exactly one "
150 "string argument.");
151 return (-1);
152 }
154 temp = strdup (ci->values[0].value.string);
155 if (temp == NULL)
156 {
157 ERROR ("filecount plugin: strdup failed.");
158 return (-1);
159 }
161 sfree (dir->name);
162 dir->name = temp;
164 return (0);
165 } /* int fc_config_add_dir_name */
167 static int fc_config_add_dir_mtime (fc_directory_conf_t *dir,
168 oconfig_item_t *ci)
169 {
170 char *endptr;
171 double temp;
173 if ((ci->values_num != 1)
174 || ((ci->values[0].type != OCONFIG_TYPE_STRING)
175 && (ci->values[0].type != OCONFIG_TYPE_NUMBER)))
176 {
177 WARNING ("filecount plugin: The `MTime' config option needs exactly one "
178 "string or numeric argument.");
179 return (-1);
180 }
182 if (ci->values[0].type == OCONFIG_TYPE_NUMBER)
183 {
184 dir->mtime = (int64_t) ci->values[0].value.number;
185 return (0);
186 }
188 errno = 0;
189 endptr = NULL;
190 temp = strtod (ci->values[0].value.string, &endptr);
191 if ((errno != 0) || (endptr == NULL)
192 || (endptr == ci->values[0].value.string))
193 {
194 WARNING ("filecount plugin: Converting `%s' to a number failed.",
195 ci->values[0].value.string);
196 return (-1);
197 }
199 switch (*endptr)
200 {
201 case 0:
202 case 's':
203 case 'S':
204 break;
206 case 'm':
207 case 'M':
208 temp *= 60;
209 break;
211 case 'h':
212 case 'H':
213 temp *= 3600;
214 break;
216 case 'd':
217 case 'D':
218 temp *= 86400;
219 break;
221 case 'w':
222 case 'W':
223 temp *= 7 * 86400;
224 break;
226 case 'y':
227 case 'Y':
228 temp *= 31557600; /* == 365.25 * 86400 */
229 break;
231 default:
232 WARNING ("filecount plugin: Invalid suffix for `MTime': `%c'", *endptr);
233 return (-1);
234 } /* switch (*endptr) */
236 dir->mtime = (int64_t) temp;
238 return (0);
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 {
244 char *endptr;
245 double temp;
247 if ((ci->values_num != 1)
248 || ((ci->values[0].type != OCONFIG_TYPE_STRING)
249 && (ci->values[0].type != OCONFIG_TYPE_NUMBER)))
250 {
251 WARNING ("filecount plugin: The `Size' config option needs exactly one "
252 "string or numeric argument.");
253 return (-1);
254 }
256 if (ci->values[0].type == OCONFIG_TYPE_NUMBER)
257 {
258 dir->size = (int64_t) ci->values[0].value.number;
259 return (0);
260 }
262 errno = 0;
263 endptr = NULL;
264 temp = strtod (ci->values[0].value.string, &endptr);
265 if ((errno != 0) || (endptr == NULL)
266 || (endptr == ci->values[0].value.string))
267 {
268 WARNING ("filecount plugin: Converting `%s' to a number failed.",
269 ci->values[0].value.string);
270 return (-1);
271 }
273 switch (*endptr)
274 {
275 case 0:
276 case 'b':
277 case 'B':
278 break;
280 case 'k':
281 case 'K':
282 temp *= 1000.0;
283 break;
285 case 'm':
286 case 'M':
287 temp *= 1000.0 * 1000.0;
288 break;
290 case 'g':
291 case 'G':
292 temp *= 1000.0 * 1000.0 * 1000.0;
293 break;
295 case 't':
296 case 'T':
297 temp *= 1000.0 * 1000.0 * 1000.0 * 1000.0;
298 break;
300 case 'p':
301 case 'P':
302 temp *= 1000.0 * 1000.0 * 1000.0 * 1000.0 * 1000.0;
303 break;
305 default:
306 WARNING ("filecount plugin: Invalid suffix for `Size': `%c'", *endptr);
307 return (-1);
308 } /* switch (*endptr) */
310 dir->size = (int64_t) temp;
312 return (0);
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)
317 {
318 if ((ci->values_num != 1)
319 || (ci->values[0].type != OCONFIG_TYPE_BOOLEAN))
320 {
321 WARNING ("filecount plugin: The `Recursive' config options needs exactly "
322 "one boolean argument.");
323 return (-1);
324 }
326 if (ci->values[0].value.boolean)
327 dir->options |= bit;
328 else
329 dir->options &= ~bit;
331 return (0);
332 } /* int fc_config_add_dir_option */
334 static int fc_config_add_dir (oconfig_item_t *ci)
335 {
336 fc_directory_conf_t *dir;
337 int status;
338 int i;
340 if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_STRING))
341 {
342 WARNING ("filecount plugin: `Directory' needs exactly one string "
343 "argument.");
344 return (-1);
345 }
347 /* Initialize `dir' */
348 dir = calloc (1, sizeof (*dir));
349 if (dir == NULL)
350 {
351 ERROR ("filecount plugin: calloc failed.");
352 return (-1);
353 }
355 dir->path = strdup (ci->values[0].value.string);
356 if (dir->path == NULL)
357 {
358 ERROR ("filecount plugin: strdup failed.");
359 sfree (dir);
360 return (-1);
361 }
363 fc_config_set_instance (dir, dir->path);
365 dir->options = FC_RECURSIVE;
367 dir->name = NULL;
368 dir->mtime = 0;
369 dir->size = 0;
371 status = 0;
372 for (i = 0; i < ci->children_num; i++)
373 {
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);
388 else
389 {
390 WARNING ("filecount plugin: fc_config_add_dir: "
391 "Option `%s' not allowed here.", option->key);
392 status = -1;
393 }
395 if (status != 0)
396 break;
397 } /* for (ci->children) */
399 if (status == 0)
400 {
401 fc_directory_conf_t **temp;
403 temp = realloc (directories,
404 sizeof (*directories) * (directories_num + 1));
405 if (temp == NULL)
406 {
407 ERROR ("filecount plugin: realloc failed.");
408 status = -1;
409 }
410 else
411 {
412 directories = temp;
413 directories[directories_num] = dir;
414 directories_num++;
415 }
416 }
418 if (status != 0)
419 {
420 sfree (dir->name);
421 sfree (dir->instance);
422 sfree (dir->path);
423 sfree (dir);
424 return (-1);
425 }
427 return (0);
428 } /* int fc_config_add_dir */
430 static int fc_config (oconfig_item_t *ci)
431 {
432 int i;
434 for (i = 0; i < ci->children_num; i++)
435 {
436 oconfig_item_t *child = ci->children + i;
437 if (strcasecmp ("Directory", child->key) == 0)
438 fc_config_add_dir (child);
439 else
440 {
441 WARNING ("filecount plugin: Ignoring unknown config option `%s'.",
442 child->key);
443 }
444 } /* for (ci->children) */
446 return (0);
447 } /* int fc_config */
449 static int fc_init (void)
450 {
451 if (directories_num < 1)
452 {
453 WARNING ("filecount plugin: No directories have been configured.");
454 return (-1);
455 }
457 return (0);
458 } /* int fc_init */
460 static int fc_read_dir_callback (const char *dirname, const char *filename,
461 void *user_data)
462 {
463 fc_directory_conf_t *dir = user_data;
464 char abs_path[PATH_MAX];
465 struct stat statbuf;
466 int status;
468 if (dir == NULL)
469 return (-1);
471 ssnprintf (abs_path, sizeof (abs_path), "%s/%s", dirname, filename);
473 status = lstat (abs_path, &statbuf);
474 if (status != 0)
475 {
476 ERROR ("filecount plugin: stat (%s) failed.", abs_path);
477 return (-1);
478 }
480 if (S_ISDIR (statbuf.st_mode) && (dir->options & FC_RECURSIVE))
481 {
482 status = walk_directory (abs_path, fc_read_dir_callback, dir,
483 /* include hidden = */ (dir->options & FC_HIDDEN) ? 1 : 0);
484 return (status);
485 }
486 else if (!S_ISREG (statbuf.st_mode))
487 {
488 return (0);
489 }
491 if (dir->name != NULL)
492 {
493 status = fnmatch (dir->name, filename, /* flags = */ 0);
494 if (status != 0)
495 return (0);
496 }
498 if (dir->mtime != 0)
499 {
500 time_t mtime = dir->now;
502 if (dir->mtime < 0)
503 mtime += dir->mtime;
504 else
505 mtime -= dir->mtime;
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)))
513 return (0);
514 }
516 if (dir->size != 0)
517 {
518 off_t size;
520 if (dir->size < 0)
521 size = (off_t) ((-1) * dir->size);
522 else
523 size = (off_t) dir->size;
525 if (((dir->size < 0) && (statbuf.st_size > size))
526 || ((dir->size > 0) && (statbuf.st_size < size)))
527 return (0);
528 }
530 dir->files_num++;
531 dir->files_size += (uint64_t) statbuf.st_size;
533 return (0);
534 } /* int fc_read_dir_callback */
536 static int fc_read_dir (fc_directory_conf_t *dir)
537 {
538 int status;
540 dir->files_num = 0;
541 dir->files_size = 0;
543 if (dir->mtime != 0)
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);
548 if (status != 0)
549 {
550 WARNING ("filecount plugin: walk_directory (%s) failed.", dir->path);
551 return (-1);
552 }
554 fc_submit_dir (dir);
556 return (0);
557 } /* int fc_read_dir */
559 static int fc_read (void)
560 {
561 size_t i;
563 for (i = 0; i < directories_num; i++)
564 fc_read_dir (directories[i]);
566 return (0);
567 } /* int fc_read */
569 void module_register (void)
570 {
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 */
576 /*
577 * vim: set sw=2 sts=2 et :
578 */