1 /**
2 * collectd - src/configfile.c
3 * Copyright (C) 2005,2006 Florian octo Forster
4 *
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.
9 *
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.
14 *
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
18 *
19 * Authors:
20 * Florian octo Forster <octo at verplant.org>
21 **/
23 #include "collectd.h"
25 #include "liboconfig/oconfig.h"
27 #include "common.h"
28 #include "plugin.h"
29 #include "configfile.h"
31 #define ESCAPE_NULL(str) ((str) == NULL ? "(null)" : (str))
33 /*
34 * Private types
35 */
36 typedef struct cf_callback
37 {
38 const char *type;
39 int (*callback) (const char *, const char *);
40 const char **keys;
41 int keys_num;
42 struct cf_callback *next;
43 } cf_callback_t;
45 typedef struct cf_value_map_s
46 {
47 char *key;
48 int (*func) (const oconfig_item_t *);
49 } cf_value_map_t;
51 typedef struct cf_global_option_s
52 {
53 char *key;
54 char *value;
55 char *def;
56 } cf_global_option_t;
58 /*
59 * Prototypes of callback functions
60 */
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);
65 /*
66 * Private variables
67 */
68 static cf_callback_t *first_callback = NULL;
70 static cf_value_map_t cf_value_map[] =
71 {
72 {"PluginDir", dispatch_value_plugindir},
73 {"LoadPlugin", dispatch_value_loadplugin},
74 {"LoadDS", dispatch_value_loadds}
75 };
76 static int cf_value_map_num = STATIC_ARRAY_LEN (cf_value_map);
78 static cf_global_option_t cf_global_options[] =
79 {
80 {"BaseDir", NULL, PKGLOCALSTATEDIR},
81 {"PIDFile", NULL, PIDFILE},
82 {"Hostname", NULL, NULL},
83 {"Interval", NULL, "10"},
84 {"ReadThreads", NULL, "5"},
85 {"TypesDS", NULL, PLUGINDIR"/types.db"} /* FIXME: Configure path */
86 };
87 static int cf_global_options_num = STATIC_ARRAY_LEN (cf_global_options);
89 /*
90 * Functions to handle register/unregister, search, and other plugin related
91 * stuff
92 */
93 static cf_callback_t *cf_search (const char *type)
94 {
95 cf_callback_t *cf_cb;
97 if (type == NULL)
98 return (NULL);
100 for (cf_cb = first_callback; cf_cb != NULL; cf_cb = cf_cb->next)
101 if (strcasecmp (cf_cb->type, type) == 0)
102 break;
104 return (cf_cb);
105 }
107 static int cf_dispatch (const char *type, const char *orig_key,
108 const char *orig_value)
109 {
110 cf_callback_t *cf_cb;
111 char *key;
112 char *value;
113 int ret;
114 int i;
116 DEBUG ("type = %s, key = %s, value = %s",
117 ESCAPE_NULL(type),
118 ESCAPE_NULL(orig_key),
119 ESCAPE_NULL(orig_value));
121 if ((cf_cb = cf_search (type)) == NULL)
122 {
123 WARNING ("Plugin `%s' did not register a callback.", type);
124 return (-1);
125 }
127 if ((key = strdup (orig_key)) == NULL)
128 return (1);
129 if ((value = strdup (orig_value)) == NULL)
130 {
131 free (key);
132 return (2);
133 }
135 ret = -1;
137 for (i = 0; i < cf_cb->keys_num; i++)
138 {
139 if (strcasecmp (cf_cb->keys[i], key) == 0)
140 {
141 ret = (*cf_cb->callback) (key, value);
142 break;
143 }
144 }
146 if (i >= cf_cb->keys_num)
147 WARNING ("Plugin `%s' did not register for value `%s'.", type, key);
149 free (key);
150 free (value);
152 DEBUG ("return (%i)", ret);
154 return (ret);
155 } /* int cf_dispatch */
157 static int dispatch_global_option (const oconfig_item_t *ci)
158 {
159 if (ci->values_num != 1)
160 return (-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)
164 {
165 char tmp[128];
166 snprintf (tmp, sizeof (tmp), "%lf", ci->values[0].value.number);
167 tmp[127] = '\0';
168 return (global_option_set (ci->key, tmp));
169 }
171 return (-1);
172 } /* int dispatch_global_option */
174 static int dispatch_value_plugindir (const oconfig_item_t *ci)
175 {
176 assert (strcasecmp (ci->key, "PluginDir") == 0);
178 if (ci->values_num != 1)
179 return (-1);
180 if (ci->values[0].type != OCONFIG_TYPE_STRING)
181 return (-1);
183 plugin_set_dir (ci->values[0].value.string);
184 return (0);
185 }
187 static int dispatch_value_loadds (const oconfig_item_t *ci)
188 {
189 assert (strcasecmp (ci->key, "LoadDS") == 0);
191 if (ci->values_num != 1)
192 return (-1);
193 if (ci->values[0].type != OCONFIG_TYPE_STRING)
194 return (-1);
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)
200 {
201 assert (strcasecmp (ci->key, "LoadPlugin") == 0);
203 if (ci->values_num != 1)
204 return (-1);
205 if (ci->values[0].type != OCONFIG_TYPE_STRING)
206 return (-1);
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)
212 {
213 char buffer[4096];
214 char *buffer_ptr;
215 int buffer_free;
216 int i;
218 buffer_ptr = buffer;
219 buffer_free = sizeof (buffer);
221 for (i = 0; i < ci->values_num; i++)
222 {
223 int status = -1;
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
234 ? "true" : "false");
236 if ((status < 0) || (status >= buffer_free))
237 return (-1);
238 buffer_free -= status;
239 buffer_ptr += status;
240 }
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)
248 {
249 int ret = -2;
250 int i;
252 for (i = 0; i < cf_value_map_num; i++)
253 if (strcasecmp (cf_value_map[i].key, ci->key) == 0)
254 {
255 ret = cf_value_map[i].func (ci);
256 break;
257 }
259 for (i = 0; i < cf_global_options_num; i++)
260 if (strcasecmp (cf_global_options[i].key, ci->key) == 0)
261 {
262 ret = dispatch_global_option (ci);
263 break;
264 }
266 return (ret);
267 } /* int dispatch_value */
269 static int dispatch_block_plugin (oconfig_item_t *ci)
270 {
271 int i;
272 char *name;
274 if (strcasecmp (ci->key, "Plugin") != 0)
275 return (-1);
276 if (ci->values_num != 1)
277 return (-1);
278 if (ci->values[0].type != OCONFIG_TYPE_STRING)
279 return (-1);
281 name = ci->values[0].value.string;
283 for (i = 0; i < ci->children_num; i++)
284 {
285 if (ci->children[i].children == NULL)
286 dispatch_value_plugin (name, ci->children + i);
287 else
288 {DEBUG ("No nested config blocks allow for plugins. Yet.");}
289 }
291 return (0);
292 }
295 static int dispatch_block (oconfig_item_t *ci)
296 {
297 if (strcasecmp (ci->key, "Plugin") == 0)
298 return (dispatch_block_plugin (ci));
300 return (0);
301 }
303 /*
304 * Public functions
305 */
306 int global_option_set (const char *option, const char *value)
307 {
308 int i;
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)
314 break;
316 if (i >= cf_global_options_num)
317 return (-1);
319 sfree (cf_global_options[i].value);
321 if (value != NULL)
322 cf_global_options[i].value = strdup (value);
323 else
324 cf_global_options[i].value = NULL;
326 return (0);
327 }
329 const char *global_option_get (const char *option)
330 {
331 int i;
333 for (i = 0; i < cf_global_options_num; i++)
334 if (strcasecmp (cf_global_options[i].key, option) == 0)
335 break;
337 if (i >= cf_global_options_num)
338 return (NULL);
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)
346 {
347 cf_callback_t *this, *prev;
349 for (prev = NULL, this = first_callback;
350 this != NULL;
351 prev = this, this = this->next)
352 if (strcasecmp (this->type, type) == 0)
353 {
354 if (prev == NULL)
355 first_callback = this->next;
356 else
357 prev->next = this->next;
359 free (this);
360 break;
361 }
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)
367 {
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)
375 return;
377 cf_cb->type = type;
378 cf_cb->callback = callback;
379 cf_cb->keys = keys;
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)
387 {
388 oconfig_item_t *conf;
389 int i;
391 conf = oconfig_parse_file (filename);
392 if (conf == NULL)
393 {
394 ERROR ("Unable to read config file %s.", filename);
395 return (-1);
396 }
398 for (i = 0; i < conf->children_num; i++)
399 {
400 if (conf->children[i].children == NULL)
401 dispatch_value (conf->children + i);
402 else
403 dispatch_block (conf->children + i);
404 }
406 return (0);
407 } /* int cf_read */