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