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 ((cf_cb->keys[i] != NULL)
160 && (strcasecmp (cf_cb->keys[i], key) == 0))
162 ret = (*cf_cb->callback) (key, value);
167 if (i >= cf_cb->keys_num)
168 WARNING ("Plugin `%s' did not register for value `%s'.", type, key);
173 DEBUG ("return (%i)", ret);
176 } /* int cf_dispatch */
178 static int dispatch_global_option (const oconfig_item_t *ci)
180 if (ci->values_num != 1)
182 if (ci->values[0].type == OCONFIG_TYPE_STRING)
183 return (global_option_set (ci->key, ci->values[0].value.string));
184 else if (ci->values[0].type == OCONFIG_TYPE_NUMBER)
187 ssnprintf (tmp, sizeof (tmp), "%lf", ci->values[0].value.number);
188 return (global_option_set (ci->key, tmp));
190 else if (ci->values[0].type == OCONFIG_TYPE_BOOLEAN)
192 if (ci->values[0].value.boolean)
193 return (global_option_set (ci->key, "true"));
195 return (global_option_set (ci->key, "false"));
199 } /* int dispatch_global_option */
201 static int dispatch_value_typesdb (const oconfig_item_t *ci)
205 assert (strcasecmp (ci->key, "TypesDB") == 0);
207 cf_default_typesdb = 0;
209 if (ci->values_num < 1)
212 for (i = 0; i < ci->values_num; ++i)
214 if (OCONFIG_TYPE_STRING != ci->values[i].type)
217 read_types_list (ci->values[i].value.string);
220 } /* int dispatch_value_typesdb */
222 static int dispatch_value_plugindir (const oconfig_item_t *ci)
224 assert (strcasecmp (ci->key, "PluginDir") == 0);
226 if (ci->values_num != 1)
228 if (ci->values[0].type != OCONFIG_TYPE_STRING)
231 plugin_set_dir (ci->values[0].value.string);
235 static int dispatch_value_loadplugin (const oconfig_item_t *ci)
237 assert (strcasecmp (ci->key, "LoadPlugin") == 0);
239 if (ci->values_num != 1)
241 if (ci->values[0].type != OCONFIG_TYPE_STRING)
244 return (plugin_load (ci->values[0].value.string));
245 } /* int dispatch_value_loadplugin */
247 static int dispatch_value_plugin (const char *plugin, oconfig_item_t *ci)
255 buffer_free = sizeof (buffer);
257 for (i = 0; i < ci->values_num; i++)
261 if (ci->values[i].type == OCONFIG_TYPE_STRING)
262 status = ssnprintf (buffer_ptr, buffer_free, " %s",
263 ci->values[i].value.string);
264 else if (ci->values[i].type == OCONFIG_TYPE_NUMBER)
265 status = ssnprintf (buffer_ptr, buffer_free, " %lf",
266 ci->values[i].value.number);
267 else if (ci->values[i].type == OCONFIG_TYPE_BOOLEAN)
268 status = ssnprintf (buffer_ptr, buffer_free, " %s",
269 ci->values[i].value.boolean
272 if ((status < 0) || (status >= buffer_free))
274 buffer_free -= status;
275 buffer_ptr += status;
277 /* skip the initial space */
278 buffer_ptr = buffer + 1;
280 return (cf_dispatch (plugin, ci->key, buffer_ptr));
281 } /* int dispatch_value_plugin */
283 static int dispatch_value (const oconfig_item_t *ci)
288 for (i = 0; i < cf_value_map_num; i++)
289 if (strcasecmp (cf_value_map[i].key, ci->key) == 0)
291 ret = cf_value_map[i].func (ci);
295 for (i = 0; i < cf_global_options_num; i++)
296 if (strcasecmp (cf_global_options[i].key, ci->key) == 0)
298 ret = dispatch_global_option (ci);
303 } /* int dispatch_value */
305 static int dispatch_block_plugin (oconfig_item_t *ci)
310 cf_complex_callback_t *cb;
312 if (strcasecmp (ci->key, "Plugin") != 0)
314 if (ci->values_num < 1)
316 if (ci->values[0].type != OCONFIG_TYPE_STRING)
319 name = ci->values[0].value.string;
321 /* Check for a complex callback first */
322 for (cb = complex_callback_head; cb != NULL; cb = cb->next)
323 if (strcasecmp (name, cb->type) == 0)
324 return (cb->callback (ci));
326 /* Hm, no complex plugin found. Dispatch the values one by one */
327 for (i = 0; i < ci->children_num; i++)
329 if (ci->children[i].children == NULL)
330 dispatch_value_plugin (name, ci->children + i);
332 {DEBUG ("No nested config blocks allowed for this plugin.");}
339 static int dispatch_block (oconfig_item_t *ci)
341 if (strcasecmp (ci->key, "Plugin") == 0)
342 return (dispatch_block_plugin (ci));
343 else if (strcasecmp (ci->key, "Threshold") == 0)
344 return (ut_config (ci));
345 else if (strcasecmp (ci->key, "Chain") == 0)
346 return (fc_configure (ci));
351 static int cf_ci_replace_child (oconfig_item_t *dst, oconfig_item_t *src,
354 oconfig_item_t *temp;
357 assert (offset >= 0);
358 assert (dst->children_num > offset);
360 /* Free the memory used by the replaced child. Usually that's the
361 * `Include "blah"' statement. */
362 temp = dst->children + offset;
363 for (i = 0; i < temp->values_num; i++)
365 if (temp->values[i].type == OCONFIG_TYPE_STRING)
367 sfree (temp->values[i].value.string);
370 sfree (temp->values);
373 /* If (src->children_num == 0) the array size is decreased. If offset
374 * is _not_ the last element, (offset < (src->children_num - 1)), then
375 * we need to move the trailing elements before resizing the array. */
376 if ((src->children_num == 0) && (offset < (src->children_num - 1)))
378 int nmemb = src->children_num - (offset + 1);
379 memmove (src->children + offset, src->children + offset + 1,
380 sizeof (oconfig_item_t) * nmemb);
383 /* Resize the memory containing the children to be big enough to hold
385 temp = (oconfig_item_t *) realloc (dst->children,
386 sizeof (oconfig_item_t)
387 * (dst->children_num + src->children_num - 1));
390 ERROR ("configfile: realloc failed.");
393 dst->children = temp;
395 /* If there are children behind the include statement, and they have
396 * not yet been moved because (src->children_num == 0), then move them
397 * to the end of the list, so that the new children have room before
399 if ((src->children_num > 0)
400 && ((dst->children_num - (offset + 1)) > 0))
402 int nmemb = dst->children_num - (offset + 1);
403 int old_offset = offset + 1;
404 int new_offset = offset + src->children_num;
406 memmove (dst->children + new_offset,
407 dst->children + old_offset,
408 sizeof (oconfig_item_t) * nmemb);
411 /* Last but not least: If there are new childrem, copy them to the
412 * memory reserved for them. */
413 if (src->children_num > 0)
415 memcpy (dst->children + offset,
417 sizeof (oconfig_item_t) * src->children_num);
420 /* Update the number of children. */
421 dst->children_num += (src->children_num - 1);
424 } /* int cf_ci_replace_child */
426 static int cf_ci_append_children (oconfig_item_t *dst, oconfig_item_t *src)
428 oconfig_item_t *temp;
430 if ((src == NULL) || (src->children_num == 0))
433 temp = (oconfig_item_t *) realloc (dst->children,
434 sizeof (oconfig_item_t)
435 * (dst->children_num + src->children_num));
438 ERROR ("configfile: realloc failed.");
441 dst->children = temp;
443 memcpy (dst->children + dst->children_num,
445 sizeof (oconfig_item_t)
446 * src->children_num);
447 dst->children_num += src->children_num;
450 } /* int cf_ci_append_children */
452 #define CF_MAX_DEPTH 8
453 static oconfig_item_t *cf_read_generic (const char *path, int depth);
455 static int cf_include_all (oconfig_item_t *root, int depth)
459 for (i = 0; i < root->children_num; i++)
464 /* Ignore all blocks, including `Include' blocks. */
465 if (root->children[i].children_num != 0)
468 if (strcasecmp (root->children[i].key, "Include") != 0)
471 old = root->children + i;
473 if ((old->values_num != 1)
474 || (old->values[0].type != OCONFIG_TYPE_STRING))
476 ERROR ("configfile: `Include' needs exactly one string argument.");
480 new = cf_read_generic (old->values[0].value.string, depth + 1);
484 /* Now replace the i'th child in `root' with `new'. */
485 cf_ci_replace_child (root, new, i);
489 } /* for (i = 0; i < root->children_num; i++) */
492 } /* int cf_include_all */
494 static oconfig_item_t *cf_read_file (const char *file, int depth)
496 oconfig_item_t *root;
498 assert (depth < CF_MAX_DEPTH);
500 root = oconfig_parse_file (file);
503 ERROR ("configfile: Cannot read file `%s'.", file);
507 cf_include_all (root, depth);
510 } /* oconfig_item_t *cf_read_file */
512 static int cf_compare_string (const void *p1, const void *p2)
514 return strcmp (*(const char **) p1, *(const char **) p2);
517 static oconfig_item_t *cf_read_dir (const char *dir, int depth)
519 oconfig_item_t *root = NULL;
522 char **filenames = NULL;
523 int filenames_num = 0;
527 assert (depth < CF_MAX_DEPTH);
533 ERROR ("configfile: opendir failed: %s",
534 sstrerror (errno, errbuf, sizeof (errbuf)));
538 root = (oconfig_item_t *) malloc (sizeof (oconfig_item_t));
541 ERROR ("configfile: malloc failed.");
544 memset (root, '\0', sizeof (oconfig_item_t));
546 while ((de = readdir (dh)) != NULL)
551 if ((de->d_name[0] == '.') || (de->d_name[0] == '\0'))
554 status = ssnprintf (name, sizeof (name), "%s/%s",
556 if (status >= sizeof (name))
558 ERROR ("configfile: Not including `%s/%s' because its"
559 " name is too long.",
561 for (i = 0; i < filenames_num; ++i)
569 tmp = (char **) realloc (filenames,
570 filenames_num * sizeof (*filenames));
572 ERROR ("configfile: realloc failed.");
573 for (i = 0; i < filenames_num - 1; ++i)
581 filenames[filenames_num - 1] = sstrdup (name);
584 qsort ((void *) filenames, filenames_num, sizeof (*filenames),
587 for (i = 0; i < filenames_num; ++i)
589 oconfig_item_t *temp;
590 char *name = filenames[i];
592 temp = cf_read_generic (name, depth);
595 for (j = i; j < filenames_num; ++j)
602 cf_ci_append_children (root, temp);
603 sfree (temp->children);
611 } /* oconfig_item_t *cf_read_dir */
616 * Path is stat'ed and either cf_read_file or cf_read_dir is called
619 * There are two versions of this function: If `wordexp' exists shell wildcards
620 * will be expanded and the function will include all matches found. If
621 * `wordexp' (or, more precisely, it's header file) is not available the
622 * simpler function is used which does not do any such expansion.
625 static oconfig_item_t *cf_read_generic (const char *path, int depth)
627 oconfig_item_t *root = NULL;
629 const char *path_ptr;
633 if (depth >= CF_MAX_DEPTH)
635 ERROR ("configfile: Not including `%s' because the maximum "
636 "nesting depth has been reached.", path);
640 status = wordexp (path, &we, WRDE_NOCMD);
643 ERROR ("configfile: wordexp (%s) failed.", path);
647 root = (oconfig_item_t *) malloc (sizeof (oconfig_item_t));
650 ERROR ("configfile: malloc failed.");
653 memset (root, '\0', sizeof (oconfig_item_t));
655 /* wordexp() might return a sorted list already. That's not
656 * documented though, so let's make sure we get what we want. */
657 qsort ((void *) we.we_wordv, we.we_wordc, sizeof (*we.we_wordv),
660 for (i = 0; i < we.we_wordc; i++)
662 oconfig_item_t *temp;
665 path_ptr = we.we_wordv[i];
667 status = stat (path_ptr, &statbuf);
671 ERROR ("configfile: stat (%s) failed: %s",
673 sstrerror (errno, errbuf, sizeof (errbuf)));
678 if (S_ISREG (statbuf.st_mode))
679 temp = cf_read_file (path_ptr, depth);
680 else if (S_ISDIR (statbuf.st_mode))
681 temp = cf_read_dir (path_ptr, depth);
684 ERROR ("configfile: %s is neither a file nor a "
694 cf_ci_append_children (root, temp);
695 sfree (temp->children);
702 } /* oconfig_item_t *cf_read_generic */
703 /* #endif HAVE_WORDEXP_H */
705 #else /* if !HAVE_WORDEXP_H */
706 static oconfig_item_t *cf_read_generic (const char *path, int depth)
711 if (depth >= CF_MAX_DEPTH)
713 ERROR ("configfile: Not including `%s' because the maximum "
714 "nesting depth has been reached.", path);
718 status = stat (path, &statbuf);
722 ERROR ("configfile: stat (%s) failed: %s",
724 sstrerror (errno, errbuf, sizeof (errbuf)));
728 if (S_ISREG (statbuf.st_mode))
729 return (cf_read_file (path, depth));
730 else if (S_ISDIR (statbuf.st_mode))
731 return (cf_read_dir (path, depth));
733 ERROR ("configfile: %s is neither a file nor a directory.", path);
735 } /* oconfig_item_t *cf_read_generic */
736 #endif /* !HAVE_WORDEXP_H */
741 int global_option_set (const char *option, const char *value)
745 DEBUG ("option = %s; value = %s;", option, value);
747 for (i = 0; i < cf_global_options_num; i++)
748 if (strcasecmp (cf_global_options[i].key, option) == 0)
751 if (i >= cf_global_options_num)
754 sfree (cf_global_options[i].value);
757 cf_global_options[i].value = strdup (value);
759 cf_global_options[i].value = NULL;
764 const char *global_option_get (const char *option)
768 for (i = 0; i < cf_global_options_num; i++)
769 if (strcasecmp (cf_global_options[i].key, option) == 0)
772 if (i >= cf_global_options_num)
775 return ((cf_global_options[i].value != NULL)
776 ? cf_global_options[i].value
777 : cf_global_options[i].def);
778 } /* char *global_option_get */
780 void cf_unregister (const char *type)
782 cf_callback_t *this, *prev;
784 for (prev = NULL, this = first_callback;
786 prev = this, this = this->next)
787 if (strcasecmp (this->type, type) == 0)
790 first_callback = this->next;
792 prev->next = this->next;
797 } /* void cf_unregister */
799 void cf_unregister_complex (const char *type)
801 cf_complex_callback_t *this, *prev;
803 for (prev = NULL, this = complex_callback_head;
805 prev = this, this = this->next)
806 if (strcasecmp (this->type, type) == 0)
809 complex_callback_head = this->next;
811 prev->next = this->next;
817 } /* void cf_unregister */
819 void cf_register (const char *type,
820 int (*callback) (const char *, const char *),
821 const char **keys, int keys_num)
823 cf_callback_t *cf_cb;
825 /* Remove this module from the list, if it already exists */
826 cf_unregister (type);
828 /* This pointer will be free'd in `cf_unregister' */
829 if ((cf_cb = (cf_callback_t *) malloc (sizeof (cf_callback_t))) == NULL)
833 cf_cb->callback = callback;
835 cf_cb->keys_num = keys_num;
837 cf_cb->next = first_callback;
838 first_callback = cf_cb;
839 } /* void cf_register */
841 int cf_register_complex (const char *type, int (*callback) (oconfig_item_t *))
843 cf_complex_callback_t *new;
845 new = (cf_complex_callback_t *) malloc (sizeof (cf_complex_callback_t));
849 new->type = strdup (type);
850 if (new->type == NULL)
856 new->callback = callback;
859 if (complex_callback_head == NULL)
861 complex_callback_head = new;
865 cf_complex_callback_t *last = complex_callback_head;
866 while (last->next != NULL)
872 } /* int cf_register_complex */
874 int cf_read (char *filename)
876 oconfig_item_t *conf;
879 conf = cf_read_generic (filename, 0 /* depth */);
882 ERROR ("Unable to read config file %s.", filename);
886 for (i = 0; i < conf->children_num; i++)
888 if (conf->children[i].children == NULL)
889 dispatch_value (conf->children + i);
891 dispatch_block (conf->children + i);
896 /* Read the default types.db if no `TypesDB' option was given. */
897 if (cf_default_typesdb)
898 read_types_list (PKGDATADIR"/types.db");