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"
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_value_map_s
49 int (*func) (const oconfig_item_t *);
52 typedef struct cf_global_option_s
60 * Prototypes of callback functions
62 static int dispatch_value_plugindir (const oconfig_item_t *ci);
63 static int dispatch_value_loadds (const oconfig_item_t *ci);
64 static int dispatch_value_loadplugin (const oconfig_item_t *ci);
69 static cf_callback_t *first_callback = NULL;
71 static cf_value_map_t cf_value_map[] =
73 {"PluginDir", dispatch_value_plugindir},
74 {"LoadPlugin", dispatch_value_loadplugin},
75 {"LoadDS", dispatch_value_loadds}
77 static int cf_value_map_num = STATIC_ARRAY_LEN (cf_value_map);
79 static cf_global_option_t cf_global_options[] =
81 {"BaseDir", NULL, PKGLOCALSTATEDIR},
82 {"PIDFile", NULL, PIDFILE},
83 {"Hostname", NULL, NULL},
84 {"Interval", NULL, "10"},
85 {"ReadThreads", NULL, "5"}
87 static int cf_global_options_num = STATIC_ARRAY_LEN (cf_global_options);
90 * Functions to handle register/unregister, search, and other plugin related
93 static cf_callback_t *cf_search (const char *type)
100 for (cf_cb = first_callback; cf_cb != NULL; cf_cb = cf_cb->next)
101 if (strcasecmp (cf_cb->type, type) == 0)
107 static int cf_dispatch (const char *type, const char *orig_key,
108 const char *orig_value)
110 cf_callback_t *cf_cb;
116 DEBUG ("type = %s, key = %s, value = %s",
118 ESCAPE_NULL(orig_key),
119 ESCAPE_NULL(orig_value));
121 if ((cf_cb = cf_search (type)) == NULL)
123 WARNING ("Plugin `%s' did not register a callback.", type);
127 if ((key = strdup (orig_key)) == NULL)
129 if ((value = strdup (orig_value)) == NULL)
137 for (i = 0; i < cf_cb->keys_num; i++)
139 if (strcasecmp (cf_cb->keys[i], key) == 0)
141 ret = (*cf_cb->callback) (key, value);
146 if (i >= cf_cb->keys_num)
147 WARNING ("Plugin `%s' did not register for value `%s'.", type, key);
152 DEBUG ("return (%i)", ret);
155 } /* int cf_dispatch */
157 static int dispatch_global_option (const oconfig_item_t *ci)
159 if (ci->values_num != 1)
161 if (ci->values[0].type == OCONFIG_TYPE_STRING)
162 return (global_option_set (ci->key, ci->values[0].value.string));
163 else if (ci->values[0].type == OCONFIG_TYPE_NUMBER)
166 snprintf (tmp, sizeof (tmp), "%lf", ci->values[0].value.number);
168 return (global_option_set (ci->key, tmp));
172 } /* int dispatch_global_option */
174 static int dispatch_value_plugindir (const oconfig_item_t *ci)
176 assert (strcasecmp (ci->key, "PluginDir") == 0);
178 if (ci->values_num != 1)
180 if (ci->values[0].type != OCONFIG_TYPE_STRING)
183 plugin_set_dir (ci->values[0].value.string);
187 static int dispatch_value_loadds (const oconfig_item_t *ci)
189 assert (strcasecmp (ci->key, "LoadDS") == 0);
191 if (ci->values_num != 1)
193 if (ci->values[0].type != OCONFIG_TYPE_STRING)
196 return (plugin_load (ci->values[0].value.string, MR_DATASETS));
197 } /* int dispatch_value_loadds */
199 static int dispatch_value_loadplugin (const oconfig_item_t *ci)
201 assert (strcasecmp (ci->key, "LoadPlugin") == 0);
203 if (ci->values_num != 1)
205 if (ci->values[0].type != OCONFIG_TYPE_STRING)
208 return (plugin_load (ci->values[0].value.string, MR_EVERYTHING));
209 } /* int dispatch_value_loadplugin */
211 static int dispatch_value_plugin (const char *plugin, oconfig_item_t *ci)
219 buffer_free = sizeof (buffer);
221 for (i = 0; i < ci->values_num; i++)
225 if (ci->values[i].type == OCONFIG_TYPE_STRING)
226 status = snprintf (buffer_ptr, buffer_free, " %s",
227 ci->values[i].value.string);
228 else if (ci->values[i].type == OCONFIG_TYPE_NUMBER)
229 status = snprintf (buffer_ptr, buffer_free, " %lf",
230 ci->values[i].value.number);
231 else if (ci->values[i].type == OCONFIG_TYPE_BOOLEAN)
232 status = snprintf (buffer_ptr, buffer_free, " %s",
233 ci->values[i].value.boolean
236 if ((status < 0) || (status >= buffer_free))
238 buffer_free -= status;
239 buffer_ptr += status;
241 /* skip the initial space */
242 buffer_ptr = buffer + 1;
244 return (cf_dispatch (plugin, ci->key, buffer_ptr));
245 } /* int plugin_conf_dispatch */
247 static int dispatch_value (const oconfig_item_t *ci)
252 for (i = 0; i < cf_value_map_num; i++)
253 if (strcasecmp (cf_value_map[i].key, ci->key) == 0)
255 ret = cf_value_map[i].func (ci);
259 for (i = 0; i < cf_global_options_num; i++)
260 if (strcasecmp (cf_global_options[i].key, ci->key) == 0)
262 ret = dispatch_global_option (ci);
267 } /* int dispatch_value */
269 static int dispatch_block_plugin (oconfig_item_t *ci)
274 if (strcasecmp (ci->key, "Plugin") != 0)
276 if (ci->values_num != 1)
278 if (ci->values[0].type != OCONFIG_TYPE_STRING)
281 name = ci->values[0].value.string;
283 for (i = 0; i < ci->children_num; i++)
285 if (ci->children[i].children == NULL)
286 dispatch_value_plugin (name, ci->children + i);
288 {DEBUG ("No nested config blocks allow for plugins. Yet.");}
295 static int dispatch_block (oconfig_item_t *ci)
297 if (strcasecmp (ci->key, "Plugin") == 0)
298 return (dispatch_block_plugin (ci));
306 int global_option_set (const char *option, const char *value)
310 DEBUG ("option = %s; value = %s;", option, value);
312 for (i = 0; i < cf_global_options_num; i++)
313 if (strcasecmp (cf_global_options[i].key, option) == 0)
316 if (i >= cf_global_options_num)
319 sfree (cf_global_options[i].value);
322 cf_global_options[i].value = strdup (value);
324 cf_global_options[i].value = NULL;
329 const char *global_option_get (const char *option)
333 for (i = 0; i < cf_global_options_num; i++)
334 if (strcasecmp (cf_global_options[i].key, option) == 0)
337 if (i >= cf_global_options_num)
340 return ((cf_global_options[i].value != NULL)
341 ? cf_global_options[i].value
342 : cf_global_options[i].def);
343 } /* char *global_option_get */
345 void cf_unregister (const char *type)
347 cf_callback_t *this, *prev;
349 for (prev = NULL, this = first_callback;
351 prev = this, this = this->next)
352 if (strcasecmp (this->type, type) == 0)
355 first_callback = this->next;
357 prev->next = this->next;
362 } /* void cf_unregister */
364 void cf_register (const char *type,
365 int (*callback) (const char *, const char *),
366 const char **keys, int keys_num)
368 cf_callback_t *cf_cb;
370 /* Remove this module from the list, if it already exists */
371 cf_unregister (type);
373 /* This pointer will be free'd in `cf_unregister' */
374 if ((cf_cb = (cf_callback_t *) malloc (sizeof (cf_callback_t))) == NULL)
378 cf_cb->callback = callback;
380 cf_cb->keys_num = keys_num;
382 cf_cb->next = first_callback;
383 first_callback = cf_cb;
384 } /* void cf_register */
386 int cf_read (char *filename)
388 oconfig_item_t *conf;
391 conf = oconfig_parse_file (filename);
394 ERROR ("Unable to read config file %s.", filename);
398 for (i = 0; i < conf->children_num; i++)
400 if (conf->children[i].children == NULL)
401 dispatch_value (conf->children + i);
403 dispatch_block (conf->children + i);