2 * collectd - src/configfile.c
3 * Copyright (C) 2005-2008 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"}
103 static int cf_global_options_num = STATIC_ARRAY_LEN (cf_global_options);
105 static int cf_default_typesdb = 1;
108 * Functions to handle register/unregister, search, and other plugin related
111 static cf_callback_t *cf_search (const char *type)
113 cf_callback_t *cf_cb;
118 for (cf_cb = first_callback; cf_cb != NULL; cf_cb = cf_cb->next)
119 if (strcasecmp (cf_cb->type, type) == 0)
125 static int cf_dispatch (const char *type, const char *orig_key,
126 const char *orig_value)
128 cf_callback_t *cf_cb;
134 DEBUG ("type = %s, key = %s, value = %s",
136 ESCAPE_NULL(orig_key),
137 ESCAPE_NULL(orig_value));
139 if ((cf_cb = cf_search (type)) == NULL)
141 WARNING ("Found a configuration for the `%s' plugin, but "
142 "the plugin isn't loaded or didn't register "
143 "a configuration callback.", type);
147 if ((key = strdup (orig_key)) == NULL)
149 if ((value = strdup (orig_value)) == NULL)
157 for (i = 0; i < cf_cb->keys_num; i++)
159 if (strcasecmp (cf_cb->keys[i], key) == 0)
161 ret = (*cf_cb->callback) (key, value);
166 if (i >= cf_cb->keys_num)
167 WARNING ("Plugin `%s' did not register for value `%s'.", type, key);
172 DEBUG ("return (%i)", ret);
175 } /* int cf_dispatch */
177 static int dispatch_global_option (const oconfig_item_t *ci)
179 if (ci->values_num != 1)
181 if (ci->values[0].type == OCONFIG_TYPE_STRING)
182 return (global_option_set (ci->key, ci->values[0].value.string));
183 else if (ci->values[0].type == OCONFIG_TYPE_NUMBER)
186 ssnprintf (tmp, sizeof (tmp), "%lf", ci->values[0].value.number);
187 return (global_option_set (ci->key, tmp));
189 else if (ci->values[0].type == OCONFIG_TYPE_BOOLEAN)
191 if (ci->values[0].value.boolean)
192 return (global_option_set (ci->key, "true"));
194 return (global_option_set (ci->key, "false"));
198 } /* int dispatch_global_option */
200 static int dispatch_value_typesdb (const oconfig_item_t *ci)
204 assert (strcasecmp (ci->key, "TypesDB") == 0);
206 cf_default_typesdb = 0;
208 if (ci->values_num < 1)
211 for (i = 0; i < ci->values_num; ++i)
213 if (OCONFIG_TYPE_STRING != ci->values[i].type)
216 read_types_list (ci->values[i].value.string);
219 } /* int dispatch_value_typesdb */
221 static int dispatch_value_plugindir (const oconfig_item_t *ci)
223 assert (strcasecmp (ci->key, "PluginDir") == 0);
225 if (ci->values_num != 1)
227 if (ci->values[0].type != OCONFIG_TYPE_STRING)
230 plugin_set_dir (ci->values[0].value.string);
234 static int dispatch_value_loadplugin (const oconfig_item_t *ci)
236 assert (strcasecmp (ci->key, "LoadPlugin") == 0);
238 if (ci->values_num != 1)
240 if (ci->values[0].type != OCONFIG_TYPE_STRING)
243 return (plugin_load (ci->values[0].value.string));
244 } /* int dispatch_value_loadplugin */
246 static int dispatch_value_plugin (const char *plugin, oconfig_item_t *ci)
254 buffer_free = sizeof (buffer);
256 for (i = 0; i < ci->values_num; i++)
260 if (ci->values[i].type == OCONFIG_TYPE_STRING)
261 status = ssnprintf (buffer_ptr, buffer_free, " %s",
262 ci->values[i].value.string);
263 else if (ci->values[i].type == OCONFIG_TYPE_NUMBER)
264 status = ssnprintf (buffer_ptr, buffer_free, " %lf",
265 ci->values[i].value.number);
266 else if (ci->values[i].type == OCONFIG_TYPE_BOOLEAN)
267 status = ssnprintf (buffer_ptr, buffer_free, " %s",
268 ci->values[i].value.boolean
271 if ((status < 0) || (status >= buffer_free))
273 buffer_free -= status;
274 buffer_ptr += status;
276 /* skip the initial space */
277 buffer_ptr = buffer + 1;
279 return (cf_dispatch (plugin, ci->key, buffer_ptr));
280 } /* int dispatch_value_plugin */
282 static int dispatch_value (const oconfig_item_t *ci)
287 for (i = 0; i < cf_value_map_num; i++)
288 if (strcasecmp (cf_value_map[i].key, ci->key) == 0)
290 ret = cf_value_map[i].func (ci);
294 for (i = 0; i < cf_global_options_num; i++)
295 if (strcasecmp (cf_global_options[i].key, ci->key) == 0)
297 ret = dispatch_global_option (ci);
302 } /* int dispatch_value */
304 static int dispatch_block_plugin (oconfig_item_t *ci)
309 cf_complex_callback_t *cb;
311 if (strcasecmp (ci->key, "Plugin") != 0)
313 if (ci->values_num < 1)
315 if (ci->values[0].type != OCONFIG_TYPE_STRING)
318 name = ci->values[0].value.string;
320 /* Check for a complex callback first */
321 for (cb = complex_callback_head; cb != NULL; cb = cb->next)
322 if (strcasecmp (name, cb->type) == 0)
323 return (cb->callback (ci));
325 /* Hm, no complex plugin found. Dispatch the values one by one */
326 for (i = 0; i < ci->children_num; i++)
328 if (ci->children[i].children == NULL)
329 dispatch_value_plugin (name, ci->children + i);
331 {DEBUG ("No nested config blocks allowed for this plugin.");}
338 static int dispatch_block (oconfig_item_t *ci)
340 if (strcasecmp (ci->key, "Plugin") == 0)
341 return (dispatch_block_plugin (ci));
342 else if (strcasecmp (ci->key, "Threshold") == 0)
343 return (ut_config (ci));
344 else if (strcasecmp (ci->key, "Chain") == 0)
345 return (fc_configure (ci));
350 static int cf_ci_replace_child (oconfig_item_t *dst, oconfig_item_t *src,
353 oconfig_item_t *temp;
356 assert (offset >= 0);
357 assert (dst->children_num > offset);
359 /* Free the memory used by the replaced child. Usually that's the
360 * `Include "blah"' statement. */
361 temp = dst->children + offset;
362 for (i = 0; i < temp->values_num; i++)
364 if (temp->values[i].type == OCONFIG_TYPE_STRING)
366 sfree (temp->values[i].value.string);
369 sfree (temp->values);
372 /* If (src->children_num == 0) the array size is decreased. If offset
373 * is _not_ the last element, (offset < (src->children_num - 1)), then
374 * we need to move the trailing elements before resizing the array. */
375 if ((src->children_num == 0) && (offset < (src->children_num - 1)))
377 int nmemb = src->children_num - (offset + 1);
378 memmove (src->children + offset, src->children + offset + 1,
379 sizeof (oconfig_item_t) * nmemb);
382 /* Resize the memory containing the children to be big enough to hold
384 temp = (oconfig_item_t *) realloc (dst->children,
385 sizeof (oconfig_item_t)
386 * (dst->children_num + src->children_num - 1));
389 ERROR ("configfile: realloc failed.");
392 dst->children = temp;
394 /* If there are children behind the include statement, and they have
395 * not yet been moved because (src->children_num == 0), then move them
396 * to the end of the list, so that the new children have room before
398 if ((src->children_num > 0)
399 && ((dst->children_num - (offset + 1)) > 0))
401 int nmemb = dst->children_num - (offset + 1);
402 int old_offset = offset + 1;
403 int new_offset = offset + src->children_num;
405 memmove (dst->children + new_offset,
406 dst->children + old_offset,
407 sizeof (oconfig_item_t) * nmemb);
410 /* Last but not least: If there are new childrem, copy them to the
411 * memory reserved for them. */
412 if (src->children_num > 0)
414 memcpy (dst->children + offset,
416 sizeof (oconfig_item_t) * src->children_num);
419 /* Update the number of children. */
420 dst->children_num += (src->children_num - 1);
423 } /* int cf_ci_replace_child */
425 static int cf_ci_append_children (oconfig_item_t *dst, oconfig_item_t *src)
427 oconfig_item_t *temp;
429 if ((src == NULL) || (src->children_num == 0))
432 temp = (oconfig_item_t *) realloc (dst->children,
433 sizeof (oconfig_item_t)
434 * (dst->children_num + src->children_num));
437 ERROR ("configfile: realloc failed.");
440 dst->children = temp;
442 memcpy (dst->children + dst->children_num,
444 sizeof (oconfig_item_t)
445 * src->children_num);
446 dst->children_num += src->children_num;
449 } /* int cf_ci_append_children */
451 #define CF_MAX_DEPTH 8
452 static oconfig_item_t *cf_read_generic (const char *path, int depth);
454 static int cf_include_all (oconfig_item_t *root, int depth)
458 for (i = 0; i < root->children_num; i++)
463 /* Ignore all blocks, including `Include' blocks. */
464 if (root->children[i].children_num != 0)
467 if (strcasecmp (root->children[i].key, "Include") != 0)
470 old = root->children + i;
472 if ((old->values_num != 1)
473 || (old->values[0].type != OCONFIG_TYPE_STRING))
475 ERROR ("configfile: `Include' needs exactly one string argument.");
479 new = cf_read_generic (old->values[0].value.string, depth + 1);
483 /* Now replace the i'th child in `root' with `new'. */
484 cf_ci_replace_child (root, new, i);
488 } /* for (i = 0; i < root->children_num; i++) */
491 } /* int cf_include_all */
493 static oconfig_item_t *cf_read_file (const char *file, int depth)
495 oconfig_item_t *root;
497 assert (depth < CF_MAX_DEPTH);
499 root = oconfig_parse_file (file);
502 ERROR ("configfile: Cannot read file `%s'.", file);
506 cf_include_all (root, depth);
509 } /* oconfig_item_t *cf_read_file */
511 static int cf_compare_string (const void *p1, const void *p2)
513 return strcmp (*(const char **) p1, *(const char **) p2);
516 static oconfig_item_t *cf_read_dir (const char *dir, int depth)
518 oconfig_item_t *root = NULL;
521 char **filenames = NULL;
522 int filenames_num = 0;
526 assert (depth < CF_MAX_DEPTH);
532 ERROR ("configfile: opendir failed: %s",
533 sstrerror (errno, errbuf, sizeof (errbuf)));
537 root = (oconfig_item_t *) malloc (sizeof (oconfig_item_t));
540 ERROR ("configfile: malloc failed.");
543 memset (root, '\0', sizeof (oconfig_item_t));
545 while ((de = readdir (dh)) != NULL)
550 if ((de->d_name[0] == '.') || (de->d_name[0] == '\0'))
553 status = ssnprintf (name, sizeof (name), "%s/%s",
555 if (status >= sizeof (name))
557 ERROR ("configfile: Not including `%s/%s' because its"
558 " name is too long.",
560 for (i = 0; i < filenames_num; ++i)
568 tmp = (char **) realloc (filenames,
569 filenames_num * sizeof (*filenames));
571 ERROR ("configfile: realloc failed.");
572 for (i = 0; i < filenames_num - 1; ++i)
580 filenames[filenames_num - 1] = sstrdup (name);
583 qsort ((void *) filenames, filenames_num, sizeof (*filenames),
586 for (i = 0; i < filenames_num; ++i)
588 oconfig_item_t *temp;
589 char *name = filenames[i];
591 temp = cf_read_generic (name, depth);
594 for (j = i; j < filenames_num; ++j)
601 cf_ci_append_children (root, temp);
602 sfree (temp->children);
610 } /* oconfig_item_t *cf_read_dir */
615 * Path is stat'ed and either cf_read_file or cf_read_dir is called
618 * There are two versions of this function: If `wordexp' exists shell wildcards
619 * will be expanded and the function will include all matches found. If
620 * `wordexp' (or, more precisely, it's header file) is not available the
621 * simpler function is used which does not do any such expansion.
624 static oconfig_item_t *cf_read_generic (const char *path, int depth)
626 oconfig_item_t *root = NULL;
628 const char *path_ptr;
632 if (depth >= CF_MAX_DEPTH)
634 ERROR ("configfile: Not including `%s' because the maximum "
635 "nesting depth has been reached.", path);
639 status = wordexp (path, &we, WRDE_NOCMD);
642 ERROR ("configfile: wordexp (%s) failed.", path);
646 root = (oconfig_item_t *) malloc (sizeof (oconfig_item_t));
649 ERROR ("configfile: malloc failed.");
652 memset (root, '\0', sizeof (oconfig_item_t));
654 /* wordexp() might return a sorted list already. That's not
655 * documented though, so let's make sure we get what we want. */
656 qsort ((void *) we.we_wordv, we.we_wordc, sizeof (*we.we_wordv),
659 for (i = 0; i < we.we_wordc; i++)
661 oconfig_item_t *temp;
664 path_ptr = we.we_wordv[i];
666 status = stat (path_ptr, &statbuf);
670 ERROR ("configfile: stat (%s) failed: %s",
672 sstrerror (errno, errbuf, sizeof (errbuf)));
677 if (S_ISREG (statbuf.st_mode))
678 temp = cf_read_file (path_ptr, depth);
679 else if (S_ISDIR (statbuf.st_mode))
680 temp = cf_read_dir (path_ptr, depth);
683 ERROR ("configfile: %s is neither a file nor a "
693 cf_ci_append_children (root, temp);
694 sfree (temp->children);
701 } /* oconfig_item_t *cf_read_generic */
702 /* #endif HAVE_WORDEXP_H */
704 #else /* if !HAVE_WORDEXP_H */
705 static oconfig_item_t *cf_read_generic (const char *path, int depth)
710 if (depth >= CF_MAX_DEPTH)
712 ERROR ("configfile: Not including `%s' because the maximum "
713 "nesting depth has been reached.", path);
717 status = stat (path, &statbuf);
721 ERROR ("configfile: stat (%s) failed: %s",
723 sstrerror (errno, errbuf, sizeof (errbuf)));
727 if (S_ISREG (statbuf.st_mode))
728 return (cf_read_file (path, depth));
729 else if (S_ISDIR (statbuf.st_mode))
730 return (cf_read_dir (path, depth));
732 ERROR ("configfile: %s is neither a file nor a directory.", path);
734 } /* oconfig_item_t *cf_read_generic */
735 #endif /* !HAVE_WORDEXP_H */
740 int global_option_set (const char *option, const char *value)
744 DEBUG ("option = %s; value = %s;", option, value);
746 for (i = 0; i < cf_global_options_num; i++)
747 if (strcasecmp (cf_global_options[i].key, option) == 0)
750 if (i >= cf_global_options_num)
753 sfree (cf_global_options[i].value);
756 cf_global_options[i].value = strdup (value);
758 cf_global_options[i].value = NULL;
763 const char *global_option_get (const char *option)
767 for (i = 0; i < cf_global_options_num; i++)
768 if (strcasecmp (cf_global_options[i].key, option) == 0)
771 if (i >= cf_global_options_num)
774 return ((cf_global_options[i].value != NULL)
775 ? cf_global_options[i].value
776 : cf_global_options[i].def);
777 } /* char *global_option_get */
779 void cf_unregister (const char *type)
781 cf_callback_t *this, *prev;
783 for (prev = NULL, this = first_callback;
785 prev = this, this = this->next)
786 if (strcasecmp (this->type, type) == 0)
789 first_callback = this->next;
791 prev->next = this->next;
796 } /* void cf_unregister */
798 void cf_unregister_complex (const char *type)
800 cf_complex_callback_t *this, *prev;
802 for (prev = NULL, this = complex_callback_head;
804 prev = this, this = this->next)
805 if (strcasecmp (this->type, type) == 0)
808 complex_callback_head = this->next;
810 prev->next = this->next;
816 } /* void cf_unregister */
818 void cf_register (const char *type,
819 int (*callback) (const char *, const char *),
820 const char **keys, int keys_num)
822 cf_callback_t *cf_cb;
824 /* Remove this module from the list, if it already exists */
825 cf_unregister (type);
827 /* This pointer will be free'd in `cf_unregister' */
828 if ((cf_cb = (cf_callback_t *) malloc (sizeof (cf_callback_t))) == NULL)
832 cf_cb->callback = callback;
834 cf_cb->keys_num = keys_num;
836 cf_cb->next = first_callback;
837 first_callback = cf_cb;
838 } /* void cf_register */
840 int cf_register_complex (const char *type, int (*callback) (oconfig_item_t *))
842 cf_complex_callback_t *new;
844 new = (cf_complex_callback_t *) malloc (sizeof (cf_complex_callback_t));
848 new->type = strdup (type);
849 if (new->type == NULL)
855 new->callback = callback;
858 if (complex_callback_head == NULL)
860 complex_callback_head = new;
864 cf_complex_callback_t *last = complex_callback_head;
865 while (last->next != NULL)
871 } /* int cf_register_complex */
873 int cf_read (char *filename)
875 oconfig_item_t *conf;
878 conf = cf_read_generic (filename, 0 /* depth */);
881 ERROR ("Unable to read config file %s.", filename);
885 for (i = 0; i < conf->children_num; i++)
887 if (conf->children[i].children == NULL)
888 dispatch_value (conf->children + i);
890 dispatch_block (conf->children + i);
895 /* Read the default types.db if no `TypesDB' option was given. */
896 if (cf_default_typesdb)
897 read_types_list (PKGDATADIR"/types.db");