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"
36 #endif /* HAVE_WORDEXP_H */
38 #define ESCAPE_NULL(str) ((str) == NULL ? "(null)" : (str))
43 typedef struct cf_callback
46 int (*callback) (const char *, const char *);
49 struct cf_callback *next;
52 typedef struct cf_complex_callback_s
55 int (*callback) (oconfig_item_t *);
56 struct cf_complex_callback_s *next;
57 } cf_complex_callback_t;
59 typedef struct cf_value_map_s
62 int (*func) (const oconfig_item_t *);
65 typedef struct cf_global_option_s
73 * Prototypes of callback functions
75 static int dispatch_value_typesdb (const oconfig_item_t *ci);
76 static int dispatch_value_plugindir (const oconfig_item_t *ci);
77 static int dispatch_value_loadplugin (const oconfig_item_t *ci);
82 static cf_callback_t *first_callback = NULL;
83 static cf_complex_callback_t *complex_callback_head = NULL;
85 static cf_value_map_t cf_value_map[] =
87 {"TypesDB", dispatch_value_typesdb},
88 {"PluginDir", dispatch_value_plugindir},
89 {"LoadPlugin", dispatch_value_loadplugin}
91 static int cf_value_map_num = STATIC_ARRAY_LEN (cf_value_map);
93 static cf_global_option_t cf_global_options[] =
95 {"BaseDir", NULL, PKGLOCALSTATEDIR},
96 {"PIDFile", NULL, PIDFILE},
97 {"Hostname", NULL, NULL},
98 {"FQDNLookup", NULL, "false"},
99 {"Interval", NULL, "10"},
100 {"ReadThreads", NULL, "5"}
102 static int cf_global_options_num = STATIC_ARRAY_LEN (cf_global_options);
104 static int cf_default_typesdb = 1;
107 * Functions to handle register/unregister, search, and other plugin related
110 static cf_callback_t *cf_search (const char *type)
112 cf_callback_t *cf_cb;
117 for (cf_cb = first_callback; cf_cb != NULL; cf_cb = cf_cb->next)
118 if (strcasecmp (cf_cb->type, type) == 0)
124 static int cf_dispatch (const char *type, const char *orig_key,
125 const char *orig_value)
127 cf_callback_t *cf_cb;
133 DEBUG ("type = %s, key = %s, value = %s",
135 ESCAPE_NULL(orig_key),
136 ESCAPE_NULL(orig_value));
138 if ((cf_cb = cf_search (type)) == NULL)
140 WARNING ("Found a configuration for the `%s' plugin, but "
141 "the plugin isn't loaded or didn't register "
142 "a configuration callback.", type);
146 if ((key = strdup (orig_key)) == NULL)
148 if ((value = strdup (orig_value)) == NULL)
156 for (i = 0; i < cf_cb->keys_num; i++)
158 if (strcasecmp (cf_cb->keys[i], key) == 0)
160 ret = (*cf_cb->callback) (key, value);
165 if (i >= cf_cb->keys_num)
166 WARNING ("Plugin `%s' did not register for value `%s'.", type, key);
171 DEBUG ("return (%i)", ret);
174 } /* int cf_dispatch */
176 static int dispatch_global_option (const oconfig_item_t *ci)
178 if (ci->values_num != 1)
180 if (ci->values[0].type == OCONFIG_TYPE_STRING)
181 return (global_option_set (ci->key, ci->values[0].value.string));
182 else if (ci->values[0].type == OCONFIG_TYPE_NUMBER)
185 snprintf (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 = snprintf (buffer_ptr, buffer_free, " %s",
262 ci->values[i].value.string);
263 else if (ci->values[i].type == OCONFIG_TYPE_NUMBER)
264 status = snprintf (buffer_ptr, buffer_free, " %lf",
265 ci->values[i].value.number);
266 else if (ci->values[i].type == OCONFIG_TYPE_BOOLEAN)
267 status = snprintf (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 plugin_conf_dispatch */
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));
348 static int cf_ci_replace_child (oconfig_item_t *dst, oconfig_item_t *src,
351 oconfig_item_t *temp;
354 assert (offset >= 0);
355 assert (dst->children_num > offset);
357 /* Free the memory used by the replaced child. Usually that's the
358 * `Include "blah"' statement. */
359 temp = dst->children + offset;
360 for (i = 0; i < temp->values_num; i++)
362 if (temp->values[i].type == OCONFIG_TYPE_STRING)
364 sfree (temp->values[i].value.string);
367 sfree (temp->values);
370 /* If (src->children_num == 0) the array size is decreased. If offset
371 * is _not_ the last element, (offset < (src->children_num - 1)), then
372 * we need to move the trailing elements before resizing the array. */
373 if ((src->children_num == 0) && (offset < (src->children_num - 1)))
375 int nmemb = src->children_num - (offset + 1);
376 memmove (src->children + offset, src->children + offset + 1,
377 sizeof (oconfig_item_t) * nmemb);
380 /* Resize the memory containing the children to be big enough to hold
382 temp = (oconfig_item_t *) realloc (dst->children,
383 sizeof (oconfig_item_t)
384 * (dst->children_num + src->children_num - 1));
387 ERROR ("configfile: realloc failed.");
390 dst->children = temp;
392 /* If there are children behind the include statement, and they have
393 * not yet been moved because (src->children_num == 0), then move them
394 * to the end of the list, so that the new children have room before
396 if ((src->children_num > 0)
397 && ((dst->children_num - (offset + 1)) > 0))
399 int nmemb = dst->children_num - (offset + 1);
400 int old_offset = offset + 1;
401 int new_offset = offset + src->children_num;
403 memmove (dst->children + new_offset,
404 dst->children + old_offset,
405 sizeof (oconfig_item_t) * nmemb);
408 /* Last but not least: If there are new childrem, copy them to the
409 * memory reserved for them. */
410 if (src->children_num > 0)
412 memcpy (dst->children + offset,
414 sizeof (oconfig_item_t) * src->children_num);
417 /* Update the number of children. */
418 dst->children_num += (src->children_num - 1);
421 } /* int cf_ci_replace_child */
423 static int cf_ci_append_children (oconfig_item_t *dst, oconfig_item_t *src)
425 oconfig_item_t *temp;
427 if ((src == NULL) || (src->children_num == 0))
430 temp = (oconfig_item_t *) realloc (dst->children,
431 sizeof (oconfig_item_t)
432 * (dst->children_num + src->children_num));
435 ERROR ("configfile: realloc failed.");
438 dst->children = temp;
440 memcpy (dst->children + dst->children_num,
442 sizeof (oconfig_item_t)
443 * src->children_num);
444 dst->children_num += src->children_num;
447 } /* int cf_ci_append_children */
449 #define CF_MAX_DEPTH 8
450 static oconfig_item_t *cf_read_generic (const char *path, int depth);
452 static int cf_include_all (oconfig_item_t *root, int depth)
456 for (i = 0; i < root->children_num; i++)
461 /* Ignore all blocks, including `Include' blocks. */
462 if (root->children[i].children_num != 0)
465 if (strcasecmp (root->children[i].key, "Include") != 0)
468 old = root->children + i;
470 if ((old->values_num != 1)
471 || (old->values[0].type != OCONFIG_TYPE_STRING))
473 ERROR ("configfile: `Include' needs exactly one string argument.");
477 new = cf_read_generic (old->values[0].value.string, depth + 1);
481 /* Now replace the i'th child in `root' with `new'. */
482 cf_ci_replace_child (root, new, i);
486 } /* for (i = 0; i < root->children_num; i++) */
489 } /* int cf_include_all */
491 static oconfig_item_t *cf_read_file (const char *file, int depth)
493 oconfig_item_t *root;
495 assert (depth < CF_MAX_DEPTH);
497 root = oconfig_parse_file (file);
500 ERROR ("configfile: Cannot read file `%s'.", file);
504 cf_include_all (root, depth);
507 } /* oconfig_item_t *cf_read_file */
509 static int cf_compare_string (const void *p1, const void *p2)
511 return strcmp (*(const char **) p1, *(const char **) p2);
514 static oconfig_item_t *cf_read_dir (const char *dir, int depth)
516 oconfig_item_t *root = NULL;
519 char **filenames = NULL;
520 int filenames_num = 0;
524 assert (depth < CF_MAX_DEPTH);
530 ERROR ("configfile: opendir failed: %s",
531 sstrerror (errno, errbuf, sizeof (errbuf)));
535 root = (oconfig_item_t *) malloc (sizeof (oconfig_item_t));
538 ERROR ("configfile: malloc failed.");
541 memset (root, '\0', sizeof (oconfig_item_t));
543 while ((de = readdir (dh)) != NULL)
548 if ((de->d_name[0] == '.') || (de->d_name[0] == '\0'))
551 status = snprintf (name, sizeof (name), "%s/%s",
553 if (status >= sizeof (name))
555 ERROR ("configfile: Not including `%s/%s' because its"
556 " name is too long.",
558 for (i = 0; i < filenames_num; ++i)
566 tmp = (char **) realloc (filenames,
567 filenames_num * sizeof (*filenames));
569 ERROR ("configfile: realloc failed.");
570 for (i = 0; i < filenames_num - 1; ++i)
578 filenames[filenames_num - 1] = sstrdup (name);
581 qsort ((void *) filenames, filenames_num, sizeof (*filenames),
584 for (i = 0; i < filenames_num; ++i)
586 oconfig_item_t *temp;
587 char *name = filenames[i];
589 temp = cf_read_generic (name, depth);
592 for (j = i; j < filenames_num; ++j)
599 cf_ci_append_children (root, temp);
600 sfree (temp->children);
608 } /* oconfig_item_t *cf_read_dir */
613 * Path is stat'ed and either cf_read_file or cf_read_dir is called
616 * There are two versions of this function: If `wordexp' exists shell wildcards
617 * will be expanded and the function will include all matches found. If
618 * `wordexp' (or, more precisely, it's header file) is not available the
619 * simpler function is used which does not do any such expansion.
622 static oconfig_item_t *cf_read_generic (const char *path, int depth)
624 oconfig_item_t *root = NULL;
626 const char *path_ptr;
630 if (depth >= CF_MAX_DEPTH)
632 ERROR ("configfile: Not including `%s' because the maximum "
633 "nesting depth has been reached.", path);
637 status = wordexp (path, &we, WRDE_NOCMD);
640 ERROR ("configfile: wordexp (%s) failed.", path);
644 root = (oconfig_item_t *) malloc (sizeof (oconfig_item_t));
647 ERROR ("configfile: malloc failed.");
650 memset (root, '\0', sizeof (oconfig_item_t));
652 /* wordexp() might return a sorted list already. That's not
653 * documented though, so let's make sure we get what we want. */
654 qsort ((void *) we.we_wordv, we.we_wordc, sizeof (*we.we_wordv),
657 for (i = 0; i < we.we_wordc; i++)
659 oconfig_item_t *temp;
662 path_ptr = we.we_wordv[i];
664 status = stat (path_ptr, &statbuf);
668 ERROR ("configfile: stat (%s) failed: %s",
670 sstrerror (errno, errbuf, sizeof (errbuf)));
675 if (S_ISREG (statbuf.st_mode))
676 temp = cf_read_file (path_ptr, depth);
677 else if (S_ISDIR (statbuf.st_mode))
678 temp = cf_read_dir (path_ptr, depth);
681 ERROR ("configfile: %s is neither a file nor a "
691 cf_ci_append_children (root, temp);
692 sfree (temp->children);
699 } /* oconfig_item_t *cf_read_generic */
700 /* #endif HAVE_WORDEXP_H */
702 #else /* if !HAVE_WORDEXP_H */
703 static oconfig_item_t *cf_read_generic (const char *path, int depth)
708 if (depth >= CF_MAX_DEPTH)
710 ERROR ("configfile: Not including `%s' because the maximum "
711 "nesting depth has been reached.", path);
715 status = stat (path, &statbuf);
719 ERROR ("configfile: stat (%s) failed: %s",
721 sstrerror (errno, errbuf, sizeof (errbuf)));
725 if (S_ISREG (statbuf.st_mode))
726 return (cf_read_file (path, depth));
727 else if (S_ISDIR (statbuf.st_mode))
728 return (cf_read_dir (path, depth));
730 ERROR ("configfile: %s is neither a file nor a directory.", path);
732 } /* oconfig_item_t *cf_read_generic */
733 #endif /* !HAVE_WORDEXP_H */
738 int global_option_set (const char *option, const char *value)
742 DEBUG ("option = %s; value = %s;", option, value);
744 for (i = 0; i < cf_global_options_num; i++)
745 if (strcasecmp (cf_global_options[i].key, option) == 0)
748 if (i >= cf_global_options_num)
751 sfree (cf_global_options[i].value);
754 cf_global_options[i].value = strdup (value);
756 cf_global_options[i].value = NULL;
761 const char *global_option_get (const char *option)
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 return ((cf_global_options[i].value != NULL)
773 ? cf_global_options[i].value
774 : cf_global_options[i].def);
775 } /* char *global_option_get */
777 void cf_unregister (const char *type)
779 cf_callback_t *this, *prev;
781 for (prev = NULL, this = first_callback;
783 prev = this, this = this->next)
784 if (strcasecmp (this->type, type) == 0)
787 first_callback = this->next;
789 prev->next = this->next;
794 } /* void cf_unregister */
796 void cf_unregister_complex (const char *type)
798 cf_complex_callback_t *this, *prev;
800 for (prev = NULL, this = complex_callback_head;
802 prev = this, this = this->next)
803 if (strcasecmp (this->type, type) == 0)
806 complex_callback_head = this->next;
808 prev->next = this->next;
814 } /* void cf_unregister */
816 void cf_register (const char *type,
817 int (*callback) (const char *, const char *),
818 const char **keys, int keys_num)
820 cf_callback_t *cf_cb;
822 /* Remove this module from the list, if it already exists */
823 cf_unregister (type);
825 /* This pointer will be free'd in `cf_unregister' */
826 if ((cf_cb = (cf_callback_t *) malloc (sizeof (cf_callback_t))) == NULL)
830 cf_cb->callback = callback;
832 cf_cb->keys_num = keys_num;
834 cf_cb->next = first_callback;
835 first_callback = cf_cb;
836 } /* void cf_register */
838 int cf_register_complex (const char *type, int (*callback) (oconfig_item_t *))
840 cf_complex_callback_t *new;
842 new = (cf_complex_callback_t *) malloc (sizeof (cf_complex_callback_t));
846 new->type = strdup (type);
847 if (new->type == NULL)
853 new->callback = callback;
856 if (complex_callback_head == NULL)
858 complex_callback_head = new;
862 cf_complex_callback_t *last = complex_callback_head;
863 while (last->next != NULL)
869 } /* int cf_register_complex */
871 int cf_read (char *filename)
873 oconfig_item_t *conf;
876 conf = cf_read_generic (filename, 0 /* depth */);
879 ERROR ("Unable to read config file %s.", filename);
883 for (i = 0; i < conf->children_num; i++)
885 if (conf->children[i].children == NULL)
886 dispatch_value (conf->children + i);
888 dispatch_block (conf->children + i);
891 if (cf_default_typesdb)
892 read_types_list (PLUGINDIR"/types.db"); /* FIXME: Configure path */