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"
31 #define ESCAPE_NULL(str) ((str) == NULL ? "(null)" : (str))
36 typedef struct cf_callback
39 int (*callback) (const char *, const char *);
42 struct cf_callback *next;
45 typedef struct cf_complex_callback_s
48 int (*callback) (oconfig_item_t *);
49 struct cf_complex_callback_s *next;
50 } cf_complex_callback_t;
52 typedef struct cf_value_map_s
55 int (*func) (const oconfig_item_t *);
58 typedef struct cf_global_option_s
66 * Prototypes of callback functions
68 static int dispatch_value_plugindir (const oconfig_item_t *ci);
69 static int dispatch_value_loadplugin (const oconfig_item_t *ci);
74 static cf_callback_t *first_callback = NULL;
75 static cf_complex_callback_t *complex_callback_head = NULL;
77 static cf_value_map_t cf_value_map[] =
79 {"PluginDir", dispatch_value_plugindir},
80 {"LoadPlugin", dispatch_value_loadplugin}
82 static int cf_value_map_num = STATIC_ARRAY_LEN (cf_value_map);
84 static cf_global_option_t cf_global_options[] =
86 {"BaseDir", NULL, PKGLOCALSTATEDIR},
87 {"PIDFile", NULL, PIDFILE},
88 {"Hostname", NULL, NULL},
89 {"Interval", NULL, "10"},
90 {"ReadThreads", NULL, "5"},
91 {"TypesDB", NULL, PLUGINDIR"/types.db"} /* FIXME: Configure path */
93 static int cf_global_options_num = STATIC_ARRAY_LEN (cf_global_options);
96 * Functions to handle register/unregister, search, and other plugin related
99 static cf_callback_t *cf_search (const char *type)
101 cf_callback_t *cf_cb;
106 for (cf_cb = first_callback; cf_cb != NULL; cf_cb = cf_cb->next)
107 if (strcasecmp (cf_cb->type, type) == 0)
113 static int cf_dispatch (const char *type, const char *orig_key,
114 const char *orig_value)
116 cf_callback_t *cf_cb;
122 DEBUG ("type = %s, key = %s, value = %s",
124 ESCAPE_NULL(orig_key),
125 ESCAPE_NULL(orig_value));
127 if ((cf_cb = cf_search (type)) == NULL)
129 WARNING ("Found a configuration for the `%s' plugin, but "
130 "the plugin isn't loaded or didn't register "
131 "a configuration callback.", type);
135 if ((key = strdup (orig_key)) == NULL)
137 if ((value = strdup (orig_value)) == NULL)
145 for (i = 0; i < cf_cb->keys_num; i++)
147 if (strcasecmp (cf_cb->keys[i], key) == 0)
149 ret = (*cf_cb->callback) (key, value);
154 if (i >= cf_cb->keys_num)
155 WARNING ("Plugin `%s' did not register for value `%s'.", type, key);
160 DEBUG ("return (%i)", ret);
163 } /* int cf_dispatch */
165 static int dispatch_global_option (const oconfig_item_t *ci)
167 if (ci->values_num != 1)
169 if (ci->values[0].type == OCONFIG_TYPE_STRING)
170 return (global_option_set (ci->key, ci->values[0].value.string));
171 else if (ci->values[0].type == OCONFIG_TYPE_NUMBER)
174 snprintf (tmp, sizeof (tmp), "%lf", ci->values[0].value.number);
176 return (global_option_set (ci->key, tmp));
180 } /* int dispatch_global_option */
182 static int dispatch_value_plugindir (const oconfig_item_t *ci)
184 assert (strcasecmp (ci->key, "PluginDir") == 0);
186 if (ci->values_num != 1)
188 if (ci->values[0].type != OCONFIG_TYPE_STRING)
191 plugin_set_dir (ci->values[0].value.string);
195 static int dispatch_value_loadplugin (const oconfig_item_t *ci)
197 assert (strcasecmp (ci->key, "LoadPlugin") == 0);
199 if (ci->values_num != 1)
201 if (ci->values[0].type != OCONFIG_TYPE_STRING)
204 return (plugin_load (ci->values[0].value.string));
205 } /* int dispatch_value_loadplugin */
207 static int dispatch_value_plugin (const char *plugin, oconfig_item_t *ci)
215 buffer_free = sizeof (buffer);
217 for (i = 0; i < ci->values_num; i++)
221 if (ci->values[i].type == OCONFIG_TYPE_STRING)
222 status = snprintf (buffer_ptr, buffer_free, " %s",
223 ci->values[i].value.string);
224 else if (ci->values[i].type == OCONFIG_TYPE_NUMBER)
225 status = snprintf (buffer_ptr, buffer_free, " %lf",
226 ci->values[i].value.number);
227 else if (ci->values[i].type == OCONFIG_TYPE_BOOLEAN)
228 status = snprintf (buffer_ptr, buffer_free, " %s",
229 ci->values[i].value.boolean
232 if ((status < 0) || (status >= buffer_free))
234 buffer_free -= status;
235 buffer_ptr += status;
237 /* skip the initial space */
238 buffer_ptr = buffer + 1;
240 return (cf_dispatch (plugin, ci->key, buffer_ptr));
241 } /* int plugin_conf_dispatch */
243 static int dispatch_value (const oconfig_item_t *ci)
248 for (i = 0; i < cf_value_map_num; i++)
249 if (strcasecmp (cf_value_map[i].key, ci->key) == 0)
251 ret = cf_value_map[i].func (ci);
255 for (i = 0; i < cf_global_options_num; i++)
256 if (strcasecmp (cf_global_options[i].key, ci->key) == 0)
258 ret = dispatch_global_option (ci);
263 } /* int dispatch_value */
265 static int dispatch_block_plugin (oconfig_item_t *ci)
270 cf_complex_callback_t *cb;
272 if (strcasecmp (ci->key, "Plugin") != 0)
274 if (ci->values_num < 1)
276 if (ci->values[0].type != OCONFIG_TYPE_STRING)
279 name = ci->values[0].value.string;
281 /* Check for a complex callback first */
282 for (cb = complex_callback_head; cb != NULL; cb = cb->next)
283 if (strcasecmp (name, cb->type) == 0)
284 return (cb->callback (ci));
286 /* Hm, no complex plugin found. Dispatch the values one by one */
287 for (i = 0; i < ci->children_num; i++)
289 if (ci->children[i].children == NULL)
290 dispatch_value_plugin (name, ci->children + i);
292 {DEBUG ("No nested config blocks allow for this plugin.");}
299 static int dispatch_block (oconfig_item_t *ci)
301 if (strcasecmp (ci->key, "Plugin") == 0)
302 return (dispatch_block_plugin (ci));
310 int global_option_set (const char *option, const char *value)
314 DEBUG ("option = %s; value = %s;", option, value);
316 for (i = 0; i < cf_global_options_num; i++)
317 if (strcasecmp (cf_global_options[i].key, option) == 0)
320 if (i >= cf_global_options_num)
323 sfree (cf_global_options[i].value);
326 cf_global_options[i].value = strdup (value);
328 cf_global_options[i].value = NULL;
333 const char *global_option_get (const char *option)
337 for (i = 0; i < cf_global_options_num; i++)
338 if (strcasecmp (cf_global_options[i].key, option) == 0)
341 if (i >= cf_global_options_num)
344 return ((cf_global_options[i].value != NULL)
345 ? cf_global_options[i].value
346 : cf_global_options[i].def);
347 } /* char *global_option_get */
349 void cf_unregister (const char *type)
351 cf_callback_t *this, *prev;
353 for (prev = NULL, this = first_callback;
355 prev = this, this = this->next)
356 if (strcasecmp (this->type, type) == 0)
359 first_callback = this->next;
361 prev->next = this->next;
366 } /* void cf_unregister */
368 void cf_unregister_complex (const char *type)
370 cf_complex_callback_t *this, *prev;
372 for (prev = NULL, this = complex_callback_head;
374 prev = this, this = this->next)
375 if (strcasecmp (this->type, type) == 0)
378 complex_callback_head = this->next;
380 prev->next = this->next;
386 } /* void cf_unregister */
388 void cf_register (const char *type,
389 int (*callback) (const char *, const char *),
390 const char **keys, int keys_num)
392 cf_callback_t *cf_cb;
394 /* Remove this module from the list, if it already exists */
395 cf_unregister (type);
397 /* This pointer will be free'd in `cf_unregister' */
398 if ((cf_cb = (cf_callback_t *) malloc (sizeof (cf_callback_t))) == NULL)
402 cf_cb->callback = callback;
404 cf_cb->keys_num = keys_num;
406 cf_cb->next = first_callback;
407 first_callback = cf_cb;
408 } /* void cf_register */
410 int cf_register_complex (const char *type, int (*callback) (oconfig_item_t *))
412 cf_complex_callback_t *new;
414 new = (cf_complex_callback_t *) malloc (sizeof (cf_complex_callback_t));
418 new->type = strdup (type);
419 if (new->type == NULL)
425 new->callback = callback;
428 if (complex_callback_head == NULL)
430 complex_callback_head = new;
434 cf_complex_callback_t *last = complex_callback_head;
435 while (last->next != NULL)
441 } /* int cf_register_complex */
443 int cf_read (char *filename)
445 oconfig_item_t *conf;
448 conf = oconfig_parse_file (filename);
451 ERROR ("Unable to read config file %s.", filename);
455 for (i = 0; i < conf->children_num; i++)
457 if (conf->children[i].children == NULL)
458 dispatch_value (conf->children + i);
460 dispatch_block (conf->children + i);