config: Implemented include filter pattern as sub-option in a <Include> block.
[collectd.git] / src / configfile.c
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  **/
23
24 #include "collectd.h"
25
26 #include "liboconfig/oconfig.h"
27
28 #include "common.h"
29 #include "plugin.h"
30 #include "configfile.h"
31 #include "types_list.h"
32 #include "filter_chain.h"
33
34 #if HAVE_WORDEXP_H
35 # include <wordexp.h>
36 #endif /* HAVE_WORDEXP_H */
37
38 #if HAVE_FNMATCH_H
39 # include <fnmatch.h>
40 #endif /* HAVE_FNMATCH_H */
41
42 #if HAVE_LIBGEN_H
43 # include <libgen.h>
44 #endif /* HAVE_LIBGEN_H */
45
46 #define ESCAPE_NULL(str) ((str) == NULL ? "(null)" : (str))
47
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;
60
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;
68
69 typedef struct cf_value_map_s
70 {
71         char *key;
72         int (*func) (const oconfig_item_t *);
73 } cf_value_map_t;
74
75 typedef struct cf_global_option_s
76 {
77         char *key;
78         char *value;
79         char *def;
80 } cf_global_option_t;
81
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);
88
89 /*
90  * Private variables
91  */
92 static cf_callback_t *first_callback = NULL;
93 static cf_complex_callback_t *complex_callback_head = NULL;
94
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_LEN (cf_value_map);
102
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         {"Timeout",     NULL, "2"},
112         {"PreCacheChain",  NULL, "PreCache"},
113         {"PostCacheChain", NULL, "PostCache"}
114 };
115 static int cf_global_options_num = STATIC_ARRAY_LEN (cf_global_options);
116
117 static int cf_default_typesdb = 1;
118
119 /*
120  * Functions to handle register/unregister, search, and other plugin related
121  * stuff
122  */
123 static cf_callback_t *cf_search (const char *type)
124 {
125         cf_callback_t *cf_cb;
126
127         if (type == NULL)
128                 return (NULL);
129
130         for (cf_cb = first_callback; cf_cb != NULL; cf_cb = cf_cb->next)
131                 if (strcasecmp (cf_cb->type, type) == 0)
132                         break;
133
134         return (cf_cb);
135 }
136
137 static int cf_dispatch (const char *type, const char *orig_key,
138                 const char *orig_value)
139 {
140         cf_callback_t *cf_cb;
141         plugin_ctx_t old_ctx;
142         char *key;
143         char *value;
144         int ret;
145         int i;
146
147         DEBUG ("type = %s, key = %s, value = %s",
148                         ESCAPE_NULL(type),
149                         ESCAPE_NULL(orig_key),
150                         ESCAPE_NULL(orig_value));
151
152         if ((cf_cb = cf_search (type)) == NULL)
153         {
154                 WARNING ("Found a configuration for the `%s' plugin, but "
155                                 "the plugin isn't loaded or didn't register "
156                                 "a configuration callback.", type);
157                 return (-1);
158         }
159
160         if ((key = strdup (orig_key)) == NULL)
161                 return (1);
162         if ((value = strdup (orig_value)) == NULL)
163         {
164                 free (key);
165                 return (2);
166         }
167
168         ret = -1;
169
170         old_ctx = plugin_set_ctx (cf_cb->ctx);
171
172         for (i = 0; i < cf_cb->keys_num; i++)
173         {
174                 if ((cf_cb->keys[i] != NULL)
175                                 && (strcasecmp (cf_cb->keys[i], key) == 0))
176                 {
177                         ret = (*cf_cb->callback) (key, value);
178                         break;
179                 }
180         }
181
182         plugin_set_ctx (old_ctx);
183
184         if (i >= cf_cb->keys_num)
185                 WARNING ("Plugin `%s' did not register for value `%s'.", type, key);
186
187         free (key);
188         free (value);
189
190         DEBUG ("cf_dispatch: return (%i)", ret);
191
192         return (ret);
193 } /* int cf_dispatch */
194
195 static int dispatch_global_option (const oconfig_item_t *ci)
196 {
197         if (ci->values_num != 1)
198                 return (-1);
199         if (ci->values[0].type == OCONFIG_TYPE_STRING)
200                 return (global_option_set (ci->key, ci->values[0].value.string));
201         else if (ci->values[0].type == OCONFIG_TYPE_NUMBER)
202         {
203                 char tmp[128];
204                 ssnprintf (tmp, sizeof (tmp), "%lf", ci->values[0].value.number);
205                 return (global_option_set (ci->key, tmp));
206         }
207         else if (ci->values[0].type == OCONFIG_TYPE_BOOLEAN)
208         {
209                 if (ci->values[0].value.boolean)
210                         return (global_option_set (ci->key, "true"));
211                 else
212                         return (global_option_set (ci->key, "false"));
213         }
214
215         return (-1);
216 } /* int dispatch_global_option */
217
218 static int dispatch_value_typesdb (const oconfig_item_t *ci)
219 {
220         int i = 0;
221
222         assert (strcasecmp (ci->key, "TypesDB") == 0);
223
224         cf_default_typesdb = 0;
225
226         if (ci->values_num < 1) {
227                 ERROR ("configfile: `TypesDB' needs at least one argument.");
228                 return (-1);
229         }
230
231         for (i = 0; i < ci->values_num; ++i)
232         {
233                 if (OCONFIG_TYPE_STRING != ci->values[i].type) {
234                         WARNING ("configfile: TypesDB: Skipping %i. argument which "
235                                         "is not a string.", i + 1);
236                         continue;
237                 }
238
239                 read_types_list (ci->values[i].value.string);
240         }
241         return (0);
242 } /* int dispatch_value_typesdb */
243
244 static int dispatch_value_plugindir (const oconfig_item_t *ci)
245 {
246         assert (strcasecmp (ci->key, "PluginDir") == 0);
247         
248         if (ci->values_num != 1)
249                 return (-1);
250         if (ci->values[0].type != OCONFIG_TYPE_STRING)
251                 return (-1);
252
253         plugin_set_dir (ci->values[0].value.string);
254         return (0);
255 }
256
257 static int dispatch_loadplugin (const oconfig_item_t *ci)
258 {
259         int i;
260         const char *name;
261         unsigned int flags = 0;
262         plugin_ctx_t ctx;
263         plugin_ctx_t old_ctx;
264         int ret_val;
265
266         assert (strcasecmp (ci->key, "LoadPlugin") == 0);
267
268         if (ci->values_num != 1)
269                 return (-1);
270         if (ci->values[0].type != OCONFIG_TYPE_STRING)
271                 return (-1);
272
273         name = ci->values[0].value.string;
274
275         /* default to the global interval set before loading this plugin */
276         memset (&ctx, 0, sizeof (ctx));
277         ctx.interval = cf_get_default_interval ();
278
279         /*
280          * XXX: Magic at work:
281          *
282          * Some of the language bindings, for example the Python and Perl
283          * plugins, need to be able to export symbols to the scripts they run.
284          * For this to happen, the "Globals" flag needs to be set.
285          * Unfortunately, this technical detail is hard to explain to the
286          * average user and she shouldn't have to worry about this, ideally.
287          * So in order to save everyone's sanity use a different default for a
288          * handful of special plugins. --octo
289          */
290         if ((strcasecmp ("Perl", name) == 0)
291                         || (strcasecmp ("Python", name) == 0))
292                 flags |= PLUGIN_FLAGS_GLOBAL;
293
294         for (i = 0; i < ci->children_num; ++i) {
295                 if (strcasecmp("Globals", ci->children[i].key) == 0)
296                         cf_util_get_flag (ci->children + i, &flags, PLUGIN_FLAGS_GLOBAL);
297                 else if (strcasecmp ("Interval", ci->children[i].key) == 0) {
298                         double interval = 0.0;
299
300                         if (cf_util_get_double (ci->children + i, &interval) != 0) {
301                                 /* cf_util_get_double will log an error */
302                                 continue;
303                         }
304
305                         ctx.interval = DOUBLE_TO_CDTIME_T (interval);
306                 }
307                 else {
308                         WARNING("Ignoring unknown LoadPlugin option \"%s\" "
309                                         "for plugin \"%s\"",
310                                         ci->children[i].key, ci->values[0].value.string);
311                 }
312         }
313
314         old_ctx = plugin_set_ctx (ctx);
315         ret_val = plugin_load (name, (uint32_t) flags);
316         /* reset to the "global" context */
317         plugin_set_ctx (old_ctx);
318
319         return (ret_val);
320 } /* int dispatch_value_loadplugin */
321
322 static int dispatch_value_plugin (const char *plugin, oconfig_item_t *ci)
323 {
324         char  buffer[4096];
325         char *buffer_ptr;
326         int   buffer_free;
327         int i;
328
329         buffer_ptr = buffer;
330         buffer_free = sizeof (buffer);
331
332         for (i = 0; i < ci->values_num; i++)
333         {
334                 int status = -1;
335
336                 if (ci->values[i].type == OCONFIG_TYPE_STRING)
337                         status = ssnprintf (buffer_ptr, buffer_free, " %s",
338                                         ci->values[i].value.string);
339                 else if (ci->values[i].type == OCONFIG_TYPE_NUMBER)
340                         status = ssnprintf (buffer_ptr, buffer_free, " %lf",
341                                         ci->values[i].value.number);
342                 else if (ci->values[i].type == OCONFIG_TYPE_BOOLEAN)
343                         status = ssnprintf (buffer_ptr, buffer_free, " %s",
344                                         ci->values[i].value.boolean
345                                         ? "true" : "false");
346
347                 if ((status < 0) || (status >= buffer_free))
348                         return (-1);
349                 buffer_free -= status;
350                 buffer_ptr  += status;
351         }
352         /* skip the initial space */
353         buffer_ptr = buffer + 1;
354
355         return (cf_dispatch (plugin, ci->key, buffer_ptr));
356 } /* int dispatch_value_plugin */
357
358 static int dispatch_value (const oconfig_item_t *ci)
359 {
360         int ret = -2;
361         int i;
362
363         for (i = 0; i < cf_value_map_num; i++)
364                 if (strcasecmp (cf_value_map[i].key, ci->key) == 0)
365                 {
366                         ret = cf_value_map[i].func (ci);
367                         break;
368                 }
369
370         for (i = 0; i < cf_global_options_num; i++)
371                 if (strcasecmp (cf_global_options[i].key, ci->key) == 0)
372                 {
373                         ret = dispatch_global_option (ci);
374                         break;
375                 }
376
377         return (ret);
378 } /* int dispatch_value */
379
380 static int dispatch_block_plugin (oconfig_item_t *ci)
381 {
382         int i;
383         char *name;
384
385         cf_complex_callback_t *cb;
386
387         if (strcasecmp (ci->key, "Plugin") != 0)
388                 return (-1);
389         if (ci->values_num < 1)
390                 return (-1);
391         if (ci->values[0].type != OCONFIG_TYPE_STRING)
392                 return (-1);
393
394         name = ci->values[0].value.string;
395
396         /* Check for a complex callback first */
397         for (cb = complex_callback_head; cb != NULL; cb = cb->next)
398         {
399                 if (strcasecmp (name, cb->type) == 0)
400                 {
401                         plugin_ctx_t old_ctx;
402                         int ret_val;
403
404                         old_ctx = plugin_set_ctx (cb->ctx);
405                         ret_val = (cb->callback (ci));
406                         plugin_set_ctx (old_ctx);
407                         return (ret_val);
408                 }
409         }
410
411         /* Hm, no complex plugin found. Dispatch the values one by one */
412         for (i = 0; i < ci->children_num; i++)
413         {
414                 if (ci->children[i].children == NULL)
415                         dispatch_value_plugin (name, ci->children + i);
416                 else
417                 {
418                         WARNING ("There is a `%s' block within the "
419                                         "configuration for the %s plugin. "
420                                         "The plugin either only expects "
421                                         "\"simple\" configuration statements "
422                                         "or wasn't loaded using `LoadPlugin'."
423                                         " Please check your configuration.",
424                                         ci->children[i].key, name);
425                 }
426         }
427
428         return (0);
429 }
430
431
432 static int dispatch_block (oconfig_item_t *ci)
433 {
434         if (strcasecmp (ci->key, "LoadPlugin") == 0)
435                 return (dispatch_loadplugin (ci));
436         else if (strcasecmp (ci->key, "Plugin") == 0)
437                 return (dispatch_block_plugin (ci));
438         else if (strcasecmp (ci->key, "Chain") == 0)
439                 return (fc_configure (ci));
440
441         return (0);
442 }
443
444 static int cf_ci_replace_child (oconfig_item_t *dst, oconfig_item_t *src,
445                 int offset)
446 {
447         oconfig_item_t *temp;
448         int i;
449
450         assert (offset >= 0);
451         assert (dst->children_num > offset);
452
453         /* Free the memory used by the replaced child. Usually that's the
454          * `Include "blah"' statement. */
455         temp = dst->children + offset;
456         for (i = 0; i < temp->values_num; i++)
457         {
458                 if (temp->values[i].type == OCONFIG_TYPE_STRING)
459                 {
460                         sfree (temp->values[i].value.string);
461                 }
462         }
463         sfree (temp->values);
464         temp = NULL;
465
466         /* If (src->children_num == 0) the array size is decreased. If offset
467          * is _not_ the last element, (offset < (dst->children_num - 1)), then
468          * we need to move the trailing elements before resizing the array. */
469         if ((src->children_num == 0) && (offset < (dst->children_num - 1)))
470         {
471                 int nmemb = dst->children_num - (offset + 1);
472                 memmove (dst->children + offset, dst->children + offset + 1,
473                                 sizeof (oconfig_item_t) * nmemb);
474         }
475
476         /* Resize the memory containing the children to be big enough to hold
477          * all children. */
478         temp = (oconfig_item_t *) realloc (dst->children,
479                         sizeof (oconfig_item_t)
480                         * (dst->children_num + src->children_num - 1));
481         if (temp == NULL)
482         {
483                 ERROR ("configfile: realloc failed.");
484                 return (-1);
485         }
486         dst->children = temp;
487
488         /* If there are children behind the include statement, and they have
489          * not yet been moved because (src->children_num == 0), then move them
490          * to the end of the list, so that the new children have room before
491          * them. */
492         if ((src->children_num > 0)
493                         && ((dst->children_num - (offset + 1)) > 0))
494         {
495                 int nmemb = dst->children_num - (offset + 1);
496                 int old_offset = offset + 1;
497                 int new_offset = offset + src->children_num;
498
499                 memmove (dst->children + new_offset,
500                                 dst->children + old_offset,
501                                 sizeof (oconfig_item_t) * nmemb);
502         }
503
504         /* Last but not least: If there are new children, copy them to the
505          * memory reserved for them. */
506         if (src->children_num > 0)
507         {
508                 memcpy (dst->children + offset,
509                                 src->children,
510                                 sizeof (oconfig_item_t) * src->children_num);
511         }
512
513         /* Update the number of children. */
514         dst->children_num += (src->children_num - 1);
515
516         return (0);
517 } /* int cf_ci_replace_child */
518
519 static int cf_ci_append_children (oconfig_item_t *dst, oconfig_item_t *src)
520 {
521         oconfig_item_t *temp;
522
523         if ((src == NULL) || (src->children_num == 0))
524                 return (0);
525
526         temp = (oconfig_item_t *) realloc (dst->children,
527                         sizeof (oconfig_item_t)
528                         * (dst->children_num + src->children_num));
529         if (temp == NULL)
530         {
531                 ERROR ("configfile: realloc failed.");
532                 return (-1);
533         }
534         dst->children = temp;
535
536         memcpy (dst->children + dst->children_num,
537                         src->children,
538                         sizeof (oconfig_item_t)
539                         * src->children_num);
540         dst->children_num += src->children_num;
541
542         return (0);
543 } /* int cf_ci_append_children */
544
545 #define CF_MAX_DEPTH 8
546 static oconfig_item_t *cf_read_generic (const char *path,
547                 const char *pattern, int depth);
548
549 static int cf_include_all (oconfig_item_t *root, int depth)
550 {
551         int i;
552
553         for (i = 0; i < root->children_num; i++)
554         {
555                 oconfig_item_t *new;
556                 oconfig_item_t *old;
557
558                 char *pattern = NULL;
559
560                 int j;
561
562                 if (strcasecmp (root->children[i].key, "Include") != 0)
563                         continue;
564
565                 old = root->children + i;
566
567                 if ((old->values_num != 1)
568                                 || (old->values[0].type != OCONFIG_TYPE_STRING))
569                 {
570                         ERROR ("configfile: `Include' needs exactly one string argument.");
571                         continue;
572                 }
573
574                 for (j = 0; j < old->children_num; ++j)
575                 {
576                         oconfig_item_t *child = old->children + j;
577
578                         if (strcasecmp (child->key, "Filter") == 0)
579                                 cf_util_get_string (child, &pattern);
580                         else
581                                 ERROR ("configfile: Option `%s' not allowed in <Include> block.",
582                                                 child->key);
583                 }
584
585                 new = cf_read_generic (old->values[0].value.string, pattern, depth + 1);
586                 if (new == NULL)
587                         continue;
588
589                 /* Now replace the i'th child in `root' with `new'. */
590                 cf_ci_replace_child (root, new, i);
591
592                 /* ... and go back to the new i'th child. */
593                 --i;
594
595                 sfree (new->values);
596                 sfree (new);
597         } /* for (i = 0; i < root->children_num; i++) */
598
599         return (0);
600 } /* int cf_include_all */
601
602 static oconfig_item_t *cf_read_file (const char *file,
603                 const char *pattern, int depth)
604 {
605         oconfig_item_t *root;
606
607         assert (depth < CF_MAX_DEPTH);
608
609         if (pattern != NULL) {
610 #if HAVE_FNMATCH_H && HAVE_LIBGEN_H
611                 char *tmp = sstrdup (file);
612                 char *filename = basename (tmp);
613
614                 if ((filename != NULL) && (fnmatch (pattern, filename, 0) != 0)) {
615                         DEBUG ("configfile: Not including `%s' because it "
616                                         "does not match pattern `%s'.",
617                                         filename, pattern);
618                         free (tmp);
619                         return (NULL);
620                 }
621
622                 free (tmp);
623 #else
624                 ERROR ("configfile: Cannot apply pattern filter '%s' "
625                                 "to file '%s': functions basename() and / or "
626                                 "fnmatch() not available.", pattern, file);
627 #endif /* HAVE_FNMATCH_H && HAVE_LIBGEN_H */
628         }
629
630         root = oconfig_parse_file (file);
631         if (root == NULL)
632         {
633                 ERROR ("configfile: Cannot read file `%s'.", file);
634                 return (NULL);
635         }
636
637         cf_include_all (root, depth);
638
639         return (root);
640 } /* oconfig_item_t *cf_read_file */
641
642 static int cf_compare_string (const void *p1, const void *p2)
643 {
644         return strcmp (*(const char **) p1, *(const char **) p2);
645 }
646
647 static oconfig_item_t *cf_read_dir (const char *dir,
648                 const char *pattern, int depth)
649 {
650         oconfig_item_t *root = NULL;
651         DIR *dh;
652         struct dirent *de;
653         char **filenames = NULL;
654         int filenames_num = 0;
655         int status;
656         int i;
657
658         assert (depth < CF_MAX_DEPTH);
659
660         dh = opendir (dir);
661         if (dh == NULL)
662         {
663                 char errbuf[1024];
664                 ERROR ("configfile: opendir failed: %s",
665                                 sstrerror (errno, errbuf, sizeof (errbuf)));
666                 return (NULL);
667         }
668
669         root = (oconfig_item_t *) malloc (sizeof (oconfig_item_t));
670         if (root == NULL)
671         {
672                 ERROR ("configfile: malloc failed.");
673                 return (NULL);
674         }
675         memset (root, 0, sizeof (oconfig_item_t));
676
677         while ((de = readdir (dh)) != NULL)
678         {
679                 char   name[1024];
680                 char **tmp;
681
682                 if ((de->d_name[0] == '.') || (de->d_name[0] == 0))
683                         continue;
684
685                 status = ssnprintf (name, sizeof (name), "%s/%s",
686                                 dir, de->d_name);
687                 if ((status < 0) || ((size_t) status >= sizeof (name)))
688                 {
689                         ERROR ("configfile: Not including `%s/%s' because its"
690                                         " name is too long.",
691                                         dir, de->d_name);
692                         for (i = 0; i < filenames_num; ++i)
693                                 free (filenames[i]);
694                         free (filenames);
695                         free (root);
696                         return (NULL);
697                 }
698
699                 ++filenames_num;
700                 tmp = (char **) realloc (filenames,
701                                 filenames_num * sizeof (*filenames));
702                 if (tmp == NULL) {
703                         ERROR ("configfile: realloc failed.");
704                         for (i = 0; i < filenames_num - 1; ++i)
705                                 free (filenames[i]);
706                         free (filenames);
707                         free (root);
708                         return (NULL);
709                 }
710                 filenames = tmp;
711
712                 filenames[filenames_num - 1] = sstrdup (name);
713         }
714
715         qsort ((void *) filenames, filenames_num, sizeof (*filenames),
716                         cf_compare_string);
717
718         for (i = 0; i < filenames_num; ++i)
719         {
720                 oconfig_item_t *temp;
721                 char *name = filenames[i];
722
723                 temp = cf_read_generic (name, pattern, depth);
724                 if (temp == NULL)
725                 {
726                         /* An error should already have been reported. */
727                         sfree (name);
728                         continue;
729                 }
730
731                 cf_ci_append_children (root, temp);
732                 sfree (temp->children);
733                 sfree (temp);
734
735                 free (name);
736         }
737
738         free(filenames);
739         return (root);
740 } /* oconfig_item_t *cf_read_dir */
741
742 /* 
743  * cf_read_generic
744  *
745  * Path is stat'ed and either cf_read_file or cf_read_dir is called
746  * accordingly.
747  *
748  * There are two versions of this function: If `wordexp' exists shell wildcards
749  * will be expanded and the function will include all matches found. If
750  * `wordexp' (or, more precisely, it's header file) is not available the
751  * simpler function is used which does not do any such expansion.
752  */
753 #if HAVE_WORDEXP_H
754 static oconfig_item_t *cf_read_generic (const char *path,
755                 const char *pattern, int depth)
756 {
757         oconfig_item_t *root = NULL;
758         int status;
759         const char *path_ptr;
760         wordexp_t we;
761         size_t i;
762
763         if (depth >= CF_MAX_DEPTH)
764         {
765                 ERROR ("configfile: Not including `%s' because the maximum "
766                                 "nesting depth has been reached.", path);
767                 return (NULL);
768         }
769
770         status = wordexp (path, &we, WRDE_NOCMD);
771         if (status != 0)
772         {
773                 ERROR ("configfile: wordexp (%s) failed.", path);
774                 return (NULL);
775         }
776
777         root = (oconfig_item_t *) malloc (sizeof (oconfig_item_t));
778         if (root == NULL)
779         {
780                 ERROR ("configfile: malloc failed.");
781                 return (NULL);
782         }
783         memset (root, '\0', sizeof (oconfig_item_t));
784
785         /* wordexp() might return a sorted list already. That's not
786          * documented though, so let's make sure we get what we want. */
787         qsort ((void *) we.we_wordv, we.we_wordc, sizeof (*we.we_wordv),
788                         cf_compare_string);
789
790         for (i = 0; i < we.we_wordc; i++)
791         {
792                 oconfig_item_t *temp;
793                 struct stat statbuf;
794
795                 path_ptr = we.we_wordv[i];
796
797                 status = stat (path_ptr, &statbuf);
798                 if (status != 0)
799                 {
800                         char errbuf[1024];
801                         WARNING ("configfile: stat (%s) failed: %s",
802                                         path_ptr,
803                                         sstrerror (errno, errbuf, sizeof (errbuf)));
804                         continue;
805                 }
806
807                 if (S_ISREG (statbuf.st_mode))
808                         temp = cf_read_file (path_ptr, pattern, depth);
809                 else if (S_ISDIR (statbuf.st_mode))
810                         temp = cf_read_dir (path_ptr, pattern, depth);
811                 else
812                 {
813                         WARNING ("configfile: %s is neither a file nor a "
814                                         "directory.", path);
815                         continue;
816                 }
817
818                 if (temp == NULL) {
819                         oconfig_free (root);
820                         return (NULL);
821                 }
822
823                 cf_ci_append_children (root, temp);
824                 sfree (temp->children);
825                 sfree (temp);
826         }
827
828         wordfree (&we);
829
830         if (root->children == NULL)
831         {
832                 oconfig_free (root);
833                 return (NULL);
834         }
835
836         return (root);
837 } /* oconfig_item_t *cf_read_generic */
838 /* #endif HAVE_WORDEXP_H */
839
840 #else /* if !HAVE_WORDEXP_H */
841 static oconfig_item_t *cf_read_generic (const char *path,
842                 const char *pattern, int depth)
843 {
844         struct stat statbuf;
845         int status;
846
847         if (depth >= CF_MAX_DEPTH)
848         {
849                 ERROR ("configfile: Not including `%s' because the maximum "
850                                 "nesting depth has been reached.", path);
851                 return (NULL);
852         }
853
854         status = stat (path, &statbuf);
855         if (status != 0)
856         {
857                 char errbuf[1024];
858                 ERROR ("configfile: stat (%s) failed: %s",
859                                 path,
860                                 sstrerror (errno, errbuf, sizeof (errbuf)));
861                 return (NULL);
862         }
863
864         if (S_ISREG (statbuf.st_mode))
865                 return (cf_read_file (path, pattern, depth));
866         else if (S_ISDIR (statbuf.st_mode))
867                 return (cf_read_dir (path, pattern, depth));
868
869         ERROR ("configfile: %s is neither a file nor a directory.", path);
870         return (NULL);
871 } /* oconfig_item_t *cf_read_generic */
872 #endif /* !HAVE_WORDEXP_H */
873
874 /* 
875  * Public functions
876  */
877 int global_option_set (const char *option, const char *value)
878 {
879         int i;
880
881         DEBUG ("option = %s; value = %s;", option, value);
882
883         for (i = 0; i < cf_global_options_num; i++)
884                 if (strcasecmp (cf_global_options[i].key, option) == 0)
885                         break;
886
887         if (i >= cf_global_options_num)
888                 return (-1);
889
890         sfree (cf_global_options[i].value);
891
892         if (value != NULL)
893                 cf_global_options[i].value = strdup (value);
894         else
895                 cf_global_options[i].value = NULL;
896
897         return (0);
898 }
899
900 const char *global_option_get (const char *option)
901 {
902         int i;
903
904         for (i = 0; i < cf_global_options_num; i++)
905                 if (strcasecmp (cf_global_options[i].key, option) == 0)
906                         break;
907
908         if (i >= cf_global_options_num)
909                 return (NULL);
910         
911         return ((cf_global_options[i].value != NULL)
912                         ? cf_global_options[i].value
913                         : cf_global_options[i].def);
914 } /* char *global_option_get */
915
916 cdtime_t cf_get_default_interval (void)
917 {
918   char const *str = global_option_get ("Interval");
919   double interval_double = COLLECTD_DEFAULT_INTERVAL;
920
921   if (str != NULL)
922   {
923     char *endptr = NULL;
924     double tmp = strtod (str, &endptr);
925
926     if ((endptr == NULL) || (endptr == str) || (*endptr != 0))
927       ERROR ("cf_get_default_interval: Unable to parse string \"%s\" "
928           "as number.", str);
929     else if (tmp <= 0.0)
930       ERROR ("cf_get_default_interval: Interval must be a positive number. "
931           "The current number is %g.", tmp);
932     else
933       interval_double = tmp;
934   }
935
936   return (DOUBLE_TO_CDTIME_T (interval_double));
937 } /* }}} cdtime_t cf_get_default_interval */
938
939 void cf_unregister (const char *type)
940 {
941         cf_callback_t *this, *prev;
942
943         for (prev = NULL, this = first_callback;
944                         this != NULL;
945                         prev = this, this = this->next)
946                 if (strcasecmp (this->type, type) == 0)
947                 {
948                         if (prev == NULL)
949                                 first_callback = this->next;
950                         else
951                                 prev->next = this->next;
952
953                         free (this);
954                         break;
955                 }
956 } /* void cf_unregister */
957
958 void cf_unregister_complex (const char *type)
959 {
960         cf_complex_callback_t *this, *prev;
961
962         for (prev = NULL, this = complex_callback_head;
963                         this != NULL;
964                         prev = this, this = this->next)
965                 if (strcasecmp (this->type, type) == 0)
966                 {
967                         if (prev == NULL)
968                                 complex_callback_head = this->next;
969                         else
970                                 prev->next = this->next;
971
972                         sfree (this->type);
973                         sfree (this);
974                         break;
975                 }
976 } /* void cf_unregister */
977
978 void cf_register (const char *type,
979                 int (*callback) (const char *, const char *),
980                 const char **keys, int keys_num)
981 {
982         cf_callback_t *cf_cb;
983
984         /* Remove this module from the list, if it already exists */
985         cf_unregister (type);
986
987         /* This pointer will be free'd in `cf_unregister' */
988         if ((cf_cb = (cf_callback_t *) malloc (sizeof (cf_callback_t))) == NULL)
989                 return;
990
991         cf_cb->type     = type;
992         cf_cb->callback = callback;
993         cf_cb->keys     = keys;
994         cf_cb->keys_num = keys_num;
995         cf_cb->ctx      = plugin_get_ctx ();
996
997         cf_cb->next = first_callback;
998         first_callback = cf_cb;
999 } /* void cf_register */
1000
1001 int cf_register_complex (const char *type, int (*callback) (oconfig_item_t *))
1002 {
1003         cf_complex_callback_t *new;
1004
1005         new = (cf_complex_callback_t *) malloc (sizeof (cf_complex_callback_t));
1006         if (new == NULL)
1007                 return (-1);
1008
1009         new->type = strdup (type);
1010         if (new->type == NULL)
1011         {
1012                 sfree (new);
1013                 return (-1);
1014         }
1015
1016         new->callback = callback;
1017         new->next = NULL;
1018
1019         new->ctx = plugin_get_ctx ();
1020
1021         if (complex_callback_head == NULL)
1022         {
1023                 complex_callback_head = new;
1024         }
1025         else
1026         {
1027                 cf_complex_callback_t *last = complex_callback_head;
1028                 while (last->next != NULL)
1029                         last = last->next;
1030                 last->next = new;
1031         }
1032
1033         return (0);
1034 } /* int cf_register_complex */
1035
1036 int cf_read (char *filename)
1037 {
1038         oconfig_item_t *conf;
1039         int i;
1040
1041         conf = cf_read_generic (filename, /* pattern = */ NULL, /* depth = */ 0);
1042         if (conf == NULL)
1043         {
1044                 ERROR ("Unable to read config file %s.", filename);
1045                 return (-1);
1046         }
1047
1048         for (i = 0; i < conf->children_num; i++)
1049         {
1050                 if (conf->children[i].children == NULL)
1051                         dispatch_value (conf->children + i);
1052                 else
1053                         dispatch_block (conf->children + i);
1054         }
1055
1056         oconfig_free (conf);
1057
1058         /* Read the default types.db if no `TypesDB' option was given. */
1059         if (cf_default_typesdb)
1060                 read_types_list (PKGDATADIR"/types.db");
1061
1062         return (0);
1063 } /* int cf_read */
1064
1065 /* Assures the config option is a string, duplicates it and returns the copy in
1066  * "ret_string". If necessary "*ret_string" is freed first. Returns zero upon
1067  * success. */
1068 int cf_util_get_string (const oconfig_item_t *ci, char **ret_string) /* {{{ */
1069 {
1070         char *string;
1071
1072         if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_STRING))
1073         {
1074                 ERROR ("cf_util_get_string: The %s option requires "
1075                                 "exactly one string argument.", ci->key);
1076                 return (-1);
1077         }
1078
1079         string = strdup (ci->values[0].value.string);
1080         if (string == NULL)
1081                 return (-1);
1082
1083         if (*ret_string != NULL)
1084                 sfree (*ret_string);
1085         *ret_string = string;
1086
1087         return (0);
1088 } /* }}} int cf_util_get_string */
1089
1090 /* Assures the config option is a string and copies it to the provided buffer.
1091  * Assures null-termination. */
1092 int cf_util_get_string_buffer (const oconfig_item_t *ci, char *buffer, /* {{{ */
1093                 size_t buffer_size)
1094 {
1095         if ((ci == NULL) || (buffer == NULL) || (buffer_size < 1))
1096                 return (EINVAL);
1097
1098         if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_STRING))
1099         {
1100                 ERROR ("cf_util_get_string_buffer: The %s option requires "
1101                                 "exactly one string argument.", ci->key);
1102                 return (-1);
1103         }
1104
1105         strncpy (buffer, ci->values[0].value.string, buffer_size);
1106         buffer[buffer_size - 1] = 0;
1107
1108         return (0);
1109 } /* }}} int cf_util_get_string_buffer */
1110
1111 /* Assures the config option is a number and returns it as an int. */
1112 int cf_util_get_int (const oconfig_item_t *ci, int *ret_value) /* {{{ */
1113 {
1114         if ((ci == NULL) || (ret_value == NULL))
1115                 return (EINVAL);
1116
1117         if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_NUMBER))
1118         {
1119                 ERROR ("cf_util_get_int: The %s option requires "
1120                                 "exactly one numeric argument.", ci->key);
1121                 return (-1);
1122         }
1123
1124         *ret_value = (int) ci->values[0].value.number;
1125
1126         return (0);
1127 } /* }}} int cf_util_get_int */
1128
1129 int cf_util_get_double (const oconfig_item_t *ci, double *ret_value) /* {{{ */
1130 {
1131         if ((ci == NULL) || (ret_value == NULL))
1132                 return (EINVAL);
1133
1134         if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_NUMBER))
1135         {
1136                 ERROR ("cf_util_get_double: The %s option requires "
1137                                 "exactly one numeric argument.", ci->key);
1138                 return (-1);
1139         }
1140
1141         *ret_value = ci->values[0].value.number;
1142
1143         return (0);
1144 } /* }}} int cf_util_get_double */
1145
1146 int cf_util_get_boolean (const oconfig_item_t *ci, _Bool *ret_bool) /* {{{ */
1147 {
1148         if ((ci == NULL) || (ret_bool == NULL))
1149                 return (EINVAL);
1150
1151         if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_BOOLEAN))
1152         {
1153                 ERROR ("cf_util_get_boolean: The %s option requires "
1154                                 "exactly one boolean argument.", ci->key);
1155                 return (-1);
1156         }
1157
1158         *ret_bool = ci->values[0].value.boolean ? 1 : 0;
1159
1160         return (0);
1161 } /* }}} int cf_util_get_boolean */
1162
1163 int cf_util_get_flag (const oconfig_item_t *ci, /* {{{ */
1164                 unsigned int *ret_value, unsigned int flag)
1165 {
1166         int status;
1167         _Bool b;
1168
1169         if (ret_value == NULL)
1170                 return (EINVAL);
1171
1172         b = 0;
1173         status = cf_util_get_boolean (ci, &b);
1174         if (status != 0)
1175                 return (status);
1176
1177         if (b)
1178         {
1179                 *ret_value |= flag;
1180         }
1181         else
1182         {
1183                 *ret_value &= ~flag;
1184         }
1185
1186         return (0);
1187 } /* }}} int cf_util_get_flag */
1188
1189 /* Assures that the config option is a string or a number if the correct range
1190  * of 1-65535. The string is then converted to a port number using
1191  * `service_name_to_port_number' and returned.
1192  * Returns the port number in the range [1-65535] or less than zero upon
1193  * failure. */
1194 int cf_util_get_port_number (const oconfig_item_t *ci) /* {{{ */
1195 {
1196         int tmp;
1197
1198         if ((ci->values_num != 1)
1199                         || ((ci->values[0].type != OCONFIG_TYPE_STRING)
1200                                 && (ci->values[0].type != OCONFIG_TYPE_NUMBER)))
1201         {
1202                 ERROR ("cf_util_get_port_number: The \"%s\" option requires "
1203                                 "exactly one string argument.", ci->key);
1204                 return (-1);
1205         }
1206
1207         if (ci->values[0].type == OCONFIG_TYPE_STRING)
1208                 return (service_name_to_port_number (ci->values[0].value.string));
1209
1210         assert (ci->values[0].type == OCONFIG_TYPE_NUMBER);
1211         tmp = (int) (ci->values[0].value.number + 0.5);
1212         if ((tmp < 1) || (tmp > 65535))
1213         {
1214                 ERROR ("cf_util_get_port_number: The \"%s\" option requires "
1215                                 "a service name or a port number. The number "
1216                                 "you specified, %i, is not in the valid "
1217                                 "range of 1-65535.",
1218                                 ci->key, tmp);
1219                 return (-1);
1220         }
1221
1222         return (tmp);
1223 } /* }}} int cf_util_get_port_number */
1224
1225 int cf_util_get_service (const oconfig_item_t *ci, char **ret_string) /* {{{ */
1226 {
1227         int port;
1228         char *service;
1229         int status;
1230
1231         if (ci->values_num != 1)
1232         {
1233                 ERROR ("cf_util_get_service: The %s option requires exactly "
1234                                 "one argument.", ci->key);
1235                 return (-1);
1236         }
1237
1238         if (ci->values[0].type == OCONFIG_TYPE_STRING)
1239                 return (cf_util_get_string (ci, ret_string));
1240         if (ci->values[0].type != OCONFIG_TYPE_NUMBER)
1241         {
1242                 ERROR ("cf_util_get_service: The %s option requires "
1243                                 "exactly one string or numeric argument.",
1244                                 ci->key);
1245         }
1246
1247         port = 0;
1248         status = cf_util_get_int (ci, &port);
1249         if (status != 0)
1250                 return (status);
1251         else if ((port < 1) || (port > 65535))
1252         {
1253                 ERROR ("cf_util_get_service: The port number given "
1254                                 "for the %s option is out of "
1255                                 "range (%i).", ci->key, port);
1256                 return (-1);
1257         }
1258
1259         service = malloc (6);
1260         if (service == NULL)
1261         {
1262                 ERROR ("cf_util_get_service: Out of memory.");
1263                 return (-1);
1264         }
1265         ssnprintf (service, 6, "%i", port);
1266
1267         sfree (*ret_string);
1268         *ret_string = service;
1269
1270         return (0);
1271 } /* }}} int cf_util_get_service */
1272
1273 int cf_util_get_cdtime (const oconfig_item_t *ci, cdtime_t *ret_value) /* {{{ */
1274 {
1275         if ((ci == NULL) || (ret_value == NULL))
1276                 return (EINVAL);
1277
1278         if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_NUMBER))
1279         {
1280                 ERROR ("cf_util_get_cdtime: The %s option requires "
1281                                 "exactly one numeric argument.", ci->key);
1282                 return (-1);
1283         }
1284
1285         if (ci->values[0].value.number < 0.0)
1286         {
1287                 ERROR ("cf_util_get_cdtime: The numeric argument of the %s "
1288                                 "option must not be negative.", ci->key);
1289                 return (-1);
1290         }
1291
1292         *ret_value = DOUBLE_TO_CDTIME_T (ci->values[0].value.number);
1293
1294         return (0);
1295 } /* }}} int cf_util_get_cdtime */
1296