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 ("cf_dispatch: 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) {
212 ERROR ("configfile: `TypesDB' needs at least one argument.");
216 for (i = 0; i < ci->values_num; ++i)
218 if (OCONFIG_TYPE_STRING != ci->values[i].type) {
219 WARNING ("configfile: TypesDB: Skipping %i. argument which "
220 "is not a string.", i + 1);
224 read_types_list (ci->values[i].value.string);
227 } /* int dispatch_value_typesdb */
229 static int dispatch_value_plugindir (const oconfig_item_t *ci)
231 assert (strcasecmp (ci->key, "PluginDir") == 0);
233 if (ci->values_num != 1)
235 if (ci->values[0].type != OCONFIG_TYPE_STRING)
238 plugin_set_dir (ci->values[0].value.string);
242 static int dispatch_value_loadplugin (const oconfig_item_t *ci)
244 assert (strcasecmp (ci->key, "LoadPlugin") == 0);
246 if (ci->values_num != 1)
248 if (ci->values[0].type != OCONFIG_TYPE_STRING)
251 return (plugin_load (ci->values[0].value.string));
252 } /* int dispatch_value_loadplugin */
254 static int dispatch_value_plugin (const char *plugin, oconfig_item_t *ci)
262 buffer_free = sizeof (buffer);
264 for (i = 0; i < ci->values_num; i++)
268 if (ci->values[i].type == OCONFIG_TYPE_STRING)
269 status = ssnprintf (buffer_ptr, buffer_free, " %s",
270 ci->values[i].value.string);
271 else if (ci->values[i].type == OCONFIG_TYPE_NUMBER)
272 status = ssnprintf (buffer_ptr, buffer_free, " %lf",
273 ci->values[i].value.number);
274 else if (ci->values[i].type == OCONFIG_TYPE_BOOLEAN)
275 status = ssnprintf (buffer_ptr, buffer_free, " %s",
276 ci->values[i].value.boolean
279 if ((status < 0) || (status >= buffer_free))
281 buffer_free -= status;
282 buffer_ptr += status;
284 /* skip the initial space */
285 buffer_ptr = buffer + 1;
287 return (cf_dispatch (plugin, ci->key, buffer_ptr));
288 } /* int dispatch_value_plugin */
290 static int dispatch_value (const oconfig_item_t *ci)
295 for (i = 0; i < cf_value_map_num; i++)
296 if (strcasecmp (cf_value_map[i].key, ci->key) == 0)
298 ret = cf_value_map[i].func (ci);
302 for (i = 0; i < cf_global_options_num; i++)
303 if (strcasecmp (cf_global_options[i].key, ci->key) == 0)
305 ret = dispatch_global_option (ci);
310 } /* int dispatch_value */
312 static int dispatch_block_plugin (oconfig_item_t *ci)
317 cf_complex_callback_t *cb;
319 if (strcasecmp (ci->key, "Plugin") != 0)
321 if (ci->values_num < 1)
323 if (ci->values[0].type != OCONFIG_TYPE_STRING)
326 name = ci->values[0].value.string;
328 /* Check for a complex callback first */
329 for (cb = complex_callback_head; cb != NULL; cb = cb->next)
330 if (strcasecmp (name, cb->type) == 0)
331 return (cb->callback (ci));
333 /* Hm, no complex plugin found. Dispatch the values one by one */
334 for (i = 0; i < ci->children_num; i++)
336 if (ci->children[i].children == NULL)
337 dispatch_value_plugin (name, ci->children + i);
340 WARNING ("There is a `%s' block within the "
341 "configuration for the %s plugin. "
342 "The plugin either only expects "
343 "\"simple\" configuration statements "
344 "or wasn't loaded using `LoadPlugin'."
345 " Please check your configuration.",
346 ci->children[i].key, name);
354 static int dispatch_block (oconfig_item_t *ci)
356 if (strcasecmp (ci->key, "Plugin") == 0)
357 return (dispatch_block_plugin (ci));
358 else if (strcasecmp (ci->key, "Threshold") == 0)
359 return (ut_config (ci));
360 else if (strcasecmp (ci->key, "Chain") == 0)
361 return (fc_configure (ci));
366 static int cf_ci_replace_child (oconfig_item_t *dst, oconfig_item_t *src,
369 oconfig_item_t *temp;
372 assert (offset >= 0);
373 assert (dst->children_num > offset);
375 /* Free the memory used by the replaced child. Usually that's the
376 * `Include "blah"' statement. */
377 temp = dst->children + offset;
378 for (i = 0; i < temp->values_num; i++)
380 if (temp->values[i].type == OCONFIG_TYPE_STRING)
382 sfree (temp->values[i].value.string);
385 sfree (temp->values);
388 /* If (src->children_num == 0) the array size is decreased. If offset
389 * is _not_ the last element, (offset < (dst->children_num - 1)), then
390 * we need to move the trailing elements before resizing the array. */
391 if ((src->children_num == 0) && (offset < (dst->children_num - 1)))
393 int nmemb = dst->children_num - (offset + 1);
394 memmove (dst->children + offset, dst->children + offset + 1,
395 sizeof (oconfig_item_t) * nmemb);
398 /* Resize the memory containing the children to be big enough to hold
400 temp = (oconfig_item_t *) realloc (dst->children,
401 sizeof (oconfig_item_t)
402 * (dst->children_num + src->children_num - 1));
405 ERROR ("configfile: realloc failed.");
408 dst->children = temp;
410 /* If there are children behind the include statement, and they have
411 * not yet been moved because (src->children_num == 0), then move them
412 * to the end of the list, so that the new children have room before
414 if ((src->children_num > 0)
415 && ((dst->children_num - (offset + 1)) > 0))
417 int nmemb = dst->children_num - (offset + 1);
418 int old_offset = offset + 1;
419 int new_offset = offset + src->children_num;
421 memmove (dst->children + new_offset,
422 dst->children + old_offset,
423 sizeof (oconfig_item_t) * nmemb);
426 /* Last but not least: If there are new children, copy them to the
427 * memory reserved for them. */
428 if (src->children_num > 0)
430 memcpy (dst->children + offset,
432 sizeof (oconfig_item_t) * src->children_num);
435 /* Update the number of children. */
436 dst->children_num += (src->children_num - 1);
439 } /* int cf_ci_replace_child */
441 static int cf_ci_append_children (oconfig_item_t *dst, oconfig_item_t *src)
443 oconfig_item_t *temp;
445 if ((src == NULL) || (src->children_num == 0))
448 temp = (oconfig_item_t *) realloc (dst->children,
449 sizeof (oconfig_item_t)
450 * (dst->children_num + src->children_num));
453 ERROR ("configfile: realloc failed.");
456 dst->children = temp;
458 memcpy (dst->children + dst->children_num,
460 sizeof (oconfig_item_t)
461 * src->children_num);
462 dst->children_num += src->children_num;
465 } /* int cf_ci_append_children */
467 #define CF_MAX_DEPTH 8
468 static oconfig_item_t *cf_read_generic (const char *path, int depth);
470 static int cf_include_all (oconfig_item_t *root, int depth)
474 for (i = 0; i < root->children_num; i++)
479 /* Ignore all blocks, including `Include' blocks. */
480 if (root->children[i].children_num != 0)
483 if (strcasecmp (root->children[i].key, "Include") != 0)
486 old = root->children + i;
488 if ((old->values_num != 1)
489 || (old->values[0].type != OCONFIG_TYPE_STRING))
491 ERROR ("configfile: `Include' needs exactly one string argument.");
495 new = cf_read_generic (old->values[0].value.string, depth + 1);
499 /* Now replace the i'th child in `root' with `new'. */
500 cf_ci_replace_child (root, new, i);
502 /* ... and go back to the new i'th child. */
507 } /* for (i = 0; i < root->children_num; i++) */
510 } /* int cf_include_all */
512 static oconfig_item_t *cf_read_file (const char *file, int depth)
514 oconfig_item_t *root;
516 assert (depth < CF_MAX_DEPTH);
518 root = oconfig_parse_file (file);
521 ERROR ("configfile: Cannot read file `%s'.", file);
525 cf_include_all (root, depth);
528 } /* oconfig_item_t *cf_read_file */
530 static int cf_compare_string (const void *p1, const void *p2)
532 return strcmp (*(const char **) p1, *(const char **) p2);
535 static oconfig_item_t *cf_read_dir (const char *dir, int depth)
537 oconfig_item_t *root = NULL;
540 char **filenames = NULL;
541 int filenames_num = 0;
545 assert (depth < CF_MAX_DEPTH);
551 ERROR ("configfile: opendir failed: %s",
552 sstrerror (errno, errbuf, sizeof (errbuf)));
556 root = (oconfig_item_t *) malloc (sizeof (oconfig_item_t));
559 ERROR ("configfile: malloc failed.");
562 memset (root, '\0', sizeof (oconfig_item_t));
564 while ((de = readdir (dh)) != NULL)
569 if ((de->d_name[0] == '.') || (de->d_name[0] == '\0'))
572 status = ssnprintf (name, sizeof (name), "%s/%s",
574 if ((status < 0) || ((size_t) status >= sizeof (name)))
576 ERROR ("configfile: Not including `%s/%s' because its"
577 " name is too long.",
579 for (i = 0; i < filenames_num; ++i)
587 tmp = (char **) realloc (filenames,
588 filenames_num * sizeof (*filenames));
590 ERROR ("configfile: realloc failed.");
591 for (i = 0; i < filenames_num - 1; ++i)
599 filenames[filenames_num - 1] = sstrdup (name);
602 qsort ((void *) filenames, filenames_num, sizeof (*filenames),
605 for (i = 0; i < filenames_num; ++i)
607 oconfig_item_t *temp;
608 char *name = filenames[i];
610 temp = cf_read_generic (name, depth);
613 for (j = i; j < filenames_num; ++j)
620 cf_ci_append_children (root, temp);
621 sfree (temp->children);
629 } /* oconfig_item_t *cf_read_dir */
634 * Path is stat'ed and either cf_read_file or cf_read_dir is called
637 * There are two versions of this function: If `wordexp' exists shell wildcards
638 * will be expanded and the function will include all matches found. If
639 * `wordexp' (or, more precisely, it's header file) is not available the
640 * simpler function is used which does not do any such expansion.
643 static oconfig_item_t *cf_read_generic (const char *path, int depth)
645 oconfig_item_t *root = NULL;
647 const char *path_ptr;
651 if (depth >= CF_MAX_DEPTH)
653 ERROR ("configfile: Not including `%s' because the maximum "
654 "nesting depth has been reached.", path);
658 status = wordexp (path, &we, WRDE_NOCMD);
661 ERROR ("configfile: wordexp (%s) failed.", path);
665 root = (oconfig_item_t *) malloc (sizeof (oconfig_item_t));
668 ERROR ("configfile: malloc failed.");
671 memset (root, '\0', sizeof (oconfig_item_t));
673 /* wordexp() might return a sorted list already. That's not
674 * documented though, so let's make sure we get what we want. */
675 qsort ((void *) we.we_wordv, we.we_wordc, sizeof (*we.we_wordv),
678 for (i = 0; i < we.we_wordc; i++)
680 oconfig_item_t *temp;
683 path_ptr = we.we_wordv[i];
685 status = stat (path_ptr, &statbuf);
689 ERROR ("configfile: stat (%s) failed: %s",
691 sstrerror (errno, errbuf, sizeof (errbuf)));
696 if (S_ISREG (statbuf.st_mode))
697 temp = cf_read_file (path_ptr, depth);
698 else if (S_ISDIR (statbuf.st_mode))
699 temp = cf_read_dir (path_ptr, depth);
702 ERROR ("configfile: %s is neither a file nor a "
712 cf_ci_append_children (root, temp);
713 sfree (temp->children);
720 } /* oconfig_item_t *cf_read_generic */
721 /* #endif HAVE_WORDEXP_H */
723 #else /* if !HAVE_WORDEXP_H */
724 static oconfig_item_t *cf_read_generic (const char *path, int depth)
729 if (depth >= CF_MAX_DEPTH)
731 ERROR ("configfile: Not including `%s' because the maximum "
732 "nesting depth has been reached.", path);
736 status = stat (path, &statbuf);
740 ERROR ("configfile: stat (%s) failed: %s",
742 sstrerror (errno, errbuf, sizeof (errbuf)));
746 if (S_ISREG (statbuf.st_mode))
747 return (cf_read_file (path, depth));
748 else if (S_ISDIR (statbuf.st_mode))
749 return (cf_read_dir (path, depth));
751 ERROR ("configfile: %s is neither a file nor a directory.", path);
753 } /* oconfig_item_t *cf_read_generic */
754 #endif /* !HAVE_WORDEXP_H */
759 int global_option_set (const char *option, const char *value)
763 DEBUG ("option = %s; value = %s;", option, value);
765 for (i = 0; i < cf_global_options_num; i++)
766 if (strcasecmp (cf_global_options[i].key, option) == 0)
769 if (i >= cf_global_options_num)
772 sfree (cf_global_options[i].value);
775 cf_global_options[i].value = strdup (value);
777 cf_global_options[i].value = NULL;
782 const char *global_option_get (const char *option)
786 for (i = 0; i < cf_global_options_num; i++)
787 if (strcasecmp (cf_global_options[i].key, option) == 0)
790 if (i >= cf_global_options_num)
793 return ((cf_global_options[i].value != NULL)
794 ? cf_global_options[i].value
795 : cf_global_options[i].def);
796 } /* char *global_option_get */
798 void cf_unregister (const char *type)
800 cf_callback_t *this, *prev;
802 for (prev = NULL, this = first_callback;
804 prev = this, this = this->next)
805 if (strcasecmp (this->type, type) == 0)
808 first_callback = this->next;
810 prev->next = this->next;
815 } /* void cf_unregister */
817 void cf_unregister_complex (const char *type)
819 cf_complex_callback_t *this, *prev;
821 for (prev = NULL, this = complex_callback_head;
823 prev = this, this = this->next)
824 if (strcasecmp (this->type, type) == 0)
827 complex_callback_head = this->next;
829 prev->next = this->next;
835 } /* void cf_unregister */
837 void cf_register (const char *type,
838 int (*callback) (const char *, const char *),
839 const char **keys, int keys_num)
841 cf_callback_t *cf_cb;
843 /* Remove this module from the list, if it already exists */
844 cf_unregister (type);
846 /* This pointer will be free'd in `cf_unregister' */
847 if ((cf_cb = (cf_callback_t *) malloc (sizeof (cf_callback_t))) == NULL)
851 cf_cb->callback = callback;
853 cf_cb->keys_num = keys_num;
855 cf_cb->next = first_callback;
856 first_callback = cf_cb;
857 } /* void cf_register */
859 int cf_register_complex (const char *type, int (*callback) (oconfig_item_t *))
861 cf_complex_callback_t *new;
863 new = (cf_complex_callback_t *) malloc (sizeof (cf_complex_callback_t));
867 new->type = strdup (type);
868 if (new->type == NULL)
874 new->callback = callback;
877 if (complex_callback_head == NULL)
879 complex_callback_head = new;
883 cf_complex_callback_t *last = complex_callback_head;
884 while (last->next != NULL)
890 } /* int cf_register_complex */
892 int cf_read (char *filename)
894 oconfig_item_t *conf;
897 conf = cf_read_generic (filename, 0 /* depth */);
900 ERROR ("Unable to read config file %s.", filename);
904 for (i = 0; i < conf->children_num; i++)
906 if (conf->children[i].children == NULL)
907 dispatch_value (conf->children + i);
909 dispatch_block (conf->children + i);
914 /* Read the default types.db if no `TypesDB' option was given. */
915 if (cf_default_typesdb)
916 read_types_list (PKGDATADIR"/types.db");
921 /* Assures the config option is a string, duplicates it and returns the copy in
922 * "ret_string". If necessary "*ret_string" is freed first. Returns zero upon
924 int cf_util_get_string (const oconfig_item_t *ci, char **ret_string) /* {{{ */
928 if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_STRING))
930 ERROR ("cf_util_get_string: The %s option requires "
931 "exactly one string argument.", ci->key);
935 string = strdup (ci->values[0].value.string);
939 if (*ret_string != NULL)
941 *ret_string = string;
944 } /* }}} int cf_util_get_string */
946 /* Assures that the config option is a string. The string is then converted to
947 * a port number using `service_name_to_port_number' and returned. Returns the
948 * port number in the range [1-65535] or less than zero upon failure. */
949 int cf_util_get_port_number (const oconfig_item_t *ci) /* {{{ */
951 if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_STRING))
953 ERROR ("cf_util_get_port_number: The %s option requires "
954 "exactly one string argument.", ci->key);
958 return (service_name_to_port_number (ci->values[0].value.string));
959 } /* }}} int cf_util_get_port_number */