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 = { 0 };
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 ctx.interval = cf_get_default_interval ();
293 ctx.flush_interval = 0;
294 ctx.flush_timeout = 0;
296 for (i = 0; i < ci->children_num; ++i)
297 {
298 oconfig_item_t *child = ci->children + i;
300 if (strcasecmp("Globals", child->key) == 0)
301 cf_util_get_flag (child, &flags, PLUGIN_FLAGS_GLOBAL);
302 else if (strcasecmp ("Interval", child->key) == 0)
303 cf_util_get_cdtime (child, &ctx.interval);
304 else if (strcasecmp ("FlushInterval", child->key) == 0)
305 cf_util_get_cdtime (child, &ctx.flush_interval);
306 else if (strcasecmp ("FlushTimeout", child->key) == 0)
307 cf_util_get_cdtime (child, &ctx.flush_timeout);
308 else {
309 WARNING("Ignoring unknown LoadPlugin option \"%s\" "
310 "for plugin \"%s\"",
311 child->key, ci->values[0].value.string);
312 }
313 }
315 old_ctx = plugin_set_ctx (ctx);
316 ret_val = plugin_load (name, (uint32_t) flags);
317 /* reset to the "global" context */
318 plugin_set_ctx (old_ctx);
320 return (ret_val);
321 } /* int dispatch_value_loadplugin */
323 static int dispatch_value_plugin (const char *plugin, oconfig_item_t *ci)
324 {
325 char buffer[4096];
326 char *buffer_ptr;
327 int buffer_free;
328 int i;
330 buffer_ptr = buffer;
331 buffer_free = sizeof (buffer);
333 for (i = 0; i < ci->values_num; i++)
334 {
335 int status = -1;
337 if (ci->values[i].type == OCONFIG_TYPE_STRING)
338 status = ssnprintf (buffer_ptr, buffer_free, " %s",
339 ci->values[i].value.string);
340 else if (ci->values[i].type == OCONFIG_TYPE_NUMBER)
341 status = ssnprintf (buffer_ptr, buffer_free, " %lf",
342 ci->values[i].value.number);
343 else if (ci->values[i].type == OCONFIG_TYPE_BOOLEAN)
344 status = ssnprintf (buffer_ptr, buffer_free, " %s",
345 ci->values[i].value.boolean
346 ? "true" : "false");
348 if ((status < 0) || (status >= buffer_free))
349 return (-1);
350 buffer_free -= status;
351 buffer_ptr += status;
352 }
353 /* skip the initial space */
354 buffer_ptr = buffer + 1;
356 return (cf_dispatch (plugin, ci->key, buffer_ptr));
357 } /* int dispatch_value_plugin */
359 static int dispatch_value (oconfig_item_t *ci)
360 {
361 int ret = 0;
362 int i;
364 for (i = 0; i < cf_value_map_num; i++)
365 if (strcasecmp (cf_value_map[i].key, ci->key) == 0)
366 {
367 ret = cf_value_map[i].func (ci);
368 break;
369 }
371 for (i = 0; i < cf_global_options_num; i++)
372 if (strcasecmp (cf_global_options[i].key, ci->key) == 0)
373 {
374 ret = dispatch_global_option (ci);
375 break;
376 }
378 return (ret);
379 } /* int dispatch_value */
381 static int dispatch_block_plugin (oconfig_item_t *ci)
382 {
383 int i;
384 const char *name;
386 cf_complex_callback_t *cb;
388 if (strcasecmp (ci->key, "Plugin") != 0)
389 return (-1);
390 if (ci->values_num < 1)
391 return (-1);
392 if (ci->values[0].type != OCONFIG_TYPE_STRING)
393 return (-1);
395 name = ci->values[0].value.string;
396 if (strcmp ("libvirt", name) == 0)
397 {
398 /* TODO(octo): Remove this legacy. */
399 WARNING ("The \"libvirt\" plugin has been renamed to \"virt\" to avoid problems with the build system. "
400 "Your configuration is still using the old name. "
401 "Please change it to use \"virt\" as soon as possible. "
402 "This compatibility code will go away eventually.");
403 name = "virt";
404 }
406 if (IS_TRUE (global_option_get ("AutoLoadPlugin")))
407 {
408 plugin_ctx_t ctx = { 0 };
409 plugin_ctx_t old_ctx;
410 int status;
412 /* default to the global interval set before loading this plugin */
413 ctx.interval = cf_get_default_interval ();
415 old_ctx = plugin_set_ctx (ctx);
416 status = plugin_load (name, /* flags = */ 0);
417 /* reset to the "global" context */
418 plugin_set_ctx (old_ctx);
420 if (status != 0)
421 {
422 ERROR ("Automatically loading plugin \"%s\" failed "
423 "with status %i.", name, status);
424 return (status);
425 }
426 }
428 /* Check for a complex callback first */
429 for (cb = complex_callback_head; cb != NULL; cb = cb->next)
430 {
431 if (strcasecmp (name, cb->type) == 0)
432 {
433 plugin_ctx_t old_ctx;
434 int ret_val;
436 old_ctx = plugin_set_ctx (cb->ctx);
437 ret_val = (cb->callback (ci));
438 plugin_set_ctx (old_ctx);
439 return (ret_val);
440 }
441 }
443 /* Hm, no complex plugin found. Dispatch the values one by one */
444 for (i = 0; i < ci->children_num; i++)
445 {
446 if (ci->children[i].children == NULL)
447 dispatch_value_plugin (name, ci->children + i);
448 else
449 {
450 WARNING ("There is a `%s' block within the "
451 "configuration for the %s plugin. "
452 "The plugin either only expects "
453 "\"simple\" configuration statements "
454 "or wasn't loaded using `LoadPlugin'."
455 " Please check your configuration.",
456 ci->children[i].key, name);
457 }
458 }
460 return (0);
461 }
464 static int dispatch_block (oconfig_item_t *ci)
465 {
466 if (strcasecmp (ci->key, "LoadPlugin") == 0)
467 return (dispatch_loadplugin (ci));
468 else if (strcasecmp (ci->key, "Plugin") == 0)
469 return (dispatch_block_plugin (ci));
470 else if (strcasecmp (ci->key, "Chain") == 0)
471 return (fc_configure (ci));
473 return (0);
474 }
476 static int cf_ci_replace_child (oconfig_item_t *dst, oconfig_item_t *src,
477 int offset)
478 {
479 oconfig_item_t *temp;
480 int i;
482 assert (offset >= 0);
483 assert (dst->children_num > offset);
485 /* Free the memory used by the replaced child. Usually that's the
486 * `Include "blah"' statement. */
487 temp = dst->children + offset;
488 for (i = 0; i < temp->values_num; i++)
489 {
490 if (temp->values[i].type == OCONFIG_TYPE_STRING)
491 {
492 sfree (temp->values[i].value.string);
493 }
494 }
495 sfree (temp->values);
496 temp = NULL;
498 /* If (src->children_num == 0) the array size is decreased. If offset
499 * is _not_ the last element, (offset < (dst->children_num - 1)), then
500 * we need to move the trailing elements before resizing the array. */
501 if ((src->children_num == 0) && (offset < (dst->children_num - 1)))
502 {
503 int nmemb = dst->children_num - (offset + 1);
504 memmove (dst->children + offset, dst->children + offset + 1,
505 sizeof (oconfig_item_t) * nmemb);
506 }
508 /* Resize the memory containing the children to be big enough to hold
509 * all children. */
510 if (dst->children_num + src->children_num - 1 == 0)
511 {
512 dst->children_num = 0;
513 return (0);
514 }
516 temp = realloc (dst->children,
517 sizeof (oconfig_item_t)
518 * (dst->children_num + src->children_num - 1));
519 if (temp == NULL)
520 {
521 ERROR ("configfile: realloc failed.");
522 return (-1);
523 }
524 dst->children = temp;
526 /* If there are children behind the include statement, and they have
527 * not yet been moved because (src->children_num == 0), then move them
528 * to the end of the list, so that the new children have room before
529 * them. */
530 if ((src->children_num > 0)
531 && ((dst->children_num - (offset + 1)) > 0))
532 {
533 int nmemb = dst->children_num - (offset + 1);
534 int old_offset = offset + 1;
535 int new_offset = offset + src->children_num;
537 memmove (dst->children + new_offset,
538 dst->children + old_offset,
539 sizeof (oconfig_item_t) * nmemb);
540 }
542 /* Last but not least: If there are new children, copy them to the
543 * memory reserved for them. */
544 if (src->children_num > 0)
545 {
546 memcpy (dst->children + offset,
547 src->children,
548 sizeof (oconfig_item_t) * src->children_num);
549 }
551 /* Update the number of children. */
552 dst->children_num += (src->children_num - 1);
554 return (0);
555 } /* int cf_ci_replace_child */
557 static int cf_ci_append_children (oconfig_item_t *dst, oconfig_item_t *src)
558 {
559 oconfig_item_t *temp;
561 if ((src == NULL) || (src->children_num == 0))
562 return (0);
564 temp = realloc (dst->children,
565 sizeof (oconfig_item_t)
566 * (dst->children_num + src->children_num));
567 if (temp == NULL)
568 {
569 ERROR ("configfile: realloc failed.");
570 return (-1);
571 }
572 dst->children = temp;
574 memcpy (dst->children + dst->children_num,
575 src->children,
576 sizeof (oconfig_item_t)
577 * src->children_num);
578 dst->children_num += src->children_num;
580 return (0);
581 } /* int cf_ci_append_children */
583 #define CF_MAX_DEPTH 8
584 static oconfig_item_t *cf_read_generic (const char *path,
585 const char *pattern, int depth);
587 static int cf_include_all (oconfig_item_t *root, int depth)
588 {
589 int i;
591 for (i = 0; i < root->children_num; i++)
592 {
593 oconfig_item_t *new;
594 oconfig_item_t *old;
596 char *pattern = NULL;
598 int j;
600 if (strcasecmp (root->children[i].key, "Include") != 0)
601 continue;
603 old = root->children + i;
605 if ((old->values_num != 1)
606 || (old->values[0].type != OCONFIG_TYPE_STRING))
607 {
608 ERROR ("configfile: `Include' needs exactly one string argument.");
609 continue;
610 }
612 for (j = 0; j < old->children_num; ++j)
613 {
614 oconfig_item_t *child = old->children + j;
616 if (strcasecmp (child->key, "Filter") == 0)
617 cf_util_get_string (child, &pattern);
618 else
619 ERROR ("configfile: Option `%s' not allowed in <Include> block.",
620 child->key);
621 }
623 new = cf_read_generic (old->values[0].value.string, pattern, depth + 1);
624 sfree (pattern);
626 if (new == NULL)
627 return (-1);
629 /* Now replace the i'th child in `root' with `new'. */
630 if (cf_ci_replace_child (root, new, i) < 0) {
631 sfree (new->values);
632 sfree (new);
633 return (-1);
634 }
636 /* ... and go back to the new i'th child. */
637 --i;
639 sfree (new->values);
640 sfree (new);
641 } /* for (i = 0; i < root->children_num; i++) */
643 return (0);
644 } /* int cf_include_all */
646 static oconfig_item_t *cf_read_file (const char *file,
647 const char *pattern, int depth)
648 {
649 oconfig_item_t *root;
650 int status;
652 assert (depth < CF_MAX_DEPTH);
654 if (pattern != NULL) {
655 #if HAVE_FNMATCH_H && HAVE_LIBGEN_H
656 char *tmp = sstrdup (file);
657 char *filename = basename (tmp);
659 if ((filename != NULL) && (fnmatch (pattern, filename, 0) != 0)) {
660 DEBUG ("configfile: Not including `%s' because it "
661 "does not match pattern `%s'.",
662 filename, pattern);
663 free (tmp);
664 return (NULL);
665 }
667 free (tmp);
668 #else
669 ERROR ("configfile: Cannot apply pattern filter '%s' "
670 "to file '%s': functions basename() and / or "
671 "fnmatch() not available.", pattern, file);
672 #endif /* HAVE_FNMATCH_H && HAVE_LIBGEN_H */
673 }
675 root = oconfig_parse_file (file);
676 if (root == NULL)
677 {
678 ERROR ("configfile: Cannot read file `%s'.", file);
679 return (NULL);
680 }
682 status = cf_include_all (root, depth);
683 if (status != 0)
684 {
685 oconfig_free (root);
686 return (NULL);
687 }
689 return (root);
690 } /* oconfig_item_t *cf_read_file */
692 static int cf_compare_string (const void *p1, const void *p2)
693 {
694 return strcmp (*(const char **) p1, *(const char **) p2);
695 }
697 static oconfig_item_t *cf_read_dir (const char *dir,
698 const char *pattern, int depth)
699 {
700 oconfig_item_t *root = NULL;
701 DIR *dh;
702 struct dirent *de;
703 char **filenames = NULL;
704 int filenames_num = 0;
705 int status;
706 int i;
708 assert (depth < CF_MAX_DEPTH);
710 dh = opendir (dir);
711 if (dh == NULL)
712 {
713 char errbuf[1024];
714 ERROR ("configfile: opendir failed: %s",
715 sstrerror (errno, errbuf, sizeof (errbuf)));
716 return (NULL);
717 }
719 root = calloc (1, sizeof (*root));
720 if (root == NULL)
721 {
722 ERROR ("configfile: calloc failed.");
723 closedir (dh);
724 return (NULL);
725 }
727 while ((de = readdir (dh)) != NULL)
728 {
729 char name[1024];
730 char **tmp;
732 if ((de->d_name[0] == '.') || (de->d_name[0] == 0))
733 continue;
735 status = ssnprintf (name, sizeof (name), "%s/%s",
736 dir, de->d_name);
737 if ((status < 0) || ((size_t) status >= sizeof (name)))
738 {
739 ERROR ("configfile: Not including `%s/%s' because its"
740 " name is too long.",
741 dir, de->d_name);
742 closedir (dh);
743 for (i = 0; i < filenames_num; ++i)
744 free (filenames[i]);
745 free (filenames);
746 free (root);
747 return (NULL);
748 }
750 ++filenames_num;
751 tmp = realloc (filenames,
752 filenames_num * sizeof (*filenames));
753 if (tmp == NULL) {
754 ERROR ("configfile: realloc failed.");
755 closedir (dh);
756 for (i = 0; i < filenames_num - 1; ++i)
757 free (filenames[i]);
758 free (filenames);
759 free (root);
760 return (NULL);
761 }
762 filenames = tmp;
764 filenames[filenames_num - 1] = sstrdup (name);
765 }
767 if (filenames == NULL)
768 {
769 closedir (dh);
770 return (root);
771 }
773 qsort ((void *) filenames, filenames_num, sizeof (*filenames),
774 cf_compare_string);
776 for (i = 0; i < filenames_num; ++i)
777 {
778 oconfig_item_t *temp;
779 char *name = filenames[i];
781 temp = cf_read_generic (name, pattern, depth);
782 if (temp == NULL)
783 {
784 /* An error should already have been reported. */
785 sfree (name);
786 continue;
787 }
789 cf_ci_append_children (root, temp);
790 sfree (temp->children);
791 sfree (temp);
793 free (name);
794 }
796 closedir (dh);
797 free(filenames);
798 return (root);
799 } /* oconfig_item_t *cf_read_dir */
801 /*
802 * cf_read_generic
803 *
804 * Path is stat'ed and either cf_read_file or cf_read_dir is called
805 * accordingly.
806 *
807 * There are two versions of this function: If `wordexp' exists shell wildcards
808 * will be expanded and the function will include all matches found. If
809 * `wordexp' (or, more precisely, it's header file) is not available the
810 * simpler function is used which does not do any such expansion.
811 */
812 #if HAVE_WORDEXP_H
813 static oconfig_item_t *cf_read_generic (const char *path,
814 const char *pattern, int depth)
815 {
816 oconfig_item_t *root = NULL;
817 int status;
818 const char *path_ptr;
819 wordexp_t we;
820 size_t i;
822 if (depth >= CF_MAX_DEPTH)
823 {
824 ERROR ("configfile: Not including `%s' because the maximum "
825 "nesting depth has been reached.", path);
826 return (NULL);
827 }
829 status = wordexp (path, &we, WRDE_NOCMD);
830 if (status != 0)
831 {
832 ERROR ("configfile: wordexp (%s) failed.", path);
833 return (NULL);
834 }
836 root = calloc (1, sizeof (*root));
837 if (root == NULL)
838 {
839 ERROR ("configfile: calloc failed.");
840 return (NULL);
841 }
843 /* wordexp() might return a sorted list already. That's not
844 * documented though, so let's make sure we get what we want. */
845 qsort ((void *) we.we_wordv, we.we_wordc, sizeof (*we.we_wordv),
846 cf_compare_string);
848 for (i = 0; i < we.we_wordc; i++)
849 {
850 oconfig_item_t *temp;
851 struct stat statbuf;
853 path_ptr = we.we_wordv[i];
855 status = stat (path_ptr, &statbuf);
856 if (status != 0)
857 {
858 char errbuf[1024];
859 WARNING ("configfile: stat (%s) failed: %s",
860 path_ptr,
861 sstrerror (errno, errbuf, sizeof (errbuf)));
862 continue;
863 }
865 if (S_ISREG (statbuf.st_mode))
866 temp = cf_read_file (path_ptr, pattern, depth);
867 else if (S_ISDIR (statbuf.st_mode))
868 temp = cf_read_dir (path_ptr, pattern, depth);
869 else
870 {
871 WARNING ("configfile: %s is neither a file nor a "
872 "directory.", path);
873 continue;
874 }
876 if (temp == NULL) {
877 oconfig_free (root);
878 return (NULL);
879 }
881 cf_ci_append_children (root, temp);
882 sfree (temp->children);
883 sfree (temp);
884 }
886 wordfree (&we);
888 return (root);
889 } /* oconfig_item_t *cf_read_generic */
890 /* #endif HAVE_WORDEXP_H */
892 #else /* if !HAVE_WORDEXP_H */
893 static oconfig_item_t *cf_read_generic (const char *path,
894 const char *pattern, int depth)
895 {
896 struct stat statbuf;
897 int status;
899 if (depth >= CF_MAX_DEPTH)
900 {
901 ERROR ("configfile: Not including `%s' because the maximum "
902 "nesting depth has been reached.", path);
903 return (NULL);
904 }
906 status = stat (path, &statbuf);
907 if (status != 0)
908 {
909 char errbuf[1024];
910 ERROR ("configfile: stat (%s) failed: %s",
911 path,
912 sstrerror (errno, errbuf, sizeof (errbuf)));
913 return (NULL);
914 }
916 if (S_ISREG (statbuf.st_mode))
917 return (cf_read_file (path, pattern, depth));
918 else if (S_ISDIR (statbuf.st_mode))
919 return (cf_read_dir (path, pattern, depth));
921 ERROR ("configfile: %s is neither a file nor a directory.", path);
922 return (NULL);
923 } /* oconfig_item_t *cf_read_generic */
924 #endif /* !HAVE_WORDEXP_H */
926 /*
927 * Public functions
928 */
929 int global_option_set (const char *option, const char *value, _Bool from_cli)
930 {
931 int i;
933 DEBUG ("option = %s; value = %s;", option, value);
935 for (i = 0; i < cf_global_options_num; i++)
936 if (strcasecmp (cf_global_options[i].key, option) == 0)
937 break;
939 if (i >= cf_global_options_num)
940 {
941 ERROR ("configfile: Cannot set unknown global option `%s'.", option);
942 return (-1);
943 }
945 if (cf_global_options[i].from_cli && (! from_cli))
946 {
947 DEBUG ("configfile: Ignoring %s `%s' option because "
948 "it was overriden by a command-line option.",
949 option, value);
950 return (0);
951 }
953 sfree (cf_global_options[i].value);
955 if (value != NULL)
956 cf_global_options[i].value = strdup (value);
957 else
958 cf_global_options[i].value = NULL;
960 cf_global_options[i].from_cli = from_cli;
962 return (0);
963 }
965 const char *global_option_get (const char *option)
966 {
967 int i;
969 for (i = 0; i < cf_global_options_num; i++)
970 if (strcasecmp (cf_global_options[i].key, option) == 0)
971 break;
973 if (i >= cf_global_options_num)
974 {
975 ERROR ("configfile: Cannot get unknown global option `%s'.", option);
976 return (NULL);
977 }
979 return ((cf_global_options[i].value != NULL)
980 ? cf_global_options[i].value
981 : cf_global_options[i].def);
982 } /* char *global_option_get */
984 long global_option_get_long (const char *option, long default_value)
985 {
986 const char *str;
987 long value;
989 str = global_option_get (option);
990 if (NULL == str)
991 return (default_value);
993 errno = 0;
994 value = strtol (str, /* endptr = */ NULL, /* base = */ 0);
995 if (errno != 0)
996 return (default_value);
998 return (value);
999 } /* char *global_option_get_long */
1001 cdtime_t global_option_get_time (const char *name, cdtime_t def) /* {{{ */
1002 {
1003 char const *optstr;
1004 char *endptr = NULL;
1005 double v;
1007 optstr = global_option_get (name);
1008 if (optstr == NULL)
1009 return (def);
1011 errno = 0;
1012 v = strtod (optstr, &endptr);
1013 if ((endptr == NULL) || (*endptr != 0) || (errno != 0))
1014 return (def);
1015 else if (v <= 0.0)
1016 return (def);
1018 return (DOUBLE_TO_CDTIME_T (v));
1019 } /* }}} cdtime_t global_option_get_time */
1021 cdtime_t cf_get_default_interval (void)
1022 {
1023 return (global_option_get_time ("Interval",
1024 DOUBLE_TO_CDTIME_T (COLLECTD_DEFAULT_INTERVAL)));
1025 }
1027 void cf_unregister (const char *type)
1028 {
1029 cf_callback_t *this, *prev;
1031 for (prev = NULL, this = first_callback;
1032 this != NULL;
1033 prev = this, this = this->next)
1034 if (strcasecmp (this->type, type) == 0)
1035 {
1036 if (prev == NULL)
1037 first_callback = this->next;
1038 else
1039 prev->next = this->next;
1041 free (this);
1042 break;
1043 }
1044 } /* void cf_unregister */
1046 void cf_unregister_complex (const char *type)
1047 {
1048 cf_complex_callback_t *this, *prev;
1050 for (prev = NULL, this = complex_callback_head;
1051 this != NULL;
1052 prev = this, this = this->next)
1053 if (strcasecmp (this->type, type) == 0)
1054 {
1055 if (prev == NULL)
1056 complex_callback_head = this->next;
1057 else
1058 prev->next = this->next;
1060 sfree (this->type);
1061 sfree (this);
1062 break;
1063 }
1064 } /* void cf_unregister */
1066 void cf_register (const char *type,
1067 int (*callback) (const char *, const char *),
1068 const char **keys, int keys_num)
1069 {
1070 cf_callback_t *cf_cb;
1072 /* Remove this module from the list, if it already exists */
1073 cf_unregister (type);
1075 /* This pointer will be free'd in `cf_unregister' */
1076 if ((cf_cb = malloc (sizeof (*cf_cb))) == NULL)
1077 return;
1079 cf_cb->type = type;
1080 cf_cb->callback = callback;
1081 cf_cb->keys = keys;
1082 cf_cb->keys_num = keys_num;
1083 cf_cb->ctx = plugin_get_ctx ();
1085 cf_cb->next = first_callback;
1086 first_callback = cf_cb;
1087 } /* void cf_register */
1089 int cf_register_complex (const char *type, int (*callback) (oconfig_item_t *))
1090 {
1091 cf_complex_callback_t *new;
1093 new = malloc (sizeof (*new));
1094 if (new == NULL)
1095 return (-1);
1097 new->type = strdup (type);
1098 if (new->type == NULL)
1099 {
1100 sfree (new);
1101 return (-1);
1102 }
1104 new->callback = callback;
1105 new->next = NULL;
1107 new->ctx = plugin_get_ctx ();
1109 if (complex_callback_head == NULL)
1110 {
1111 complex_callback_head = new;
1112 }
1113 else
1114 {
1115 cf_complex_callback_t *last = complex_callback_head;
1116 while (last->next != NULL)
1117 last = last->next;
1118 last->next = new;
1119 }
1121 return (0);
1122 } /* int cf_register_complex */
1124 int cf_read (const char *filename)
1125 {
1126 oconfig_item_t *conf;
1127 int i;
1128 int ret = 0;
1130 conf = cf_read_generic (filename, /* pattern = */ NULL, /* depth = */ 0);
1131 if (conf == NULL)
1132 {
1133 ERROR ("Unable to read config file %s.", filename);
1134 return (-1);
1135 }
1136 else if (conf->children_num == 0)
1137 {
1138 ERROR ("Configuration file %s is empty.", filename);
1139 oconfig_free (conf);
1140 return (-1);
1141 }
1143 for (i = 0; i < conf->children_num; i++)
1144 {
1145 if (conf->children[i].children == NULL)
1146 {
1147 if (dispatch_value (conf->children + i) != 0)
1148 ret = -1;
1149 }
1150 else
1151 {
1152 if (dispatch_block (conf->children + i) != 0)
1153 ret = -1;
1154 }
1155 }
1157 oconfig_free (conf);
1159 /* Read the default types.db if no `TypesDB' option was given. */
1160 if (cf_default_typesdb)
1161 {
1162 if (read_types_list (PKGDATADIR"/types.db") != 0)
1163 ret = -1;
1164 }
1166 return ret;
1167 } /* int cf_read */
1169 /* Assures the config option is a string, duplicates it and returns the copy in
1170 * "ret_string". If necessary "*ret_string" is freed first. Returns zero upon
1171 * success. */
1172 int cf_util_get_string (const oconfig_item_t *ci, char **ret_string) /* {{{ */
1173 {
1174 char *string;
1176 if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_STRING))
1177 {
1178 ERROR ("cf_util_get_string: The %s option requires "
1179 "exactly one string argument.", ci->key);
1180 return (-1);
1181 }
1183 string = strdup (ci->values[0].value.string);
1184 if (string == NULL)
1185 return (-1);
1187 if (*ret_string != NULL)
1188 sfree (*ret_string);
1189 *ret_string = string;
1191 return (0);
1192 } /* }}} int cf_util_get_string */
1194 /* Assures the config option is a string and copies it to the provided buffer.
1195 * Assures null-termination. */
1196 int cf_util_get_string_buffer (const oconfig_item_t *ci, char *buffer, /* {{{ */
1197 size_t buffer_size)
1198 {
1199 if ((ci == NULL) || (buffer == NULL) || (buffer_size < 1))
1200 return (EINVAL);
1202 if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_STRING))
1203 {
1204 ERROR ("cf_util_get_string_buffer: The %s option requires "
1205 "exactly one string argument.", ci->key);
1206 return (-1);
1207 }
1209 strncpy (buffer, ci->values[0].value.string, buffer_size);
1210 buffer[buffer_size - 1] = 0;
1212 return (0);
1213 } /* }}} int cf_util_get_string_buffer */
1215 /* Assures the config option is a number and returns it as an int. */
1216 int cf_util_get_int (const oconfig_item_t *ci, int *ret_value) /* {{{ */
1217 {
1218 if ((ci == NULL) || (ret_value == NULL))
1219 return (EINVAL);
1221 if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_NUMBER))
1222 {
1223 ERROR ("cf_util_get_int: The %s option requires "
1224 "exactly one numeric argument.", ci->key);
1225 return (-1);
1226 }
1228 *ret_value = (int) ci->values[0].value.number;
1230 return (0);
1231 } /* }}} int cf_util_get_int */
1233 int cf_util_get_double (const oconfig_item_t *ci, double *ret_value) /* {{{ */
1234 {
1235 if ((ci == NULL) || (ret_value == NULL))
1236 return (EINVAL);
1238 if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_NUMBER))
1239 {
1240 ERROR ("cf_util_get_double: The %s option requires "
1241 "exactly one numeric argument.", ci->key);
1242 return (-1);
1243 }
1245 *ret_value = ci->values[0].value.number;
1247 return (0);
1248 } /* }}} int cf_util_get_double */
1250 int cf_util_get_boolean (const oconfig_item_t *ci, _Bool *ret_bool) /* {{{ */
1251 {
1252 if ((ci == NULL) || (ret_bool == NULL))
1253 return (EINVAL);
1255 if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_BOOLEAN))
1256 {
1257 ERROR ("cf_util_get_boolean: The %s option requires "
1258 "exactly one boolean argument.", ci->key);
1259 return (-1);
1260 }
1262 *ret_bool = ci->values[0].value.boolean ? 1 : 0;
1264 return (0);
1265 } /* }}} int cf_util_get_boolean */
1267 int cf_util_get_flag (const oconfig_item_t *ci, /* {{{ */
1268 unsigned int *ret_value, unsigned int flag)
1269 {
1270 int status;
1271 _Bool b;
1273 if (ret_value == NULL)
1274 return (EINVAL);
1276 b = 0;
1277 status = cf_util_get_boolean (ci, &b);
1278 if (status != 0)
1279 return (status);
1281 if (b)
1282 {
1283 *ret_value |= flag;
1284 }
1285 else
1286 {
1287 *ret_value &= ~flag;
1288 }
1290 return (0);
1291 } /* }}} int cf_util_get_flag */
1293 /* Assures that the config option is a string or a number if the correct range
1294 * of 1-65535. The string is then converted to a port number using
1295 * `service_name_to_port_number' and returned.
1296 * Returns the port number in the range [1-65535] or less than zero upon
1297 * failure. */
1298 int cf_util_get_port_number (const oconfig_item_t *ci) /* {{{ */
1299 {
1300 int tmp;
1302 if ((ci->values_num != 1)
1303 || ((ci->values[0].type != OCONFIG_TYPE_STRING)
1304 && (ci->values[0].type != OCONFIG_TYPE_NUMBER)))
1305 {
1306 ERROR ("cf_util_get_port_number: The \"%s\" option requires "
1307 "exactly one string argument.", ci->key);
1308 return (-1);
1309 }
1311 if (ci->values[0].type == OCONFIG_TYPE_STRING)
1312 return (service_name_to_port_number (ci->values[0].value.string));
1314 assert (ci->values[0].type == OCONFIG_TYPE_NUMBER);
1315 tmp = (int) (ci->values[0].value.number + 0.5);
1316 if ((tmp < 1) || (tmp > 65535))
1317 {
1318 ERROR ("cf_util_get_port_number: The \"%s\" option requires "
1319 "a service name or a port number. The number "
1320 "you specified, %i, is not in the valid "
1321 "range of 1-65535.",
1322 ci->key, tmp);
1323 return (-1);
1324 }
1326 return (tmp);
1327 } /* }}} int cf_util_get_port_number */
1329 int cf_util_get_service (const oconfig_item_t *ci, char **ret_string) /* {{{ */
1330 {
1331 int port;
1332 char *service;
1333 int status;
1335 if (ci->values_num != 1)
1336 {
1337 ERROR ("cf_util_get_service: The %s option requires exactly "
1338 "one argument.", ci->key);
1339 return (-1);
1340 }
1342 if (ci->values[0].type == OCONFIG_TYPE_STRING)
1343 return (cf_util_get_string (ci, ret_string));
1344 if (ci->values[0].type != OCONFIG_TYPE_NUMBER)
1345 {
1346 ERROR ("cf_util_get_service: The %s option requires "
1347 "exactly one string or numeric argument.",
1348 ci->key);
1349 }
1351 port = 0;
1352 status = cf_util_get_int (ci, &port);
1353 if (status != 0)
1354 return (status);
1355 else if ((port < 1) || (port > 65535))
1356 {
1357 ERROR ("cf_util_get_service: The port number given "
1358 "for the %s option is out of "
1359 "range (%i).", ci->key, port);
1360 return (-1);
1361 }
1363 service = malloc (6);
1364 if (service == NULL)
1365 {
1366 ERROR ("cf_util_get_service: Out of memory.");
1367 return (-1);
1368 }
1369 ssnprintf (service, 6, "%i", port);
1371 sfree (*ret_string);
1372 *ret_string = service;
1374 return (0);
1375 } /* }}} int cf_util_get_service */
1377 int cf_util_get_cdtime (const oconfig_item_t *ci, cdtime_t *ret_value) /* {{{ */
1378 {
1379 if ((ci == NULL) || (ret_value == NULL))
1380 return (EINVAL);
1382 if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_NUMBER))
1383 {
1384 ERROR ("cf_util_get_cdtime: The %s option requires "
1385 "exactly one numeric argument.", ci->key);
1386 return (-1);
1387 }
1389 if (ci->values[0].value.number < 0.0)
1390 {
1391 ERROR ("cf_util_get_cdtime: The numeric argument of the %s "
1392 "option must not be negative.", ci->key);
1393 return (-1);
1394 }
1396 *ret_value = DOUBLE_TO_CDTIME_T (ci->values[0].value.number);
1398 return (0);
1399 } /* }}} int cf_util_get_cdtime */