Code

plugin.[ch]: Implemented `plugin_unregister_config'.
[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  **/
23 #include "collectd.h"
25 #include "liboconfig/oconfig.h"
27 #include "common.h"
28 #include "plugin.h"
29 #include "configfile.h"
30 #include "network.h"
32 #define ESCAPE_NULL(str) ((str) == NULL ? "(null)" : (str))
34 /*
35  * Private types
36  */
37 typedef struct cf_callback
38 {
39         const char  *type;
40         int  (*callback) (const char *, const char *);
41         const char **keys;
42         int    keys_num;
43         struct cf_callback *next;
44 } cf_callback_t;
46 typedef struct cf_value_map_s
47 {
48         char *key;
49         int (*func) (const oconfig_item_t *);
50 } cf_value_map_t;
52 typedef struct cf_global_option_s
53 {
54         char *key;
55         char *value;
56         char *def;
57 } cf_global_option_t;
59 /*
60  * Prototypes of callback functions
61  */
62 static int dispatch_value_plugindir (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 };
75 static int cf_value_map_num = STATIC_ARRAY_LEN (cf_value_map);
77 static cf_global_option_t cf_global_options[] =
78 {
79         {"BaseDir",   NULL, PKGLOCALSTATEDIR},
80         {"PIDFile",   NULL, PIDFILE},
81         {"Hostname",  NULL, NULL},
82         {"Interval",  NULL, "10"}
83 };
84 static int cf_global_options_num = STATIC_ARRAY_LEN (cf_global_options);
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;
94         if (type == NULL)
95                 return (NULL);
97         for (cf_cb = first_callback; cf_cb != NULL; cf_cb = cf_cb->next)
98                 if (strcasecmp (cf_cb->type, type) == 0)
99                         break;
101         return (cf_cb);
104 static int cf_dispatch (const char *type, const char *orig_key,
105                 const char *orig_value)
107         cf_callback_t *cf_cb;
108         char *key;
109         char *value;
110         int ret;
111         int i;
113         DEBUG ("type = %s, key = %s, value = %s",
114                         ESCAPE_NULL(type),
115                         ESCAPE_NULL(orig_key),
116                         ESCAPE_NULL(orig_value));
118         if ((cf_cb = cf_search (type)) == NULL)
119         {
120                 WARNING ("Plugin `%s' did not register a callback.", type);
121                 return (-1);
122         }
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         }
132         ret = -1;
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         }
143         if (i >= cf_cb->keys_num)
144                 WARNING ("Plugin `%s' did not register for value `%s'.", type, key);
146         free (key);
147         free (value);
149         DEBUG ("return (%i)", ret);
151         return (ret);
152 } /* int cf_dispatch */
154 static int dispatch_global_option (const oconfig_item_t *ci)
156         if (ci->values_num != 1)
157                 return (-1);
158         if (ci->values[0].type != OCONFIG_TYPE_STRING)
159                 return (-1);
161         return (global_option_set (ci->key, ci->values[0].value.string));
164 static int dispatch_value_plugindir (const oconfig_item_t *ci)
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);
173         plugin_set_dir (ci->values[0].value.string);
174         return (0);
177 static int dispatch_value_loadplugin (const oconfig_item_t *ci)
179         assert (strcasecmp (ci->key, "LoadPlugin") == 0);
181         if (ci->values_num != 1)
182                 return (-1);
183         if (ci->values[0].type != OCONFIG_TYPE_STRING)
184                 return (-1);
186         return (plugin_load (ci->values[0].value.string));
187 } /* int dispatch_value_loadplugin */
189 static int dispatch_value_plugin (const char *plugin, oconfig_item_t *ci)
191         char  buffer[4096];
192         char *buffer_ptr;
193         int   buffer_free;
194         int i;
196         buffer_ptr = buffer;
197         buffer_free = sizeof (buffer);
199         for (i = 0; i < ci->values_num; i++)
200         {
201                 int status = -1;
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");
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;
222         return (cf_dispatch (plugin, ci->key, buffer_ptr));
223 } /* int plugin_conf_dispatch */
225 static int dispatch_value (const oconfig_item_t *ci)
227         int ret = -2;
228         int i;
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                 }
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                 }
244         return (ret);
245 } /* int dispatch_value */
247 static int dispatch_block_plugin (oconfig_item_t *ci)
249         int i;
250         char *name;
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);
259         name = ci->values[0].value.string;
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                         {DEBUG ("No nested config blocks allow for plugins. Yet.");}
267         }
269         return (0);
273 static int dispatch_block (oconfig_item_t *ci)
275         if (strcasecmp (ci->key, "Plugin") == 0)
276                 return (dispatch_block_plugin (ci));
278         return (0);
281 /* 
282  * Public functions
283  */
284 int global_option_set (const char *option, const char *value)
286         int i;
288         DEBUG ("option = %s; value = %s;", option, value);
290         for (i = 0; i < cf_global_options_num; i++)
291                 if (strcasecmp (cf_global_options[i].key, option) == 0)
292                         break;
294         if (i >= cf_global_options_num)
295                 return (-1);
297         if (cf_global_options[i].value != NULL)
298                 free (cf_global_options[i].value);
300         if (value != NULL)
301                 cf_global_options[i].value = strdup (value);
302         else
303                 cf_global_options[i].value = NULL;
305         return (0);
308 const char *global_option_get (const char *option)
310         int i;
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 (NULL);
318         
319         return ((cf_global_options[i].value != NULL)
320                         ? cf_global_options[i].value
321                         : cf_global_options[i].def);
322 } /* char *global_option_get */
324 void cf_unregister (const char *type)
326         cf_callback_t *this, *prev;
328         for (prev = NULL, this = first_callback;
329                         this != NULL;
330                         prev = this, this = this->next)
331                 if (strcasecmp (this->type, type) == 0)
332                 {
333                         if (prev == NULL)
334                                 first_callback = this->next;
335                         else
336                                 prev->next = this->next;
338                         free (this);
339                         break;
340                 }
341 } /* void cf_unregister */
343 void cf_register (const char *type,
344                 int (*callback) (const char *, const char *),
345                 const char **keys, int keys_num)
347         cf_callback_t *cf_cb;
349         /* Remove this module from the list, if it already exists */
350         cf_unregister (type);
352         /* This pointer will be free'd in `cf_unregister' */
353         if ((cf_cb = (cf_callback_t *) malloc (sizeof (cf_callback_t))) == NULL)
354                 return;
356         cf_cb->type     = type;
357         cf_cb->callback = callback;
358         cf_cb->keys     = keys;
359         cf_cb->keys_num = keys_num;
361         cf_cb->next = first_callback;
362         first_callback = cf_cb;
363 } /* void cf_register */
365 int cf_read (char *filename)
367         oconfig_item_t *conf;
368         int i;
370         conf = oconfig_parse_file (filename);
371         if (conf == NULL)
372         {
373                 ERROR ("Unable to read config file %s.", filename);
374                 return (-1);
375         }
377         for (i = 0; i < conf->children_num; i++)
378         {
379                 if (conf->children[i].children == NULL)
380                         dispatch_value (conf->children + i);
381                 else
382                         dispatch_block (conf->children + i);
383         }
385         return (0);
386 } /* int cf_read */