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