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 #define CF_MAX_DEPTH 8
344 static oconfig_item_t *cf_read_file (const char *file, int depth);
346 static int cf_include_all (oconfig_item_t *root, int depth)
350 for (i = 0; i < root->children_num; i++)
355 /* Ignore all blocks, including `Include' blocks. */
356 if (root->children[i].children_num != 0)
359 if (strcasecmp (root->children[i].key, "Include") != 0)
362 old = root->children + i;
364 if ((old->values_num != 1)
365 || (old->values[0].type != OCONFIG_TYPE_STRING))
367 ERROR ("configfile: `Include' needs exactly one string argument.");
371 new = cf_read_file (old->values[0].value.string, depth + 1);
375 /* There are more children now. We need to expand
377 if (new->children_num > 1)
379 oconfig_item_t *temp;
381 DEBUG ("configfile: Resizing root-children from %i to %i elements.",
383 root->children_num + new->children_num - 1);
385 temp = (oconfig_item_t *) realloc (root->children,
386 sizeof (oconfig_item_t)
387 * (root->children_num + new->children_num - 1));
390 ERROR ("configfile: realloc failed.");
394 root->children = temp;
397 /* Clean up the old include directive while we still have a
399 DEBUG ("configfile: Cleaning up `old'");
400 /* sfree (old->values[0].value.string); */
403 /* If there are trailing children and the number of children
404 * changes, we need to move the trailing ones either one to the
405 * front or (new->num - 1) to the back */
406 if (((root->children_num - i) > 1)
407 && (new->children_num != 1))
409 DEBUG ("configfile: Moving trailing children.");
410 memmove (root->children + i + new->children_num,
411 root->children + i + 1,
412 sizeof (oconfig_item_t)
413 * (root->children_num - (i + 1)));
416 /* Now copy the new children to where the include statement was */
417 if (new->children_num > 0)
419 DEBUG ("configfile: Copying new children.");
420 memcpy (root->children + i,
422 sizeof (oconfig_item_t)
423 * new->children_num);
426 /* Adjust the number of children and the position in the list. */
427 root->children_num = root->children_num + new->children_num - 1;
428 i = i + new->children_num - 1;
430 /* Clean up the `new' struct. We set `new->children' to NULL so
431 * the stuff we've just copied pointers to isn't freed by
433 DEBUG ("configfile: Cleaning up `new'");
434 sfree (new->values); /* should be NULL anyway */
437 } /* for (i = 0; i < root->children_num; i++) */
440 } /* int cf_include_all */
442 static oconfig_item_t *cf_read_file (const char *file, int depth)
444 oconfig_item_t *root;
446 if (depth >= CF_MAX_DEPTH)
448 ERROR ("configfile: Not including `%s' because the maximum nesting depth has been reached.",
453 root = oconfig_parse_file (file);
456 ERROR ("configfile: Cannot read file `%s'.", file);
460 cf_include_all (root, depth);
463 } /* oconfig_item_t *cf_read_file */
468 int global_option_set (const char *option, const char *value)
472 DEBUG ("option = %s; value = %s;", option, value);
474 for (i = 0; i < cf_global_options_num; i++)
475 if (strcasecmp (cf_global_options[i].key, option) == 0)
478 if (i >= cf_global_options_num)
481 sfree (cf_global_options[i].value);
484 cf_global_options[i].value = strdup (value);
486 cf_global_options[i].value = NULL;
491 const char *global_option_get (const char *option)
495 for (i = 0; i < cf_global_options_num; i++)
496 if (strcasecmp (cf_global_options[i].key, option) == 0)
499 if (i >= cf_global_options_num)
502 return ((cf_global_options[i].value != NULL)
503 ? cf_global_options[i].value
504 : cf_global_options[i].def);
505 } /* char *global_option_get */
507 void cf_unregister (const char *type)
509 cf_callback_t *this, *prev;
511 for (prev = NULL, this = first_callback;
513 prev = this, this = this->next)
514 if (strcasecmp (this->type, type) == 0)
517 first_callback = this->next;
519 prev->next = this->next;
524 } /* void cf_unregister */
526 void cf_unregister_complex (const char *type)
528 cf_complex_callback_t *this, *prev;
530 for (prev = NULL, this = complex_callback_head;
532 prev = this, this = this->next)
533 if (strcasecmp (this->type, type) == 0)
536 complex_callback_head = this->next;
538 prev->next = this->next;
544 } /* void cf_unregister */
546 void cf_register (const char *type,
547 int (*callback) (const char *, const char *),
548 const char **keys, int keys_num)
550 cf_callback_t *cf_cb;
552 /* Remove this module from the list, if it already exists */
553 cf_unregister (type);
555 /* This pointer will be free'd in `cf_unregister' */
556 if ((cf_cb = (cf_callback_t *) malloc (sizeof (cf_callback_t))) == NULL)
560 cf_cb->callback = callback;
562 cf_cb->keys_num = keys_num;
564 cf_cb->next = first_callback;
565 first_callback = cf_cb;
566 } /* void cf_register */
568 int cf_register_complex (const char *type, int (*callback) (oconfig_item_t *))
570 cf_complex_callback_t *new;
572 new = (cf_complex_callback_t *) malloc (sizeof (cf_complex_callback_t));
576 new->type = strdup (type);
577 if (new->type == NULL)
583 new->callback = callback;
586 if (complex_callback_head == NULL)
588 complex_callback_head = new;
592 cf_complex_callback_t *last = complex_callback_head;
593 while (last->next != NULL)
599 } /* int cf_register_complex */
601 int cf_read (char *filename)
603 oconfig_item_t *conf;
606 conf = cf_read_file (filename, 0 /* depth */);
609 ERROR ("Unable to read config file %s.", filename);
613 for (i = 0; i < conf->children_num; i++)
615 if (conf->children[i].children == NULL)
616 dispatch_value (conf->children + i);
618 dispatch_block (conf->children + i);
621 if (cf_default_typesdb)
622 read_types_list (PLUGINDIR"/types.db"); /* FIXME: Configure path */