src/configfile.c: Actually set the `BaseDir' option.
[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_plugindir (const oconfig_item_t *ci);
64 static int dispatch_value_loadplugin (const oconfig_item_t *ci);
65
66 /*
67  * Private variables
68  */
69 static cf_callback_t *first_callback = NULL;
70
71 static cf_value_map_t cf_value_map[] =
72 {
73         {"PluginDir",  dispatch_value_plugindir},
74         {"LoadPlugin", dispatch_value_loadplugin}
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         {"LogFile", NULL, LOGFILE},
82         {"PIDFile", NULL, PIDFILE}
83 };
84 static int cf_global_options_num = STATIC_ARRAY_LEN (cf_global_options);
85
86 /*
87  * Functions to handle register/unregister, search, and other plugin related
88  * stuff
89  */
90 static cf_callback_t *cf_search (const char *type)
91 {
92         cf_callback_t *cf_cb;
93
94         if (type == NULL)
95                 return (NULL);
96
97         for (cf_cb = first_callback; cf_cb != NULL; cf_cb = cf_cb->next)
98                 if (strcasecmp (cf_cb->type, type) == 0)
99                         break;
100
101         return (cf_cb);
102 }
103
104 static int cf_dispatch (const char *type, const char *orig_key,
105                 const char *orig_value)
106 {
107         cf_callback_t *cf_cb;
108         char *key;
109         char *value;
110         int ret;
111         int i;
112
113         DBG ("type = %s, key = %s, value = %s",
114                         ESCAPE_NULL(type),
115                         ESCAPE_NULL(orig_key),
116                         ESCAPE_NULL(orig_value));
117
118         if ((cf_cb = cf_search (type)) == NULL)
119         {
120                 syslog (LOG_WARNING, "Plugin `%s' did not register a callback.", type);
121                 return (-1);
122         }
123
124         if ((key = strdup (orig_key)) == NULL)
125                 return (1);
126         if ((value = strdup (orig_value)) == NULL)
127         {
128                 free (key);
129                 return (2);
130         }
131
132         ret = -1;
133
134         for (i = 0; i < cf_cb->keys_num; i++)
135         {
136                 if (strcasecmp (cf_cb->keys[i], key) == 0)
137                 {
138                         ret = (*cf_cb->callback) (key, value);
139                         break;
140                 }
141         }
142
143         if (i >= cf_cb->keys_num)
144                 syslog (LOG_WARNING, "Plugin `%s' did not register for value `%s'.", type, key);
145
146         free (key);
147         free (value);
148
149         DBG ("return (%i)", ret);
150
151         return (ret);
152 } /* int cf_dispatch */
153
154 static int dispatch_global_option (const oconfig_item_t *ci)
155 {
156         if (ci->values_num != 1)
157                 return (-1);
158         if (ci->values[0].type != OCONFIG_TYPE_STRING)
159                 return (-1);
160
161         return (global_option_set (ci->key, ci->values[0].value.string));
162 }
163
164 static int dispatch_value_plugindir (const oconfig_item_t *ci)
165 {
166         assert (strcasecmp (ci->key, "PluginDir") == 0);
167         
168         if (ci->values_num != 1)
169                 return (-1);
170         if (ci->values[0].type != OCONFIG_TYPE_STRING)
171                 return (-1);
172
173         plugin_set_dir (ci->values[0].value.string);
174         return (0);
175 }
176
177 static int dispatch_value_loadplugin (const oconfig_item_t *ci)
178 {
179         assert (strcasecmp (ci->key, "LoadPlugin") == 0);
180
181         if (ci->values_num != 1)
182                 return (-1);
183         if (ci->values[0].type != OCONFIG_TYPE_STRING)
184                 return (-1);
185
186         return (plugin_load (ci->values[0].value.string));
187 } /* int dispatch_value_loadplugin */
188
189 static int dispatch_value_plugin (const char *plugin, oconfig_item_t *ci)
190 {
191         char  buffer[4096];
192         char *buffer_ptr;
193         int   buffer_free;
194         int i;
195
196         buffer_ptr = buffer;
197         buffer_free = sizeof (buffer);
198
199         for (i = 0; i < ci->values_num; i++)
200         {
201                 int status = -1;
202
203                 if (ci->values[i].type == OCONFIG_TYPE_STRING)
204                         status = snprintf (buffer_ptr, buffer_free, " %s",
205                                         ci->values[i].value.string);
206                 else if (ci->values[i].type == OCONFIG_TYPE_NUMBER)
207                         status = snprintf (buffer_ptr, buffer_free, " %lf",
208                                         ci->values[i].value.number);
209                 else if (ci->values[i].type == OCONFIG_TYPE_BOOLEAN)
210                         status = snprintf (buffer_ptr, buffer_free, " %s",
211                                         ci->values[i].value.boolean
212                                         ? "true" : "false");
213
214                 if ((status < 0) || (status >= buffer_free))
215                         return (-1);
216                 buffer_free -= status;
217                 buffer_ptr  += status;
218         }
219         /* skip the initial space */
220         buffer_ptr = buffer + 1;
221
222         return (cf_dispatch (plugin, ci->key, buffer_ptr));
223 } /* int plugin_conf_dispatch */
224
225 static int dispatch_value (const oconfig_item_t *ci)
226 {
227         int ret = -2;
228         int i;
229
230         for (i = 0; i < cf_value_map_num; i++)
231                 if (strcasecmp (cf_value_map[i].key, ci->key) == 0)
232                 {
233                         ret = cf_value_map[i].func (ci);
234                         break;
235                 }
236
237         for (i = 0; i < cf_global_options_num; i++)
238                 if (strcasecmp (cf_global_options[i].key, ci->key) == 0)
239                 {
240                         ret = dispatch_global_option (ci);
241                         break;
242                 }
243
244         return (ret);
245 } /* int dispatch_value */
246
247 static int dispatch_block_plugin (oconfig_item_t *ci)
248 {
249         int i;
250         char *name;
251
252         if (strcasecmp (ci->key, "Plugin") != 0)
253                 return (-1);
254         if (ci->values_num != 1)
255                 return (-1);
256         if (ci->values[0].type != OCONFIG_TYPE_STRING)
257                 return (-1);
258
259         name = ci->values[0].value.string;
260
261         for (i = 0; i < ci->children_num; i++)
262         {
263                 if (ci->children[i].children == NULL)
264                         dispatch_value_plugin (name, ci->children + i);
265                 else
266                         {DBG ("No nested config blocks allow for plugins. Yet.");}
267         }
268
269         return (0);
270 }
271
272
273 static int dispatch_block (oconfig_item_t *ci)
274 {
275         if (strcasecmp (ci->key, "Plugin") == 0)
276                 return (dispatch_block_plugin (ci));
277
278         return (0);
279 }
280
281 /* 
282  * Public functions
283  */
284 int global_option_set (const char *option, const char *value)
285 {
286         int i;
287
288         for (i = 0; i < cf_global_options_num; i++)
289                 if (strcasecmp (cf_global_options[i].key, option) == 0)
290                         break;
291
292         if (i >= cf_global_options_num)
293                 return (-1);
294
295         if (cf_global_options[i].value != NULL)
296                 free (cf_global_options[i].value);
297
298         if (value != NULL)
299                 cf_global_options[i].value = strdup (value);
300         else
301                 cf_global_options[i].value = NULL;
302
303         return (0);
304 }
305
306 const char *global_option_get (const char *option)
307 {
308         int i;
309
310         for (i = 0; i < cf_global_options_num; i++)
311                 if (strcasecmp (cf_global_options[i].key, option) == 0)
312                         break;
313
314         if (i >= cf_global_options_num)
315                 return (NULL);
316         
317         return ((cf_global_options[i].value != NULL)
318                         ? cf_global_options[i].value
319                         : cf_global_options[i].def);
320 } /* char *global_option_get */
321
322 void cf_unregister (const char *type)
323 {
324         cf_callback_t *this, *prev;
325
326         for (prev = NULL, this = first_callback;
327                         this != NULL;
328                         prev = this, this = this->next)
329                 if (strcasecmp (this->type, type) == 0)
330                 {
331                         if (prev == NULL)
332                                 first_callback = this->next;
333                         else
334                                 prev->next = this->next;
335
336                         free (this);
337                         break;
338                 }
339 }
340
341 void cf_register (const char *type,
342                 int (*callback) (const char *, const char *),
343                 const char **keys, int keys_num)
344 {
345         cf_callback_t *cf_cb;
346
347         /* Remove this module from the list, if it already exists */
348         cf_unregister (type);
349
350         /* This pointer will be free'd in `cf_unregister' */
351         if ((cf_cb = (cf_callback_t *) malloc (sizeof (cf_callback_t))) == NULL)
352                 return;
353
354         cf_cb->type     = type;
355         cf_cb->callback = callback;
356         cf_cb->keys     = keys;
357         cf_cb->keys_num = keys_num;
358
359         cf_cb->next = first_callback;
360         first_callback = cf_cb;
361 } /* void cf_register */
362
363 int cf_read (char *filename)
364 {
365         oconfig_item_t *conf;
366         int i;
367
368         conf = oconfig_parse_file (filename);
369         if (conf == NULL)
370         {
371                 syslog (LOG_ERR, "Unable to read config file %s.", filename);
372                 return (-1);
373         }
374
375         for (i = 0; i < conf->children_num; i++)
376         {
377                 if (conf->children[i].children == NULL)
378                         dispatch_value (conf->children + i);
379                 else
380                         dispatch_block (conf->children + i);
381         }
382
383         return (0);
384 } /* int cf_read */