2 * collectd - src/configfile.c
3 * Copyright (C) 2005-2009 Florian octo Forster
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.
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.
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
20 * Florian octo Forster <octo at verplant.org>
21 * Sebastian tokkee Harl <sh at tokkee.org>
26 #include "liboconfig/oconfig.h"
30 #include "configfile.h"
31 #include "types_list.h"
32 #include "utils_threshold.h"
33 #include "filter_chain.h"
37 #endif /* HAVE_WORDEXP_H */
39 #define ESCAPE_NULL(str) ((str) == NULL ? "(null)" : (str))
44 typedef struct cf_callback
47 int (*callback) (const char *, const char *);
50 struct cf_callback *next;
53 typedef struct cf_complex_callback_s
56 int (*callback) (oconfig_item_t *);
57 struct cf_complex_callback_s *next;
58 } cf_complex_callback_t;
60 typedef struct cf_value_map_s
63 int (*func) (const oconfig_item_t *);
66 typedef struct cf_global_option_s
74 * Prototypes of callback functions
76 static int dispatch_value_typesdb (const oconfig_item_t *ci);
77 static int dispatch_value_plugindir (const oconfig_item_t *ci);
78 static int dispatch_value_loadplugin (const oconfig_item_t *ci);
83 static cf_callback_t *first_callback = NULL;
84 static cf_complex_callback_t *complex_callback_head = NULL;
86 static cf_value_map_t cf_value_map[] =
88 {"TypesDB", dispatch_value_typesdb},
89 {"PluginDir", dispatch_value_plugindir},
90 {"LoadPlugin", dispatch_value_loadplugin}
92 static int cf_value_map_num = STATIC_ARRAY_LEN (cf_value_map);
94 static cf_global_option_t cf_global_options[] =
96 {"BaseDir", NULL, PKGLOCALSTATEDIR},
97 {"PIDFile", NULL, PIDFILE},
98 {"Hostname", NULL, NULL},
99 {"FQDNLookup", NULL, "false"},
100 {"Interval", NULL, "10"},
101 {"ReadThreads", NULL, "5"},
102 {"PreCacheChain", NULL, "PreCache"},
103 {"PostCacheChain", NULL, "PostCache"}
105 static int cf_global_options_num = STATIC_ARRAY_LEN (cf_global_options);
107 static int cf_default_typesdb = 1;
110 * Functions to handle register/unregister, search, and other plugin related
113 static cf_callback_t *cf_search (const char *type)
115 cf_callback_t *cf_cb;
120 for (cf_cb = first_callback; cf_cb != NULL; cf_cb = cf_cb->next)
121 if (strcasecmp (cf_cb->type, type) == 0)
127 static int cf_dispatch (const char *type, const char *orig_key,
128 const char *orig_value)
130 cf_callback_t *cf_cb;
136 DEBUG ("type = %s, key = %s, value = %s",
138 ESCAPE_NULL(orig_key),
139 ESCAPE_NULL(orig_value));
141 if ((cf_cb = cf_search (type)) == NULL)
143 WARNING ("Found a configuration for the `%s' plugin, but "
144 "the plugin isn't loaded or didn't register "
145 "a configuration callback.", type);
149 if ((key = strdup (orig_key)) == NULL)
151 if ((value = strdup (orig_value)) == NULL)
159 for (i = 0; i < cf_cb->keys_num; i++)
161 if ((cf_cb->keys[i] != NULL)
162 && (strcasecmp (cf_cb->keys[i], key) == 0))
164 ret = (*cf_cb->callback) (key, value);
169 if (i >= cf_cb->keys_num)
170 WARNING ("Plugin `%s' did not register for value `%s'.", type, key);
175 DEBUG ("return (%i)", ret);
178 } /* int cf_dispatch */
180 static int dispatch_global_option (const oconfig_item_t *ci)
182 if (ci->values_num != 1)
184 if (ci->values[0].type == OCONFIG_TYPE_STRING)
185 return (global_option_set (ci->key, ci->values[0].value.string));
186 else if (ci->values[0].type == OCONFIG_TYPE_NUMBER)
189 ssnprintf (tmp, sizeof (tmp), "%lf", ci->values[0].value.number);
190 return (global_option_set (ci->key, tmp));
192 else if (ci->values[0].type == OCONFIG_TYPE_BOOLEAN)
194 if (ci->values[0].value.boolean)
195 return (global_option_set (ci->key, "true"));
197 return (global_option_set (ci->key, "false"));
201 } /* int dispatch_global_option */
203 static int dispatch_value_typesdb (const oconfig_item_t *ci)
207 assert (strcasecmp (ci->key, "TypesDB") == 0);
209 cf_default_typesdb = 0;
211 if (ci->values_num < 1)
214 for (i = 0; i < ci->values_num; ++i)
216 if (OCONFIG_TYPE_STRING != ci->values[i].type)
219 read_types_list (ci->values[i].value.string);
222 } /* int dispatch_value_typesdb */
224 static int dispatch_value_plugindir (const oconfig_item_t *ci)
226 assert (strcasecmp (ci->key, "PluginDir") == 0);
228 if (ci->values_num != 1)
230 if (ci->values[0].type != OCONFIG_TYPE_STRING)
233 plugin_set_dir (ci->values[0].value.string);
237 static int dispatch_value_loadplugin (const oconfig_item_t *ci)
239 assert (strcasecmp (ci->key, "LoadPlugin") == 0);
241 if (ci->values_num != 1)
243 if (ci->values[0].type != OCONFIG_TYPE_STRING)
246 return (plugin_load (ci->values[0].value.string));
247 } /* int dispatch_value_loadplugin */
249 static int dispatch_value_plugin (const char *plugin, oconfig_item_t *ci)
257 buffer_free = sizeof (buffer);
259 for (i = 0; i < ci->values_num; i++)
263 if (ci->values[i].type == OCONFIG_TYPE_STRING)
264 status = ssnprintf (buffer_ptr, buffer_free, " %s",
265 ci->values[i].value.string);
266 else if (ci->values[i].type == OCONFIG_TYPE_NUMBER)
267 status = ssnprintf (buffer_ptr, buffer_free, " %lf",
268 ci->values[i].value.number);
269 else if (ci->values[i].type == OCONFIG_TYPE_BOOLEAN)
270 status = ssnprintf (buffer_ptr, buffer_free, " %s",
271 ci->values[i].value.boolean
274 if ((status < 0) || (status >= buffer_free))
276 buffer_free -= status;
277 buffer_ptr += status;
279 /* skip the initial space */
280 buffer_ptr = buffer + 1;
282 return (cf_dispatch (plugin, ci->key, buffer_ptr));
283 } /* int dispatch_value_plugin */
285 static int dispatch_value (const oconfig_item_t *ci)
290 for (i = 0; i < cf_value_map_num; i++)
291 if (strcasecmp (cf_value_map[i].key, ci->key) == 0)
293 ret = cf_value_map[i].func (ci);
297 for (i = 0; i < cf_global_options_num; i++)
298 if (strcasecmp (cf_global_options[i].key, ci->key) == 0)
300 ret = dispatch_global_option (ci);
305 } /* int dispatch_value */
307 static int dispatch_block_plugin (oconfig_item_t *ci)
312 cf_complex_callback_t *cb;
314 if (strcasecmp (ci->key, "Plugin") != 0)
316 if (ci->values_num < 1)
318 if (ci->values[0].type != OCONFIG_TYPE_STRING)
321 name = ci->values[0].value.string;
323 /* Check for a complex callback first */
324 for (cb = complex_callback_head; cb != NULL; cb = cb->next)
325 if (strcasecmp (name, cb->type) == 0)
326 return (cb->callback (ci));
328 /* Hm, no complex plugin found. Dispatch the values one by one */
329 for (i = 0; i < ci->children_num; i++)
331 if (ci->children[i].children == NULL)
332 dispatch_value_plugin (name, ci->children + i);
334 {DEBUG ("No nested config blocks allowed for this plugin.");}
341 static int dispatch_block (oconfig_item_t *ci)
343 if (strcasecmp (ci->key, "Plugin") == 0)
344 return (dispatch_block_plugin (ci));
345 else if (strcasecmp (ci->key, "Threshold") == 0)
346 return (ut_config (ci));
347 else if (strcasecmp (ci->key, "Chain") == 0)
348 return (fc_configure (ci));
353 static int cf_ci_replace_child (oconfig_item_t *dst, oconfig_item_t *src,
356 oconfig_item_t *temp;
359 assert (offset >= 0);
360 assert (dst->children_num > offset);
362 /* Free the memory used by the replaced child. Usually that's the
363 * `Include "blah"' statement. */
364 temp = dst->children + offset;
365 for (i = 0; i < temp->values_num; i++)
367 if (temp->values[i].type == OCONFIG_TYPE_STRING)
369 sfree (temp->values[i].value.string);
372 sfree (temp->values);
375 /* If (src->children_num == 0) the array size is decreased. If offset
376 * is _not_ the last element, (offset < (src->children_num - 1)), then
377 * we need to move the trailing elements before resizing the array. */
378 if ((src->children_num == 0) && (offset < (src->children_num - 1)))
380 int nmemb = src->children_num - (offset + 1);
381 memmove (src->children + offset, src->children + offset + 1,
382 sizeof (oconfig_item_t) * nmemb);
385 /* Resize the memory containing the children to be big enough to hold
387 temp = (oconfig_item_t *) realloc (dst->children,
388 sizeof (oconfig_item_t)
389 * (dst->children_num + src->children_num - 1));
392 ERROR ("configfile: realloc failed.");
395 dst->children = temp;
397 /* If there are children behind the include statement, and they have
398 * not yet been moved because (src->children_num == 0), then move them
399 * to the end of the list, so that the new children have room before
401 if ((src->children_num > 0)
402 && ((dst->children_num - (offset + 1)) > 0))
404 int nmemb = dst->children_num - (offset + 1);
405 int old_offset = offset + 1;
406 int new_offset = offset + src->children_num;
408 memmove (dst->children + new_offset,
409 dst->children + old_offset,
410 sizeof (oconfig_item_t) * nmemb);
413 /* Last but not least: If there are new childrem, copy them to the
414 * memory reserved for them. */
415 if (src->children_num > 0)
417 memcpy (dst->children + offset,
419 sizeof (oconfig_item_t) * src->children_num);
422 /* Update the number of children. */
423 dst->children_num += (src->children_num - 1);
426 } /* int cf_ci_replace_child */
428 static int cf_ci_append_children (oconfig_item_t *dst, oconfig_item_t *src)
430 oconfig_item_t *temp;
432 if ((src == NULL) || (src->children_num == 0))
435 temp = (oconfig_item_t *) realloc (dst->children,
436 sizeof (oconfig_item_t)
437 * (dst->children_num + src->children_num));
440 ERROR ("configfile: realloc failed.");
443 dst->children = temp;
445 memcpy (dst->children + dst->children_num,
447 sizeof (oconfig_item_t)
448 * src->children_num);
449 dst->children_num += src->children_num;
452 } /* int cf_ci_append_children */
454 #define CF_MAX_DEPTH 8
455 static oconfig_item_t *cf_read_generic (const char *path, int depth);
457 static int cf_include_all (oconfig_item_t *root, int depth)
461 for (i = 0; i < root->children_num; i++)
466 /* Ignore all blocks, including `Include' blocks. */
467 if (root->children[i].children_num != 0)
470 if (strcasecmp (root->children[i].key, "Include") != 0)
473 old = root->children + i;
475 if ((old->values_num != 1)
476 || (old->values[0].type != OCONFIG_TYPE_STRING))
478 ERROR ("configfile: `Include' needs exactly one string argument.");
482 new = cf_read_generic (old->values[0].value.string, depth + 1);
486 /* Now replace the i'th child in `root' with `new'. */
487 cf_ci_replace_child (root, new, i);
491 } /* for (i = 0; i < root->children_num; i++) */
494 } /* int cf_include_all */
496 static oconfig_item_t *cf_read_file (const char *file, int depth)
498 oconfig_item_t *root;
500 assert (depth < CF_MAX_DEPTH);
502 root = oconfig_parse_file (file);
505 ERROR ("configfile: Cannot read file `%s'.", file);
509 cf_include_all (root, depth);
512 } /* oconfig_item_t *cf_read_file */
514 static int cf_compare_string (const void *p1, const void *p2)
516 return strcmp (*(const char **) p1, *(const char **) p2);
519 static oconfig_item_t *cf_read_dir (const char *dir, int depth)
521 oconfig_item_t *root = NULL;
524 char **filenames = NULL;
525 int filenames_num = 0;
529 assert (depth < CF_MAX_DEPTH);
535 ERROR ("configfile: opendir failed: %s",
536 sstrerror (errno, errbuf, sizeof (errbuf)));
540 root = (oconfig_item_t *) malloc (sizeof (oconfig_item_t));
543 ERROR ("configfile: malloc failed.");
546 memset (root, '\0', sizeof (oconfig_item_t));
548 while ((de = readdir (dh)) != NULL)
553 if ((de->d_name[0] == '.') || (de->d_name[0] == '\0'))
556 status = ssnprintf (name, sizeof (name), "%s/%s",
558 if ((status < 0) || ((size_t) status >= sizeof (name)))
560 ERROR ("configfile: Not including `%s/%s' because its"
561 " name is too long.",
563 for (i = 0; i < filenames_num; ++i)
571 tmp = (char **) realloc (filenames,
572 filenames_num * sizeof (*filenames));
574 ERROR ("configfile: realloc failed.");
575 for (i = 0; i < filenames_num - 1; ++i)
583 filenames[filenames_num - 1] = sstrdup (name);
586 qsort ((void *) filenames, filenames_num, sizeof (*filenames),
589 for (i = 0; i < filenames_num; ++i)
591 oconfig_item_t *temp;
592 char *name = filenames[i];
594 temp = cf_read_generic (name, depth);
597 for (j = i; j < filenames_num; ++j)
604 cf_ci_append_children (root, temp);
605 sfree (temp->children);
613 } /* oconfig_item_t *cf_read_dir */
618 * Path is stat'ed and either cf_read_file or cf_read_dir is called
621 * There are two versions of this function: If `wordexp' exists shell wildcards
622 * will be expanded and the function will include all matches found. If
623 * `wordexp' (or, more precisely, it's header file) is not available the
624 * simpler function is used which does not do any such expansion.
627 static oconfig_item_t *cf_read_generic (const char *path, int depth)
629 oconfig_item_t *root = NULL;
631 const char *path_ptr;
635 if (depth >= CF_MAX_DEPTH)
637 ERROR ("configfile: Not including `%s' because the maximum "
638 "nesting depth has been reached.", path);
642 status = wordexp (path, &we, WRDE_NOCMD);
645 ERROR ("configfile: wordexp (%s) failed.", path);
649 root = (oconfig_item_t *) malloc (sizeof (oconfig_item_t));
652 ERROR ("configfile: malloc failed.");
655 memset (root, '\0', sizeof (oconfig_item_t));
657 /* wordexp() might return a sorted list already. That's not
658 * documented though, so let's make sure we get what we want. */
659 qsort ((void *) we.we_wordv, we.we_wordc, sizeof (*we.we_wordv),
662 for (i = 0; i < we.we_wordc; i++)
664 oconfig_item_t *temp;
667 path_ptr = we.we_wordv[i];
669 status = stat (path_ptr, &statbuf);
673 ERROR ("configfile: stat (%s) failed: %s",
675 sstrerror (errno, errbuf, sizeof (errbuf)));
680 if (S_ISREG (statbuf.st_mode))
681 temp = cf_read_file (path_ptr, depth);
682 else if (S_ISDIR (statbuf.st_mode))
683 temp = cf_read_dir (path_ptr, depth);
686 ERROR ("configfile: %s is neither a file nor a "
696 cf_ci_append_children (root, temp);
697 sfree (temp->children);
704 } /* oconfig_item_t *cf_read_generic */
705 /* #endif HAVE_WORDEXP_H */
707 #else /* if !HAVE_WORDEXP_H */
708 static oconfig_item_t *cf_read_generic (const char *path, int depth)
713 if (depth >= CF_MAX_DEPTH)
715 ERROR ("configfile: Not including `%s' because the maximum "
716 "nesting depth has been reached.", path);
720 status = stat (path, &statbuf);
724 ERROR ("configfile: stat (%s) failed: %s",
726 sstrerror (errno, errbuf, sizeof (errbuf)));
730 if (S_ISREG (statbuf.st_mode))
731 return (cf_read_file (path, depth));
732 else if (S_ISDIR (statbuf.st_mode))
733 return (cf_read_dir (path, depth));
735 ERROR ("configfile: %s is neither a file nor a directory.", path);
737 } /* oconfig_item_t *cf_read_generic */
738 #endif /* !HAVE_WORDEXP_H */
743 int global_option_set (const char *option, const char *value)
747 DEBUG ("option = %s; value = %s;", option, value);
749 for (i = 0; i < cf_global_options_num; i++)
750 if (strcasecmp (cf_global_options[i].key, option) == 0)
753 if (i >= cf_global_options_num)
756 sfree (cf_global_options[i].value);
759 cf_global_options[i].value = strdup (value);
761 cf_global_options[i].value = NULL;
766 const char *global_option_get (const char *option)
770 for (i = 0; i < cf_global_options_num; i++)
771 if (strcasecmp (cf_global_options[i].key, option) == 0)
774 if (i >= cf_global_options_num)
777 return ((cf_global_options[i].value != NULL)
778 ? cf_global_options[i].value
779 : cf_global_options[i].def);
780 } /* char *global_option_get */
782 void cf_unregister (const char *type)
784 cf_callback_t *this, *prev;
786 for (prev = NULL, this = first_callback;
788 prev = this, this = this->next)
789 if (strcasecmp (this->type, type) == 0)
792 first_callback = this->next;
794 prev->next = this->next;
799 } /* void cf_unregister */
801 void cf_unregister_complex (const char *type)
803 cf_complex_callback_t *this, *prev;
805 for (prev = NULL, this = complex_callback_head;
807 prev = this, this = this->next)
808 if (strcasecmp (this->type, type) == 0)
811 complex_callback_head = this->next;
813 prev->next = this->next;
819 } /* void cf_unregister */
821 void cf_register (const char *type,
822 int (*callback) (const char *, const char *),
823 const char **keys, int keys_num)
825 cf_callback_t *cf_cb;
827 /* Remove this module from the list, if it already exists */
828 cf_unregister (type);
830 /* This pointer will be free'd in `cf_unregister' */
831 if ((cf_cb = (cf_callback_t *) malloc (sizeof (cf_callback_t))) == NULL)
835 cf_cb->callback = callback;
837 cf_cb->keys_num = keys_num;
839 cf_cb->next = first_callback;
840 first_callback = cf_cb;
841 } /* void cf_register */
843 int cf_register_complex (const char *type, int (*callback) (oconfig_item_t *))
845 cf_complex_callback_t *new;
847 new = (cf_complex_callback_t *) malloc (sizeof (cf_complex_callback_t));
851 new->type = strdup (type);
852 if (new->type == NULL)
858 new->callback = callback;
861 if (complex_callback_head == NULL)
863 complex_callback_head = new;
867 cf_complex_callback_t *last = complex_callback_head;
868 while (last->next != NULL)
874 } /* int cf_register_complex */
876 int cf_read (char *filename)
878 oconfig_item_t *conf;
881 conf = cf_read_generic (filename, 0 /* depth */);
884 ERROR ("Unable to read config file %s.", filename);
888 for (i = 0; i < conf->children_num; i++)
890 if (conf->children[i].children == NULL)
891 dispatch_value (conf->children + i);
893 dispatch_block (conf->children + i);
898 /* Read the default types.db if no `TypesDB' option was given. */
899 if (cf_default_typesdb)
900 read_types_list (PKGDATADIR"/types.db");