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"
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_value_map_s
48 int (*func) (const oconfig_item_t *);
51 typedef struct cf_global_option_s
59 * Prototypes of callback functions
61 static int dispatch_value_plugindir (const oconfig_item_t *ci);
62 static int dispatch_value_loadds (const oconfig_item_t *ci);
63 static int dispatch_value_loadplugin (const oconfig_item_t *ci);
68 static cf_callback_t *first_callback = NULL;
70 static cf_value_map_t cf_value_map[] =
72 {"PluginDir", dispatch_value_plugindir},
73 {"LoadPlugin", dispatch_value_loadplugin},
74 {"LoadDS", dispatch_value_loadds}
76 static int cf_value_map_num = STATIC_ARRAY_LEN (cf_value_map);
78 static cf_global_option_t cf_global_options[] =
80 {"BaseDir", NULL, PKGLOCALSTATEDIR},
81 {"PIDFile", NULL, PIDFILE},
82 {"Hostname", NULL, NULL},
83 {"Interval", NULL, "10"},
84 {"ReadThreads", NULL, "5"}
86 static int cf_global_options_num = STATIC_ARRAY_LEN (cf_global_options);
89 * Functions to handle register/unregister, search, and other plugin related
92 static cf_callback_t *cf_search (const char *type)
99 for (cf_cb = first_callback; cf_cb != NULL; cf_cb = cf_cb->next)
100 if (strcasecmp (cf_cb->type, type) == 0)
106 static int cf_dispatch (const char *type, const char *orig_key,
107 const char *orig_value)
109 cf_callback_t *cf_cb;
115 DEBUG ("type = %s, key = %s, value = %s",
117 ESCAPE_NULL(orig_key),
118 ESCAPE_NULL(orig_value));
120 if ((cf_cb = cf_search (type)) == NULL)
122 WARNING ("Plugin `%s' did not register a callback.", type);
126 if ((key = strdup (orig_key)) == NULL)
128 if ((value = strdup (orig_value)) == NULL)
136 for (i = 0; i < cf_cb->keys_num; i++)
138 if (strcasecmp (cf_cb->keys[i], key) == 0)
140 ret = (*cf_cb->callback) (key, value);
145 if (i >= cf_cb->keys_num)
146 WARNING ("Plugin `%s' did not register for value `%s'.", type, key);
151 DEBUG ("return (%i)", ret);
154 } /* int cf_dispatch */
156 static int dispatch_global_option (const oconfig_item_t *ci)
158 if (ci->values_num != 1)
160 if (ci->values[0].type == OCONFIG_TYPE_STRING)
161 return (global_option_set (ci->key, ci->values[0].value.string));
162 else if (ci->values[0].type == OCONFIG_TYPE_NUMBER)
165 snprintf (tmp, sizeof (tmp), "%lf", ci->values[0].value.number);
167 return (global_option_set (ci->key, tmp));
171 } /* int dispatch_global_option */
173 static int dispatch_value_plugindir (const oconfig_item_t *ci)
175 assert (strcasecmp (ci->key, "PluginDir") == 0);
177 if (ci->values_num != 1)
179 if (ci->values[0].type != OCONFIG_TYPE_STRING)
182 plugin_set_dir (ci->values[0].value.string);
186 static int dispatch_value_loadds (const oconfig_item_t *ci)
188 assert (strcasecmp (ci->key, "LoadDS") == 0);
190 if (ci->values_num != 1)
192 if (ci->values[0].type != OCONFIG_TYPE_STRING)
195 return (plugin_load (ci->values[0].value.string, MR_DATASETS));
196 } /* int dispatch_value_loadds */
198 static int dispatch_value_loadplugin (const oconfig_item_t *ci)
200 assert (strcasecmp (ci->key, "LoadPlugin") == 0);
202 if (ci->values_num != 1)
204 if (ci->values[0].type != OCONFIG_TYPE_STRING)
207 return (plugin_load (ci->values[0].value.string, MR_EVERYTHING));
208 } /* int dispatch_value_loadplugin */
210 static int dispatch_value_plugin (const char *plugin, oconfig_item_t *ci)
218 buffer_free = sizeof (buffer);
220 for (i = 0; i < ci->values_num; i++)
224 if (ci->values[i].type == OCONFIG_TYPE_STRING)
225 status = snprintf (buffer_ptr, buffer_free, " %s",
226 ci->values[i].value.string);
227 else if (ci->values[i].type == OCONFIG_TYPE_NUMBER)
228 status = snprintf (buffer_ptr, buffer_free, " %lf",
229 ci->values[i].value.number);
230 else if (ci->values[i].type == OCONFIG_TYPE_BOOLEAN)
231 status = snprintf (buffer_ptr, buffer_free, " %s",
232 ci->values[i].value.boolean
235 if ((status < 0) || (status >= buffer_free))
237 buffer_free -= status;
238 buffer_ptr += status;
240 /* skip the initial space */
241 buffer_ptr = buffer + 1;
243 return (cf_dispatch (plugin, ci->key, buffer_ptr));
244 } /* int plugin_conf_dispatch */
246 static int dispatch_value (const oconfig_item_t *ci)
251 for (i = 0; i < cf_value_map_num; i++)
252 if (strcasecmp (cf_value_map[i].key, ci->key) == 0)
254 ret = cf_value_map[i].func (ci);
258 for (i = 0; i < cf_global_options_num; i++)
259 if (strcasecmp (cf_global_options[i].key, ci->key) == 0)
261 ret = dispatch_global_option (ci);
266 } /* int dispatch_value */
268 static int dispatch_block_plugin (oconfig_item_t *ci)
273 if (strcasecmp (ci->key, "Plugin") != 0)
275 if (ci->values_num != 1)
277 if (ci->values[0].type != OCONFIG_TYPE_STRING)
280 name = ci->values[0].value.string;
282 for (i = 0; i < ci->children_num; i++)
284 if (ci->children[i].children == NULL)
285 dispatch_value_plugin (name, ci->children + i);
287 {DEBUG ("No nested config blocks allow for plugins. Yet.");}
294 static int dispatch_block (oconfig_item_t *ci)
296 if (strcasecmp (ci->key, "Plugin") == 0)
297 return (dispatch_block_plugin (ci));
305 int global_option_set (const char *option, const char *value)
309 DEBUG ("option = %s; value = %s;", option, value);
311 for (i = 0; i < cf_global_options_num; i++)
312 if (strcasecmp (cf_global_options[i].key, option) == 0)
315 if (i >= cf_global_options_num)
318 sfree (cf_global_options[i].value);
321 cf_global_options[i].value = strdup (value);
323 cf_global_options[i].value = NULL;
328 const char *global_option_get (const char *option)
332 for (i = 0; i < cf_global_options_num; i++)
333 if (strcasecmp (cf_global_options[i].key, option) == 0)
336 if (i >= cf_global_options_num)
339 return ((cf_global_options[i].value != NULL)
340 ? cf_global_options[i].value
341 : cf_global_options[i].def);
342 } /* char *global_option_get */
344 void cf_unregister (const char *type)
346 cf_callback_t *this, *prev;
348 for (prev = NULL, this = first_callback;
350 prev = this, this = this->next)
351 if (strcasecmp (this->type, type) == 0)
354 first_callback = this->next;
356 prev->next = this->next;
361 } /* void cf_unregister */
363 void cf_register (const char *type,
364 int (*callback) (const char *, const char *),
365 const char **keys, int keys_num)
367 cf_callback_t *cf_cb;
369 /* Remove this module from the list, if it already exists */
370 cf_unregister (type);
372 /* This pointer will be free'd in `cf_unregister' */
373 if ((cf_cb = (cf_callback_t *) malloc (sizeof (cf_callback_t))) == NULL)
377 cf_cb->callback = callback;
379 cf_cb->keys_num = keys_num;
381 cf_cb->next = first_callback;
382 first_callback = cf_cb;
383 } /* void cf_register */
385 int cf_read (char *filename)
387 oconfig_item_t *conf;
390 conf = oconfig_parse_file (filename);
393 ERROR ("Unable to read config file %s.", filename);
397 for (i = 0; i < conf->children_num; i++)
399 if (conf->children[i].children == NULL)
400 dispatch_value (conf->children + i);
402 dispatch_block (conf->children + i);