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>
25 #include "liboconfig/oconfig.h"
29 #include "configfile.h"
30 #include "types_list.h"
31 #include "utils_threshold.h"
35 #endif /* HAVE_WORDEXP_H */
37 #define ESCAPE_NULL(str) ((str) == NULL ? "(null)" : (str))
42 typedef struct cf_callback
45 int (*callback) (const char *, const char *);
48 struct cf_callback *next;
51 typedef struct cf_complex_callback_s
54 int (*callback) (oconfig_item_t *);
55 struct cf_complex_callback_s *next;
56 } cf_complex_callback_t;
58 typedef struct cf_value_map_s
61 int (*func) (const oconfig_item_t *);
64 typedef struct cf_global_option_s
72 * Prototypes of callback functions
74 static int dispatch_value_typesdb (const oconfig_item_t *ci);
75 static int dispatch_value_plugindir (const oconfig_item_t *ci);
76 static int dispatch_value_loadplugin (const oconfig_item_t *ci);
81 static cf_callback_t *first_callback = NULL;
82 static cf_complex_callback_t *complex_callback_head = NULL;
84 static cf_value_map_t cf_value_map[] =
86 {"TypesDB", dispatch_value_typesdb},
87 {"PluginDir", dispatch_value_plugindir},
88 {"LoadPlugin", dispatch_value_loadplugin}
90 static int cf_value_map_num = STATIC_ARRAY_LEN (cf_value_map);
92 static cf_global_option_t cf_global_options[] =
94 {"BaseDir", NULL, PKGLOCALSTATEDIR},
95 {"PIDFile", NULL, PIDFILE},
96 {"Hostname", NULL, NULL},
97 {"FQDNLookup", NULL, "false"},
98 {"Interval", NULL, "10"},
99 {"ReadThreads", NULL, "5"}
101 static int cf_global_options_num = STATIC_ARRAY_LEN (cf_global_options);
103 static int cf_default_typesdb = 1;
106 * Functions to handle register/unregister, search, and other plugin related
109 static cf_callback_t *cf_search (const char *type)
111 cf_callback_t *cf_cb;
116 for (cf_cb = first_callback; cf_cb != NULL; cf_cb = cf_cb->next)
117 if (strcasecmp (cf_cb->type, type) == 0)
123 static int cf_dispatch (const char *type, const char *orig_key,
124 const char *orig_value)
126 cf_callback_t *cf_cb;
132 DEBUG ("type = %s, key = %s, value = %s",
134 ESCAPE_NULL(orig_key),
135 ESCAPE_NULL(orig_value));
137 if ((cf_cb = cf_search (type)) == NULL)
139 WARNING ("Found a configuration for the `%s' plugin, but "
140 "the plugin isn't loaded or didn't register "
141 "a configuration callback.", type);
145 if ((key = strdup (orig_key)) == NULL)
147 if ((value = strdup (orig_value)) == NULL)
155 for (i = 0; i < cf_cb->keys_num; i++)
157 if (strcasecmp (cf_cb->keys[i], key) == 0)
159 ret = (*cf_cb->callback) (key, value);
164 if (i >= cf_cb->keys_num)
165 WARNING ("Plugin `%s' did not register for value `%s'.", type, key);
170 DEBUG ("return (%i)", ret);
173 } /* int cf_dispatch */
175 static int dispatch_global_option (const oconfig_item_t *ci)
177 if (ci->values_num != 1)
179 if (ci->values[0].type == OCONFIG_TYPE_STRING)
180 return (global_option_set (ci->key, ci->values[0].value.string));
181 else if (ci->values[0].type == OCONFIG_TYPE_NUMBER)
184 snprintf (tmp, sizeof (tmp), "%lf", ci->values[0].value.number);
186 return (global_option_set (ci->key, tmp));
188 else if (ci->values[0].type == OCONFIG_TYPE_BOOLEAN)
190 if (ci->values[0].value.boolean)
191 return (global_option_set (ci->key, "true"));
193 return (global_option_set (ci->key, "false"));
197 } /* int dispatch_global_option */
199 static int dispatch_value_typesdb (const oconfig_item_t *ci)
203 assert (strcasecmp (ci->key, "TypesDB") == 0);
205 cf_default_typesdb = 0;
207 if (ci->values_num < 1)
210 for (i = 0; i < ci->values_num; ++i)
212 if (OCONFIG_TYPE_STRING != ci->values[i].type)
215 read_types_list (ci->values[i].value.string);
218 } /* int dispatch_value_typesdb */
220 static int dispatch_value_plugindir (const oconfig_item_t *ci)
222 assert (strcasecmp (ci->key, "PluginDir") == 0);
224 if (ci->values_num != 1)
226 if (ci->values[0].type != OCONFIG_TYPE_STRING)
229 plugin_set_dir (ci->values[0].value.string);
233 static int dispatch_value_loadplugin (const oconfig_item_t *ci)
235 assert (strcasecmp (ci->key, "LoadPlugin") == 0);
237 if (ci->values_num != 1)
239 if (ci->values[0].type != OCONFIG_TYPE_STRING)
242 return (plugin_load (ci->values[0].value.string));
243 } /* int dispatch_value_loadplugin */
245 static int dispatch_value_plugin (const char *plugin, oconfig_item_t *ci)
253 buffer_free = sizeof (buffer);
255 for (i = 0; i < ci->values_num; i++)
259 if (ci->values[i].type == OCONFIG_TYPE_STRING)
260 status = snprintf (buffer_ptr, buffer_free, " %s",
261 ci->values[i].value.string);
262 else if (ci->values[i].type == OCONFIG_TYPE_NUMBER)
263 status = snprintf (buffer_ptr, buffer_free, " %lf",
264 ci->values[i].value.number);
265 else if (ci->values[i].type == OCONFIG_TYPE_BOOLEAN)
266 status = snprintf (buffer_ptr, buffer_free, " %s",
267 ci->values[i].value.boolean
270 if ((status < 0) || (status >= buffer_free))
272 buffer_free -= status;
273 buffer_ptr += status;
275 /* skip the initial space */
276 buffer_ptr = buffer + 1;
278 return (cf_dispatch (plugin, ci->key, buffer_ptr));
279 } /* int plugin_conf_dispatch */
281 static int dispatch_value (const oconfig_item_t *ci)
286 for (i = 0; i < cf_value_map_num; i++)
287 if (strcasecmp (cf_value_map[i].key, ci->key) == 0)
289 ret = cf_value_map[i].func (ci);
293 for (i = 0; i < cf_global_options_num; i++)
294 if (strcasecmp (cf_global_options[i].key, ci->key) == 0)
296 ret = dispatch_global_option (ci);
301 } /* int dispatch_value */
303 static int dispatch_block_plugin (oconfig_item_t *ci)
308 cf_complex_callback_t *cb;
310 if (strcasecmp (ci->key, "Plugin") != 0)
312 if (ci->values_num < 1)
314 if (ci->values[0].type != OCONFIG_TYPE_STRING)
317 name = ci->values[0].value.string;
319 /* Check for a complex callback first */
320 for (cb = complex_callback_head; cb != NULL; cb = cb->next)
321 if (strcasecmp (name, cb->type) == 0)
322 return (cb->callback (ci));
324 /* Hm, no complex plugin found. Dispatch the values one by one */
325 for (i = 0; i < ci->children_num; i++)
327 if (ci->children[i].children == NULL)
328 dispatch_value_plugin (name, ci->children + i);
330 {DEBUG ("No nested config blocks allow for this plugin.");}
337 static int dispatch_block (oconfig_item_t *ci)
339 if (strcasecmp (ci->key, "Plugin") == 0)
340 return (dispatch_block_plugin (ci));
341 else if (strcasecmp (ci->key, "Threshold") == 0)
342 return (ut_config (ci));
347 static int cf_ci_replace_child (oconfig_item_t *dst, oconfig_item_t *src,
350 oconfig_item_t *temp;
353 assert (offset >= 0);
354 assert (dst->children_num > offset);
356 /* Free the memory used by the replaced child. Usually that's the
357 * `Include "blah"' statement. */
358 temp = dst->children + offset;
359 for (i = 0; i < temp->values_num; i++)
361 if (temp->values[i].type == OCONFIG_TYPE_STRING)
363 sfree (temp->values[i].value.string);
366 sfree (temp->values);
369 /* If (src->children_num == 0) the array size is decreased. If offset
370 * is _not_ the last element, (offset < (src->children_num - 1)), then
371 * we need to move the trailing elements before resizing the array. */
372 if ((src->children_num == 0) && (offset < (src->children_num - 1)))
374 int nmemb = src->children_num - (offset + 1);
375 memmove (src->children + offset, src->children + offset + 1,
376 sizeof (oconfig_item_t) * nmemb);
379 /* Resize the memory containing the children to be big enough to hold
381 temp = (oconfig_item_t *) realloc (dst->children,
382 sizeof (oconfig_item_t)
383 * (dst->children_num + src->children_num - 1));
386 ERROR ("configfile: realloc failed.");
389 dst->children = temp;
391 /* If there are children behind the include statement, and they have
392 * not yet been moved because (src->children_num == 0), then move them
393 * to the end of the list, so that the new children have room before
395 if ((src->children_num > 0)
396 && ((dst->children_num - (offset + 1)) > 0))
398 int nmemb = dst->children_num - (offset + 1);
399 int old_offset = offset + 1;
400 int new_offset = offset + src->children_num;
402 memmove (dst->children + new_offset,
403 dst->children + old_offset,
404 sizeof (oconfig_item_t) * nmemb);
407 /* Last but not least: If there are new childrem, copy them to the
408 * memory reserved for them. */
409 if (src->children_num > 0)
411 memcpy (dst->children + offset,
413 sizeof (oconfig_item_t) * src->children_num);
416 /* Update the number of children. */
417 dst->children_num += (src->children_num - 1);
420 } /* int cf_ci_replace_child */
422 static int cf_ci_append_children (oconfig_item_t *dst, oconfig_item_t *src)
424 oconfig_item_t *temp;
426 if ((src == NULL) || (src->children_num == 0))
429 temp = (oconfig_item_t *) realloc (dst->children,
430 sizeof (oconfig_item_t)
431 * (dst->children_num + src->children_num));
434 ERROR ("configfile: realloc failed.");
437 dst->children = temp;
439 memcpy (dst->children + dst->children_num,
441 sizeof (oconfig_item_t)
442 * src->children_num);
443 dst->children_num += src->children_num;
446 } /* int cf_ci_append_children */
448 #define CF_MAX_DEPTH 8
449 static oconfig_item_t *cf_read_generic (const char *path, int depth);
451 static int cf_include_all (oconfig_item_t *root, int depth)
455 for (i = 0; i < root->children_num; i++)
460 /* Ignore all blocks, including `Include' blocks. */
461 if (root->children[i].children_num != 0)
464 if (strcasecmp (root->children[i].key, "Include") != 0)
467 old = root->children + i;
469 if ((old->values_num != 1)
470 || (old->values[0].type != OCONFIG_TYPE_STRING))
472 ERROR ("configfile: `Include' needs exactly one string argument.");
476 new = cf_read_generic (old->values[0].value.string, depth + 1);
480 /* Now replace the i'th child in `root' with `new'. */
481 cf_ci_replace_child (root, new, i);
485 } /* for (i = 0; i < root->children_num; i++) */
488 } /* int cf_include_all */
490 static oconfig_item_t *cf_read_file (const char *file, int depth)
492 oconfig_item_t *root;
494 assert (depth < CF_MAX_DEPTH);
496 root = oconfig_parse_file (file);
499 ERROR ("configfile: Cannot read file `%s'.", file);
503 cf_include_all (root, depth);
506 } /* oconfig_item_t *cf_read_file */
508 static oconfig_item_t *cf_read_dir (const char *dir, int depth)
510 oconfig_item_t *root = NULL;
516 assert (depth < CF_MAX_DEPTH);
522 ERROR ("configfile: opendir failed: %s",
523 sstrerror (errno, errbuf, sizeof (errbuf)));
527 root = (oconfig_item_t *) malloc (sizeof (oconfig_item_t));
530 ERROR ("configfile: malloc failed.");
533 memset (root, '\0', sizeof (oconfig_item_t));
535 while ((de = readdir (dh)) != NULL)
537 oconfig_item_t *temp;
539 if ((de->d_name[0] == '.') || (de->d_name[0] == '\0'))
542 status = snprintf (name, sizeof (name), "%s/%s",
544 if (status >= sizeof (name))
546 ERROR ("configfile: Not including `%s/%s' because its"
547 " name is too long.",
552 temp = cf_read_generic (name, depth);
556 cf_ci_append_children (root, temp);
557 sfree (temp->children);
562 } /* oconfig_item_t *cf_read_dir */
567 * Path is stat'ed and either cf_read_file or cf_read_dir is called
570 * There are two versions of this function: If `wordexp' exists shell wildcards
571 * will be expanded and the function will include all matches found. If
572 * `wordexp' (or, more precisely, it's header file) is not available the
573 * simpler function is used which does not do any such expansion.
576 static oconfig_item_t *cf_read_generic (const char *path, int depth)
578 oconfig_item_t *root = NULL;
580 const char *path_ptr;
584 if (depth >= CF_MAX_DEPTH)
586 ERROR ("configfile: Not including `%s' because the maximum "
587 "nesting depth has been reached.", path);
591 status = wordexp (path, &we, WRDE_NOCMD);
594 ERROR ("configfile: wordexp (%s) failed.", path);
598 root = (oconfig_item_t *) malloc (sizeof (oconfig_item_t));
601 ERROR ("configfile: malloc failed.");
604 memset (root, '\0', sizeof (oconfig_item_t));
606 for (i = 0; i < we.we_wordc; i++)
608 oconfig_item_t *temp;
611 path_ptr = we.we_wordv[i];
613 status = stat (path_ptr, &statbuf);
617 ERROR ("configfile: stat (%s) failed: %s",
619 sstrerror (errno, errbuf, sizeof (errbuf)));
624 if (S_ISREG (statbuf.st_mode))
625 temp = cf_read_file (path_ptr, depth);
626 else if (S_ISDIR (statbuf.st_mode))
627 temp = cf_read_dir (path_ptr, depth);
630 ERROR ("configfile: %s is neither a file nor a "
640 cf_ci_append_children (root, temp);
641 sfree (temp->children);
648 } /* oconfig_item_t *cf_read_generic */
649 /* #endif HAVE_WORDEXP_H */
651 #else /* if !HAVE_WORDEXP_H */
652 static oconfig_item_t *cf_read_generic (const char *path, int depth)
657 if (depth >= CF_MAX_DEPTH)
659 ERROR ("configfile: Not including `%s' because the maximum "
660 "nesting depth has been reached.", path);
664 status = stat (path, &statbuf);
668 ERROR ("configfile: stat (%s) failed: %s",
670 sstrerror (errno, errbuf, sizeof (errbuf)));
674 if (S_ISREG (statbuf.st_mode))
675 return (cf_read_file (path, depth));
676 else if (S_ISDIR (statbuf.st_mode))
677 return (cf_read_dir (path, depth));
679 ERROR ("configfile: %s is neither a file nor a directory.", path);
681 } /* oconfig_item_t *cf_read_generic */
682 #endif /* !HAVE_WORDEXP_H */
687 int global_option_set (const char *option, const char *value)
691 DEBUG ("option = %s; value = %s;", option, value);
693 for (i = 0; i < cf_global_options_num; i++)
694 if (strcasecmp (cf_global_options[i].key, option) == 0)
697 if (i >= cf_global_options_num)
700 sfree (cf_global_options[i].value);
703 cf_global_options[i].value = strdup (value);
705 cf_global_options[i].value = NULL;
710 const char *global_option_get (const char *option)
714 for (i = 0; i < cf_global_options_num; i++)
715 if (strcasecmp (cf_global_options[i].key, option) == 0)
718 if (i >= cf_global_options_num)
721 return ((cf_global_options[i].value != NULL)
722 ? cf_global_options[i].value
723 : cf_global_options[i].def);
724 } /* char *global_option_get */
726 void cf_unregister (const char *type)
728 cf_callback_t *this, *prev;
730 for (prev = NULL, this = first_callback;
732 prev = this, this = this->next)
733 if (strcasecmp (this->type, type) == 0)
736 first_callback = this->next;
738 prev->next = this->next;
743 } /* void cf_unregister */
745 void cf_unregister_complex (const char *type)
747 cf_complex_callback_t *this, *prev;
749 for (prev = NULL, this = complex_callback_head;
751 prev = this, this = this->next)
752 if (strcasecmp (this->type, type) == 0)
755 complex_callback_head = this->next;
757 prev->next = this->next;
763 } /* void cf_unregister */
765 void cf_register (const char *type,
766 int (*callback) (const char *, const char *),
767 const char **keys, int keys_num)
769 cf_callback_t *cf_cb;
771 /* Remove this module from the list, if it already exists */
772 cf_unregister (type);
774 /* This pointer will be free'd in `cf_unregister' */
775 if ((cf_cb = (cf_callback_t *) malloc (sizeof (cf_callback_t))) == NULL)
779 cf_cb->callback = callback;
781 cf_cb->keys_num = keys_num;
783 cf_cb->next = first_callback;
784 first_callback = cf_cb;
785 } /* void cf_register */
787 int cf_register_complex (const char *type, int (*callback) (oconfig_item_t *))
789 cf_complex_callback_t *new;
791 new = (cf_complex_callback_t *) malloc (sizeof (cf_complex_callback_t));
795 new->type = strdup (type);
796 if (new->type == NULL)
802 new->callback = callback;
805 if (complex_callback_head == NULL)
807 complex_callback_head = new;
811 cf_complex_callback_t *last = complex_callback_head;
812 while (last->next != NULL)
818 } /* int cf_register_complex */
820 int cf_read (char *filename)
822 oconfig_item_t *conf;
825 conf = cf_read_generic (filename, 0 /* depth */);
828 ERROR ("Unable to read config file %s.", filename);
832 for (i = 0; i < conf->children_num; i++)
834 if (conf->children[i].children == NULL)
835 dispatch_value (conf->children + i);
837 dispatch_block (conf->children + i);
840 if (cf_default_typesdb)
841 read_types_list (PLUGINDIR"/types.db"); /* FIXME: Configure path */