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_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, "false"},
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 if (dst->children_num + src->children_num - 1 == 0)
419 dst->children_num = 0;
423 temp = (oconfig_item_t *) realloc (dst->children,
424 sizeof (oconfig_item_t)
425 * (dst->children_num + src->children_num - 1));
428 ERROR ("configfile: realloc failed.");
431 dst->children = temp;
433 /* If there are children behind the include statement, and they have
434 * not yet been moved because (src->children_num == 0), then move them
435 * to the end of the list, so that the new children have room before
437 if ((src->children_num > 0)
438 && ((dst->children_num - (offset + 1)) > 0))
440 int nmemb = dst->children_num - (offset + 1);
441 int old_offset = offset + 1;
442 int new_offset = offset + src->children_num;
444 memmove (dst->children + new_offset,
445 dst->children + old_offset,
446 sizeof (oconfig_item_t) * nmemb);
449 /* Last but not least: If there are new children, copy them to the
450 * memory reserved for them. */
451 if (src->children_num > 0)
453 memcpy (dst->children + offset,
455 sizeof (oconfig_item_t) * src->children_num);
458 /* Update the number of children. */
459 dst->children_num += (src->children_num - 1);
462 } /* int cf_ci_replace_child */
464 static int cf_ci_append_children (oconfig_item_t *dst, oconfig_item_t *src)
466 oconfig_item_t *temp;
468 if ((src == NULL) || (src->children_num == 0))
471 temp = (oconfig_item_t *) realloc (dst->children,
472 sizeof (oconfig_item_t)
473 * (dst->children_num + src->children_num));
476 ERROR ("configfile: realloc failed.");
479 dst->children = temp;
481 memcpy (dst->children + dst->children_num,
483 sizeof (oconfig_item_t)
484 * src->children_num);
485 dst->children_num += src->children_num;
488 } /* int cf_ci_append_children */
490 #define CF_MAX_DEPTH 8
491 static oconfig_item_t *cf_read_generic (const char *path, int depth);
493 static int cf_include_all (oconfig_item_t *root, int depth)
497 for (i = 0; i < root->children_num; i++)
502 /* Ignore all blocks, including `Include' blocks. */
503 if (root->children[i].children_num != 0)
506 if (strcasecmp (root->children[i].key, "Include") != 0)
509 old = root->children + i;
511 if ((old->values_num != 1)
512 || (old->values[0].type != OCONFIG_TYPE_STRING))
514 ERROR ("configfile: `Include' needs exactly one string argument.");
518 new = cf_read_generic (old->values[0].value.string, depth + 1);
522 /* Now replace the i'th child in `root' with `new'. */
523 if (cf_ci_replace_child (root, new, i) < 0)
526 /* ... and go back to the new i'th child. */
531 } /* for (i = 0; i < root->children_num; i++) */
534 } /* int cf_include_all */
536 static oconfig_item_t *cf_read_file (const char *file, int depth)
538 oconfig_item_t *root;
540 assert (depth < CF_MAX_DEPTH);
542 root = oconfig_parse_file (file);
545 ERROR ("configfile: Cannot read file `%s'.", file);
549 cf_include_all (root, depth);
552 } /* oconfig_item_t *cf_read_file */
554 static int cf_compare_string (const void *p1, const void *p2)
556 return strcmp (*(const char **) p1, *(const char **) p2);
559 static oconfig_item_t *cf_read_dir (const char *dir, int depth)
561 oconfig_item_t *root = NULL;
564 char **filenames = NULL;
565 int filenames_num = 0;
569 assert (depth < CF_MAX_DEPTH);
575 ERROR ("configfile: opendir failed: %s",
576 sstrerror (errno, errbuf, sizeof (errbuf)));
580 root = (oconfig_item_t *) malloc (sizeof (oconfig_item_t));
583 ERROR ("configfile: malloc failed.");
586 memset (root, 0, sizeof (oconfig_item_t));
588 while ((de = readdir (dh)) != NULL)
593 if ((de->d_name[0] == '.') || (de->d_name[0] == 0))
596 status = ssnprintf (name, sizeof (name), "%s/%s",
598 if ((status < 0) || ((size_t) status >= sizeof (name)))
600 ERROR ("configfile: Not including `%s/%s' because its"
601 " name is too long.",
603 for (i = 0; i < filenames_num; ++i)
611 tmp = (char **) realloc (filenames,
612 filenames_num * sizeof (*filenames));
614 ERROR ("configfile: realloc failed.");
615 for (i = 0; i < filenames_num - 1; ++i)
623 filenames[filenames_num - 1] = sstrdup (name);
626 qsort ((void *) filenames, filenames_num, sizeof (*filenames),
629 for (i = 0; i < filenames_num; ++i)
631 oconfig_item_t *temp;
632 char *name = filenames[i];
634 temp = cf_read_generic (name, depth);
637 /* An error should already have been reported. */
642 cf_ci_append_children (root, temp);
643 sfree (temp->children);
651 } /* oconfig_item_t *cf_read_dir */
656 * Path is stat'ed and either cf_read_file or cf_read_dir is called
659 * There are two versions of this function: If `wordexp' exists shell wildcards
660 * will be expanded and the function will include all matches found. If
661 * `wordexp' (or, more precisely, it's header file) is not available the
662 * simpler function is used which does not do any such expansion.
665 static oconfig_item_t *cf_read_generic (const char *path, int depth)
667 oconfig_item_t *root = NULL;
669 const char *path_ptr;
673 if (depth >= CF_MAX_DEPTH)
675 ERROR ("configfile: Not including `%s' because the maximum "
676 "nesting depth has been reached.", path);
680 status = wordexp (path, &we, WRDE_NOCMD);
683 ERROR ("configfile: wordexp (%s) failed.", path);
687 root = (oconfig_item_t *) malloc (sizeof (oconfig_item_t));
690 ERROR ("configfile: malloc failed.");
693 memset (root, '\0', sizeof (oconfig_item_t));
695 /* wordexp() might return a sorted list already. That's not
696 * documented though, so let's make sure we get what we want. */
697 qsort ((void *) we.we_wordv, we.we_wordc, sizeof (*we.we_wordv),
700 for (i = 0; i < we.we_wordc; i++)
702 oconfig_item_t *temp;
705 path_ptr = we.we_wordv[i];
707 status = stat (path_ptr, &statbuf);
711 WARNING ("configfile: stat (%s) failed: %s",
713 sstrerror (errno, errbuf, sizeof (errbuf)));
717 if (S_ISREG (statbuf.st_mode))
718 temp = cf_read_file (path_ptr, depth);
719 else if (S_ISDIR (statbuf.st_mode))
720 temp = cf_read_dir (path_ptr, depth);
723 WARNING ("configfile: %s is neither a file nor a "
733 cf_ci_append_children (root, temp);
734 sfree (temp->children);
740 if (root->children == NULL)
747 } /* oconfig_item_t *cf_read_generic */
748 /* #endif HAVE_WORDEXP_H */
750 #else /* if !HAVE_WORDEXP_H */
751 static oconfig_item_t *cf_read_generic (const char *path, int depth)
756 if (depth >= CF_MAX_DEPTH)
758 ERROR ("configfile: Not including `%s' because the maximum "
759 "nesting depth has been reached.", path);
763 status = stat (path, &statbuf);
767 ERROR ("configfile: stat (%s) failed: %s",
769 sstrerror (errno, errbuf, sizeof (errbuf)));
773 if (S_ISREG (statbuf.st_mode))
774 return (cf_read_file (path, depth));
775 else if (S_ISDIR (statbuf.st_mode))
776 return (cf_read_dir (path, depth));
778 ERROR ("configfile: %s is neither a file nor a directory.", path);
780 } /* oconfig_item_t *cf_read_generic */
781 #endif /* !HAVE_WORDEXP_H */
786 int global_option_set (const char *option, const char *value)
790 DEBUG ("option = %s; value = %s;", option, value);
792 for (i = 0; i < cf_global_options_num; i++)
793 if (strcasecmp (cf_global_options[i].key, option) == 0)
796 if (i >= cf_global_options_num)
799 sfree (cf_global_options[i].value);
802 cf_global_options[i].value = strdup (value);
804 cf_global_options[i].value = NULL;
809 const char *global_option_get (const char *option)
813 for (i = 0; i < cf_global_options_num; i++)
814 if (strcasecmp (cf_global_options[i].key, option) == 0)
817 if (i >= cf_global_options_num)
820 return ((cf_global_options[i].value != NULL)
821 ? cf_global_options[i].value
822 : cf_global_options[i].def);
823 } /* char *global_option_get */
825 void cf_unregister (const char *type)
827 cf_callback_t *this, *prev;
829 for (prev = NULL, this = first_callback;
831 prev = this, this = this->next)
832 if (strcasecmp (this->type, type) == 0)
835 first_callback = this->next;
837 prev->next = this->next;
842 } /* void cf_unregister */
844 void cf_unregister_complex (const char *type)
846 cf_complex_callback_t *this, *prev;
848 for (prev = NULL, this = complex_callback_head;
850 prev = this, this = this->next)
851 if (strcasecmp (this->type, type) == 0)
854 complex_callback_head = this->next;
856 prev->next = this->next;
862 } /* void cf_unregister */
864 void cf_register (const char *type,
865 int (*callback) (const char *, const char *),
866 const char **keys, int keys_num)
868 cf_callback_t *cf_cb;
870 /* Remove this module from the list, if it already exists */
871 cf_unregister (type);
873 /* This pointer will be free'd in `cf_unregister' */
874 if ((cf_cb = (cf_callback_t *) malloc (sizeof (cf_callback_t))) == NULL)
878 cf_cb->callback = callback;
880 cf_cb->keys_num = keys_num;
882 cf_cb->next = first_callback;
883 first_callback = cf_cb;
884 } /* void cf_register */
886 int cf_register_complex (const char *type, int (*callback) (oconfig_item_t *))
888 cf_complex_callback_t *new;
890 new = (cf_complex_callback_t *) malloc (sizeof (cf_complex_callback_t));
894 new->type = strdup (type);
895 if (new->type == NULL)
901 new->callback = callback;
904 if (complex_callback_head == NULL)
906 complex_callback_head = new;
910 cf_complex_callback_t *last = complex_callback_head;
911 while (last->next != NULL)
917 } /* int cf_register_complex */
919 int cf_read (char *filename)
921 oconfig_item_t *conf;
924 conf = cf_read_generic (filename, 0 /* depth */);
927 ERROR ("Unable to read config file %s.", filename);
931 for (i = 0; i < conf->children_num; i++)
933 if (conf->children[i].children == NULL)
934 dispatch_value (conf->children + i);
936 dispatch_block (conf->children + i);
941 /* Read the default types.db if no `TypesDB' option was given. */
942 if (cf_default_typesdb)
943 read_types_list (PKGDATADIR"/types.db");
948 /* Assures the config option is a string, duplicates it and returns the copy in
949 * "ret_string". If necessary "*ret_string" is freed first. Returns zero upon
951 int cf_util_get_string (const oconfig_item_t *ci, char **ret_string) /* {{{ */
955 if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_STRING))
957 ERROR ("cf_util_get_string: The %s option requires "
958 "exactly one string argument.", ci->key);
962 string = strdup (ci->values[0].value.string);
966 if (*ret_string != NULL)
968 *ret_string = string;
971 } /* }}} int cf_util_get_string */
973 /* Assures the config option is a string and copies it to the provided buffer.
974 * Assures null-termination. */
975 int cf_util_get_string_buffer (const oconfig_item_t *ci, char *buffer, /* {{{ */
978 if ((ci == NULL) || (buffer == NULL) || (buffer_size < 1))
981 if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_STRING))
983 ERROR ("cf_util_get_string_buffer: The %s option requires "
984 "exactly one string argument.", ci->key);
988 strncpy (buffer, ci->values[0].value.string, buffer_size);
989 buffer[buffer_size - 1] = 0;
992 } /* }}} int cf_util_get_string_buffer */
994 /* Assures the config option is a number and returns it as an int. */
995 int cf_util_get_int (const oconfig_item_t *ci, int *ret_value) /* {{{ */
997 if ((ci == NULL) || (ret_value == NULL))
1000 if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_NUMBER))
1002 ERROR ("cf_util_get_int: The %s option requires "
1003 "exactly one numeric argument.", ci->key);
1007 *ret_value = (int) ci->values[0].value.number;
1010 } /* }}} int cf_util_get_int */
1012 int cf_util_get_boolean (const oconfig_item_t *ci, _Bool *ret_bool) /* {{{ */
1014 if ((ci == NULL) || (ret_bool == NULL))
1017 if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_BOOLEAN))
1019 ERROR ("cf_util_get_boolean: The %s option requires "
1020 "exactly one boolean argument.", ci->key);
1024 *ret_bool = ci->values[0].value.boolean ? true : false;
1027 } /* }}} int cf_util_get_boolean */
1029 /* Assures that the config option is a string. The string is then converted to
1030 * a port number using `service_name_to_port_number' and returned. Returns the
1031 * port number in the range [1-65535] or less than zero upon failure. */
1032 int cf_util_get_port_number (const oconfig_item_t *ci) /* {{{ */
1034 if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_STRING))
1036 ERROR ("cf_util_get_port_number: The %s option requires "
1037 "exactly one string argument.", ci->key);
1041 return (service_name_to_port_number (ci->values[0].value.string));
1042 } /* }}} int cf_util_get_port_number */