2 * collectd - src/configfile.c
3 * Copyright (C) 2005,2006 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 "utils_threshold.h"
32 #define ESCAPE_NULL(str) ((str) == NULL ? "(null)" : (str))
37 typedef struct cf_callback
40 int (*callback) (const char *, const char *);
43 struct cf_callback *next;
46 typedef struct cf_complex_callback_s
49 int (*callback) (oconfig_item_t *);
50 struct cf_complex_callback_s *next;
51 } cf_complex_callback_t;
53 typedef struct cf_value_map_s
56 int (*func) (const oconfig_item_t *);
59 typedef struct cf_global_option_s
67 * Prototypes of callback functions
69 static int dispatch_value_plugindir (const oconfig_item_t *ci);
70 static int dispatch_value_loadplugin (const oconfig_item_t *ci);
75 static cf_callback_t *first_callback = NULL;
76 static cf_complex_callback_t *complex_callback_head = NULL;
78 static cf_value_map_t cf_value_map[] =
80 {"PluginDir", dispatch_value_plugindir},
81 {"LoadPlugin", dispatch_value_loadplugin}
83 static int cf_value_map_num = STATIC_ARRAY_LEN (cf_value_map);
85 static cf_global_option_t cf_global_options[] =
87 {"BaseDir", NULL, PKGLOCALSTATEDIR},
88 {"PIDFile", NULL, PIDFILE},
89 {"Hostname", NULL, NULL},
90 {"Interval", NULL, "10"},
91 {"ReadThreads", NULL, "5"},
92 {"TypesDB", NULL, PLUGINDIR"/types.db"} /* FIXME: Configure path */
94 static int cf_global_options_num = STATIC_ARRAY_LEN (cf_global_options);
97 * Functions to handle register/unregister, search, and other plugin related
100 static cf_callback_t *cf_search (const char *type)
102 cf_callback_t *cf_cb;
107 for (cf_cb = first_callback; cf_cb != NULL; cf_cb = cf_cb->next)
108 if (strcasecmp (cf_cb->type, type) == 0)
114 static int cf_dispatch (const char *type, const char *orig_key,
115 const char *orig_value)
117 cf_callback_t *cf_cb;
123 DEBUG ("type = %s, key = %s, value = %s",
125 ESCAPE_NULL(orig_key),
126 ESCAPE_NULL(orig_value));
128 if ((cf_cb = cf_search (type)) == NULL)
130 WARNING ("Plugin `%s' did not register a callback.", type);
134 if ((key = strdup (orig_key)) == NULL)
136 if ((value = strdup (orig_value)) == NULL)
144 for (i = 0; i < cf_cb->keys_num; i++)
146 if (strcasecmp (cf_cb->keys[i], key) == 0)
148 ret = (*cf_cb->callback) (key, value);
153 if (i >= cf_cb->keys_num)
154 WARNING ("Plugin `%s' did not register for value `%s'.", type, key);
159 DEBUG ("return (%i)", ret);
162 } /* int cf_dispatch */
164 static int dispatch_global_option (const oconfig_item_t *ci)
166 if (ci->values_num != 1)
168 if (ci->values[0].type == OCONFIG_TYPE_STRING)
169 return (global_option_set (ci->key, ci->values[0].value.string));
170 else if (ci->values[0].type == OCONFIG_TYPE_NUMBER)
173 snprintf (tmp, sizeof (tmp), "%lf", ci->values[0].value.number);
175 return (global_option_set (ci->key, tmp));
179 } /* int dispatch_global_option */
181 static int dispatch_value_plugindir (const oconfig_item_t *ci)
183 assert (strcasecmp (ci->key, "PluginDir") == 0);
185 if (ci->values_num != 1)
187 if (ci->values[0].type != OCONFIG_TYPE_STRING)
190 plugin_set_dir (ci->values[0].value.string);
194 static int dispatch_value_loadplugin (const oconfig_item_t *ci)
196 assert (strcasecmp (ci->key, "LoadPlugin") == 0);
198 if (ci->values_num != 1)
200 if (ci->values[0].type != OCONFIG_TYPE_STRING)
203 return (plugin_load (ci->values[0].value.string));
204 } /* int dispatch_value_loadplugin */
206 static int dispatch_value_plugin (const char *plugin, oconfig_item_t *ci)
214 buffer_free = sizeof (buffer);
216 for (i = 0; i < ci->values_num; i++)
220 if (ci->values[i].type == OCONFIG_TYPE_STRING)
221 status = snprintf (buffer_ptr, buffer_free, " %s",
222 ci->values[i].value.string);
223 else if (ci->values[i].type == OCONFIG_TYPE_NUMBER)
224 status = snprintf (buffer_ptr, buffer_free, " %lf",
225 ci->values[i].value.number);
226 else if (ci->values[i].type == OCONFIG_TYPE_BOOLEAN)
227 status = snprintf (buffer_ptr, buffer_free, " %s",
228 ci->values[i].value.boolean
231 if ((status < 0) || (status >= buffer_free))
233 buffer_free -= status;
234 buffer_ptr += status;
236 /* skip the initial space */
237 buffer_ptr = buffer + 1;
239 return (cf_dispatch (plugin, ci->key, buffer_ptr));
240 } /* int plugin_conf_dispatch */
242 static int dispatch_value (const oconfig_item_t *ci)
247 for (i = 0; i < cf_value_map_num; i++)
248 if (strcasecmp (cf_value_map[i].key, ci->key) == 0)
250 ret = cf_value_map[i].func (ci);
254 for (i = 0; i < cf_global_options_num; i++)
255 if (strcasecmp (cf_global_options[i].key, ci->key) == 0)
257 ret = dispatch_global_option (ci);
262 } /* int dispatch_value */
264 static int dispatch_block_plugin (oconfig_item_t *ci)
269 cf_complex_callback_t *cb;
271 if (strcasecmp (ci->key, "Plugin") != 0)
273 if (ci->values_num < 1)
275 if (ci->values[0].type != OCONFIG_TYPE_STRING)
278 name = ci->values[0].value.string;
280 /* Check for a complex callback first */
281 for (cb = complex_callback_head; cb != NULL; cb = cb->next)
282 if (strcasecmp (name, cb->type) == 0)
283 return (cb->callback (ci));
285 /* Hm, no complex plugin found. Dispatch the values one by one */
286 for (i = 0; i < ci->children_num; i++)
288 if (ci->children[i].children == NULL)
289 dispatch_value_plugin (name, ci->children + i);
291 {DEBUG ("No nested config blocks allow for this plugin.");}
298 static int dispatch_block (oconfig_item_t *ci)
300 if (strcasecmp (ci->key, "Plugin") == 0)
301 return (dispatch_block_plugin (ci));
302 else if (strcasecmp (ci->key, "Threshold") == 0)
303 return (ut_config (ci));
308 #define CF_MAX_DEPTH 8
309 static oconfig_item_t *cf_read_file (const char *file, int depth);
311 static int cf_include_all (oconfig_item_t *root, int depth)
315 for (i = 0; i < root->children_num; i++)
320 /* Ignore all blocks, including `Include' blocks. */
321 if (root->children[i].children_num != 0)
324 if (strcasecmp (root->children[i].key, "Include") != 0)
327 old = root->children + i;
329 if ((old->values_num != 1)
330 || (old->values[0].type != OCONFIG_TYPE_STRING))
332 ERROR ("configfile: `Include' needs exactly one string argument.");
336 new = cf_read_file (old->values[0].value.string, depth + 1);
340 /* There are more children now. We need to expand
342 if (new->children_num > 1)
344 oconfig_item_t *temp;
346 DEBUG ("configfile: Resizing root-children from %i to %i elements.",
348 root->children_num + new->children_num - 1);
350 temp = (oconfig_item_t *) realloc (root->children,
351 sizeof (oconfig_item_t)
352 * (root->children_num + new->children_num - 1));
355 ERROR ("configfile: realloc failed.");
359 root->children = temp;
362 /* Clean up the old include directive while we still have a
364 DEBUG ("configfile: Cleaning up `old'");
365 /* sfree (old->values[0].value.string); */
368 /* If there are trailing children and the number of children
369 * changes, we need to move the trailing ones either one to the
370 * front or (new->num - 1) to the back */
371 if (((root->children_num - i) > 1)
372 && (new->children_num != 1))
374 DEBUG ("configfile: Moving trailing children.");
375 memmove (root->children + i + new->children_num,
376 root->children + i + 1,
377 sizeof (oconfig_item_t)
378 * (root->children_num - (i + 1)));
381 /* Now copy the new children to where the include statement was */
382 if (new->children_num > 0)
384 DEBUG ("configfile: Copying new children.");
385 memcpy (root->children + i,
387 sizeof (oconfig_item_t)
388 * new->children_num);
391 /* Adjust the number of children and the position in the list. */
392 root->children_num = root->children_num + new->children_num - 1;
393 i = i + new->children_num - 1;
395 /* Clean up the `new' struct. We set `new->children' to NULL so
396 * the stuff we've just copied pointers to isn't freed by
398 DEBUG ("configfile: Cleaning up `new'");
399 sfree (new->values); /* should be NULL anyway */
402 } /* for (i = 0; i < root->children_num; i++) */
405 } /* int cf_include_all */
407 static oconfig_item_t *cf_read_file (const char *file, int depth)
409 oconfig_item_t *root;
411 if (depth >= CF_MAX_DEPTH)
413 ERROR ("configfile: Not including `%s' because the maximum nesting depth has been reached.",
418 root = oconfig_parse_file (file);
421 ERROR ("configfile: Cannot read file `%s'.", file);
425 cf_include_all (root, depth);
428 } /* oconfig_item_t *cf_read_file */
433 int global_option_set (const char *option, const char *value)
437 DEBUG ("option = %s; value = %s;", option, value);
439 for (i = 0; i < cf_global_options_num; i++)
440 if (strcasecmp (cf_global_options[i].key, option) == 0)
443 if (i >= cf_global_options_num)
446 sfree (cf_global_options[i].value);
449 cf_global_options[i].value = strdup (value);
451 cf_global_options[i].value = NULL;
456 const char *global_option_get (const char *option)
460 for (i = 0; i < cf_global_options_num; i++)
461 if (strcasecmp (cf_global_options[i].key, option) == 0)
464 if (i >= cf_global_options_num)
467 return ((cf_global_options[i].value != NULL)
468 ? cf_global_options[i].value
469 : cf_global_options[i].def);
470 } /* char *global_option_get */
472 void cf_unregister (const char *type)
474 cf_callback_t *this, *prev;
476 for (prev = NULL, this = first_callback;
478 prev = this, this = this->next)
479 if (strcasecmp (this->type, type) == 0)
482 first_callback = this->next;
484 prev->next = this->next;
489 } /* void cf_unregister */
491 void cf_unregister_complex (const char *type)
493 cf_complex_callback_t *this, *prev;
495 for (prev = NULL, this = complex_callback_head;
497 prev = this, this = this->next)
498 if (strcasecmp (this->type, type) == 0)
501 complex_callback_head = this->next;
503 prev->next = this->next;
509 } /* void cf_unregister */
511 void cf_register (const char *type,
512 int (*callback) (const char *, const char *),
513 const char **keys, int keys_num)
515 cf_callback_t *cf_cb;
517 /* Remove this module from the list, if it already exists */
518 cf_unregister (type);
520 /* This pointer will be free'd in `cf_unregister' */
521 if ((cf_cb = (cf_callback_t *) malloc (sizeof (cf_callback_t))) == NULL)
525 cf_cb->callback = callback;
527 cf_cb->keys_num = keys_num;
529 cf_cb->next = first_callback;
530 first_callback = cf_cb;
531 } /* void cf_register */
533 int cf_register_complex (const char *type, int (*callback) (oconfig_item_t *))
535 cf_complex_callback_t *new;
537 new = (cf_complex_callback_t *) malloc (sizeof (cf_complex_callback_t));
541 new->type = strdup (type);
542 if (new->type == NULL)
548 new->callback = callback;
551 if (complex_callback_head == NULL)
553 complex_callback_head = new;
557 cf_complex_callback_t *last = complex_callback_head;
558 while (last->next != NULL)
564 } /* int cf_register_complex */
566 int cf_read (char *filename)
568 oconfig_item_t *conf;
571 conf = cf_read_file (filename, 0 /* depth */);
574 ERROR ("Unable to read config file %s.", filename);
578 for (i = 0; i < conf->children_num; i++)
580 if (conf->children[i].children == NULL)
581 dispatch_value (conf->children + i);
583 dispatch_block (conf->children + i);