Code

Introduce two global variables: `hostname_g' and `interval_g'.
[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"
31 #include "utils_debug.h"
33 #define ESCAPE_NULL(str) ((str) == NULL ? "(null)" : (str))
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;
47 typedef struct cf_value_map_s
48 {
49         char *key;
50         int (*func) (const oconfig_item_t *);
51 } cf_value_map_t;
53 typedef struct cf_global_option_s
54 {
55         char *key;
56         char *value;
57         char *def;
58 } cf_global_option_t;
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);
66 /*
67  * Private variables
68  */
69 static cf_callback_t *first_callback = NULL;
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);
78 static cf_global_option_t cf_global_options[] =
79 {
80         {"BaseDir",   NULL, PKGLOCALSTATEDIR},
81         {"LogFile",   NULL, LOGFILE},
82         {"PIDFile",   NULL, PIDFILE},
83         {"Hostname",  NULL, NULL},
84         {"Interval",  NULL, "10"}
85 };
86 static int cf_global_options_num = STATIC_ARRAY_LEN (cf_global_options);
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;
96         if (type == NULL)
97                 return (NULL);
99         for (cf_cb = first_callback; cf_cb != NULL; cf_cb = cf_cb->next)
100                 if (strcasecmp (cf_cb->type, type) == 0)
101                         break;
103         return (cf_cb);
106 static int cf_dispatch (const char *type, const char *orig_key,
107                 const char *orig_value)
109         cf_callback_t *cf_cb;
110         char *key;
111         char *value;
112         int ret;
113         int i;
115         DBG ("type = %s, key = %s, value = %s",
116                         ESCAPE_NULL(type),
117                         ESCAPE_NULL(orig_key),
118                         ESCAPE_NULL(orig_value));
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         }
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         }
134         ret = -1;
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         }
145         if (i >= cf_cb->keys_num)
146                 syslog (LOG_WARNING, "Plugin `%s' did not register for value `%s'.", type, key);
148         free (key);
149         free (value);
151         DBG ("return (%i)", ret);
153         return (ret);
154 } /* int cf_dispatch */
156 static int dispatch_global_option (const oconfig_item_t *ci)
158         if (ci->values_num != 1)
159                 return (-1);
160         if (ci->values[0].type != OCONFIG_TYPE_STRING)
161                 return (-1);
163         return (global_option_set (ci->key, ci->values[0].value.string));
166 static int dispatch_value_plugindir (const oconfig_item_t *ci)
168         assert (strcasecmp (ci->key, "PluginDir") == 0);
169         
170         if (ci->values_num != 1)
171                 return (-1);
172         if (ci->values[0].type != OCONFIG_TYPE_STRING)
173                 return (-1);
175         plugin_set_dir (ci->values[0].value.string);
176         return (0);
179 static int dispatch_value_loadplugin (const oconfig_item_t *ci)
181         assert (strcasecmp (ci->key, "LoadPlugin") == 0);
183         if (ci->values_num != 1)
184                 return (-1);
185         if (ci->values[0].type != OCONFIG_TYPE_STRING)
186                 return (-1);
188         return (plugin_load (ci->values[0].value.string));
189 } /* int dispatch_value_loadplugin */
191 static int dispatch_value_plugin (const char *plugin, oconfig_item_t *ci)
193         char  buffer[4096];
194         char *buffer_ptr;
195         int   buffer_free;
196         int i;
198         buffer_ptr = buffer;
199         buffer_free = sizeof (buffer);
201         for (i = 0; i < ci->values_num; i++)
202         {
203                 int status = -1;
205                 if (ci->values[i].type == OCONFIG_TYPE_STRING)
206                         status = snprintf (buffer_ptr, buffer_free, " %s",
207                                         ci->values[i].value.string);
208                 else if (ci->values[i].type == OCONFIG_TYPE_NUMBER)
209                         status = snprintf (buffer_ptr, buffer_free, " %lf",
210                                         ci->values[i].value.number);
211                 else if (ci->values[i].type == OCONFIG_TYPE_BOOLEAN)
212                         status = snprintf (buffer_ptr, buffer_free, " %s",
213                                         ci->values[i].value.boolean
214                                         ? "true" : "false");
216                 if ((status < 0) || (status >= buffer_free))
217                         return (-1);
218                 buffer_free -= status;
219                 buffer_ptr  += status;
220         }
221         /* skip the initial space */
222         buffer_ptr = buffer + 1;
224         return (cf_dispatch (plugin, ci->key, buffer_ptr));
225 } /* int plugin_conf_dispatch */
227 static int dispatch_value (const oconfig_item_t *ci)
229         int ret = -2;
230         int i;
232         for (i = 0; i < cf_value_map_num; i++)
233                 if (strcasecmp (cf_value_map[i].key, ci->key) == 0)
234                 {
235                         ret = cf_value_map[i].func (ci);
236                         break;
237                 }
239         for (i = 0; i < cf_global_options_num; i++)
240                 if (strcasecmp (cf_global_options[i].key, ci->key) == 0)
241                 {
242                         ret = dispatch_global_option (ci);
243                         break;
244                 }
246         return (ret);
247 } /* int dispatch_value */
249 static int dispatch_block_plugin (oconfig_item_t *ci)
251         int i;
252         char *name;
254         if (strcasecmp (ci->key, "Plugin") != 0)
255                 return (-1);
256         if (ci->values_num != 1)
257                 return (-1);
258         if (ci->values[0].type != OCONFIG_TYPE_STRING)
259                 return (-1);
261         name = ci->values[0].value.string;
263         for (i = 0; i < ci->children_num; i++)
264         {
265                 if (ci->children[i].children == NULL)
266                         dispatch_value_plugin (name, ci->children + i);
267                 else
268                         {DBG ("No nested config blocks allow for plugins. Yet.");}
269         }
271         return (0);
275 static int dispatch_block (oconfig_item_t *ci)
277         if (strcasecmp (ci->key, "Plugin") == 0)
278                 return (dispatch_block_plugin (ci));
280         return (0);
283 /* 
284  * Public functions
285  */
286 int global_option_set (const char *option, const char *value)
288         int i;
290         DBG ("option = %s; value = %s;", option, value);
292         for (i = 0; i < cf_global_options_num; i++)
293                 if (strcasecmp (cf_global_options[i].key, option) == 0)
294                         break;
296         if (i >= cf_global_options_num)
297                 return (-1);
299         if (cf_global_options[i].value != NULL)
300                 free (cf_global_options[i].value);
302         if (value != NULL)
303                 cf_global_options[i].value = strdup (value);
304         else
305                 cf_global_options[i].value = NULL;
307         return (0);
310 const char *global_option_get (const char *option)
312         int i;
314         for (i = 0; i < cf_global_options_num; i++)
315                 if (strcasecmp (cf_global_options[i].key, option) == 0)
316                         break;
318         if (i >= cf_global_options_num)
319                 return (NULL);
320         
321         return ((cf_global_options[i].value != NULL)
322                         ? cf_global_options[i].value
323                         : cf_global_options[i].def);
324 } /* char *global_option_get */
326 void cf_unregister (const char *type)
328         cf_callback_t *this, *prev;
330         for (prev = NULL, this = first_callback;
331                         this != NULL;
332                         prev = this, this = this->next)
333                 if (strcasecmp (this->type, type) == 0)
334                 {
335                         if (prev == NULL)
336                                 first_callback = this->next;
337                         else
338                                 prev->next = this->next;
340                         free (this);
341                         break;
342                 }
345 void cf_register (const char *type,
346                 int (*callback) (const char *, const char *),
347                 const char **keys, int keys_num)
349         cf_callback_t *cf_cb;
351         /* Remove this module from the list, if it already exists */
352         cf_unregister (type);
354         /* This pointer will be free'd in `cf_unregister' */
355         if ((cf_cb = (cf_callback_t *) malloc (sizeof (cf_callback_t))) == NULL)
356                 return;
358         cf_cb->type     = type;
359         cf_cb->callback = callback;
360         cf_cb->keys     = keys;
361         cf_cb->keys_num = keys_num;
363         cf_cb->next = first_callback;
364         first_callback = cf_cb;
365 } /* void cf_register */
367 int cf_read (char *filename)
369         oconfig_item_t *conf;
370         int i;
372         conf = oconfig_parse_file (filename);
373         if (conf == NULL)
374         {
375                 syslog (LOG_ERR, "Unable to read config file %s.", filename);
376                 return (-1);
377         }
379         for (i = 0; i < conf->children_num; i++)
380         {
381                 if (conf->children[i].children == NULL)
382                         dispatch_value (conf->children + i);
383                 else
384                         dispatch_block (conf->children + i);
385         }
387         return (0);
388 } /* int cf_read */