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"
33 #define ESCAPE_NULL(str) ((str) == NULL ? "(null)" : (str))
38 typedef struct cf_callback
41 int (*callback) (const char *, const char *);
44 struct cf_callback *next;
47 typedef struct cf_complex_callback_s
50 int (*callback) (oconfig_item_t *);
51 struct cf_complex_callback_s *next;
52 } cf_complex_callback_t;
54 typedef struct cf_value_map_s
57 int (*func) (const oconfig_item_t *);
60 typedef struct cf_global_option_s
68 * Prototypes of callback functions
70 static int dispatch_value_typesdb (const oconfig_item_t *ci);
71 static int dispatch_value_plugindir (const oconfig_item_t *ci);
72 static int dispatch_value_loadplugin (const oconfig_item_t *ci);
77 static cf_callback_t *first_callback = NULL;
78 static cf_complex_callback_t *complex_callback_head = NULL;
80 static cf_value_map_t cf_value_map[] =
82 {"TypesDB", dispatch_value_typesdb},
83 {"PluginDir", dispatch_value_plugindir},
84 {"LoadPlugin", dispatch_value_loadplugin}
86 static int cf_value_map_num = STATIC_ARRAY_LEN (cf_value_map);
88 static cf_global_option_t cf_global_options[] =
90 {"BaseDir", NULL, PKGLOCALSTATEDIR},
91 {"PIDFile", NULL, PIDFILE},
92 {"Hostname", NULL, NULL},
93 {"FQDNLookup", NULL, "false"},
94 {"Interval", NULL, "10"},
95 {"ReadThreads", NULL, "5"}
97 static int cf_global_options_num = STATIC_ARRAY_LEN (cf_global_options);
99 static int cf_default_typesdb = 1;
102 * Functions to handle register/unregister, search, and other plugin related
105 static cf_callback_t *cf_search (const char *type)
107 cf_callback_t *cf_cb;
112 for (cf_cb = first_callback; cf_cb != NULL; cf_cb = cf_cb->next)
113 if (strcasecmp (cf_cb->type, type) == 0)
119 static int cf_dispatch (const char *type, const char *orig_key,
120 const char *orig_value)
122 cf_callback_t *cf_cb;
128 DEBUG ("type = %s, key = %s, value = %s",
130 ESCAPE_NULL(orig_key),
131 ESCAPE_NULL(orig_value));
133 if ((cf_cb = cf_search (type)) == NULL)
135 WARNING ("Found a configuration for the `%s' plugin, but "
136 "the plugin isn't loaded or didn't register "
137 "a configuration callback.", type);
141 if ((key = strdup (orig_key)) == NULL)
143 if ((value = strdup (orig_value)) == NULL)
151 for (i = 0; i < cf_cb->keys_num; i++)
153 if (strcasecmp (cf_cb->keys[i], key) == 0)
155 ret = (*cf_cb->callback) (key, value);
160 if (i >= cf_cb->keys_num)
161 WARNING ("Plugin `%s' did not register for value `%s'.", type, key);
166 DEBUG ("return (%i)", ret);
169 } /* int cf_dispatch */
171 static int dispatch_global_option (const oconfig_item_t *ci)
173 if (ci->values_num != 1)
175 if (ci->values[0].type == OCONFIG_TYPE_STRING)
176 return (global_option_set (ci->key, ci->values[0].value.string));
177 else if (ci->values[0].type == OCONFIG_TYPE_NUMBER)
180 snprintf (tmp, sizeof (tmp), "%lf", ci->values[0].value.number);
182 return (global_option_set (ci->key, tmp));
184 else if (ci->values[0].type == OCONFIG_TYPE_BOOLEAN)
186 if (ci->values[0].value.boolean)
187 return (global_option_set (ci->key, "true"));
189 return (global_option_set (ci->key, "false"));
193 } /* int dispatch_global_option */
195 static int dispatch_value_typesdb (const oconfig_item_t *ci)
199 assert (strcasecmp (ci->key, "TypesDB") == 0);
201 cf_default_typesdb = 0;
203 if (ci->values_num < 1)
206 for (i = 0; i < ci->values_num; ++i)
208 if (OCONFIG_TYPE_STRING != ci->values[i].type)
211 read_types_list (ci->values[i].value.string);
214 } /* int dispatch_value_typesdb */
216 static int dispatch_value_plugindir (const oconfig_item_t *ci)
218 assert (strcasecmp (ci->key, "PluginDir") == 0);
220 if (ci->values_num != 1)
222 if (ci->values[0].type != OCONFIG_TYPE_STRING)
225 plugin_set_dir (ci->values[0].value.string);
229 static int dispatch_value_loadplugin (const oconfig_item_t *ci)
231 assert (strcasecmp (ci->key, "LoadPlugin") == 0);
233 if (ci->values_num != 1)
235 if (ci->values[0].type != OCONFIG_TYPE_STRING)
238 return (plugin_load (ci->values[0].value.string));
239 } /* int dispatch_value_loadplugin */
241 static int dispatch_value_plugin (const char *plugin, oconfig_item_t *ci)
249 buffer_free = sizeof (buffer);
251 for (i = 0; i < ci->values_num; i++)
255 if (ci->values[i].type == OCONFIG_TYPE_STRING)
256 status = snprintf (buffer_ptr, buffer_free, " %s",
257 ci->values[i].value.string);
258 else if (ci->values[i].type == OCONFIG_TYPE_NUMBER)
259 status = snprintf (buffer_ptr, buffer_free, " %lf",
260 ci->values[i].value.number);
261 else if (ci->values[i].type == OCONFIG_TYPE_BOOLEAN)
262 status = snprintf (buffer_ptr, buffer_free, " %s",
263 ci->values[i].value.boolean
266 if ((status < 0) || (status >= buffer_free))
268 buffer_free -= status;
269 buffer_ptr += status;
271 /* skip the initial space */
272 buffer_ptr = buffer + 1;
274 return (cf_dispatch (plugin, ci->key, buffer_ptr));
275 } /* int plugin_conf_dispatch */
277 static int dispatch_value (const oconfig_item_t *ci)
282 for (i = 0; i < cf_value_map_num; i++)
283 if (strcasecmp (cf_value_map[i].key, ci->key) == 0)
285 ret = cf_value_map[i].func (ci);
289 for (i = 0; i < cf_global_options_num; i++)
290 if (strcasecmp (cf_global_options[i].key, ci->key) == 0)
292 ret = dispatch_global_option (ci);
297 } /* int dispatch_value */
299 static int dispatch_block_plugin (oconfig_item_t *ci)
304 cf_complex_callback_t *cb;
306 if (strcasecmp (ci->key, "Plugin") != 0)
308 if (ci->values_num < 1)
310 if (ci->values[0].type != OCONFIG_TYPE_STRING)
313 name = ci->values[0].value.string;
315 /* Check for a complex callback first */
316 for (cb = complex_callback_head; cb != NULL; cb = cb->next)
317 if (strcasecmp (name, cb->type) == 0)
318 return (cb->callback (ci));
320 /* Hm, no complex plugin found. Dispatch the values one by one */
321 for (i = 0; i < ci->children_num; i++)
323 if (ci->children[i].children == NULL)
324 dispatch_value_plugin (name, ci->children + i);
326 {DEBUG ("No nested config blocks allow for this plugin.");}
333 static int dispatch_block (oconfig_item_t *ci)
335 if (strcasecmp (ci->key, "Plugin") == 0)
336 return (dispatch_block_plugin (ci));
337 else if (strcasecmp (ci->key, "Threshold") == 0)
338 return (ut_config (ci));
343 static int cf_ci_replace_child (oconfig_item_t *dst, oconfig_item_t *src,
346 oconfig_item_t *temp;
349 assert (offset >= 0);
350 assert (dst->children_num > offset);
352 /* Resize the memory containing the children to be big enough to hold
354 temp = (oconfig_item_t *) realloc (dst->children,
355 sizeof (oconfig_item_t)
356 * (dst->children_num + src->children_num - 1));
359 ERROR ("configfile: realloc failed.");
362 dst->children = temp;
364 /* Free the memory used by the replaced child. Usually that's the
365 * `Include "blah"' statement. */
366 temp = dst->children + offset;
367 for (i = 0; i < temp->values_num; i++)
369 if (temp->values[i].type == OCONFIG_TYPE_STRING)
371 sfree (temp->values[i].value.string);
374 sfree (temp->values);
377 /* If there are children behind the include statement, move them to the
378 * end of the list, so that the new children have room before them. */
379 if ((dst->children_num - (offset + 1)) > 0)
381 int nmemb = dst->children_num - (offset + 1);
382 int old_offset = offset + 1;
383 int new_offset = offset + src->children_num;
385 memmove (dst->children + new_offset,
386 dst->children + old_offset,
387 sizeof (oconfig_item_t) * nmemb);
390 /* Last but not least: If there are new childrem, copy them to the
391 * memory reserved for them. */
392 if (src->children_num > 0)
394 memcpy (dst->children + offset,
396 sizeof (oconfig_item_t) * src->children_num);
399 /* Update the number of children. */
400 dst->children_num += (src->children_num - 1);
403 } /* int cf_ci_replace_child */
405 static int cf_ci_append_children (oconfig_item_t *dst, oconfig_item_t *src)
407 oconfig_item_t *temp;
409 temp = (oconfig_item_t *) realloc (dst->children,
410 sizeof (oconfig_item_t)
411 * (dst->children_num + src->children_num));
414 ERROR ("configfile: realloc failed.");
417 dst->children = temp;
419 memcpy (dst->children + dst->children_num,
421 sizeof (oconfig_item_t)
422 * src->children_num);
423 dst->children_num += src->children_num;
426 } /* int cf_ci_append_children */
428 #define CF_MAX_DEPTH 8
429 static oconfig_item_t *cf_read_generic (const char *path, int depth);
431 static int cf_include_all (oconfig_item_t *root, int depth)
435 for (i = 0; i < root->children_num; i++)
440 /* Ignore all blocks, including `Include' blocks. */
441 if (root->children[i].children_num != 0)
444 if (strcasecmp (root->children[i].key, "Include") != 0)
447 old = root->children + i;
449 if ((old->values_num != 1)
450 || (old->values[0].type != OCONFIG_TYPE_STRING))
452 ERROR ("configfile: `Include' needs exactly one string argument.");
456 new = cf_read_generic (old->values[0].value.string, depth + 1);
460 /* Now replace the i'th child in `root' with `new'. */
461 cf_ci_replace_child (root, new, i);
465 } /* for (i = 0; i < root->children_num; i++) */
468 } /* int cf_include_all */
470 static oconfig_item_t *cf_read_file (const char *file, int depth)
472 oconfig_item_t *root;
474 assert (depth < CF_MAX_DEPTH);
476 root = oconfig_parse_file (file);
479 ERROR ("configfile: Cannot read file `%s'.", file);
483 cf_include_all (root, depth);
486 } /* oconfig_item_t *cf_read_file */
488 static oconfig_item_t *cf_read_dir (const char *dir, int depth)
490 oconfig_item_t *root = NULL;
496 assert (depth < CF_MAX_DEPTH);
502 ERROR ("configfile: opendir failed: %s",
503 sstrerror (errno, errbuf, sizeof (errbuf)));
507 root = (oconfig_item_t *) malloc (sizeof (oconfig_item_t));
510 ERROR ("configfile: malloc failed.");
513 memset (root, '\0', sizeof (oconfig_item_t));
515 while ((de = readdir (dh)) != NULL)
517 oconfig_item_t *temp;
519 if ((de->d_name[0] == '.') || (de->d_name[0] == '\0'))
522 status = snprintf (name, sizeof (name), "%s/%s",
524 if (status >= sizeof (name))
526 ERROR ("configfile: Not including `%s/%s' because its"
527 " name is too long.",
532 temp = cf_read_generic (name, depth);
536 cf_ci_append_children (root, temp);
537 sfree (temp->children);
542 } /* oconfig_item_t *cf_read_dir */
547 * Path is stat'ed and either cf_read_file or cf_read_dir is called
550 static oconfig_item_t *cf_read_generic (const char *path, int depth)
555 if (depth >= CF_MAX_DEPTH)
557 ERROR ("configfile: Not including `%s' because the maximum "
558 "nesting depth has been reached.", path);
562 fprintf (stderr, "cf_read_generic (path = %s, depth = %i);", path, depth);
564 status = stat (path, &statbuf);
568 ERROR ("configfile: stat (%s) failed: %s",
570 sstrerror (errno, errbuf, sizeof (errbuf)));
574 if (S_ISREG (statbuf.st_mode))
575 return (cf_read_file (path, depth));
576 else if (S_ISDIR (statbuf.st_mode))
577 return (cf_read_dir (path, depth));
579 ERROR ("configfile: %s is neither a file nor a directory.", path);
581 } /* oconfig_item_t *cf_read_generic */
586 int global_option_set (const char *option, const char *value)
590 DEBUG ("option = %s; value = %s;", option, value);
592 for (i = 0; i < cf_global_options_num; i++)
593 if (strcasecmp (cf_global_options[i].key, option) == 0)
596 if (i >= cf_global_options_num)
599 sfree (cf_global_options[i].value);
602 cf_global_options[i].value = strdup (value);
604 cf_global_options[i].value = NULL;
609 const char *global_option_get (const char *option)
613 for (i = 0; i < cf_global_options_num; i++)
614 if (strcasecmp (cf_global_options[i].key, option) == 0)
617 if (i >= cf_global_options_num)
620 return ((cf_global_options[i].value != NULL)
621 ? cf_global_options[i].value
622 : cf_global_options[i].def);
623 } /* char *global_option_get */
625 void cf_unregister (const char *type)
627 cf_callback_t *this, *prev;
629 for (prev = NULL, this = first_callback;
631 prev = this, this = this->next)
632 if (strcasecmp (this->type, type) == 0)
635 first_callback = this->next;
637 prev->next = this->next;
642 } /* void cf_unregister */
644 void cf_unregister_complex (const char *type)
646 cf_complex_callback_t *this, *prev;
648 for (prev = NULL, this = complex_callback_head;
650 prev = this, this = this->next)
651 if (strcasecmp (this->type, type) == 0)
654 complex_callback_head = this->next;
656 prev->next = this->next;
662 } /* void cf_unregister */
664 void cf_register (const char *type,
665 int (*callback) (const char *, const char *),
666 const char **keys, int keys_num)
668 cf_callback_t *cf_cb;
670 /* Remove this module from the list, if it already exists */
671 cf_unregister (type);
673 /* This pointer will be free'd in `cf_unregister' */
674 if ((cf_cb = (cf_callback_t *) malloc (sizeof (cf_callback_t))) == NULL)
678 cf_cb->callback = callback;
680 cf_cb->keys_num = keys_num;
682 cf_cb->next = first_callback;
683 first_callback = cf_cb;
684 } /* void cf_register */
686 int cf_register_complex (const char *type, int (*callback) (oconfig_item_t *))
688 cf_complex_callback_t *new;
690 new = (cf_complex_callback_t *) malloc (sizeof (cf_complex_callback_t));
694 new->type = strdup (type);
695 if (new->type == NULL)
701 new->callback = callback;
704 if (complex_callback_head == NULL)
706 complex_callback_head = new;
710 cf_complex_callback_t *last = complex_callback_head;
711 while (last->next != NULL)
717 } /* int cf_register_complex */
719 int cf_read (char *filename)
721 oconfig_item_t *conf;
724 conf = cf_read_generic (filename, 0 /* depth */);
727 ERROR ("Unable to read config file %s.", filename);
731 for (i = 0; i < conf->children_num; i++)
733 if (conf->children[i].children == NULL)
734 dispatch_value (conf->children + i);
736 dispatch_block (conf->children + i);
739 if (cf_default_typesdb)
740 read_types_list (PLUGINDIR"/types.db"); /* FIXME: Configure path */