Merge branch 'master' into collectd-4
[collectd.git] / src / configfile.c
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  **/
22
23 #include "collectd.h"
24
25 #include "liboconfig/oconfig.h"
26
27 #include "common.h"
28 #include "plugin.h"
29 #include "configfile.h"
30
31 #define ESCAPE_NULL(str) ((str) == NULL ? "(null)" : (str))
32
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;
44
45 typedef struct cf_value_map_s
46 {
47         char *key;
48         int (*func) (const oconfig_item_t *);
49 } cf_value_map_t;
50
51 typedef struct cf_global_option_s
52 {
53         char *key;
54         char *value;
55         char *def;
56 } cf_global_option_t;
57
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);
64
65 /*
66  * Private variables
67  */
68 static cf_callback_t *first_callback = NULL;
69
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);
77
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 };
86 static int cf_global_options_num = STATIC_ARRAY_LEN (cf_global_options);
87
88 /*
89  * Functions to handle register/unregister, search, and other plugin related
90  * stuff
91  */
92 static cf_callback_t *cf_search (const char *type)
93 {
94         cf_callback_t *cf_cb;
95
96         if (type == NULL)
97                 return (NULL);
98
99         for (cf_cb = first_callback; cf_cb != NULL; cf_cb = cf_cb->next)
100                 if (strcasecmp (cf_cb->type, type) == 0)
101                         break;
102
103         return (cf_cb);
104 }
105
106 static int cf_dispatch (const char *type, const char *orig_key,
107                 const char *orig_value)
108 {
109         cf_callback_t *cf_cb;
110         char *key;
111         char *value;
112         int ret;
113         int i;
114
115         DEBUG ("type = %s, key = %s, value = %s",
116                         ESCAPE_NULL(type),
117                         ESCAPE_NULL(orig_key),
118                         ESCAPE_NULL(orig_value));
119
120         if ((cf_cb = cf_search (type)) == NULL)
121         {
122                 WARNING ("Plugin `%s' did not register a callback.", type);
123                 return (-1);
124         }
125
126         if ((key = strdup (orig_key)) == NULL)
127                 return (1);
128         if ((value = strdup (orig_value)) == NULL)
129         {
130                 free (key);
131                 return (2);
132         }
133
134         ret = -1;
135
136         for (i = 0; i < cf_cb->keys_num; i++)
137         {
138                 if (strcasecmp (cf_cb->keys[i], key) == 0)
139                 {
140                         ret = (*cf_cb->callback) (key, value);
141                         break;
142                 }
143         }
144
145         if (i >= cf_cb->keys_num)
146                 WARNING ("Plugin `%s' did not register for value `%s'.", type, key);
147
148         free (key);
149         free (value);
150
151         DEBUG ("return (%i)", ret);
152
153         return (ret);
154 } /* int cf_dispatch */
155
156 static int dispatch_global_option (const oconfig_item_t *ci)
157 {
158         if (ci->values_num != 1)
159                 return (-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)
163         {
164                 char tmp[128];
165                 snprintf (tmp, sizeof (tmp), "%lf", ci->values[0].value.number);
166                 tmp[127] = '\0';
167                 return (global_option_set (ci->key, tmp));
168         }
169
170         return (-1);
171 } /* int dispatch_global_option */
172
173 static int dispatch_value_plugindir (const oconfig_item_t *ci)
174 {
175         assert (strcasecmp (ci->key, "PluginDir") == 0);
176         
177         if (ci->values_num != 1)
178                 return (-1);
179         if (ci->values[0].type != OCONFIG_TYPE_STRING)
180                 return (-1);
181
182         plugin_set_dir (ci->values[0].value.string);
183         return (0);
184 }
185
186 static int dispatch_value_loadds (const oconfig_item_t *ci)
187 {
188         assert (strcasecmp (ci->key, "LoadDS") == 0);
189
190         if (ci->values_num != 1)
191                 return (-1);
192         if (ci->values[0].type != OCONFIG_TYPE_STRING)
193                 return (-1);
194
195         return (plugin_load (ci->values[0].value.string, MR_DATASETS));
196 } /* int dispatch_value_loadds */
197
198 static int dispatch_value_loadplugin (const oconfig_item_t *ci)
199 {
200         assert (strcasecmp (ci->key, "LoadPlugin") == 0);
201
202         if (ci->values_num != 1)
203                 return (-1);
204         if (ci->values[0].type != OCONFIG_TYPE_STRING)
205                 return (-1);
206
207         return (plugin_load (ci->values[0].value.string, MR_EVERYTHING));
208 } /* int dispatch_value_loadplugin */
209
210 static int dispatch_value_plugin (const char *plugin, oconfig_item_t *ci)
211 {
212         char  buffer[4096];
213         char *buffer_ptr;
214         int   buffer_free;
215         int i;
216
217         buffer_ptr = buffer;
218         buffer_free = sizeof (buffer);
219
220         for (i = 0; i < ci->values_num; i++)
221         {
222                 int status = -1;
223
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
233                                         ? "true" : "false");
234
235                 if ((status < 0) || (status >= buffer_free))
236                         return (-1);
237                 buffer_free -= status;
238                 buffer_ptr  += status;
239         }
240         /* skip the initial space */
241         buffer_ptr = buffer + 1;
242
243         return (cf_dispatch (plugin, ci->key, buffer_ptr));
244 } /* int plugin_conf_dispatch */
245
246 static int dispatch_value (const oconfig_item_t *ci)
247 {
248         int ret = -2;
249         int i;
250
251         for (i = 0; i < cf_value_map_num; i++)
252                 if (strcasecmp (cf_value_map[i].key, ci->key) == 0)
253                 {
254                         ret = cf_value_map[i].func (ci);
255                         break;
256                 }
257
258         for (i = 0; i < cf_global_options_num; i++)
259                 if (strcasecmp (cf_global_options[i].key, ci->key) == 0)
260                 {
261                         ret = dispatch_global_option (ci);
262                         break;
263                 }
264
265         return (ret);
266 } /* int dispatch_value */
267
268 static int dispatch_block_plugin (oconfig_item_t *ci)
269 {
270         int i;
271         char *name;
272
273         if (strcasecmp (ci->key, "Plugin") != 0)
274                 return (-1);
275         if (ci->values_num != 1)
276                 return (-1);
277         if (ci->values[0].type != OCONFIG_TYPE_STRING)
278                 return (-1);
279
280         name = ci->values[0].value.string;
281
282         for (i = 0; i < ci->children_num; i++)
283         {
284                 if (ci->children[i].children == NULL)
285                         dispatch_value_plugin (name, ci->children + i);
286                 else
287                         {DEBUG ("No nested config blocks allow for plugins. Yet.");}
288         }
289
290         return (0);
291 }
292
293
294 static int dispatch_block (oconfig_item_t *ci)
295 {
296         if (strcasecmp (ci->key, "Plugin") == 0)
297                 return (dispatch_block_plugin (ci));
298
299         return (0);
300 }
301
302 /* 
303  * Public functions
304  */
305 int global_option_set (const char *option, const char *value)
306 {
307         int i;
308
309         DEBUG ("option = %s; value = %s;", option, value);
310
311         for (i = 0; i < cf_global_options_num; i++)
312                 if (strcasecmp (cf_global_options[i].key, option) == 0)
313                         break;
314
315         if (i >= cf_global_options_num)
316                 return (-1);
317
318         sfree (cf_global_options[i].value);
319
320         if (value != NULL)
321                 cf_global_options[i].value = strdup (value);
322         else
323                 cf_global_options[i].value = NULL;
324
325         return (0);
326 }
327
328 const char *global_option_get (const char *option)
329 {
330         int i;
331
332         for (i = 0; i < cf_global_options_num; i++)
333                 if (strcasecmp (cf_global_options[i].key, option) == 0)
334                         break;
335
336         if (i >= cf_global_options_num)
337                 return (NULL);
338         
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 */
343
344 void cf_unregister (const char *type)
345 {
346         cf_callback_t *this, *prev;
347
348         for (prev = NULL, this = first_callback;
349                         this != NULL;
350                         prev = this, this = this->next)
351                 if (strcasecmp (this->type, type) == 0)
352                 {
353                         if (prev == NULL)
354                                 first_callback = this->next;
355                         else
356                                 prev->next = this->next;
357
358                         free (this);
359                         break;
360                 }
361 } /* void cf_unregister */
362
363 void cf_register (const char *type,
364                 int (*callback) (const char *, const char *),
365                 const char **keys, int keys_num)
366 {
367         cf_callback_t *cf_cb;
368
369         /* Remove this module from the list, if it already exists */
370         cf_unregister (type);
371
372         /* This pointer will be free'd in `cf_unregister' */
373         if ((cf_cb = (cf_callback_t *) malloc (sizeof (cf_callback_t))) == NULL)
374                 return;
375
376         cf_cb->type     = type;
377         cf_cb->callback = callback;
378         cf_cb->keys     = keys;
379         cf_cb->keys_num = keys_num;
380
381         cf_cb->next = first_callback;
382         first_callback = cf_cb;
383 } /* void cf_register */
384
385 int cf_read (char *filename)
386 {
387         oconfig_item_t *conf;
388         int i;
389
390         conf = oconfig_parse_file (filename);
391         if (conf == NULL)
392         {
393                 ERROR ("Unable to read config file %s.", filename);
394                 return (-1);
395         }
396
397         for (i = 0; i < conf->children_num; i++)
398         {
399                 if (conf->children[i].children == NULL)
400                         dispatch_value (conf->children + i);
401                 else
402                         dispatch_block (conf->children + i);
403         }
404
405         return (0);
406 } /* int cf_read */