Merge branch 'ff/avl-tree'
[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 #include "network.h"
31 #include "utils_debug.h"
32
33 #define ESCAPE_NULL(str) ((str) == NULL ? "(null)" : (str))
34
35 /*
36  * Private types
37  */
38 typedef struct cf_callback
39 {
40         const char  *type;
41         int  (*callback) (const char *, const char *);
42         const char **keys;
43         int    keys_num;
44         struct cf_callback *next;
45 } cf_callback_t;
46
47 typedef struct cf_value_map_s
48 {
49         char *key;
50         int (*func) (const oconfig_item_t *);
51 } cf_value_map_t;
52
53 typedef struct cf_global_option_s
54 {
55         char *key;
56         char *value;
57         char *def;
58 } cf_global_option_t;
59
60 /*
61  * Prototypes of callback functions
62  */
63 static int dispatch_value_pidfile (const oconfig_item_t *ci);
64 static int dispatch_value_plugindir (const oconfig_item_t *ci);
65 static int dispatch_value_loadplugin (const oconfig_item_t *ci);
66
67 /*
68  * Private variables
69  */
70 static cf_callback_t *first_callback = NULL;
71
72 static cf_value_map_t cf_value_map[] =
73 {
74         {"PIDFile",    dispatch_value_pidfile},
75         {"PluginDir",  dispatch_value_plugindir},
76         {"LoadPlugin", dispatch_value_loadplugin}
77 };
78 static int cf_value_map_num = STATIC_ARRAY_LEN (cf_value_map);
79
80 static cf_global_option_t cf_global_options[] =
81 {
82         {"BaseDir", NULL, PKGLOCALSTATEDIR},
83         {"LogFile", NULL, LOGFILE},
84         {"PIDFile", NULL, PIDFILE}
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         DBG ("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                 syslog (LOG_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                 syslog (LOG_WARNING, "Plugin `%s' did not register for value `%s'.", type, key);
147
148         free (key);
149         free (value);
150
151         DBG ("return (%i)", ret);
152
153         return (ret);
154 }
155
156 static int dispatch_value_pidfile (const oconfig_item_t *ci)
157 {
158         assert (strcasecmp (ci->key, "PIDFile") == 0);
159         
160         if (ci->values_num != 1)
161                 return (-1);
162         if (ci->values[0].type != OCONFIG_TYPE_STRING)
163                 return (-1);
164
165         return (global_option_set ("PIDFile", ci->values[0].value.string));
166 }
167
168 static int dispatch_value_plugindir (const oconfig_item_t *ci)
169 {
170         assert (strcasecmp (ci->key, "PluginDir") == 0);
171         
172         if (ci->values_num != 1)
173                 return (-1);
174         if (ci->values[0].type != OCONFIG_TYPE_STRING)
175                 return (-1);
176
177         plugin_set_dir (ci->values[0].value.string);
178         return (0);
179 }
180
181 static int dispatch_value_loadplugin (const oconfig_item_t *ci)
182 {
183         assert (strcasecmp (ci->key, "LoadPlugin") == 0);
184
185         if (ci->values_num != 1)
186                 return (-1);
187         if (ci->values[0].type != OCONFIG_TYPE_STRING)
188                 return (-1);
189
190         return (plugin_load (ci->values[0].value.string));
191 } /* int dispatch_value_loadplugin */
192
193 static int dispatch_value_plugin (const char *plugin, oconfig_item_t *ci)
194 {
195         char  buffer[4096];
196         char *buffer_ptr;
197         int   buffer_free;
198         int i;
199
200         buffer_ptr = buffer;
201         buffer_free = sizeof (buffer);
202
203         for (i = 0; i < ci->values_num; i++)
204         {
205                 int status = -1;
206
207                 if (ci->values[i].type == OCONFIG_TYPE_STRING)
208                         status = snprintf (buffer_ptr, buffer_free, " %s",
209                                         ci->values[i].value.string);
210                 else if (ci->values[i].type == OCONFIG_TYPE_NUMBER)
211                         status = snprintf (buffer_ptr, buffer_free, " %lf",
212                                         ci->values[i].value.number);
213                 else if (ci->values[i].type == OCONFIG_TYPE_BOOLEAN)
214                         status = snprintf (buffer_ptr, buffer_free, " %s",
215                                         ci->values[i].value.boolean
216                                         ? "true" : "false");
217
218                 if ((status < 0) || (status >= buffer_free))
219                         return (-1);
220                 buffer_free -= status;
221                 buffer_ptr  += status;
222         }
223         /* skip the initial space */
224         buffer_ptr = buffer + 1;
225
226         return (cf_dispatch (plugin, ci->key, buffer_ptr));
227 } /* int plugin_conf_dispatch */
228
229 static int dispatch_value (const oconfig_item_t *ci)
230 {
231         int ret = -2;
232         int i;
233
234         for (i = 0; i < cf_value_map_num; i++)
235                 if (strcasecmp (cf_value_map[i].key, ci->key) == 0)
236                 {
237                         ret = cf_value_map[i].func (ci);
238                         break;
239                 }
240
241         return (ret);
242 } /* int dispatch_value */
243
244 static int dispatch_block_plugin (oconfig_item_t *ci)
245 {
246         int i;
247         char *name;
248
249         if (strcasecmp (ci->key, "Plugin") != 0)
250                 return (-1);
251         if (ci->values_num != 1)
252                 return (-1);
253         if (ci->values[0].type != OCONFIG_TYPE_STRING)
254                 return (-1);
255
256         name = ci->values[0].value.string;
257
258         for (i = 0; i < ci->children_num; i++)
259         {
260                 if (ci->children[i].children == NULL)
261                         dispatch_value_plugin (name, ci->children + i);
262                 else
263                         {DBG ("No nested config blocks allow for plugins. Yet.");}
264         }
265
266         return (0);
267 }
268
269
270 static int dispatch_block (oconfig_item_t *ci)
271 {
272         if (strcasecmp (ci->key, "Plugin") == 0)
273                 return (dispatch_block_plugin (ci));
274
275         return (0);
276 }
277
278 /* 
279  * Public functions
280  */
281 int global_option_set (const char *option, const char *value)
282 {
283         int i;
284
285         for (i = 0; i < cf_global_options_num; i++)
286                 if (strcasecmp (cf_global_options[i].key, option) == 0)
287                         break;
288
289         if (i >= cf_global_options_num)
290                 return (-1);
291
292         if (cf_global_options[i].value != NULL)
293                 free (cf_global_options[i].value);
294
295         if (value != NULL)
296                 cf_global_options[i].value = strdup (value);
297         else
298                 cf_global_options[i].value = NULL;
299
300         return (0);
301 }
302
303 const char *global_option_get (const char *option)
304 {
305         int i;
306
307         for (i = 0; i < cf_global_options_num; i++)
308                 if (strcasecmp (cf_global_options[i].key, option) == 0)
309                         break;
310
311         if (i >= cf_global_options_num)
312                 return (NULL);
313         
314         return ((cf_global_options[i].value != NULL)
315                         ? cf_global_options[i].value
316                         : cf_global_options[i].def);
317 } /* char *global_option_get */
318
319 void cf_unregister (const char *type)
320 {
321         cf_callback_t *this, *prev;
322
323         for (prev = NULL, this = first_callback;
324                         this != NULL;
325                         prev = this, this = this->next)
326                 if (strcasecmp (this->type, type) == 0)
327                 {
328                         if (prev == NULL)
329                                 first_callback = this->next;
330                         else
331                                 prev->next = this->next;
332
333                         free (this);
334                         break;
335                 }
336 }
337
338 void cf_register (const char *type,
339                 int (*callback) (const char *, const char *),
340                 const char **keys, int keys_num)
341 {
342         cf_callback_t *cf_cb;
343
344         /* Remove this module from the list, if it already exists */
345         cf_unregister (type);
346
347         /* This pointer will be free'd in `cf_unregister' */
348         if ((cf_cb = (cf_callback_t *) malloc (sizeof (cf_callback_t))) == NULL)
349                 return;
350
351         cf_cb->type     = type;
352         cf_cb->callback = callback;
353         cf_cb->keys     = keys;
354         cf_cb->keys_num = keys_num;
355
356         cf_cb->next = first_callback;
357         first_callback = cf_cb;
358 } /* void cf_register */
359
360 int cf_read (char *filename)
361 {
362         oconfig_item_t *conf;
363         int i;
364
365         conf = oconfig_parse_file (filename);
366         if (conf == NULL)
367         {
368                 syslog (LOG_ERR, "Unable to read config file %s.", filename);
369                 return (-1);
370         }
371
372         for (i = 0; i < conf->children_num; i++)
373         {
374                 if (conf->children[i].children == NULL)
375                         dispatch_value (conf->children + i);
376                 else
377                         dispatch_block (conf->children + i);
378         }
379
380         return (0);
381 } /* int cf_read */