4b6803ed68a398cbdff4881ad9e599b2ad9d3639
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"
31 #define ESCAPE_NULL(str) ((str) == NULL ? "(null)" : (str))
33 /*
34 * Private types
35 */
36 typedef struct cf_callback
37 {
38 const char *type;
39 int (*callback) (const char *, const char *);
40 const char **keys;
41 int keys_num;
42 struct cf_callback *next;
43 } cf_callback_t;
45 typedef struct cf_value_map_s
46 {
47 char *key;
48 int (*func) (const oconfig_item_t *);
49 } cf_value_map_t;
51 typedef struct cf_global_option_s
52 {
53 char *key;
54 char *value;
55 char *def;
56 } cf_global_option_t;
58 /*
59 * Prototypes of callback functions
60 */
61 static int dispatch_value_plugindir (const oconfig_item_t *ci);
62 static int dispatch_value_loadplugin (const oconfig_item_t *ci);
64 /*
65 * Private variables
66 */
67 static cf_callback_t *first_callback = NULL;
69 static cf_value_map_t cf_value_map[] =
70 {
71 {"PluginDir", dispatch_value_plugindir},
72 {"LoadPlugin", dispatch_value_loadplugin}
73 };
74 static int cf_value_map_num = STATIC_ARRAY_LEN (cf_value_map);
76 static cf_global_option_t cf_global_options[] =
77 {
78 {"BaseDir", NULL, PKGLOCALSTATEDIR},
79 {"PIDFile", NULL, PIDFILE},
80 {"Hostname", NULL, NULL},
81 {"Interval", NULL, "10"},
82 {"ReadThreads", NULL, "5"},
83 {"TypesDB", NULL, PLUGINDIR"/types.db"} /* FIXME: Configure path */
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);
103 }
105 static int cf_dispatch (const char *type, const char *orig_key,
106 const char *orig_value)
107 {
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)
156 {
157 if (ci->values_num != 1)
158 return (-1);
159 if (ci->values[0].type == OCONFIG_TYPE_STRING)
160 return (global_option_set (ci->key, ci->values[0].value.string));
161 else if (ci->values[0].type == OCONFIG_TYPE_NUMBER)
162 {
163 char tmp[128];
164 snprintf (tmp, sizeof (tmp), "%lf", ci->values[0].value.number);
165 tmp[127] = '\0';
166 return (global_option_set (ci->key, tmp));
167 }
169 return (-1);
170 } /* int dispatch_global_option */
172 static int dispatch_value_plugindir (const oconfig_item_t *ci)
173 {
174 assert (strcasecmp (ci->key, "PluginDir") == 0);
176 if (ci->values_num != 1)
177 return (-1);
178 if (ci->values[0].type != OCONFIG_TYPE_STRING)
179 return (-1);
181 plugin_set_dir (ci->values[0].value.string);
182 return (0);
183 }
185 static int dispatch_value_loadplugin (const oconfig_item_t *ci)
186 {
187 assert (strcasecmp (ci->key, "LoadPlugin") == 0);
189 if (ci->values_num != 1)
190 return (-1);
191 if (ci->values[0].type != OCONFIG_TYPE_STRING)
192 return (-1);
194 return (plugin_load (ci->values[0].value.string));
195 } /* int dispatch_value_loadplugin */
197 static int dispatch_value_plugin (const char *plugin, oconfig_item_t *ci)
198 {
199 char buffer[4096];
200 char *buffer_ptr;
201 int buffer_free;
202 int i;
204 buffer_ptr = buffer;
205 buffer_free = sizeof (buffer);
207 for (i = 0; i < ci->values_num; i++)
208 {
209 int status = -1;
211 if (ci->values[i].type == OCONFIG_TYPE_STRING)
212 status = snprintf (buffer_ptr, buffer_free, " %s",
213 ci->values[i].value.string);
214 else if (ci->values[i].type == OCONFIG_TYPE_NUMBER)
215 status = snprintf (buffer_ptr, buffer_free, " %lf",
216 ci->values[i].value.number);
217 else if (ci->values[i].type == OCONFIG_TYPE_BOOLEAN)
218 status = snprintf (buffer_ptr, buffer_free, " %s",
219 ci->values[i].value.boolean
220 ? "true" : "false");
222 if ((status < 0) || (status >= buffer_free))
223 return (-1);
224 buffer_free -= status;
225 buffer_ptr += status;
226 }
227 /* skip the initial space */
228 buffer_ptr = buffer + 1;
230 return (cf_dispatch (plugin, ci->key, buffer_ptr));
231 } /* int plugin_conf_dispatch */
233 static int dispatch_value (const oconfig_item_t *ci)
234 {
235 int ret = -2;
236 int i;
238 for (i = 0; i < cf_value_map_num; i++)
239 if (strcasecmp (cf_value_map[i].key, ci->key) == 0)
240 {
241 ret = cf_value_map[i].func (ci);
242 break;
243 }
245 for (i = 0; i < cf_global_options_num; i++)
246 if (strcasecmp (cf_global_options[i].key, ci->key) == 0)
247 {
248 ret = dispatch_global_option (ci);
249 break;
250 }
252 return (ret);
253 } /* int dispatch_value */
255 static int dispatch_block_plugin (oconfig_item_t *ci)
256 {
257 int i;
258 char *name;
260 if (strcasecmp (ci->key, "Plugin") != 0)
261 return (-1);
262 if (ci->values_num != 1)
263 return (-1);
264 if (ci->values[0].type != OCONFIG_TYPE_STRING)
265 return (-1);
267 name = ci->values[0].value.string;
269 for (i = 0; i < ci->children_num; i++)
270 {
271 if (ci->children[i].children == NULL)
272 dispatch_value_plugin (name, ci->children + i);
273 else
274 {DEBUG ("No nested config blocks allow for plugins. Yet.");}
275 }
277 return (0);
278 }
281 static int dispatch_block (oconfig_item_t *ci)
282 {
283 if (strcasecmp (ci->key, "Plugin") == 0)
284 return (dispatch_block_plugin (ci));
286 return (0);
287 }
289 /*
290 * Public functions
291 */
292 int global_option_set (const char *option, const char *value)
293 {
294 int i;
296 DEBUG ("option = %s; value = %s;", option, value);
298 for (i = 0; i < cf_global_options_num; i++)
299 if (strcasecmp (cf_global_options[i].key, option) == 0)
300 break;
302 if (i >= cf_global_options_num)
303 return (-1);
305 sfree (cf_global_options[i].value);
307 if (value != NULL)
308 cf_global_options[i].value = strdup (value);
309 else
310 cf_global_options[i].value = NULL;
312 return (0);
313 }
315 const char *global_option_get (const char *option)
316 {
317 int i;
319 for (i = 0; i < cf_global_options_num; i++)
320 if (strcasecmp (cf_global_options[i].key, option) == 0)
321 break;
323 if (i >= cf_global_options_num)
324 return (NULL);
326 return ((cf_global_options[i].value != NULL)
327 ? cf_global_options[i].value
328 : cf_global_options[i].def);
329 } /* char *global_option_get */
331 void cf_unregister (const char *type)
332 {
333 cf_callback_t *this, *prev;
335 for (prev = NULL, this = first_callback;
336 this != NULL;
337 prev = this, this = this->next)
338 if (strcasecmp (this->type, type) == 0)
339 {
340 if (prev == NULL)
341 first_callback = this->next;
342 else
343 prev->next = this->next;
345 free (this);
346 break;
347 }
348 } /* void cf_unregister */
350 void cf_register (const char *type,
351 int (*callback) (const char *, const char *),
352 const char **keys, int keys_num)
353 {
354 cf_callback_t *cf_cb;
356 /* Remove this module from the list, if it already exists */
357 cf_unregister (type);
359 /* This pointer will be free'd in `cf_unregister' */
360 if ((cf_cb = (cf_callback_t *) malloc (sizeof (cf_callback_t))) == NULL)
361 return;
363 cf_cb->type = type;
364 cf_cb->callback = callback;
365 cf_cb->keys = keys;
366 cf_cb->keys_num = keys_num;
368 cf_cb->next = first_callback;
369 first_callback = cf_cb;
370 } /* void cf_register */
372 int cf_read (char *filename)
373 {
374 oconfig_item_t *conf;
375 int i;
377 conf = oconfig_parse_file (filename);
378 if (conf == NULL)
379 {
380 ERROR ("Unable to read config file %s.", filename);
381 return (-1);
382 }
384 for (i = 0; i < conf->children_num; i++)
385 {
386 if (conf->children[i].children == NULL)
387 dispatch_value (conf->children + i);
388 else
389 dispatch_block (conf->children + i);
390 }
392 return (0);
393 } /* int cf_read */