2 * collectd - src/configfile.c
3 * Copyright (C) 2005-2010 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_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_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, "true"},
100 {"Interval", NULL, "10"},
101 {"ReadThreads", NULL, "5"},
102 {"Timeout", NULL, "2"},
103 {"PreCacheChain", NULL, "PreCache"},
104 {"PostCacheChain", NULL, "PostCache"}
106 static int cf_global_options_num = STATIC_ARRAY_LEN (cf_global_options);
108 static int cf_default_typesdb = 1;
111 * Functions to handle register/unregister, search, and other plugin related
114 static cf_callback_t *cf_search (const char *type)
116 cf_callback_t *cf_cb;
121 for (cf_cb = first_callback; cf_cb != NULL; cf_cb = cf_cb->next)
122 if (strcasecmp (cf_cb->type, type) == 0)
128 static int cf_dispatch (const char *type, const char *orig_key,
129 const char *orig_value)
131 cf_callback_t *cf_cb;
137 DEBUG ("type = %s, key = %s, value = %s",
139 ESCAPE_NULL(orig_key),
140 ESCAPE_NULL(orig_value));
142 if ((cf_cb = cf_search (type)) == NULL)
144 WARNING ("Found a configuration for the `%s' plugin, but "
145 "the plugin isn't loaded or didn't register "
146 "a configuration callback.", type);
150 if ((key = strdup (orig_key)) == NULL)
152 if ((value = strdup (orig_value)) == NULL)
160 for (i = 0; i < cf_cb->keys_num; i++)
162 if ((cf_cb->keys[i] != NULL)
163 && (strcasecmp (cf_cb->keys[i], key) == 0))
165 ret = (*cf_cb->callback) (key, value);
170 if (i >= cf_cb->keys_num)
171 WARNING ("Plugin `%s' did not register for value `%s'.", type, key);
176 DEBUG ("cf_dispatch: return (%i)", ret);
179 } /* int cf_dispatch */
181 static int dispatch_global_option (const oconfig_item_t *ci)
183 if (ci->values_num != 1)
185 if (ci->values[0].type == OCONFIG_TYPE_STRING)
186 return (global_option_set (ci->key, ci->values[0].value.string));
187 else if (ci->values[0].type == OCONFIG_TYPE_NUMBER)
190 ssnprintf (tmp, sizeof (tmp), "%lf", ci->values[0].value.number);
191 return (global_option_set (ci->key, tmp));
193 else if (ci->values[0].type == OCONFIG_TYPE_BOOLEAN)
195 if (ci->values[0].value.boolean)
196 return (global_option_set (ci->key, "true"));
198 return (global_option_set (ci->key, "false"));
202 } /* int dispatch_global_option */
204 static int dispatch_value_typesdb (const oconfig_item_t *ci)
208 assert (strcasecmp (ci->key, "TypesDB") == 0);
210 cf_default_typesdb = 0;
212 if (ci->values_num < 1) {
213 ERROR ("configfile: `TypesDB' needs at least one argument.");
217 for (i = 0; i < ci->values_num; ++i)
219 if (OCONFIG_TYPE_STRING != ci->values[i].type) {
220 WARNING ("configfile: TypesDB: Skipping %i. argument which "
221 "is not a string.", i + 1);
225 read_types_list (ci->values[i].value.string);
228 } /* int dispatch_value_typesdb */
230 static int dispatch_value_plugindir (const oconfig_item_t *ci)
232 assert (strcasecmp (ci->key, "PluginDir") == 0);
234 if (ci->values_num != 1)
236 if (ci->values[0].type != OCONFIG_TYPE_STRING)
239 plugin_set_dir (ci->values[0].value.string);
243 static int dispatch_loadplugin (const oconfig_item_t *ci)
247 assert (strcasecmp (ci->key, "LoadPlugin") == 0);
249 if (ci->values_num != 1)
251 if (ci->values[0].type != OCONFIG_TYPE_STRING)
254 for (i = 0; i < ci->children_num; ++i) {
255 if (ci->children[i].values_num != 1 ||
256 ci->children[i].values[0].type != OCONFIG_TYPE_BOOLEAN) {
257 WARNING("Ignoring unknown LoadPlugin option %s for plugin %s", ci->children[i].key, ci->values[0].value.string);
260 if (strcasecmp(ci->children[i].key, "globals") == 0) {
261 flags |= PLUGIN_FLAGS_GLOBAL;
263 WARNING("Ignoring unknown LoadPlugin option %s for plugin %s", ci->children[i].key, ci->values[0].value.string);
266 return (plugin_load (ci->values[0].value.string, flags));
267 } /* int dispatch_value_loadplugin */
269 static int dispatch_value_plugin (const char *plugin, oconfig_item_t *ci)
277 buffer_free = sizeof (buffer);
279 for (i = 0; i < ci->values_num; i++)
283 if (ci->values[i].type == OCONFIG_TYPE_STRING)
284 status = ssnprintf (buffer_ptr, buffer_free, " %s",
285 ci->values[i].value.string);
286 else if (ci->values[i].type == OCONFIG_TYPE_NUMBER)
287 status = ssnprintf (buffer_ptr, buffer_free, " %lf",
288 ci->values[i].value.number);
289 else if (ci->values[i].type == OCONFIG_TYPE_BOOLEAN)
290 status = ssnprintf (buffer_ptr, buffer_free, " %s",
291 ci->values[i].value.boolean
294 if ((status < 0) || (status >= buffer_free))
296 buffer_free -= status;
297 buffer_ptr += status;
299 /* skip the initial space */
300 buffer_ptr = buffer + 1;
302 return (cf_dispatch (plugin, ci->key, buffer_ptr));
303 } /* int dispatch_value_plugin */
305 static int dispatch_value (const oconfig_item_t *ci)
310 for (i = 0; i < cf_value_map_num; i++)
311 if (strcasecmp (cf_value_map[i].key, ci->key) == 0)
313 ret = cf_value_map[i].func (ci);
317 for (i = 0; i < cf_global_options_num; i++)
318 if (strcasecmp (cf_global_options[i].key, ci->key) == 0)
320 ret = dispatch_global_option (ci);
325 } /* int dispatch_value */
327 static int dispatch_block_plugin (oconfig_item_t *ci)
332 cf_complex_callback_t *cb;
334 if (strcasecmp (ci->key, "Plugin") != 0)
336 if (ci->values_num < 1)
338 if (ci->values[0].type != OCONFIG_TYPE_STRING)
341 name = ci->values[0].value.string;
343 /* Check for a complex callback first */
344 for (cb = complex_callback_head; cb != NULL; cb = cb->next)
345 if (strcasecmp (name, cb->type) == 0)
346 return (cb->callback (ci));
348 /* Hm, no complex plugin found. Dispatch the values one by one */
349 for (i = 0; i < ci->children_num; i++)
351 if (ci->children[i].children == NULL)
352 dispatch_value_plugin (name, ci->children + i);
355 WARNING ("There is a `%s' block within the "
356 "configuration for the %s plugin. "
357 "The plugin either only expects "
358 "\"simple\" configuration statements "
359 "or wasn't loaded using `LoadPlugin'."
360 " Please check your configuration.",
361 ci->children[i].key, name);
369 static int dispatch_block (oconfig_item_t *ci)
371 if (strcasecmp (ci->key, "LoadPlugin") == 0)
372 return (dispatch_loadplugin (ci));
373 else if (strcasecmp (ci->key, "Plugin") == 0)
374 return (dispatch_block_plugin (ci));
375 else if (strcasecmp (ci->key, "Threshold") == 0)
376 return (ut_config (ci));
377 else if (strcasecmp (ci->key, "Chain") == 0)
378 return (fc_configure (ci));
383 static int cf_ci_replace_child (oconfig_item_t *dst, oconfig_item_t *src,
386 oconfig_item_t *temp;
389 assert (offset >= 0);
390 assert (dst->children_num > offset);
392 /* Free the memory used by the replaced child. Usually that's the
393 * `Include "blah"' statement. */
394 temp = dst->children + offset;
395 for (i = 0; i < temp->values_num; i++)
397 if (temp->values[i].type == OCONFIG_TYPE_STRING)
399 sfree (temp->values[i].value.string);
402 sfree (temp->values);
405 /* If (src->children_num == 0) the array size is decreased. If offset
406 * is _not_ the last element, (offset < (dst->children_num - 1)), then
407 * we need to move the trailing elements before resizing the array. */
408 if ((src->children_num == 0) && (offset < (dst->children_num - 1)))
410 int nmemb = dst->children_num - (offset + 1);
411 memmove (dst->children + offset, dst->children + offset + 1,
412 sizeof (oconfig_item_t) * nmemb);
415 /* Resize the memory containing the children to be big enough to hold
417 temp = (oconfig_item_t *) realloc (dst->children,
418 sizeof (oconfig_item_t)
419 * (dst->children_num + src->children_num - 1));
422 ERROR ("configfile: realloc failed.");
425 dst->children = temp;
427 /* If there are children behind the include statement, and they have
428 * not yet been moved because (src->children_num == 0), then move them
429 * to the end of the list, so that the new children have room before
431 if ((src->children_num > 0)
432 && ((dst->children_num - (offset + 1)) > 0))
434 int nmemb = dst->children_num - (offset + 1);
435 int old_offset = offset + 1;
436 int new_offset = offset + src->children_num;
438 memmove (dst->children + new_offset,
439 dst->children + old_offset,
440 sizeof (oconfig_item_t) * nmemb);
443 /* Last but not least: If there are new children, copy them to the
444 * memory reserved for them. */
445 if (src->children_num > 0)
447 memcpy (dst->children + offset,
449 sizeof (oconfig_item_t) * src->children_num);
452 /* Update the number of children. */
453 dst->children_num += (src->children_num - 1);
456 } /* int cf_ci_replace_child */
458 static int cf_ci_append_children (oconfig_item_t *dst, oconfig_item_t *src)
460 oconfig_item_t *temp;
462 if ((src == NULL) || (src->children_num == 0))
465 temp = (oconfig_item_t *) realloc (dst->children,
466 sizeof (oconfig_item_t)
467 * (dst->children_num + src->children_num));
470 ERROR ("configfile: realloc failed.");
473 dst->children = temp;
475 memcpy (dst->children + dst->children_num,
477 sizeof (oconfig_item_t)
478 * src->children_num);
479 dst->children_num += src->children_num;
482 } /* int cf_ci_append_children */
484 #define CF_MAX_DEPTH 8
485 static oconfig_item_t *cf_read_generic (const char *path, int depth);
487 static int cf_include_all (oconfig_item_t *root, int depth)
491 for (i = 0; i < root->children_num; i++)
496 /* Ignore all blocks, including `Include' blocks. */
497 if (root->children[i].children_num != 0)
500 if (strcasecmp (root->children[i].key, "Include") != 0)
503 old = root->children + i;
505 if ((old->values_num != 1)
506 || (old->values[0].type != OCONFIG_TYPE_STRING))
508 ERROR ("configfile: `Include' needs exactly one string argument.");
512 new = cf_read_generic (old->values[0].value.string, depth + 1);
516 /* Now replace the i'th child in `root' with `new'. */
517 cf_ci_replace_child (root, new, i);
519 /* ... and go back to the new i'th child. */
524 } /* for (i = 0; i < root->children_num; i++) */
527 } /* int cf_include_all */
529 static oconfig_item_t *cf_read_file (const char *file, int depth)
531 oconfig_item_t *root;
533 assert (depth < CF_MAX_DEPTH);
535 root = oconfig_parse_file (file);
538 ERROR ("configfile: Cannot read file `%s'.", file);
542 cf_include_all (root, depth);
545 } /* oconfig_item_t *cf_read_file */
547 static int cf_compare_string (const void *p1, const void *p2)
549 return strcmp (*(const char **) p1, *(const char **) p2);
552 static oconfig_item_t *cf_read_dir (const char *dir, int depth)
554 oconfig_item_t *root = NULL;
557 char **filenames = NULL;
558 int filenames_num = 0;
562 assert (depth < CF_MAX_DEPTH);
568 ERROR ("configfile: opendir failed: %s",
569 sstrerror (errno, errbuf, sizeof (errbuf)));
573 root = (oconfig_item_t *) malloc (sizeof (oconfig_item_t));
576 ERROR ("configfile: malloc failed.");
579 memset (root, 0, sizeof (oconfig_item_t));
581 while ((de = readdir (dh)) != NULL)
586 if ((de->d_name[0] == '.') || (de->d_name[0] == 0))
589 status = ssnprintf (name, sizeof (name), "%s/%s",
591 if ((status < 0) || ((size_t) status >= sizeof (name)))
593 ERROR ("configfile: Not including `%s/%s' because its"
594 " name is too long.",
596 for (i = 0; i < filenames_num; ++i)
604 tmp = (char **) realloc (filenames,
605 filenames_num * sizeof (*filenames));
607 ERROR ("configfile: realloc failed.");
608 for (i = 0; i < filenames_num - 1; ++i)
616 filenames[filenames_num - 1] = sstrdup (name);
619 qsort ((void *) filenames, filenames_num, sizeof (*filenames),
622 for (i = 0; i < filenames_num; ++i)
624 oconfig_item_t *temp;
625 char *name = filenames[i];
627 temp = cf_read_generic (name, depth);
630 /* An error should already have been reported. */
635 cf_ci_append_children (root, temp);
636 sfree (temp->children);
644 } /* oconfig_item_t *cf_read_dir */
649 * Path is stat'ed and either cf_read_file or cf_read_dir is called
652 * There are two versions of this function: If `wordexp' exists shell wildcards
653 * will be expanded and the function will include all matches found. If
654 * `wordexp' (or, more precisely, it's header file) is not available the
655 * simpler function is used which does not do any such expansion.
658 static oconfig_item_t *cf_read_generic (const char *path, int depth)
660 oconfig_item_t *root = NULL;
662 const char *path_ptr;
666 if (depth >= CF_MAX_DEPTH)
668 ERROR ("configfile: Not including `%s' because the maximum "
669 "nesting depth has been reached.", path);
673 status = wordexp (path, &we, WRDE_NOCMD);
676 ERROR ("configfile: wordexp (%s) failed.", path);
680 root = (oconfig_item_t *) malloc (sizeof (oconfig_item_t));
683 ERROR ("configfile: malloc failed.");
686 memset (root, '\0', sizeof (oconfig_item_t));
688 /* wordexp() might return a sorted list already. That's not
689 * documented though, so let's make sure we get what we want. */
690 qsort ((void *) we.we_wordv, we.we_wordc, sizeof (*we.we_wordv),
693 for (i = 0; i < we.we_wordc; i++)
695 oconfig_item_t *temp;
698 path_ptr = we.we_wordv[i];
700 status = stat (path_ptr, &statbuf);
704 ERROR ("configfile: stat (%s) failed: %s",
706 sstrerror (errno, errbuf, sizeof (errbuf)));
711 if (S_ISREG (statbuf.st_mode))
712 temp = cf_read_file (path_ptr, depth);
713 else if (S_ISDIR (statbuf.st_mode))
714 temp = cf_read_dir (path_ptr, depth);
717 ERROR ("configfile: %s is neither a file nor a "
727 cf_ci_append_children (root, temp);
728 sfree (temp->children);
735 } /* oconfig_item_t *cf_read_generic */
736 /* #endif HAVE_WORDEXP_H */
738 #else /* if !HAVE_WORDEXP_H */
739 static oconfig_item_t *cf_read_generic (const char *path, int depth)
744 if (depth >= CF_MAX_DEPTH)
746 ERROR ("configfile: Not including `%s' because the maximum "
747 "nesting depth has been reached.", path);
751 status = stat (path, &statbuf);
755 ERROR ("configfile: stat (%s) failed: %s",
757 sstrerror (errno, errbuf, sizeof (errbuf)));
761 if (S_ISREG (statbuf.st_mode))
762 return (cf_read_file (path, depth));
763 else if (S_ISDIR (statbuf.st_mode))
764 return (cf_read_dir (path, depth));
766 ERROR ("configfile: %s is neither a file nor a directory.", path);
768 } /* oconfig_item_t *cf_read_generic */
769 #endif /* !HAVE_WORDEXP_H */
774 int global_option_set (const char *option, const char *value)
778 DEBUG ("option = %s; value = %s;", option, value);
780 for (i = 0; i < cf_global_options_num; i++)
781 if (strcasecmp (cf_global_options[i].key, option) == 0)
784 if (i >= cf_global_options_num)
787 sfree (cf_global_options[i].value);
790 cf_global_options[i].value = strdup (value);
792 cf_global_options[i].value = NULL;
797 const char *global_option_get (const char *option)
801 for (i = 0; i < cf_global_options_num; i++)
802 if (strcasecmp (cf_global_options[i].key, option) == 0)
805 if (i >= cf_global_options_num)
808 return ((cf_global_options[i].value != NULL)
809 ? cf_global_options[i].value
810 : cf_global_options[i].def);
811 } /* char *global_option_get */
813 void cf_unregister (const char *type)
815 cf_callback_t *this, *prev;
817 for (prev = NULL, this = first_callback;
819 prev = this, this = this->next)
820 if (strcasecmp (this->type, type) == 0)
823 first_callback = this->next;
825 prev->next = this->next;
830 } /* void cf_unregister */
832 void cf_unregister_complex (const char *type)
834 cf_complex_callback_t *this, *prev;
836 for (prev = NULL, this = complex_callback_head;
838 prev = this, this = this->next)
839 if (strcasecmp (this->type, type) == 0)
842 complex_callback_head = this->next;
844 prev->next = this->next;
850 } /* void cf_unregister */
852 void cf_register (const char *type,
853 int (*callback) (const char *, const char *),
854 const char **keys, int keys_num)
856 cf_callback_t *cf_cb;
858 /* Remove this module from the list, if it already exists */
859 cf_unregister (type);
861 /* This pointer will be free'd in `cf_unregister' */
862 if ((cf_cb = (cf_callback_t *) malloc (sizeof (cf_callback_t))) == NULL)
866 cf_cb->callback = callback;
868 cf_cb->keys_num = keys_num;
870 cf_cb->next = first_callback;
871 first_callback = cf_cb;
872 } /* void cf_register */
874 int cf_register_complex (const char *type, int (*callback) (oconfig_item_t *))
876 cf_complex_callback_t *new;
878 new = (cf_complex_callback_t *) malloc (sizeof (cf_complex_callback_t));
882 new->type = strdup (type);
883 if (new->type == NULL)
889 new->callback = callback;
892 if (complex_callback_head == NULL)
894 complex_callback_head = new;
898 cf_complex_callback_t *last = complex_callback_head;
899 while (last->next != NULL)
905 } /* int cf_register_complex */
907 int cf_read (char *filename)
909 oconfig_item_t *conf;
912 conf = cf_read_generic (filename, 0 /* depth */);
915 ERROR ("Unable to read config file %s.", filename);
919 for (i = 0; i < conf->children_num; i++)
921 if (conf->children[i].children == NULL)
922 dispatch_value (conf->children + i);
924 dispatch_block (conf->children + i);
929 /* Read the default types.db if no `TypesDB' option was given. */
930 if (cf_default_typesdb)
931 read_types_list (PKGDATADIR"/types.db");
936 /* Assures the config option is a string, duplicates it and returns the copy in
937 * "ret_string". If necessary "*ret_string" is freed first. Returns zero upon
939 int cf_util_get_string (const oconfig_item_t *ci, char **ret_string) /* {{{ */
943 if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_STRING))
945 ERROR ("cf_util_get_string: The %s option requires "
946 "exactly one string argument.", ci->key);
950 string = strdup (ci->values[0].value.string);
954 if (*ret_string != NULL)
956 *ret_string = string;
959 } /* }}} int cf_util_get_string */
961 /* Assures the config option is a string and copies it to the provided buffer.
962 * Assures null-termination. */
963 int cf_util_get_string_buffer (const oconfig_item_t *ci, char *buffer, /* {{{ */
966 if ((ci == NULL) || (buffer == NULL) || (buffer_size < 1))
969 if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_STRING))
971 ERROR ("cf_util_get_string_buffer: The %s option requires "
972 "exactly one string argument.", ci->key);
976 strncpy (buffer, ci->values[0].value.string, buffer_size);
977 buffer[buffer_size - 1] = 0;
980 } /* }}} int cf_util_get_string_buffer */
982 /* Assures the config option is a number and returns it as an int. */
983 int cf_util_get_int (const oconfig_item_t *ci, int *ret_value) /* {{{ */
985 if ((ci == NULL) || (ret_value == NULL))
988 if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_NUMBER))
990 ERROR ("cf_util_get_int: The %s option requires "
991 "exactly one numeric argument.", ci->key);
995 *ret_value = (int) ci->values[0].value.number;
998 } /* }}} int cf_util_get_int */
1000 int cf_util_get_boolean (const oconfig_item_t *ci, _Bool *ret_bool) /* {{{ */
1002 if ((ci == NULL) || (ret_bool == NULL))
1005 if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_BOOLEAN))
1007 ERROR ("cf_util_get_boolean: The %s option requires "
1008 "exactly one boolean argument.", ci->key);
1012 *ret_bool = ci->values[0].value.boolean ? 1 : 0;
1015 } /* }}} int cf_util_get_boolean */
1017 int cf_util_get_flag (const oconfig_item_t *ci, /* {{{ */
1018 unsigned int *ret_value, unsigned int flag)
1023 if (ret_value == NULL)
1027 status = cf_util_get_boolean (ci, &b);
1037 *ret_value &= ~flag;
1041 } /* }}} int cf_util_get_flag */
1043 /* Assures that the config option is a string. The string is then converted to
1044 * a port number using `service_name_to_port_number' and returned. Returns the
1045 * port number in the range [1-65535] or less than zero upon failure. */
1046 int cf_util_get_port_number (const oconfig_item_t *ci) /* {{{ */
1048 if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_STRING))
1050 ERROR ("cf_util_get_port_number: The %s option requires "
1051 "exactly one string argument.", ci->key);
1055 return (service_name_to_port_number (ci->values[0].value.string));
1056 } /* }}} int cf_util_get_port_number */