1 /**
2 * collectd - src/configfile.c
3 * Copyright (C) 2005-2011 Florian octo Forster
4 *
5 * Permission is hereby granted, free of charge, to any person obtaining a
6 * copy of this software and associated documentation files (the "Software"),
7 * to deal in the Software without restriction, including without limitation
8 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9 * and/or sell copies of the Software, and to permit persons to whom the
10 * Software is furnished to do so, subject to the following conditions:
11 *
12 * The above copyright notice and this permission notice shall be included in
13 * all copies or substantial portions of the Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21 * DEALINGS IN THE SOFTWARE.
22 *
23 * Authors:
24 * Florian octo Forster <octo at collectd.org>
25 * Sebastian tokkee Harl <sh at tokkee.org>
26 **/
28 #include "collectd.h"
30 #include "liboconfig/oconfig.h"
32 #include "common.h"
33 #include "plugin.h"
34 #include "configfile.h"
35 #include "types_list.h"
36 #include "filter_chain.h"
38 #if HAVE_WORDEXP_H
39 # include <wordexp.h>
40 #endif /* HAVE_WORDEXP_H */
42 #if HAVE_FNMATCH_H
43 # include <fnmatch.h>
44 #endif /* HAVE_FNMATCH_H */
46 #if HAVE_LIBGEN_H
47 # include <libgen.h>
48 #endif /* HAVE_LIBGEN_H */
50 #define ESCAPE_NULL(str) ((str) == NULL ? "(null)" : (str))
52 /*
53 * Private types
54 */
55 typedef struct cf_callback
56 {
57 const char *type;
58 int (*callback) (const char *, const char *);
59 const char **keys;
60 int keys_num;
61 plugin_ctx_t ctx;
62 struct cf_callback *next;
63 } cf_callback_t;
65 typedef struct cf_complex_callback_s
66 {
67 char *type;
68 int (*callback) (oconfig_item_t *);
69 plugin_ctx_t ctx;
70 struct cf_complex_callback_s *next;
71 } cf_complex_callback_t;
73 typedef struct cf_value_map_s
74 {
75 const char *key;
76 int (*func) (oconfig_item_t *);
77 } cf_value_map_t;
79 typedef struct cf_global_option_s
80 {
81 const char *key;
82 char *value;
83 _Bool from_cli; /* value set from CLI */
84 const char *def;
85 } cf_global_option_t;
87 /*
88 * Prototypes of callback functions
89 */
90 static int dispatch_value_typesdb (oconfig_item_t *ci);
91 static int dispatch_value_plugindir (oconfig_item_t *ci);
92 static int dispatch_loadplugin (oconfig_item_t *ci);
93 static int dispatch_block_plugin (oconfig_item_t *ci);
95 /*
96 * Private variables
97 */
98 static cf_callback_t *first_callback = NULL;
99 static cf_complex_callback_t *complex_callback_head = NULL;
101 static cf_value_map_t cf_value_map[] =
102 {
103 {"TypesDB", dispatch_value_typesdb},
104 {"PluginDir", dispatch_value_plugindir},
105 {"LoadPlugin", dispatch_loadplugin},
106 {"Plugin", dispatch_block_plugin}
107 };
108 static int cf_value_map_num = STATIC_ARRAY_SIZE (cf_value_map);
110 static cf_global_option_t cf_global_options[] =
111 {
112 {"BaseDir", NULL, 0, PKGLOCALSTATEDIR},
113 {"PIDFile", NULL, 0, PIDFILE},
114 {"Hostname", NULL, 0, NULL},
115 {"FQDNLookup", NULL, 0, "true"},
116 {"Interval", NULL, 0, NULL},
117 {"ReadThreads", NULL, 0, "5"},
118 {"WriteThreads", NULL, 0, "5"},
119 {"WriteQueueLimitHigh", NULL, 0, NULL},
120 {"WriteQueueLimitLow", NULL, 0, NULL},
121 {"Timeout", NULL, 0, "2"},
122 {"AutoLoadPlugin", NULL, 0, "false"},
123 {"CollectInternalStats", NULL, 0, "false"},
124 {"PreCacheChain", NULL, 0, "PreCache"},
125 {"PostCacheChain", NULL, 0, "PostCache"},
126 {"MaxReadInterval", NULL, 0, "86400"}
127 };
128 static int cf_global_options_num = STATIC_ARRAY_SIZE (cf_global_options);
130 static int cf_default_typesdb = 1;
132 /*
133 * Functions to handle register/unregister, search, and other plugin related
134 * stuff
135 */
136 static cf_callback_t *cf_search (const char *type)
137 {
138 cf_callback_t *cf_cb;
140 if (type == NULL)
141 return (NULL);
143 for (cf_cb = first_callback; cf_cb != NULL; cf_cb = cf_cb->next)
144 if (strcasecmp (cf_cb->type, type) == 0)
145 break;
147 return (cf_cb);
148 }
150 static int cf_dispatch (const char *type, const char *orig_key,
151 const char *orig_value)
152 {
153 cf_callback_t *cf_cb;
154 plugin_ctx_t old_ctx;
155 char *key;
156 char *value;
157 int ret;
158 int i;
160 if (orig_key == NULL)
161 return (EINVAL);
163 DEBUG ("type = %s, key = %s, value = %s",
164 ESCAPE_NULL(type),
165 orig_key,
166 ESCAPE_NULL(orig_value));
168 if ((cf_cb = cf_search (type)) == NULL)
169 {
170 WARNING ("Found a configuration for the `%s' plugin, but "
171 "the plugin isn't loaded or didn't register "
172 "a configuration callback.", type);
173 return (-1);
174 }
176 if ((key = strdup (orig_key)) == NULL)
177 return (1);
178 if ((value = strdup (orig_value)) == NULL)
179 {
180 free (key);
181 return (2);
182 }
184 ret = -1;
186 old_ctx = plugin_set_ctx (cf_cb->ctx);
188 for (i = 0; i < cf_cb->keys_num; i++)
189 {
190 if ((cf_cb->keys[i] != NULL)
191 && (strcasecmp (cf_cb->keys[i], key) == 0))
192 {
193 ret = (*cf_cb->callback) (key, value);
194 break;
195 }
196 }
198 plugin_set_ctx (old_ctx);
200 if (i >= cf_cb->keys_num)
201 WARNING ("Plugin `%s' did not register for value `%s'.", type, key);
203 free (key);
204 free (value);
206 return (ret);
207 } /* int cf_dispatch */
209 static int dispatch_global_option (const oconfig_item_t *ci)
210 {
211 if (ci->values_num != 1)
212 return (-1);
213 if (ci->values[0].type == OCONFIG_TYPE_STRING)
214 return (global_option_set (ci->key, ci->values[0].value.string, 0));
215 else if (ci->values[0].type == OCONFIG_TYPE_NUMBER)
216 {
217 char tmp[128];
218 ssnprintf (tmp, sizeof (tmp), "%lf", ci->values[0].value.number);
219 return (global_option_set (ci->key, tmp, 0));
220 }
221 else if (ci->values[0].type == OCONFIG_TYPE_BOOLEAN)
222 {
223 if (ci->values[0].value.boolean)
224 return (global_option_set (ci->key, "true", 0));
225 else
226 return (global_option_set (ci->key, "false", 0));
227 }
229 return (-1);
230 } /* int dispatch_global_option */
232 static int dispatch_value_typesdb (oconfig_item_t *ci)
233 {
234 int i = 0;
236 assert (strcasecmp (ci->key, "TypesDB") == 0);
238 cf_default_typesdb = 0;
240 if (ci->values_num < 1) {
241 ERROR ("configfile: `TypesDB' needs at least one argument.");
242 return (-1);
243 }
245 for (i = 0; i < ci->values_num; ++i)
246 {
247 if (OCONFIG_TYPE_STRING != ci->values[i].type) {
248 WARNING ("configfile: TypesDB: Skipping %i. argument which "
249 "is not a string.", i + 1);
250 continue;
251 }
253 read_types_list (ci->values[i].value.string);
254 }
255 return (0);
256 } /* int dispatch_value_typesdb */
258 static int dispatch_value_plugindir (oconfig_item_t *ci)
259 {
260 assert (strcasecmp (ci->key, "PluginDir") == 0);
262 if (ci->values_num != 1)
263 return (-1);
264 if (ci->values[0].type != OCONFIG_TYPE_STRING)
265 return (-1);
267 plugin_set_dir (ci->values[0].value.string);
268 return (0);
269 }
271 static int dispatch_loadplugin (oconfig_item_t *ci)
272 {
273 int i;
274 const char *name;
275 unsigned int flags = 0;
276 plugin_ctx_t ctx;
277 plugin_ctx_t old_ctx;
278 int ret_val;
280 assert (strcasecmp (ci->key, "LoadPlugin") == 0);
282 if (ci->values_num != 1)
283 return (-1);
284 if (ci->values[0].type != OCONFIG_TYPE_STRING)
285 return (-1);
287 name = ci->values[0].value.string;
288 if (strcmp ("libvirt", name) == 0)
289 name = "virt";
291 /* default to the global interval set before loading this plugin */
292 memset (&ctx, 0, sizeof (ctx));
293 ctx.interval = cf_get_default_interval ();
294 ctx.flush_interval = 0;
295 ctx.flush_timeout = 0;
297 for (i = 0; i < ci->children_num; ++i)
298 {
299 oconfig_item_t *child = ci->children + i;
301 if (strcasecmp("Globals", child->key) == 0)
302 cf_util_get_flag (child, &flags, PLUGIN_FLAGS_GLOBAL);
303 else if (strcasecmp ("Interval", child->key) == 0)
304 cf_util_get_cdtime (child, &ctx.interval);
305 else if (strcasecmp ("FlushInterval", child->key) == 0)
306 cf_util_get_cdtime (child, &ctx.flush_interval);
307 else if (strcasecmp ("FlushTimeout", child->key) == 0)
308 cf_util_get_cdtime (child, &ctx.flush_timeout);
309 else {
310 WARNING("Ignoring unknown LoadPlugin option \"%s\" "
311 "for plugin \"%s\"",
312 child->key, ci->values[0].value.string);
313 }
314 }
316 old_ctx = plugin_set_ctx (ctx);
317 ret_val = plugin_load (name, (uint32_t) flags);
318 /* reset to the "global" context */
319 plugin_set_ctx (old_ctx);
321 return (ret_val);
322 } /* int dispatch_value_loadplugin */
324 static int dispatch_value_plugin (const char *plugin, oconfig_item_t *ci)
325 {
326 char buffer[4096];
327 char *buffer_ptr;
328 int buffer_free;
329 int i;
331 buffer_ptr = buffer;
332 buffer_free = sizeof (buffer);
334 for (i = 0; i < ci->values_num; i++)
335 {
336 int status = -1;
338 if (ci->values[i].type == OCONFIG_TYPE_STRING)
339 status = ssnprintf (buffer_ptr, buffer_free, " %s",
340 ci->values[i].value.string);
341 else if (ci->values[i].type == OCONFIG_TYPE_NUMBER)
342 status = ssnprintf (buffer_ptr, buffer_free, " %lf",
343 ci->values[i].value.number);
344 else if (ci->values[i].type == OCONFIG_TYPE_BOOLEAN)
345 status = ssnprintf (buffer_ptr, buffer_free, " %s",
346 ci->values[i].value.boolean
347 ? "true" : "false");
349 if ((status < 0) || (status >= buffer_free))
350 return (-1);
351 buffer_free -= status;
352 buffer_ptr += status;
353 }
354 /* skip the initial space */
355 buffer_ptr = buffer + 1;
357 return (cf_dispatch (plugin, ci->key, buffer_ptr));
358 } /* int dispatch_value_plugin */
360 static int dispatch_value (oconfig_item_t *ci)
361 {
362 int ret = 0;
363 int i;
365 for (i = 0; i < cf_value_map_num; i++)
366 if (strcasecmp (cf_value_map[i].key, ci->key) == 0)
367 {
368 ret = cf_value_map[i].func (ci);
369 break;
370 }
372 for (i = 0; i < cf_global_options_num; i++)
373 if (strcasecmp (cf_global_options[i].key, ci->key) == 0)
374 {
375 ret = dispatch_global_option (ci);
376 break;
377 }
379 return (ret);
380 } /* int dispatch_value */
382 static int dispatch_block_plugin (oconfig_item_t *ci)
383 {
384 int i;
385 const char *name;
387 cf_complex_callback_t *cb;
389 if (strcasecmp (ci->key, "Plugin") != 0)
390 return (-1);
391 if (ci->values_num < 1)
392 return (-1);
393 if (ci->values[0].type != OCONFIG_TYPE_STRING)
394 return (-1);
396 name = ci->values[0].value.string;
397 if (strcmp ("libvirt", name) == 0)
398 {
399 /* TODO(octo): Remove this legacy. */
400 WARNING ("The \"libvirt\" plugin has been renamed to \"virt\" to avoid problems with the build system. "
401 "Your configuration is still using the old name. "
402 "Please change it to use \"virt\" as soon as possible. "
403 "This compatibility code will go away eventually.");
404 name = "virt";
405 }
407 if (IS_TRUE (global_option_get ("AutoLoadPlugin")))
408 {
409 plugin_ctx_t ctx;
410 plugin_ctx_t old_ctx;
411 int status;
413 /* default to the global interval set before loading this plugin */
414 memset (&ctx, 0, sizeof (ctx));
415 ctx.interval = cf_get_default_interval ();
417 old_ctx = plugin_set_ctx (ctx);
418 status = plugin_load (name, /* flags = */ 0);
419 /* reset to the "global" context */
420 plugin_set_ctx (old_ctx);
422 if (status != 0)
423 {
424 ERROR ("Automatically loading plugin \"%s\" failed "
425 "with status %i.", name, status);
426 return (status);
427 }
428 }
430 /* Check for a complex callback first */
431 for (cb = complex_callback_head; cb != NULL; cb = cb->next)
432 {
433 if (strcasecmp (name, cb->type) == 0)
434 {
435 plugin_ctx_t old_ctx;
436 int ret_val;
438 old_ctx = plugin_set_ctx (cb->ctx);
439 ret_val = (cb->callback (ci));
440 plugin_set_ctx (old_ctx);
441 return (ret_val);
442 }
443 }
445 /* Hm, no complex plugin found. Dispatch the values one by one */
446 for (i = 0; i < ci->children_num; i++)
447 {
448 if (ci->children[i].children == NULL)
449 dispatch_value_plugin (name, ci->children + i);
450 else
451 {
452 WARNING ("There is a `%s' block within the "
453 "configuration for the %s plugin. "
454 "The plugin either only expects "
455 "\"simple\" configuration statements "
456 "or wasn't loaded using `LoadPlugin'."
457 " Please check your configuration.",
458 ci->children[i].key, name);
459 }
460 }
462 return (0);
463 }
466 static int dispatch_block (oconfig_item_t *ci)
467 {
468 if (strcasecmp (ci->key, "LoadPlugin") == 0)
469 return (dispatch_loadplugin (ci));
470 else if (strcasecmp (ci->key, "Plugin") == 0)
471 return (dispatch_block_plugin (ci));
472 else if (strcasecmp (ci->key, "Chain") == 0)
473 return (fc_configure (ci));
475 return (0);
476 }
478 static int cf_ci_replace_child (oconfig_item_t *dst, oconfig_item_t *src,
479 int offset)
480 {
481 oconfig_item_t *temp;
482 int i;
484 assert (offset >= 0);
485 assert (dst->children_num > offset);
487 /* Free the memory used by the replaced child. Usually that's the
488 * `Include "blah"' statement. */
489 temp = dst->children + offset;
490 for (i = 0; i < temp->values_num; i++)
491 {
492 if (temp->values[i].type == OCONFIG_TYPE_STRING)
493 {
494 sfree (temp->values[i].value.string);
495 }
496 }
497 sfree (temp->values);
498 temp = NULL;
500 /* If (src->children_num == 0) the array size is decreased. If offset
501 * is _not_ the last element, (offset < (dst->children_num - 1)), then
502 * we need to move the trailing elements before resizing the array. */
503 if ((src->children_num == 0) && (offset < (dst->children_num - 1)))
504 {
505 int nmemb = dst->children_num - (offset + 1);
506 memmove (dst->children + offset, dst->children + offset + 1,
507 sizeof (oconfig_item_t) * nmemb);
508 }
510 /* Resize the memory containing the children to be big enough to hold
511 * all children. */
512 if (dst->children_num + src->children_num - 1 == 0)
513 {
514 dst->children_num = 0;
515 return (0);
516 }
518 temp = realloc (dst->children,
519 sizeof (oconfig_item_t)
520 * (dst->children_num + src->children_num - 1));
521 if (temp == NULL)
522 {
523 ERROR ("configfile: realloc failed.");
524 return (-1);
525 }
526 dst->children = temp;
528 /* If there are children behind the include statement, and they have
529 * not yet been moved because (src->children_num == 0), then move them
530 * to the end of the list, so that the new children have room before
531 * them. */
532 if ((src->children_num > 0)
533 && ((dst->children_num - (offset + 1)) > 0))
534 {
535 int nmemb = dst->children_num - (offset + 1);
536 int old_offset = offset + 1;
537 int new_offset = offset + src->children_num;
539 memmove (dst->children + new_offset,
540 dst->children + old_offset,
541 sizeof (oconfig_item_t) * nmemb);
542 }
544 /* Last but not least: If there are new children, copy them to the
545 * memory reserved for them. */
546 if (src->children_num > 0)
547 {
548 memcpy (dst->children + offset,
549 src->children,
550 sizeof (oconfig_item_t) * src->children_num);
551 }
553 /* Update the number of children. */
554 dst->children_num += (src->children_num - 1);
556 return (0);
557 } /* int cf_ci_replace_child */
559 static int cf_ci_append_children (oconfig_item_t *dst, oconfig_item_t *src)
560 {
561 oconfig_item_t *temp;
563 if ((src == NULL) || (src->children_num == 0))
564 return (0);
566 temp = realloc (dst->children,
567 sizeof (oconfig_item_t)
568 * (dst->children_num + src->children_num));
569 if (temp == NULL)
570 {
571 ERROR ("configfile: realloc failed.");
572 return (-1);
573 }
574 dst->children = temp;
576 memcpy (dst->children + dst->children_num,
577 src->children,
578 sizeof (oconfig_item_t)
579 * src->children_num);
580 dst->children_num += src->children_num;
582 return (0);
583 } /* int cf_ci_append_children */
585 #define CF_MAX_DEPTH 8
586 static oconfig_item_t *cf_read_generic (const char *path,
587 const char *pattern, int depth);
589 static int cf_include_all (oconfig_item_t *root, int depth)
590 {
591 int i;
593 for (i = 0; i < root->children_num; i++)
594 {
595 oconfig_item_t *new;
596 oconfig_item_t *old;
598 char *pattern = NULL;
600 int j;
602 if (strcasecmp (root->children[i].key, "Include") != 0)
603 continue;
605 old = root->children + i;
607 if ((old->values_num != 1)
608 || (old->values[0].type != OCONFIG_TYPE_STRING))
609 {
610 ERROR ("configfile: `Include' needs exactly one string argument.");
611 continue;
612 }
614 for (j = 0; j < old->children_num; ++j)
615 {
616 oconfig_item_t *child = old->children + j;
618 if (strcasecmp (child->key, "Filter") == 0)
619 cf_util_get_string (child, &pattern);
620 else
621 ERROR ("configfile: Option `%s' not allowed in <Include> block.",
622 child->key);
623 }
625 new = cf_read_generic (old->values[0].value.string, pattern, depth + 1);
626 sfree (pattern);
628 if (new == NULL)
629 return (-1);
631 /* Now replace the i'th child in `root' with `new'. */
632 if (cf_ci_replace_child (root, new, i) < 0) {
633 sfree (new->values);
634 sfree (new);
635 return (-1);
636 }
638 /* ... and go back to the new i'th child. */
639 --i;
641 sfree (new->values);
642 sfree (new);
643 } /* for (i = 0; i < root->children_num; i++) */
645 return (0);
646 } /* int cf_include_all */
648 static oconfig_item_t *cf_read_file (const char *file,
649 const char *pattern, int depth)
650 {
651 oconfig_item_t *root;
652 int status;
654 assert (depth < CF_MAX_DEPTH);
656 if (pattern != NULL) {
657 #if HAVE_FNMATCH_H && HAVE_LIBGEN_H
658 char *tmp = sstrdup (file);
659 char *filename = basename (tmp);
661 if ((filename != NULL) && (fnmatch (pattern, filename, 0) != 0)) {
662 DEBUG ("configfile: Not including `%s' because it "
663 "does not match pattern `%s'.",
664 filename, pattern);
665 free (tmp);
666 return (NULL);
667 }
669 free (tmp);
670 #else
671 ERROR ("configfile: Cannot apply pattern filter '%s' "
672 "to file '%s': functions basename() and / or "
673 "fnmatch() not available.", pattern, file);
674 #endif /* HAVE_FNMATCH_H && HAVE_LIBGEN_H */
675 }
677 root = oconfig_parse_file (file);
678 if (root == NULL)
679 {
680 ERROR ("configfile: Cannot read file `%s'.", file);
681 return (NULL);
682 }
684 status = cf_include_all (root, depth);
685 if (status != 0)
686 {
687 oconfig_free (root);
688 return (NULL);
689 }
691 return (root);
692 } /* oconfig_item_t *cf_read_file */
694 static int cf_compare_string (const void *p1, const void *p2)
695 {
696 return strcmp (*(const char **) p1, *(const char **) p2);
697 }
699 static oconfig_item_t *cf_read_dir (const char *dir,
700 const char *pattern, int depth)
701 {
702 oconfig_item_t *root = NULL;
703 DIR *dh;
704 struct dirent *de;
705 char **filenames = NULL;
706 int filenames_num = 0;
707 int status;
708 int i;
710 assert (depth < CF_MAX_DEPTH);
712 dh = opendir (dir);
713 if (dh == NULL)
714 {
715 char errbuf[1024];
716 ERROR ("configfile: opendir failed: %s",
717 sstrerror (errno, errbuf, sizeof (errbuf)));
718 return (NULL);
719 }
721 root = calloc (1, sizeof (*root));
722 if (root == NULL)
723 {
724 ERROR ("configfile: calloc failed.");
725 closedir (dh);
726 return (NULL);
727 }
729 while ((de = readdir (dh)) != NULL)
730 {
731 char name[1024];
732 char **tmp;
734 if ((de->d_name[0] == '.') || (de->d_name[0] == 0))
735 continue;
737 status = ssnprintf (name, sizeof (name), "%s/%s",
738 dir, de->d_name);
739 if ((status < 0) || ((size_t) status >= sizeof (name)))
740 {
741 ERROR ("configfile: Not including `%s/%s' because its"
742 " name is too long.",
743 dir, de->d_name);
744 closedir (dh);
745 for (i = 0; i < filenames_num; ++i)
746 free (filenames[i]);
747 free (filenames);
748 free (root);
749 return (NULL);
750 }
752 ++filenames_num;
753 tmp = realloc (filenames,
754 filenames_num * sizeof (*filenames));
755 if (tmp == NULL) {
756 ERROR ("configfile: realloc failed.");
757 closedir (dh);
758 for (i = 0; i < filenames_num - 1; ++i)
759 free (filenames[i]);
760 free (filenames);
761 free (root);
762 return (NULL);
763 }
764 filenames = tmp;
766 filenames[filenames_num - 1] = sstrdup (name);
767 }
769 if (filenames == NULL)
770 {
771 closedir (dh);
772 return (root);
773 }
775 qsort ((void *) filenames, filenames_num, sizeof (*filenames),
776 cf_compare_string);
778 for (i = 0; i < filenames_num; ++i)
779 {
780 oconfig_item_t *temp;
781 char *name = filenames[i];
783 temp = cf_read_generic (name, pattern, depth);
784 if (temp == NULL)
785 {
786 /* An error should already have been reported. */
787 sfree (name);
788 continue;
789 }
791 cf_ci_append_children (root, temp);
792 sfree (temp->children);
793 sfree (temp);
795 free (name);
796 }
798 closedir (dh);
799 free(filenames);
800 return (root);
801 } /* oconfig_item_t *cf_read_dir */
803 /*
804 * cf_read_generic
805 *
806 * Path is stat'ed and either cf_read_file or cf_read_dir is called
807 * accordingly.
808 *
809 * There are two versions of this function: If `wordexp' exists shell wildcards
810 * will be expanded and the function will include all matches found. If
811 * `wordexp' (or, more precisely, it's header file) is not available the
812 * simpler function is used which does not do any such expansion.
813 */
814 #if HAVE_WORDEXP_H
815 static oconfig_item_t *cf_read_generic (const char *path,
816 const char *pattern, int depth)
817 {
818 oconfig_item_t *root = NULL;
819 int status;
820 const char *path_ptr;
821 wordexp_t we;
822 size_t i;
824 if (depth >= CF_MAX_DEPTH)
825 {
826 ERROR ("configfile: Not including `%s' because the maximum "
827 "nesting depth has been reached.", path);
828 return (NULL);
829 }
831 status = wordexp (path, &we, WRDE_NOCMD);
832 if (status != 0)
833 {
834 ERROR ("configfile: wordexp (%s) failed.", path);
835 return (NULL);
836 }
838 root = calloc (1, sizeof (*root));
839 if (root == NULL)
840 {
841 ERROR ("configfile: calloc failed.");
842 return (NULL);
843 }
845 /* wordexp() might return a sorted list already. That's not
846 * documented though, so let's make sure we get what we want. */
847 qsort ((void *) we.we_wordv, we.we_wordc, sizeof (*we.we_wordv),
848 cf_compare_string);
850 for (i = 0; i < we.we_wordc; i++)
851 {
852 oconfig_item_t *temp;
853 struct stat statbuf;
855 path_ptr = we.we_wordv[i];
857 status = stat (path_ptr, &statbuf);
858 if (status != 0)
859 {
860 char errbuf[1024];
861 WARNING ("configfile: stat (%s) failed: %s",
862 path_ptr,
863 sstrerror (errno, errbuf, sizeof (errbuf)));
864 continue;
865 }
867 if (S_ISREG (statbuf.st_mode))
868 temp = cf_read_file (path_ptr, pattern, depth);
869 else if (S_ISDIR (statbuf.st_mode))
870 temp = cf_read_dir (path_ptr, pattern, depth);
871 else
872 {
873 WARNING ("configfile: %s is neither a file nor a "
874 "directory.", path);
875 continue;
876 }
878 if (temp == NULL) {
879 oconfig_free (root);
880 return (NULL);
881 }
883 cf_ci_append_children (root, temp);
884 sfree (temp->children);
885 sfree (temp);
886 }
888 wordfree (&we);
890 return (root);
891 } /* oconfig_item_t *cf_read_generic */
892 /* #endif HAVE_WORDEXP_H */
894 #else /* if !HAVE_WORDEXP_H */
895 static oconfig_item_t *cf_read_generic (const char *path,
896 const char *pattern, int depth)
897 {
898 struct stat statbuf;
899 int status;
901 if (depth >= CF_MAX_DEPTH)
902 {
903 ERROR ("configfile: Not including `%s' because the maximum "
904 "nesting depth has been reached.", path);
905 return (NULL);
906 }
908 status = stat (path, &statbuf);
909 if (status != 0)
910 {
911 char errbuf[1024];
912 ERROR ("configfile: stat (%s) failed: %s",
913 path,
914 sstrerror (errno, errbuf, sizeof (errbuf)));
915 return (NULL);
916 }
918 if (S_ISREG (statbuf.st_mode))
919 return (cf_read_file (path, pattern, depth));
920 else if (S_ISDIR (statbuf.st_mode))
921 return (cf_read_dir (path, pattern, depth));
923 ERROR ("configfile: %s is neither a file nor a directory.", path);
924 return (NULL);
925 } /* oconfig_item_t *cf_read_generic */
926 #endif /* !HAVE_WORDEXP_H */
928 /*
929 * Public functions
930 */
931 int global_option_set (const char *option, const char *value, _Bool from_cli)
932 {
933 int i;
935 DEBUG ("option = %s; value = %s;", option, value);
937 for (i = 0; i < cf_global_options_num; i++)
938 if (strcasecmp (cf_global_options[i].key, option) == 0)
939 break;
941 if (i >= cf_global_options_num)
942 {
943 ERROR ("configfile: Cannot set unknown global option `%s'.", option);
944 return (-1);
945 }
947 if (cf_global_options[i].from_cli && (! from_cli))
948 {
949 DEBUG ("configfile: Ignoring %s `%s' option because "
950 "it was overriden by a command-line option.",
951 option, value);
952 return (0);
953 }
955 sfree (cf_global_options[i].value);
957 if (value != NULL)
958 cf_global_options[i].value = strdup (value);
959 else
960 cf_global_options[i].value = NULL;
962 cf_global_options[i].from_cli = from_cli;
964 return (0);
965 }
967 const char *global_option_get (const char *option)
968 {
969 int i;
971 for (i = 0; i < cf_global_options_num; i++)
972 if (strcasecmp (cf_global_options[i].key, option) == 0)
973 break;
975 if (i >= cf_global_options_num)
976 {
977 ERROR ("configfile: Cannot get unknown global option `%s'.", option);
978 return (NULL);
979 }
981 return ((cf_global_options[i].value != NULL)
982 ? cf_global_options[i].value
983 : cf_global_options[i].def);
984 } /* char *global_option_get */
986 long global_option_get_long (const char *option, long default_value)
987 {
988 const char *str;
989 long value;
991 str = global_option_get (option);
992 if (NULL == str)
993 return (default_value);
995 errno = 0;
996 value = strtol (str, /* endptr = */ NULL, /* base = */ 0);
997 if (errno != 0)
998 return (default_value);
1000 return (value);
1001 } /* char *global_option_get_long */
1003 cdtime_t global_option_get_time (const char *name, cdtime_t def) /* {{{ */
1004 {
1005 char const *optstr;
1006 char *endptr = NULL;
1007 double v;
1009 optstr = global_option_get (name);
1010 if (optstr == NULL)
1011 return (def);
1013 errno = 0;
1014 v = strtod (optstr, &endptr);
1015 if ((endptr == NULL) || (*endptr != 0) || (errno != 0))
1016 return (def);
1017 else if (v <= 0.0)
1018 return (def);
1020 return (DOUBLE_TO_CDTIME_T (v));
1021 } /* }}} cdtime_t global_option_get_time */
1023 cdtime_t cf_get_default_interval (void)
1024 {
1025 return (global_option_get_time ("Interval",
1026 DOUBLE_TO_CDTIME_T (COLLECTD_DEFAULT_INTERVAL)));
1027 }
1029 void cf_unregister (const char *type)
1030 {
1031 cf_callback_t *this, *prev;
1033 for (prev = NULL, this = first_callback;
1034 this != NULL;
1035 prev = this, this = this->next)
1036 if (strcasecmp (this->type, type) == 0)
1037 {
1038 if (prev == NULL)
1039 first_callback = this->next;
1040 else
1041 prev->next = this->next;
1043 free (this);
1044 break;
1045 }
1046 } /* void cf_unregister */
1048 void cf_unregister_complex (const char *type)
1049 {
1050 cf_complex_callback_t *this, *prev;
1052 for (prev = NULL, this = complex_callback_head;
1053 this != NULL;
1054 prev = this, this = this->next)
1055 if (strcasecmp (this->type, type) == 0)
1056 {
1057 if (prev == NULL)
1058 complex_callback_head = this->next;
1059 else
1060 prev->next = this->next;
1062 sfree (this->type);
1063 sfree (this);
1064 break;
1065 }
1066 } /* void cf_unregister */
1068 void cf_register (const char *type,
1069 int (*callback) (const char *, const char *),
1070 const char **keys, int keys_num)
1071 {
1072 cf_callback_t *cf_cb;
1074 /* Remove this module from the list, if it already exists */
1075 cf_unregister (type);
1077 /* This pointer will be free'd in `cf_unregister' */
1078 if ((cf_cb = malloc (sizeof (*cf_cb))) == NULL)
1079 return;
1081 cf_cb->type = type;
1082 cf_cb->callback = callback;
1083 cf_cb->keys = keys;
1084 cf_cb->keys_num = keys_num;
1085 cf_cb->ctx = plugin_get_ctx ();
1087 cf_cb->next = first_callback;
1088 first_callback = cf_cb;
1089 } /* void cf_register */
1091 int cf_register_complex (const char *type, int (*callback) (oconfig_item_t *))
1092 {
1093 cf_complex_callback_t *new;
1095 new = malloc (sizeof (*new));
1096 if (new == NULL)
1097 return (-1);
1099 new->type = strdup (type);
1100 if (new->type == NULL)
1101 {
1102 sfree (new);
1103 return (-1);
1104 }
1106 new->callback = callback;
1107 new->next = NULL;
1109 new->ctx = plugin_get_ctx ();
1111 if (complex_callback_head == NULL)
1112 {
1113 complex_callback_head = new;
1114 }
1115 else
1116 {
1117 cf_complex_callback_t *last = complex_callback_head;
1118 while (last->next != NULL)
1119 last = last->next;
1120 last->next = new;
1121 }
1123 return (0);
1124 } /* int cf_register_complex */
1126 int cf_read (const char *filename)
1127 {
1128 oconfig_item_t *conf;
1129 int i;
1130 int ret = 0;
1132 conf = cf_read_generic (filename, /* pattern = */ NULL, /* depth = */ 0);
1133 if (conf == NULL)
1134 {
1135 ERROR ("Unable to read config file %s.", filename);
1136 return (-1);
1137 }
1138 else if (conf->children_num == 0)
1139 {
1140 ERROR ("Configuration file %s is empty.", filename);
1141 oconfig_free (conf);
1142 return (-1);
1143 }
1145 for (i = 0; i < conf->children_num; i++)
1146 {
1147 if (conf->children[i].children == NULL)
1148 {
1149 if (dispatch_value (conf->children + i) != 0)
1150 ret = -1;
1151 }
1152 else
1153 {
1154 if (dispatch_block (conf->children + i) != 0)
1155 ret = -1;
1156 }
1157 }
1159 oconfig_free (conf);
1161 /* Read the default types.db if no `TypesDB' option was given. */
1162 if (cf_default_typesdb)
1163 {
1164 if (read_types_list (PKGDATADIR"/types.db") != 0)
1165 ret = -1;
1166 }
1168 return ret;
1169 } /* int cf_read */
1171 /* Assures the config option is a string, duplicates it and returns the copy in
1172 * "ret_string". If necessary "*ret_string" is freed first. Returns zero upon
1173 * success. */
1174 int cf_util_get_string (const oconfig_item_t *ci, char **ret_string) /* {{{ */
1175 {
1176 char *string;
1178 if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_STRING))
1179 {
1180 ERROR ("cf_util_get_string: The %s option requires "
1181 "exactly one string argument.", ci->key);
1182 return (-1);
1183 }
1185 string = strdup (ci->values[0].value.string);
1186 if (string == NULL)
1187 return (-1);
1189 if (*ret_string != NULL)
1190 sfree (*ret_string);
1191 *ret_string = string;
1193 return (0);
1194 } /* }}} int cf_util_get_string */
1196 /* Assures the config option is a string and copies it to the provided buffer.
1197 * Assures null-termination. */
1198 int cf_util_get_string_buffer (const oconfig_item_t *ci, char *buffer, /* {{{ */
1199 size_t buffer_size)
1200 {
1201 if ((ci == NULL) || (buffer == NULL) || (buffer_size < 1))
1202 return (EINVAL);
1204 if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_STRING))
1205 {
1206 ERROR ("cf_util_get_string_buffer: The %s option requires "
1207 "exactly one string argument.", ci->key);
1208 return (-1);
1209 }
1211 strncpy (buffer, ci->values[0].value.string, buffer_size);
1212 buffer[buffer_size - 1] = 0;
1214 return (0);
1215 } /* }}} int cf_util_get_string_buffer */
1217 /* Assures the config option is a number and returns it as an int. */
1218 int cf_util_get_int (const oconfig_item_t *ci, int *ret_value) /* {{{ */
1219 {
1220 if ((ci == NULL) || (ret_value == NULL))
1221 return (EINVAL);
1223 if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_NUMBER))
1224 {
1225 ERROR ("cf_util_get_int: The %s option requires "
1226 "exactly one numeric argument.", ci->key);
1227 return (-1);
1228 }
1230 *ret_value = (int) ci->values[0].value.number;
1232 return (0);
1233 } /* }}} int cf_util_get_int */
1235 int cf_util_get_double (const oconfig_item_t *ci, double *ret_value) /* {{{ */
1236 {
1237 if ((ci == NULL) || (ret_value == NULL))
1238 return (EINVAL);
1240 if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_NUMBER))
1241 {
1242 ERROR ("cf_util_get_double: The %s option requires "
1243 "exactly one numeric argument.", ci->key);
1244 return (-1);
1245 }
1247 *ret_value = ci->values[0].value.number;
1249 return (0);
1250 } /* }}} int cf_util_get_double */
1252 int cf_util_get_boolean (const oconfig_item_t *ci, _Bool *ret_bool) /* {{{ */
1253 {
1254 if ((ci == NULL) || (ret_bool == NULL))
1255 return (EINVAL);
1257 if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_BOOLEAN))
1258 {
1259 ERROR ("cf_util_get_boolean: The %s option requires "
1260 "exactly one boolean argument.", ci->key);
1261 return (-1);
1262 }
1264 *ret_bool = ci->values[0].value.boolean ? 1 : 0;
1266 return (0);
1267 } /* }}} int cf_util_get_boolean */
1269 int cf_util_get_flag (const oconfig_item_t *ci, /* {{{ */
1270 unsigned int *ret_value, unsigned int flag)
1271 {
1272 int status;
1273 _Bool b;
1275 if (ret_value == NULL)
1276 return (EINVAL);
1278 b = 0;
1279 status = cf_util_get_boolean (ci, &b);
1280 if (status != 0)
1281 return (status);
1283 if (b)
1284 {
1285 *ret_value |= flag;
1286 }
1287 else
1288 {
1289 *ret_value &= ~flag;
1290 }
1292 return (0);
1293 } /* }}} int cf_util_get_flag */
1295 /* Assures that the config option is a string or a number if the correct range
1296 * of 1-65535. The string is then converted to a port number using
1297 * `service_name_to_port_number' and returned.
1298 * Returns the port number in the range [1-65535] or less than zero upon
1299 * failure. */
1300 int cf_util_get_port_number (const oconfig_item_t *ci) /* {{{ */
1301 {
1302 int tmp;
1304 if ((ci->values_num != 1)
1305 || ((ci->values[0].type != OCONFIG_TYPE_STRING)
1306 && (ci->values[0].type != OCONFIG_TYPE_NUMBER)))
1307 {
1308 ERROR ("cf_util_get_port_number: The \"%s\" option requires "
1309 "exactly one string argument.", ci->key);
1310 return (-1);
1311 }
1313 if (ci->values[0].type == OCONFIG_TYPE_STRING)
1314 return (service_name_to_port_number (ci->values[0].value.string));
1316 assert (ci->values[0].type == OCONFIG_TYPE_NUMBER);
1317 tmp = (int) (ci->values[0].value.number + 0.5);
1318 if ((tmp < 1) || (tmp > 65535))
1319 {
1320 ERROR ("cf_util_get_port_number: The \"%s\" option requires "
1321 "a service name or a port number. The number "
1322 "you specified, %i, is not in the valid "
1323 "range of 1-65535.",
1324 ci->key, tmp);
1325 return (-1);
1326 }
1328 return (tmp);
1329 } /* }}} int cf_util_get_port_number */
1331 int cf_util_get_service (const oconfig_item_t *ci, char **ret_string) /* {{{ */
1332 {
1333 int port;
1334 char *service;
1335 int status;
1337 if (ci->values_num != 1)
1338 {
1339 ERROR ("cf_util_get_service: The %s option requires exactly "
1340 "one argument.", ci->key);
1341 return (-1);
1342 }
1344 if (ci->values[0].type == OCONFIG_TYPE_STRING)
1345 return (cf_util_get_string (ci, ret_string));
1346 if (ci->values[0].type != OCONFIG_TYPE_NUMBER)
1347 {
1348 ERROR ("cf_util_get_service: The %s option requires "
1349 "exactly one string or numeric argument.",
1350 ci->key);
1351 }
1353 port = 0;
1354 status = cf_util_get_int (ci, &port);
1355 if (status != 0)
1356 return (status);
1357 else if ((port < 1) || (port > 65535))
1358 {
1359 ERROR ("cf_util_get_service: The port number given "
1360 "for the %s option is out of "
1361 "range (%i).", ci->key, port);
1362 return (-1);
1363 }
1365 service = malloc (6);
1366 if (service == NULL)
1367 {
1368 ERROR ("cf_util_get_service: Out of memory.");
1369 return (-1);
1370 }
1371 ssnprintf (service, 6, "%i", port);
1373 sfree (*ret_string);
1374 *ret_string = service;
1376 return (0);
1377 } /* }}} int cf_util_get_service */
1379 int cf_util_get_cdtime (const oconfig_item_t *ci, cdtime_t *ret_value) /* {{{ */
1380 {
1381 if ((ci == NULL) || (ret_value == NULL))
1382 return (EINVAL);
1384 if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_NUMBER))
1385 {
1386 ERROR ("cf_util_get_cdtime: The %s option requires "
1387 "exactly one numeric argument.", ci->key);
1388 return (-1);
1389 }
1391 if (ci->values[0].value.number < 0.0)
1392 {
1393 ERROR ("cf_util_get_cdtime: The numeric argument of the %s "
1394 "option must not be negative.", ci->key);
1395 return (-1);
1396 }
1398 *ret_value = DOUBLE_TO_CDTIME_T (ci->values[0].value.number);
1400 return (0);
1401 } /* }}} int cf_util_get_cdtime */