1 /**
2 * collectd - src/configfile.c
3 * Copyright (C) 2005-2011 Florian octo Forster
4 *
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License as published by the
7 * Free Software Foundation; either version 2 of the License, or (at your
8 * option) any later version.
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 * Florian octo Forster <octo at collectd.org>
21 * Sebastian tokkee Harl <sh at tokkee.org>
22 **/
24 #include "collectd.h"
26 #include "liboconfig/oconfig.h"
28 #include "common.h"
29 #include "plugin.h"
30 #include "configfile.h"
31 #include "types_list.h"
32 #include "filter_chain.h"
34 #if HAVE_WORDEXP_H
35 # include <wordexp.h>
36 #endif /* HAVE_WORDEXP_H */
38 #if HAVE_FNMATCH_H
39 # include <fnmatch.h>
40 #endif /* HAVE_FNMATCH_H */
42 #if HAVE_LIBGEN_H
43 # include <libgen.h>
44 #endif /* HAVE_LIBGEN_H */
46 #define ESCAPE_NULL(str) ((str) == NULL ? "(null)" : (str))
48 /*
49 * Private types
50 */
51 typedef struct cf_callback
52 {
53 const char *type;
54 int (*callback) (const char *, const char *);
55 const char **keys;
56 int keys_num;
57 plugin_ctx_t ctx;
58 struct cf_callback *next;
59 } cf_callback_t;
61 typedef struct cf_complex_callback_s
62 {
63 char *type;
64 int (*callback) (oconfig_item_t *);
65 plugin_ctx_t ctx;
66 struct cf_complex_callback_s *next;
67 } cf_complex_callback_t;
69 typedef struct cf_value_map_s
70 {
71 char *key;
72 int (*func) (const oconfig_item_t *);
73 } cf_value_map_t;
75 typedef struct cf_global_option_s
76 {
77 char *key;
78 char *value;
79 char *def;
80 } cf_global_option_t;
82 /*
83 * Prototypes of callback functions
84 */
85 static int dispatch_value_typesdb (const oconfig_item_t *ci);
86 static int dispatch_value_plugindir (const oconfig_item_t *ci);
87 static int dispatch_loadplugin (const oconfig_item_t *ci);
89 /*
90 * Private variables
91 */
92 static cf_callback_t *first_callback = NULL;
93 static cf_complex_callback_t *complex_callback_head = NULL;
95 static cf_value_map_t cf_value_map[] =
96 {
97 {"TypesDB", dispatch_value_typesdb},
98 {"PluginDir", dispatch_value_plugindir},
99 {"LoadPlugin", dispatch_loadplugin}
100 };
101 static int cf_value_map_num = STATIC_ARRAY_SIZE (cf_value_map);
103 static cf_global_option_t cf_global_options[] =
104 {
105 {"BaseDir", NULL, PKGLOCALSTATEDIR},
106 {"PIDFile", NULL, PIDFILE},
107 {"Hostname", NULL, NULL},
108 {"FQDNLookup", NULL, "true"},
109 {"Interval", NULL, NULL},
110 {"ReadThreads", NULL, "5"},
111 {"WriteThreads", NULL, "5"},
112 {"Timeout", NULL, "2"},
113 {"PreCacheChain", NULL, "PreCache"},
114 {"PostCacheChain", NULL, "PostCache"}
115 };
116 static int cf_global_options_num = STATIC_ARRAY_SIZE (cf_global_options);
118 static int cf_default_typesdb = 1;
120 /*
121 * Functions to handle register/unregister, search, and other plugin related
122 * stuff
123 */
124 static cf_callback_t *cf_search (const char *type)
125 {
126 cf_callback_t *cf_cb;
128 if (type == NULL)
129 return (NULL);
131 for (cf_cb = first_callback; cf_cb != NULL; cf_cb = cf_cb->next)
132 if (strcasecmp (cf_cb->type, type) == 0)
133 break;
135 return (cf_cb);
136 }
138 static int cf_dispatch (const char *type, const char *orig_key,
139 const char *orig_value)
140 {
141 cf_callback_t *cf_cb;
142 plugin_ctx_t old_ctx;
143 char *key;
144 char *value;
145 int ret;
146 int i;
148 DEBUG ("type = %s, key = %s, value = %s",
149 ESCAPE_NULL(type),
150 ESCAPE_NULL(orig_key),
151 ESCAPE_NULL(orig_value));
153 if ((cf_cb = cf_search (type)) == NULL)
154 {
155 WARNING ("Found a configuration for the `%s' plugin, but "
156 "the plugin isn't loaded or didn't register "
157 "a configuration callback.", type);
158 return (-1);
159 }
161 if ((key = strdup (orig_key)) == NULL)
162 return (1);
163 if ((value = strdup (orig_value)) == NULL)
164 {
165 free (key);
166 return (2);
167 }
169 ret = -1;
171 old_ctx = plugin_set_ctx (cf_cb->ctx);
173 for (i = 0; i < cf_cb->keys_num; i++)
174 {
175 if ((cf_cb->keys[i] != NULL)
176 && (strcasecmp (cf_cb->keys[i], key) == 0))
177 {
178 ret = (*cf_cb->callback) (key, value);
179 break;
180 }
181 }
183 plugin_set_ctx (old_ctx);
185 if (i >= cf_cb->keys_num)
186 WARNING ("Plugin `%s' did not register for value `%s'.", type, key);
188 free (key);
189 free (value);
191 DEBUG ("cf_dispatch: return (%i)", ret);
193 return (ret);
194 } /* int cf_dispatch */
196 static int dispatch_global_option (const oconfig_item_t *ci)
197 {
198 if (ci->values_num != 1)
199 return (-1);
200 if (ci->values[0].type == OCONFIG_TYPE_STRING)
201 return (global_option_set (ci->key, ci->values[0].value.string));
202 else if (ci->values[0].type == OCONFIG_TYPE_NUMBER)
203 {
204 char tmp[128];
205 ssnprintf (tmp, sizeof (tmp), "%lf", ci->values[0].value.number);
206 return (global_option_set (ci->key, tmp));
207 }
208 else if (ci->values[0].type == OCONFIG_TYPE_BOOLEAN)
209 {
210 if (ci->values[0].value.boolean)
211 return (global_option_set (ci->key, "true"));
212 else
213 return (global_option_set (ci->key, "false"));
214 }
216 return (-1);
217 } /* int dispatch_global_option */
219 static int dispatch_value_typesdb (const oconfig_item_t *ci)
220 {
221 int i = 0;
223 assert (strcasecmp (ci->key, "TypesDB") == 0);
225 cf_default_typesdb = 0;
227 if (ci->values_num < 1) {
228 ERROR ("configfile: `TypesDB' needs at least one argument.");
229 return (-1);
230 }
232 for (i = 0; i < ci->values_num; ++i)
233 {
234 if (OCONFIG_TYPE_STRING != ci->values[i].type) {
235 WARNING ("configfile: TypesDB: Skipping %i. argument which "
236 "is not a string.", i + 1);
237 continue;
238 }
240 read_types_list (ci->values[i].value.string);
241 }
242 return (0);
243 } /* int dispatch_value_typesdb */
245 static int dispatch_value_plugindir (const oconfig_item_t *ci)
246 {
247 assert (strcasecmp (ci->key, "PluginDir") == 0);
249 if (ci->values_num != 1)
250 return (-1);
251 if (ci->values[0].type != OCONFIG_TYPE_STRING)
252 return (-1);
254 plugin_set_dir (ci->values[0].value.string);
255 return (0);
256 }
258 static int dispatch_loadplugin (const oconfig_item_t *ci)
259 {
260 int i;
261 const char *name;
262 unsigned int flags = 0;
263 plugin_ctx_t ctx;
264 plugin_ctx_t old_ctx;
265 int ret_val;
267 assert (strcasecmp (ci->key, "LoadPlugin") == 0);
269 if (ci->values_num != 1)
270 return (-1);
271 if (ci->values[0].type != OCONFIG_TYPE_STRING)
272 return (-1);
274 name = ci->values[0].value.string;
276 /* default to the global interval set before loading this plugin */
277 memset (&ctx, 0, sizeof (ctx));
278 ctx.interval = cf_get_default_interval ();
280 /*
281 * XXX: Magic at work:
282 *
283 * Some of the language bindings, for example the Python and Perl
284 * plugins, need to be able to export symbols to the scripts they run.
285 * For this to happen, the "Globals" flag needs to be set.
286 * Unfortunately, this technical detail is hard to explain to the
287 * average user and she shouldn't have to worry about this, ideally.
288 * So in order to save everyone's sanity use a different default for a
289 * handful of special plugins. --octo
290 */
291 if ((strcasecmp ("Perl", name) == 0)
292 || (strcasecmp ("Python", name) == 0))
293 flags |= PLUGIN_FLAGS_GLOBAL;
295 for (i = 0; i < ci->children_num; ++i) {
296 if (strcasecmp("Globals", ci->children[i].key) == 0)
297 cf_util_get_flag (ci->children + i, &flags, PLUGIN_FLAGS_GLOBAL);
298 else if (strcasecmp ("Interval", ci->children[i].key) == 0) {
299 double interval = 0.0;
301 if (cf_util_get_double (ci->children + i, &interval) != 0) {
302 /* cf_util_get_double will log an error */
303 continue;
304 }
306 ctx.interval = DOUBLE_TO_CDTIME_T (interval);
307 }
308 else {
309 WARNING("Ignoring unknown LoadPlugin option \"%s\" "
310 "for plugin \"%s\"",
311 ci->children[i].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 (const oconfig_item_t *ci)
360 {
361 int ret = -2;
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 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;
397 /* Check for a complex callback first */
398 for (cb = complex_callback_head; cb != NULL; cb = cb->next)
399 {
400 if (strcasecmp (name, cb->type) == 0)
401 {
402 plugin_ctx_t old_ctx;
403 int ret_val;
405 old_ctx = plugin_set_ctx (cb->ctx);
406 ret_val = (cb->callback (ci));
407 plugin_set_ctx (old_ctx);
408 return (ret_val);
409 }
410 }
412 /* Hm, no complex plugin found. Dispatch the values one by one */
413 for (i = 0; i < ci->children_num; i++)
414 {
415 if (ci->children[i].children == NULL)
416 dispatch_value_plugin (name, ci->children + i);
417 else
418 {
419 WARNING ("There is a `%s' block within the "
420 "configuration for the %s plugin. "
421 "The plugin either only expects "
422 "\"simple\" configuration statements "
423 "or wasn't loaded using `LoadPlugin'."
424 " Please check your configuration.",
425 ci->children[i].key, name);
426 }
427 }
429 return (0);
430 }
433 static int dispatch_block (oconfig_item_t *ci)
434 {
435 if (strcasecmp (ci->key, "LoadPlugin") == 0)
436 return (dispatch_loadplugin (ci));
437 else if (strcasecmp (ci->key, "Plugin") == 0)
438 return (dispatch_block_plugin (ci));
439 else if (strcasecmp (ci->key, "Chain") == 0)
440 return (fc_configure (ci));
442 return (0);
443 }
445 static int cf_ci_replace_child (oconfig_item_t *dst, oconfig_item_t *src,
446 int offset)
447 {
448 oconfig_item_t *temp;
449 int i;
451 assert (offset >= 0);
452 assert (dst->children_num > offset);
454 /* Free the memory used by the replaced child. Usually that's the
455 * `Include "blah"' statement. */
456 temp = dst->children + offset;
457 for (i = 0; i < temp->values_num; i++)
458 {
459 if (temp->values[i].type == OCONFIG_TYPE_STRING)
460 {
461 sfree (temp->values[i].value.string);
462 }
463 }
464 sfree (temp->values);
465 temp = NULL;
467 /* If (src->children_num == 0) the array size is decreased. If offset
468 * is _not_ the last element, (offset < (dst->children_num - 1)), then
469 * we need to move the trailing elements before resizing the array. */
470 if ((src->children_num == 0) && (offset < (dst->children_num - 1)))
471 {
472 int nmemb = dst->children_num - (offset + 1);
473 memmove (dst->children + offset, dst->children + offset + 1,
474 sizeof (oconfig_item_t) * nmemb);
475 }
477 /* Resize the memory containing the children to be big enough to hold
478 * all children. */
479 temp = (oconfig_item_t *) realloc (dst->children,
480 sizeof (oconfig_item_t)
481 * (dst->children_num + src->children_num - 1));
482 if (temp == NULL)
483 {
484 ERROR ("configfile: realloc failed.");
485 return (-1);
486 }
487 dst->children = temp;
489 /* If there are children behind the include statement, and they have
490 * not yet been moved because (src->children_num == 0), then move them
491 * to the end of the list, so that the new children have room before
492 * them. */
493 if ((src->children_num > 0)
494 && ((dst->children_num - (offset + 1)) > 0))
495 {
496 int nmemb = dst->children_num - (offset + 1);
497 int old_offset = offset + 1;
498 int new_offset = offset + src->children_num;
500 memmove (dst->children + new_offset,
501 dst->children + old_offset,
502 sizeof (oconfig_item_t) * nmemb);
503 }
505 /* Last but not least: If there are new children, copy them to the
506 * memory reserved for them. */
507 if (src->children_num > 0)
508 {
509 memcpy (dst->children + offset,
510 src->children,
511 sizeof (oconfig_item_t) * src->children_num);
512 }
514 /* Update the number of children. */
515 dst->children_num += (src->children_num - 1);
517 return (0);
518 } /* int cf_ci_replace_child */
520 static int cf_ci_append_children (oconfig_item_t *dst, oconfig_item_t *src)
521 {
522 oconfig_item_t *temp;
524 if ((src == NULL) || (src->children_num == 0))
525 return (0);
527 temp = (oconfig_item_t *) realloc (dst->children,
528 sizeof (oconfig_item_t)
529 * (dst->children_num + src->children_num));
530 if (temp == NULL)
531 {
532 ERROR ("configfile: realloc failed.");
533 return (-1);
534 }
535 dst->children = temp;
537 memcpy (dst->children + dst->children_num,
538 src->children,
539 sizeof (oconfig_item_t)
540 * src->children_num);
541 dst->children_num += src->children_num;
543 return (0);
544 } /* int cf_ci_append_children */
546 #define CF_MAX_DEPTH 8
547 static oconfig_item_t *cf_read_generic (const char *path,
548 const char *pattern, int depth);
550 static int cf_include_all (oconfig_item_t *root, int depth)
551 {
552 int i;
554 for (i = 0; i < root->children_num; i++)
555 {
556 oconfig_item_t *new;
557 oconfig_item_t *old;
559 char *pattern = NULL;
561 int j;
563 if (strcasecmp (root->children[i].key, "Include") != 0)
564 continue;
566 old = root->children + i;
568 if ((old->values_num != 1)
569 || (old->values[0].type != OCONFIG_TYPE_STRING))
570 {
571 ERROR ("configfile: `Include' needs exactly one string argument.");
572 continue;
573 }
575 for (j = 0; j < old->children_num; ++j)
576 {
577 oconfig_item_t *child = old->children + j;
579 if (strcasecmp (child->key, "Filter") == 0)
580 cf_util_get_string (child, &pattern);
581 else
582 ERROR ("configfile: Option `%s' not allowed in <Include> block.",
583 child->key);
584 }
586 new = cf_read_generic (old->values[0].value.string, pattern, depth + 1);
587 sfree (pattern);
589 if (new == NULL)
590 return (-1);
592 /* Now replace the i'th child in `root' with `new'. */
593 cf_ci_replace_child (root, new, i);
595 /* ... and go back to the new i'th child. */
596 --i;
598 sfree (new->values);
599 sfree (new);
600 } /* for (i = 0; i < root->children_num; i++) */
602 return (0);
603 } /* int cf_include_all */
605 static oconfig_item_t *cf_read_file (const char *file,
606 const char *pattern, int depth)
607 {
608 oconfig_item_t *root;
609 int status;
611 assert (depth < CF_MAX_DEPTH);
613 if (pattern != NULL) {
614 #if HAVE_FNMATCH_H && HAVE_LIBGEN_H
615 char *tmp = sstrdup (file);
616 char *filename = basename (tmp);
618 if ((filename != NULL) && (fnmatch (pattern, filename, 0) != 0)) {
619 DEBUG ("configfile: Not including `%s' because it "
620 "does not match pattern `%s'.",
621 filename, pattern);
622 free (tmp);
623 return (NULL);
624 }
626 free (tmp);
627 #else
628 ERROR ("configfile: Cannot apply pattern filter '%s' "
629 "to file '%s': functions basename() and / or "
630 "fnmatch() not available.", pattern, file);
631 #endif /* HAVE_FNMATCH_H && HAVE_LIBGEN_H */
632 }
634 root = oconfig_parse_file (file);
635 if (root == NULL)
636 {
637 ERROR ("configfile: Cannot read file `%s'.", file);
638 return (NULL);
639 }
641 status = cf_include_all (root, depth);
642 if (status != 0)
643 {
644 oconfig_free (root);
645 return (NULL);
646 }
648 return (root);
649 } /* oconfig_item_t *cf_read_file */
651 static int cf_compare_string (const void *p1, const void *p2)
652 {
653 return strcmp (*(const char **) p1, *(const char **) p2);
654 }
656 static oconfig_item_t *cf_read_dir (const char *dir,
657 const char *pattern, int depth)
658 {
659 oconfig_item_t *root = NULL;
660 DIR *dh;
661 struct dirent *de;
662 char **filenames = NULL;
663 int filenames_num = 0;
664 int status;
665 int i;
667 assert (depth < CF_MAX_DEPTH);
669 dh = opendir (dir);
670 if (dh == NULL)
671 {
672 char errbuf[1024];
673 ERROR ("configfile: opendir failed: %s",
674 sstrerror (errno, errbuf, sizeof (errbuf)));
675 return (NULL);
676 }
678 root = (oconfig_item_t *) malloc (sizeof (oconfig_item_t));
679 if (root == NULL)
680 {
681 ERROR ("configfile: malloc failed.");
682 return (NULL);
683 }
684 memset (root, 0, sizeof (oconfig_item_t));
686 while ((de = readdir (dh)) != NULL)
687 {
688 char name[1024];
689 char **tmp;
691 if ((de->d_name[0] == '.') || (de->d_name[0] == 0))
692 continue;
694 status = ssnprintf (name, sizeof (name), "%s/%s",
695 dir, de->d_name);
696 if ((status < 0) || ((size_t) status >= sizeof (name)))
697 {
698 ERROR ("configfile: Not including `%s/%s' because its"
699 " name is too long.",
700 dir, de->d_name);
701 for (i = 0; i < filenames_num; ++i)
702 free (filenames[i]);
703 free (filenames);
704 free (root);
705 return (NULL);
706 }
708 ++filenames_num;
709 tmp = (char **) realloc (filenames,
710 filenames_num * sizeof (*filenames));
711 if (tmp == NULL) {
712 ERROR ("configfile: realloc failed.");
713 for (i = 0; i < filenames_num - 1; ++i)
714 free (filenames[i]);
715 free (filenames);
716 free (root);
717 return (NULL);
718 }
719 filenames = tmp;
721 filenames[filenames_num - 1] = sstrdup (name);
722 }
724 qsort ((void *) filenames, filenames_num, sizeof (*filenames),
725 cf_compare_string);
727 for (i = 0; i < filenames_num; ++i)
728 {
729 oconfig_item_t *temp;
730 char *name = filenames[i];
732 temp = cf_read_generic (name, pattern, depth);
733 if (temp == NULL)
734 {
735 /* An error should already have been reported. */
736 sfree (name);
737 continue;
738 }
740 cf_ci_append_children (root, temp);
741 sfree (temp->children);
742 sfree (temp);
744 free (name);
745 }
747 free(filenames);
748 return (root);
749 } /* oconfig_item_t *cf_read_dir */
751 /*
752 * cf_read_generic
753 *
754 * Path is stat'ed and either cf_read_file or cf_read_dir is called
755 * accordingly.
756 *
757 * There are two versions of this function: If `wordexp' exists shell wildcards
758 * will be expanded and the function will include all matches found. If
759 * `wordexp' (or, more precisely, it's header file) is not available the
760 * simpler function is used which does not do any such expansion.
761 */
762 #if HAVE_WORDEXP_H
763 static oconfig_item_t *cf_read_generic (const char *path,
764 const char *pattern, int depth)
765 {
766 oconfig_item_t *root = NULL;
767 int status;
768 const char *path_ptr;
769 wordexp_t we;
770 size_t i;
772 if (depth >= CF_MAX_DEPTH)
773 {
774 ERROR ("configfile: Not including `%s' because the maximum "
775 "nesting depth has been reached.", path);
776 return (NULL);
777 }
779 status = wordexp (path, &we, WRDE_NOCMD);
780 if (status != 0)
781 {
782 ERROR ("configfile: wordexp (%s) failed.", path);
783 return (NULL);
784 }
786 root = (oconfig_item_t *) malloc (sizeof (oconfig_item_t));
787 if (root == NULL)
788 {
789 ERROR ("configfile: malloc failed.");
790 return (NULL);
791 }
792 memset (root, '\0', sizeof (oconfig_item_t));
794 /* wordexp() might return a sorted list already. That's not
795 * documented though, so let's make sure we get what we want. */
796 qsort ((void *) we.we_wordv, we.we_wordc, sizeof (*we.we_wordv),
797 cf_compare_string);
799 for (i = 0; i < we.we_wordc; i++)
800 {
801 oconfig_item_t *temp;
802 struct stat statbuf;
804 path_ptr = we.we_wordv[i];
806 status = stat (path_ptr, &statbuf);
807 if (status != 0)
808 {
809 char errbuf[1024];
810 WARNING ("configfile: stat (%s) failed: %s",
811 path_ptr,
812 sstrerror (errno, errbuf, sizeof (errbuf)));
813 continue;
814 }
816 if (S_ISREG (statbuf.st_mode))
817 temp = cf_read_file (path_ptr, pattern, depth);
818 else if (S_ISDIR (statbuf.st_mode))
819 temp = cf_read_dir (path_ptr, pattern, depth);
820 else
821 {
822 WARNING ("configfile: %s is neither a file nor a "
823 "directory.", path);
824 continue;
825 }
827 if (temp == NULL) {
828 oconfig_free (root);
829 return (NULL);
830 }
832 cf_ci_append_children (root, temp);
833 sfree (temp->children);
834 sfree (temp);
835 }
837 wordfree (&we);
839 return (root);
840 } /* oconfig_item_t *cf_read_generic */
841 /* #endif HAVE_WORDEXP_H */
843 #else /* if !HAVE_WORDEXP_H */
844 static oconfig_item_t *cf_read_generic (const char *path,
845 const char *pattern, int depth)
846 {
847 struct stat statbuf;
848 int status;
850 if (depth >= CF_MAX_DEPTH)
851 {
852 ERROR ("configfile: Not including `%s' because the maximum "
853 "nesting depth has been reached.", path);
854 return (NULL);
855 }
857 status = stat (path, &statbuf);
858 if (status != 0)
859 {
860 char errbuf[1024];
861 ERROR ("configfile: stat (%s) failed: %s",
862 path,
863 sstrerror (errno, errbuf, sizeof (errbuf)));
864 return (NULL);
865 }
867 if (S_ISREG (statbuf.st_mode))
868 return (cf_read_file (path, pattern, depth));
869 else if (S_ISDIR (statbuf.st_mode))
870 return (cf_read_dir (path, pattern, depth));
872 ERROR ("configfile: %s is neither a file nor a directory.", path);
873 return (NULL);
874 } /* oconfig_item_t *cf_read_generic */
875 #endif /* !HAVE_WORDEXP_H */
877 /*
878 * Public functions
879 */
880 int global_option_set (const char *option, const char *value)
881 {
882 int i;
884 DEBUG ("option = %s; value = %s;", option, value);
886 for (i = 0; i < cf_global_options_num; i++)
887 if (strcasecmp (cf_global_options[i].key, option) == 0)
888 break;
890 if (i >= cf_global_options_num)
891 return (-1);
893 sfree (cf_global_options[i].value);
895 if (value != NULL)
896 cf_global_options[i].value = strdup (value);
897 else
898 cf_global_options[i].value = NULL;
900 return (0);
901 }
903 const char *global_option_get (const char *option)
904 {
905 int i;
907 for (i = 0; i < cf_global_options_num; i++)
908 if (strcasecmp (cf_global_options[i].key, option) == 0)
909 break;
911 if (i >= cf_global_options_num)
912 return (NULL);
914 return ((cf_global_options[i].value != NULL)
915 ? cf_global_options[i].value
916 : cf_global_options[i].def);
917 } /* char *global_option_get */
919 cdtime_t cf_get_default_interval (void)
920 {
921 char const *str = global_option_get ("Interval");
922 double interval_double = COLLECTD_DEFAULT_INTERVAL;
924 if (str != NULL)
925 {
926 char *endptr = NULL;
927 double tmp = strtod (str, &endptr);
929 if ((endptr == NULL) || (endptr == str) || (*endptr != 0))
930 ERROR ("cf_get_default_interval: Unable to parse string \"%s\" "
931 "as number.", str);
932 else if (tmp <= 0.0)
933 ERROR ("cf_get_default_interval: Interval must be a positive number. "
934 "The current number is %g.", tmp);
935 else
936 interval_double = tmp;
937 }
939 return (DOUBLE_TO_CDTIME_T (interval_double));
940 } /* }}} cdtime_t cf_get_default_interval */
942 void cf_unregister (const char *type)
943 {
944 cf_callback_t *this, *prev;
946 for (prev = NULL, this = first_callback;
947 this != NULL;
948 prev = this, this = this->next)
949 if (strcasecmp (this->type, type) == 0)
950 {
951 if (prev == NULL)
952 first_callback = this->next;
953 else
954 prev->next = this->next;
956 free (this);
957 break;
958 }
959 } /* void cf_unregister */
961 void cf_unregister_complex (const char *type)
962 {
963 cf_complex_callback_t *this, *prev;
965 for (prev = NULL, this = complex_callback_head;
966 this != NULL;
967 prev = this, this = this->next)
968 if (strcasecmp (this->type, type) == 0)
969 {
970 if (prev == NULL)
971 complex_callback_head = this->next;
972 else
973 prev->next = this->next;
975 sfree (this->type);
976 sfree (this);
977 break;
978 }
979 } /* void cf_unregister */
981 void cf_register (const char *type,
982 int (*callback) (const char *, const char *),
983 const char **keys, int keys_num)
984 {
985 cf_callback_t *cf_cb;
987 /* Remove this module from the list, if it already exists */
988 cf_unregister (type);
990 /* This pointer will be free'd in `cf_unregister' */
991 if ((cf_cb = (cf_callback_t *) malloc (sizeof (cf_callback_t))) == NULL)
992 return;
994 cf_cb->type = type;
995 cf_cb->callback = callback;
996 cf_cb->keys = keys;
997 cf_cb->keys_num = keys_num;
998 cf_cb->ctx = plugin_get_ctx ();
1000 cf_cb->next = first_callback;
1001 first_callback = cf_cb;
1002 } /* void cf_register */
1004 int cf_register_complex (const char *type, int (*callback) (oconfig_item_t *))
1005 {
1006 cf_complex_callback_t *new;
1008 new = (cf_complex_callback_t *) malloc (sizeof (cf_complex_callback_t));
1009 if (new == NULL)
1010 return (-1);
1012 new->type = strdup (type);
1013 if (new->type == NULL)
1014 {
1015 sfree (new);
1016 return (-1);
1017 }
1019 new->callback = callback;
1020 new->next = NULL;
1022 new->ctx = plugin_get_ctx ();
1024 if (complex_callback_head == NULL)
1025 {
1026 complex_callback_head = new;
1027 }
1028 else
1029 {
1030 cf_complex_callback_t *last = complex_callback_head;
1031 while (last->next != NULL)
1032 last = last->next;
1033 last->next = new;
1034 }
1036 return (0);
1037 } /* int cf_register_complex */
1039 int cf_read (char *filename)
1040 {
1041 oconfig_item_t *conf;
1042 int i;
1044 conf = cf_read_generic (filename, /* pattern = */ NULL, /* depth = */ 0);
1045 if (conf == NULL)
1046 {
1047 ERROR ("Unable to read config file %s.", filename);
1048 return (-1);
1049 }
1050 else if (conf->children_num == 0)
1051 {
1052 ERROR ("Configuration file %s is empty.", filename);
1053 oconfig_free (conf);
1054 return (-1);
1055 }
1057 for (i = 0; i < conf->children_num; i++)
1058 {
1059 if (conf->children[i].children == NULL)
1060 dispatch_value (conf->children + i);
1061 else
1062 dispatch_block (conf->children + i);
1063 }
1065 oconfig_free (conf);
1067 /* Read the default types.db if no `TypesDB' option was given. */
1068 if (cf_default_typesdb)
1069 read_types_list (PKGDATADIR"/types.db");
1071 return (0);
1072 } /* int cf_read */
1074 /* Assures the config option is a string, duplicates it and returns the copy in
1075 * "ret_string". If necessary "*ret_string" is freed first. Returns zero upon
1076 * success. */
1077 int cf_util_get_string (const oconfig_item_t *ci, char **ret_string) /* {{{ */
1078 {
1079 char *string;
1081 if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_STRING))
1082 {
1083 ERROR ("cf_util_get_string: The %s option requires "
1084 "exactly one string argument.", ci->key);
1085 return (-1);
1086 }
1088 string = strdup (ci->values[0].value.string);
1089 if (string == NULL)
1090 return (-1);
1092 if (*ret_string != NULL)
1093 sfree (*ret_string);
1094 *ret_string = string;
1096 return (0);
1097 } /* }}} int cf_util_get_string */
1099 /* Assures the config option is a string and copies it to the provided buffer.
1100 * Assures null-termination. */
1101 int cf_util_get_string_buffer (const oconfig_item_t *ci, char *buffer, /* {{{ */
1102 size_t buffer_size)
1103 {
1104 if ((ci == NULL) || (buffer == NULL) || (buffer_size < 1))
1105 return (EINVAL);
1107 if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_STRING))
1108 {
1109 ERROR ("cf_util_get_string_buffer: The %s option requires "
1110 "exactly one string argument.", ci->key);
1111 return (-1);
1112 }
1114 strncpy (buffer, ci->values[0].value.string, buffer_size);
1115 buffer[buffer_size - 1] = 0;
1117 return (0);
1118 } /* }}} int cf_util_get_string_buffer */
1120 /* Assures the config option is a number and returns it as an int. */
1121 int cf_util_get_int (const oconfig_item_t *ci, int *ret_value) /* {{{ */
1122 {
1123 if ((ci == NULL) || (ret_value == NULL))
1124 return (EINVAL);
1126 if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_NUMBER))
1127 {
1128 ERROR ("cf_util_get_int: The %s option requires "
1129 "exactly one numeric argument.", ci->key);
1130 return (-1);
1131 }
1133 *ret_value = (int) ci->values[0].value.number;
1135 return (0);
1136 } /* }}} int cf_util_get_int */
1138 int cf_util_get_double (const oconfig_item_t *ci, double *ret_value) /* {{{ */
1139 {
1140 if ((ci == NULL) || (ret_value == NULL))
1141 return (EINVAL);
1143 if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_NUMBER))
1144 {
1145 ERROR ("cf_util_get_double: The %s option requires "
1146 "exactly one numeric argument.", ci->key);
1147 return (-1);
1148 }
1150 *ret_value = ci->values[0].value.number;
1152 return (0);
1153 } /* }}} int cf_util_get_double */
1155 int cf_util_get_boolean (const oconfig_item_t *ci, _Bool *ret_bool) /* {{{ */
1156 {
1157 if ((ci == NULL) || (ret_bool == NULL))
1158 return (EINVAL);
1160 if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_BOOLEAN))
1161 {
1162 ERROR ("cf_util_get_boolean: The %s option requires "
1163 "exactly one boolean argument.", ci->key);
1164 return (-1);
1165 }
1167 *ret_bool = ci->values[0].value.boolean ? 1 : 0;
1169 return (0);
1170 } /* }}} int cf_util_get_boolean */
1172 int cf_util_get_flag (const oconfig_item_t *ci, /* {{{ */
1173 unsigned int *ret_value, unsigned int flag)
1174 {
1175 int status;
1176 _Bool b;
1178 if (ret_value == NULL)
1179 return (EINVAL);
1181 b = 0;
1182 status = cf_util_get_boolean (ci, &b);
1183 if (status != 0)
1184 return (status);
1186 if (b)
1187 {
1188 *ret_value |= flag;
1189 }
1190 else
1191 {
1192 *ret_value &= ~flag;
1193 }
1195 return (0);
1196 } /* }}} int cf_util_get_flag */
1198 /* Assures that the config option is a string or a number if the correct range
1199 * of 1-65535. The string is then converted to a port number using
1200 * `service_name_to_port_number' and returned.
1201 * Returns the port number in the range [1-65535] or less than zero upon
1202 * failure. */
1203 int cf_util_get_port_number (const oconfig_item_t *ci) /* {{{ */
1204 {
1205 int tmp;
1207 if ((ci->values_num != 1)
1208 || ((ci->values[0].type != OCONFIG_TYPE_STRING)
1209 && (ci->values[0].type != OCONFIG_TYPE_NUMBER)))
1210 {
1211 ERROR ("cf_util_get_port_number: The \"%s\" option requires "
1212 "exactly one string argument.", ci->key);
1213 return (-1);
1214 }
1216 if (ci->values[0].type == OCONFIG_TYPE_STRING)
1217 return (service_name_to_port_number (ci->values[0].value.string));
1219 assert (ci->values[0].type == OCONFIG_TYPE_NUMBER);
1220 tmp = (int) (ci->values[0].value.number + 0.5);
1221 if ((tmp < 1) || (tmp > 65535))
1222 {
1223 ERROR ("cf_util_get_port_number: The \"%s\" option requires "
1224 "a service name or a port number. The number "
1225 "you specified, %i, is not in the valid "
1226 "range of 1-65535.",
1227 ci->key, tmp);
1228 return (-1);
1229 }
1231 return (tmp);
1232 } /* }}} int cf_util_get_port_number */
1234 int cf_util_get_service (const oconfig_item_t *ci, char **ret_string) /* {{{ */
1235 {
1236 int port;
1237 char *service;
1238 int status;
1240 if (ci->values_num != 1)
1241 {
1242 ERROR ("cf_util_get_service: The %s option requires exactly "
1243 "one argument.", ci->key);
1244 return (-1);
1245 }
1247 if (ci->values[0].type == OCONFIG_TYPE_STRING)
1248 return (cf_util_get_string (ci, ret_string));
1249 if (ci->values[0].type != OCONFIG_TYPE_NUMBER)
1250 {
1251 ERROR ("cf_util_get_service: The %s option requires "
1252 "exactly one string or numeric argument.",
1253 ci->key);
1254 }
1256 port = 0;
1257 status = cf_util_get_int (ci, &port);
1258 if (status != 0)
1259 return (status);
1260 else if ((port < 1) || (port > 65535))
1261 {
1262 ERROR ("cf_util_get_service: The port number given "
1263 "for the %s option is out of "
1264 "range (%i).", ci->key, port);
1265 return (-1);
1266 }
1268 service = malloc (6);
1269 if (service == NULL)
1270 {
1271 ERROR ("cf_util_get_service: Out of memory.");
1272 return (-1);
1273 }
1274 ssnprintf (service, 6, "%i", port);
1276 sfree (*ret_string);
1277 *ret_string = service;
1279 return (0);
1280 } /* }}} int cf_util_get_service */
1282 int cf_util_get_cdtime (const oconfig_item_t *ci, cdtime_t *ret_value) /* {{{ */
1283 {
1284 if ((ci == NULL) || (ret_value == NULL))
1285 return (EINVAL);
1287 if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_NUMBER))
1288 {
1289 ERROR ("cf_util_get_cdtime: The %s option requires "
1290 "exactly one numeric argument.", ci->key);
1291 return (-1);
1292 }
1294 if (ci->values[0].value.number < 0.0)
1295 {
1296 ERROR ("cf_util_get_cdtime: The numeric argument of the %s "
1297 "option must not be negative.", ci->key);
1298 return (-1);
1299 }
1301 *ret_value = DOUBLE_TO_CDTIME_T (ci->values[0].value.number);
1303 return (0);
1304 } /* }}} int cf_util_get_cdtime */