Code

Replace all syslog-calls with one of the new logging-macros.
[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         {"LogFile",   NULL, LOGFILE},
81         {"PIDFile",   NULL, PIDFILE},
82         {"Hostname",  NULL, NULL},
83         {"Interval",  NULL, "10"}
84 };
85 static int cf_global_options_num = STATIC_ARRAY_LEN (cf_global_options);
87 /*
88  * Functions to handle register/unregister, search, and other plugin related
89  * stuff
90  */
91 static cf_callback_t *cf_search (const char *type)
92 {
93         cf_callback_t *cf_cb;
95         if (type == NULL)
96                 return (NULL);
98         for (cf_cb = first_callback; cf_cb != NULL; cf_cb = cf_cb->next)
99                 if (strcasecmp (cf_cb->type, type) == 0)
100                         break;
102         return (cf_cb);
105 static int cf_dispatch (const char *type, const char *orig_key,
106                 const char *orig_value)
108         cf_callback_t *cf_cb;
109         char *key;
110         char *value;
111         int ret;
112         int i;
114         DEBUG ("type = %s, key = %s, value = %s",
115                         ESCAPE_NULL(type),
116                         ESCAPE_NULL(orig_key),
117                         ESCAPE_NULL(orig_value));
119         if ((cf_cb = cf_search (type)) == NULL)
120         {
121                 WARNING ("Plugin `%s' did not register a callback.", type);
122                 return (-1);
123         }
125         if ((key = strdup (orig_key)) == NULL)
126                 return (1);
127         if ((value = strdup (orig_value)) == NULL)
128         {
129                 free (key);
130                 return (2);
131         }
133         ret = -1;
135         for (i = 0; i < cf_cb->keys_num; i++)
136         {
137                 if (strcasecmp (cf_cb->keys[i], key) == 0)
138                 {
139                         ret = (*cf_cb->callback) (key, value);
140                         break;
141                 }
142         }
144         if (i >= cf_cb->keys_num)
145                 WARNING ("Plugin `%s' did not register for value `%s'.", type, key);
147         free (key);
148         free (value);
150         DEBUG ("return (%i)", ret);
152         return (ret);
153 } /* int cf_dispatch */
155 static int dispatch_global_option (const oconfig_item_t *ci)
157         if (ci->values_num != 1)
158                 return (-1);
159         if (ci->values[0].type != OCONFIG_TYPE_STRING)
160                 return (-1);
162         return (global_option_set (ci->key, ci->values[0].value.string));
165 static int dispatch_value_plugindir (const oconfig_item_t *ci)
167         assert (strcasecmp (ci->key, "PluginDir") == 0);
168         
169         if (ci->values_num != 1)
170                 return (-1);
171         if (ci->values[0].type != OCONFIG_TYPE_STRING)
172                 return (-1);
174         plugin_set_dir (ci->values[0].value.string);
175         return (0);
178 static int dispatch_value_loadplugin (const oconfig_item_t *ci)
180         assert (strcasecmp (ci->key, "LoadPlugin") == 0);
182         if (ci->values_num != 1)
183                 return (-1);
184         if (ci->values[0].type != OCONFIG_TYPE_STRING)
185                 return (-1);
187         return (plugin_load (ci->values[0].value.string));
188 } /* int dispatch_value_loadplugin */
190 static int dispatch_value_plugin (const char *plugin, oconfig_item_t *ci)
192         char  buffer[4096];
193         char *buffer_ptr;
194         int   buffer_free;
195         int i;
197         buffer_ptr = buffer;
198         buffer_free = sizeof (buffer);
200         for (i = 0; i < ci->values_num; i++)
201         {
202                 int status = -1;
204                 if (ci->values[i].type == OCONFIG_TYPE_STRING)
205                         status = snprintf (buffer_ptr, buffer_free, " %s",
206                                         ci->values[i].value.string);
207                 else if (ci->values[i].type == OCONFIG_TYPE_NUMBER)
208                         status = snprintf (buffer_ptr, buffer_free, " %lf",
209                                         ci->values[i].value.number);
210                 else if (ci->values[i].type == OCONFIG_TYPE_BOOLEAN)
211                         status = snprintf (buffer_ptr, buffer_free, " %s",
212                                         ci->values[i].value.boolean
213                                         ? "true" : "false");
215                 if ((status < 0) || (status >= buffer_free))
216                         return (-1);
217                 buffer_free -= status;
218                 buffer_ptr  += status;
219         }
220         /* skip the initial space */
221         buffer_ptr = buffer + 1;
223         return (cf_dispatch (plugin, ci->key, buffer_ptr));
224 } /* int plugin_conf_dispatch */
226 static int dispatch_value (const oconfig_item_t *ci)
228         int ret = -2;
229         int i;
231         for (i = 0; i < cf_value_map_num; i++)
232                 if (strcasecmp (cf_value_map[i].key, ci->key) == 0)
233                 {
234                         ret = cf_value_map[i].func (ci);
235                         break;
236                 }
238         for (i = 0; i < cf_global_options_num; i++)
239                 if (strcasecmp (cf_global_options[i].key, ci->key) == 0)
240                 {
241                         ret = dispatch_global_option (ci);
242                         break;
243                 }
245         return (ret);
246 } /* int dispatch_value */
248 static int dispatch_block_plugin (oconfig_item_t *ci)
250         int i;
251         char *name;
253         if (strcasecmp (ci->key, "Plugin") != 0)
254                 return (-1);
255         if (ci->values_num != 1)
256                 return (-1);
257         if (ci->values[0].type != OCONFIG_TYPE_STRING)
258                 return (-1);
260         name = ci->values[0].value.string;
262         for (i = 0; i < ci->children_num; i++)
263         {
264                 if (ci->children[i].children == NULL)
265                         dispatch_value_plugin (name, ci->children + i);
266                 else
267                         {DEBUG ("No nested config blocks allow for plugins. Yet.");}
268         }
270         return (0);
274 static int dispatch_block (oconfig_item_t *ci)
276         if (strcasecmp (ci->key, "Plugin") == 0)
277                 return (dispatch_block_plugin (ci));
279         return (0);
282 /* 
283  * Public functions
284  */
285 int global_option_set (const char *option, const char *value)
287         int i;
289         DEBUG ("option = %s; value = %s;", option, value);
291         for (i = 0; i < cf_global_options_num; i++)
292                 if (strcasecmp (cf_global_options[i].key, option) == 0)
293                         break;
295         if (i >= cf_global_options_num)
296                 return (-1);
298         if (cf_global_options[i].value != NULL)
299                 free (cf_global_options[i].value);
301         if (value != NULL)
302                 cf_global_options[i].value = strdup (value);
303         else
304                 cf_global_options[i].value = NULL;
306         return (0);
309 const char *global_option_get (const char *option)
311         int i;
313         for (i = 0; i < cf_global_options_num; i++)
314                 if (strcasecmp (cf_global_options[i].key, option) == 0)
315                         break;
317         if (i >= cf_global_options_num)
318                 return (NULL);
319         
320         return ((cf_global_options[i].value != NULL)
321                         ? cf_global_options[i].value
322                         : cf_global_options[i].def);
323 } /* char *global_option_get */
325 void cf_unregister (const char *type)
327         cf_callback_t *this, *prev;
329         for (prev = NULL, this = first_callback;
330                         this != NULL;
331                         prev = this, this = this->next)
332                 if (strcasecmp (this->type, type) == 0)
333                 {
334                         if (prev == NULL)
335                                 first_callback = this->next;
336                         else
337                                 prev->next = this->next;
339                         free (this);
340                         break;
341                 }
344 void cf_register (const char *type,
345                 int (*callback) (const char *, const char *),
346                 const char **keys, int keys_num)
348         cf_callback_t *cf_cb;
350         /* Remove this module from the list, if it already exists */
351         cf_unregister (type);
353         /* This pointer will be free'd in `cf_unregister' */
354         if ((cf_cb = (cf_callback_t *) malloc (sizeof (cf_callback_t))) == NULL)
355                 return;
357         cf_cb->type     = type;
358         cf_cb->callback = callback;
359         cf_cb->keys     = keys;
360         cf_cb->keys_num = keys_num;
362         cf_cb->next = first_callback;
363         first_callback = cf_cb;
364 } /* void cf_register */
366 int cf_read (char *filename)
368         oconfig_item_t *conf;
369         int i;
371         conf = oconfig_parse_file (filename);
372         if (conf == NULL)
373         {
374                 ERROR ("Unable to read config file %s.", filename);
375                 return (-1);
376         }
378         for (i = 0; i < conf->children_num; i++)
379         {
380                 if (conf->children[i].children == NULL)
381                         dispatch_value (conf->children + i);
382                 else
383                         dispatch_block (conf->children + i);
384         }
386         return (0);
387 } /* int cf_read */