Merge branch 'collectd-4.2'
[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 "utils_threshold.h"
31
32 #define ESCAPE_NULL(str) ((str) == NULL ? "(null)" : (str))
33
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;
45
46 typedef struct cf_complex_callback_s
47 {
48         char *type;
49         int (*callback) (oconfig_item_t *);
50         struct cf_complex_callback_s *next;
51 } cf_complex_callback_t;
52
53 typedef struct cf_value_map_s
54 {
55         char *key;
56         int (*func) (const oconfig_item_t *);
57 } cf_value_map_t;
58
59 typedef struct cf_global_option_s
60 {
61         char *key;
62         char *value;
63         char *def;
64 } cf_global_option_t;
65
66 /*
67  * Prototypes of callback functions
68  */
69 static int dispatch_value_plugindir (const oconfig_item_t *ci);
70 static int dispatch_value_loadplugin (const oconfig_item_t *ci);
71
72 /*
73  * Private variables
74  */
75 static cf_callback_t *first_callback = NULL;
76 static cf_complex_callback_t *complex_callback_head = NULL;
77
78 static cf_value_map_t cf_value_map[] =
79 {
80         {"PluginDir",  dispatch_value_plugindir},
81         {"LoadPlugin", dispatch_value_loadplugin}
82 };
83 static int cf_value_map_num = STATIC_ARRAY_LEN (cf_value_map);
84
85 static cf_global_option_t cf_global_options[] =
86 {
87         {"BaseDir",   NULL, PKGLOCALSTATEDIR},
88         {"PIDFile",   NULL, PIDFILE},
89         {"Hostname",  NULL, NULL},
90         {"Interval",  NULL, "10"},
91         {"ReadThreads", NULL, "5"},
92         {"TypesDB",   NULL, PLUGINDIR"/types.db"} /* FIXME: Configure path */
93 };
94 static int cf_global_options_num = STATIC_ARRAY_LEN (cf_global_options);
95
96 /*
97  * Functions to handle register/unregister, search, and other plugin related
98  * stuff
99  */
100 static cf_callback_t *cf_search (const char *type)
101 {
102         cf_callback_t *cf_cb;
103
104         if (type == NULL)
105                 return (NULL);
106
107         for (cf_cb = first_callback; cf_cb != NULL; cf_cb = cf_cb->next)
108                 if (strcasecmp (cf_cb->type, type) == 0)
109                         break;
110
111         return (cf_cb);
112 }
113
114 static int cf_dispatch (const char *type, const char *orig_key,
115                 const char *orig_value)
116 {
117         cf_callback_t *cf_cb;
118         char *key;
119         char *value;
120         int ret;
121         int i;
122
123         DEBUG ("type = %s, key = %s, value = %s",
124                         ESCAPE_NULL(type),
125                         ESCAPE_NULL(orig_key),
126                         ESCAPE_NULL(orig_value));
127
128         if ((cf_cb = cf_search (type)) == NULL)
129         {
130                 WARNING ("Plugin `%s' did not register a callback.", type);
131                 return (-1);
132         }
133
134         if ((key = strdup (orig_key)) == NULL)
135                 return (1);
136         if ((value = strdup (orig_value)) == NULL)
137         {
138                 free (key);
139                 return (2);
140         }
141
142         ret = -1;
143
144         for (i = 0; i < cf_cb->keys_num; i++)
145         {
146                 if (strcasecmp (cf_cb->keys[i], key) == 0)
147                 {
148                         ret = (*cf_cb->callback) (key, value);
149                         break;
150                 }
151         }
152
153         if (i >= cf_cb->keys_num)
154                 WARNING ("Plugin `%s' did not register for value `%s'.", type, key);
155
156         free (key);
157         free (value);
158
159         DEBUG ("return (%i)", ret);
160
161         return (ret);
162 } /* int cf_dispatch */
163
164 static int dispatch_global_option (const oconfig_item_t *ci)
165 {
166         if (ci->values_num != 1)
167                 return (-1);
168         if (ci->values[0].type == OCONFIG_TYPE_STRING)
169                 return (global_option_set (ci->key, ci->values[0].value.string));
170         else if (ci->values[0].type == OCONFIG_TYPE_NUMBER)
171         {
172                 char tmp[128];
173                 snprintf (tmp, sizeof (tmp), "%lf", ci->values[0].value.number);
174                 tmp[127] = '\0';
175                 return (global_option_set (ci->key, tmp));
176         }
177
178         return (-1);
179 } /* int dispatch_global_option */
180
181 static int dispatch_value_plugindir (const oconfig_item_t *ci)
182 {
183         assert (strcasecmp (ci->key, "PluginDir") == 0);
184         
185         if (ci->values_num != 1)
186                 return (-1);
187         if (ci->values[0].type != OCONFIG_TYPE_STRING)
188                 return (-1);
189
190         plugin_set_dir (ci->values[0].value.string);
191         return (0);
192 }
193
194 static int dispatch_value_loadplugin (const oconfig_item_t *ci)
195 {
196         assert (strcasecmp (ci->key, "LoadPlugin") == 0);
197
198         if (ci->values_num != 1)
199                 return (-1);
200         if (ci->values[0].type != OCONFIG_TYPE_STRING)
201                 return (-1);
202
203         return (plugin_load (ci->values[0].value.string));
204 } /* int dispatch_value_loadplugin */
205
206 static int dispatch_value_plugin (const char *plugin, oconfig_item_t *ci)
207 {
208         char  buffer[4096];
209         char *buffer_ptr;
210         int   buffer_free;
211         int i;
212
213         buffer_ptr = buffer;
214         buffer_free = sizeof (buffer);
215
216         for (i = 0; i < ci->values_num; i++)
217         {
218                 int status = -1;
219
220                 if (ci->values[i].type == OCONFIG_TYPE_STRING)
221                         status = snprintf (buffer_ptr, buffer_free, " %s",
222                                         ci->values[i].value.string);
223                 else if (ci->values[i].type == OCONFIG_TYPE_NUMBER)
224                         status = snprintf (buffer_ptr, buffer_free, " %lf",
225                                         ci->values[i].value.number);
226                 else if (ci->values[i].type == OCONFIG_TYPE_BOOLEAN)
227                         status = snprintf (buffer_ptr, buffer_free, " %s",
228                                         ci->values[i].value.boolean
229                                         ? "true" : "false");
230
231                 if ((status < 0) || (status >= buffer_free))
232                         return (-1);
233                 buffer_free -= status;
234                 buffer_ptr  += status;
235         }
236         /* skip the initial space */
237         buffer_ptr = buffer + 1;
238
239         return (cf_dispatch (plugin, ci->key, buffer_ptr));
240 } /* int plugin_conf_dispatch */
241
242 static int dispatch_value (const oconfig_item_t *ci)
243 {
244         int ret = -2;
245         int i;
246
247         for (i = 0; i < cf_value_map_num; i++)
248                 if (strcasecmp (cf_value_map[i].key, ci->key) == 0)
249                 {
250                         ret = cf_value_map[i].func (ci);
251                         break;
252                 }
253
254         for (i = 0; i < cf_global_options_num; i++)
255                 if (strcasecmp (cf_global_options[i].key, ci->key) == 0)
256                 {
257                         ret = dispatch_global_option (ci);
258                         break;
259                 }
260
261         return (ret);
262 } /* int dispatch_value */
263
264 static int dispatch_block_plugin (oconfig_item_t *ci)
265 {
266         int i;
267         char *name;
268
269         cf_complex_callback_t *cb;
270
271         if (strcasecmp (ci->key, "Plugin") != 0)
272                 return (-1);
273         if (ci->values_num < 1)
274                 return (-1);
275         if (ci->values[0].type != OCONFIG_TYPE_STRING)
276                 return (-1);
277
278         name = ci->values[0].value.string;
279
280         /* Check for a complex callback first */
281         for (cb = complex_callback_head; cb != NULL; cb = cb->next)
282                 if (strcasecmp (name, cb->type) == 0)
283                         return (cb->callback (ci));
284
285         /* Hm, no complex plugin found. Dispatch the values one by one */
286         for (i = 0; i < ci->children_num; i++)
287         {
288                 if (ci->children[i].children == NULL)
289                         dispatch_value_plugin (name, ci->children + i);
290                 else
291                         {DEBUG ("No nested config blocks allow for this plugin.");}
292         }
293
294         return (0);
295 }
296
297
298 static int dispatch_block (oconfig_item_t *ci)
299 {
300         if (strcasecmp (ci->key, "Plugin") == 0)
301                 return (dispatch_block_plugin (ci));
302         else if (strcasecmp (ci->key, "Threshold") == 0)
303                 return (ut_config (ci));
304
305         return (0);
306 }
307
308 #define CF_MAX_DEPTH 8
309 static oconfig_item_t *cf_read_file (const char *file, int depth);
310
311 static int cf_include_all (oconfig_item_t *root, int depth)
312 {
313         int i;
314
315         for (i = 0; i < root->children_num; i++)
316         {
317                 oconfig_item_t *new;
318                 oconfig_item_t *old;
319
320                 /* Ignore all blocks, including `Include' blocks. */
321                 if (root->children[i].children_num != 0)
322                         continue;
323
324                 if (strcasecmp (root->children[i].key, "Include") != 0)
325                         continue;
326
327                 old = root->children + i;
328
329                 if ((old->values_num != 1)
330                                 || (old->values[0].type != OCONFIG_TYPE_STRING))
331                 {
332                         ERROR ("configfile: `Include' needs exactly one string argument.");
333                         continue;
334                 }
335
336                 new = cf_read_file (old->values[0].value.string, depth + 1);
337                 if (new == NULL)
338                         continue;
339
340                 /* There are more children now. We need to expand
341                  * root->children. */
342                 if (new->children_num > 1)
343                 {
344                         oconfig_item_t *temp;
345
346                         DEBUG ("configfile: Resizing root-children from %i to %i elements.",
347                                         root->children_num,
348                                         root->children_num + new->children_num - 1);
349
350                         temp = (oconfig_item_t *) realloc (root->children,
351                                         sizeof (oconfig_item_t)
352                                         * (root->children_num + new->children_num - 1));
353                         if (temp == NULL)
354                         {
355                                 ERROR ("configfile: realloc failed.");
356                                 oconfig_free (new);
357                                 continue;
358                         }
359                         root->children = temp;
360                 }
361
362                 /* Clean up the old include directive while we still have a
363                  * valid pointer */
364                 DEBUG ("configfile: Cleaning up `old'");
365                 /* sfree (old->values[0].value.string); */
366                 sfree (old->values);
367
368                 /* If there are trailing children and the number of children
369                  * changes, we need to move the trailing ones either one to the
370                  * front or (new->num - 1) to the back */
371                 if (((root->children_num - i) > 1)
372                                 && (new->children_num != 1))
373                 {
374                         DEBUG ("configfile: Moving trailing children.");
375                         memmove (root->children + i + new->children_num,
376                                         root->children + i + 1,
377                                         sizeof (oconfig_item_t)
378                                         * (root->children_num - (i + 1)));
379                 }
380
381                 /* Now copy the new children to where the include statement was */
382                 if (new->children_num > 0)
383                 {
384                         DEBUG ("configfile: Copying new children.");
385                         memcpy (root->children + i,
386                                         new->children,
387                                         sizeof (oconfig_item_t)
388                                         * new->children_num);
389                 }
390
391                 /* Adjust the number of children and the position in the list. */
392                 root->children_num = root->children_num + new->children_num - 1;
393                 i = i + new->children_num - 1;
394
395                 /* Clean up the `new' struct. We set `new->children' to NULL so
396                  * the stuff we've just copied pointers to isn't freed by
397                  * `oconfig_free' */
398                 DEBUG ("configfile: Cleaning up `new'");
399                 sfree (new->values); /* should be NULL anyway */
400                 sfree (new);
401                 new = NULL;
402         } /* for (i = 0; i < root->children_num; i++) */
403
404         return (0);
405 } /* int cf_include_all */
406
407 static oconfig_item_t *cf_read_file (const char *file, int depth)
408 {
409         oconfig_item_t *root;
410
411         if (depth >= CF_MAX_DEPTH)
412         {
413                 ERROR ("configfile: Not including `%s' because the maximum nesting depth has been reached.",
414                                 file);
415                 return (NULL);
416         }
417
418         root = oconfig_parse_file (file);
419         if (root == NULL)
420         {
421                 ERROR ("configfile: Cannot read file `%s'.", file);
422                 return (NULL);
423         }
424
425         cf_include_all (root, depth);
426
427         return (root);
428 } /* oconfig_item_t *cf_read_file */
429
430 /* 
431  * Public functions
432  */
433 int global_option_set (const char *option, const char *value)
434 {
435         int i;
436
437         DEBUG ("option = %s; value = %s;", option, value);
438
439         for (i = 0; i < cf_global_options_num; i++)
440                 if (strcasecmp (cf_global_options[i].key, option) == 0)
441                         break;
442
443         if (i >= cf_global_options_num)
444                 return (-1);
445
446         sfree (cf_global_options[i].value);
447
448         if (value != NULL)
449                 cf_global_options[i].value = strdup (value);
450         else
451                 cf_global_options[i].value = NULL;
452
453         return (0);
454 }
455
456 const char *global_option_get (const char *option)
457 {
458         int i;
459
460         for (i = 0; i < cf_global_options_num; i++)
461                 if (strcasecmp (cf_global_options[i].key, option) == 0)
462                         break;
463
464         if (i >= cf_global_options_num)
465                 return (NULL);
466         
467         return ((cf_global_options[i].value != NULL)
468                         ? cf_global_options[i].value
469                         : cf_global_options[i].def);
470 } /* char *global_option_get */
471
472 void cf_unregister (const char *type)
473 {
474         cf_callback_t *this, *prev;
475
476         for (prev = NULL, this = first_callback;
477                         this != NULL;
478                         prev = this, this = this->next)
479                 if (strcasecmp (this->type, type) == 0)
480                 {
481                         if (prev == NULL)
482                                 first_callback = this->next;
483                         else
484                                 prev->next = this->next;
485
486                         free (this);
487                         break;
488                 }
489 } /* void cf_unregister */
490
491 void cf_unregister_complex (const char *type)
492 {
493         cf_complex_callback_t *this, *prev;
494
495         for (prev = NULL, this = complex_callback_head;
496                         this != NULL;
497                         prev = this, this = this->next)
498                 if (strcasecmp (this->type, type) == 0)
499                 {
500                         if (prev == NULL)
501                                 complex_callback_head = this->next;
502                         else
503                                 prev->next = this->next;
504
505                         sfree (this->type);
506                         sfree (this);
507                         break;
508                 }
509 } /* void cf_unregister */
510
511 void cf_register (const char *type,
512                 int (*callback) (const char *, const char *),
513                 const char **keys, int keys_num)
514 {
515         cf_callback_t *cf_cb;
516
517         /* Remove this module from the list, if it already exists */
518         cf_unregister (type);
519
520         /* This pointer will be free'd in `cf_unregister' */
521         if ((cf_cb = (cf_callback_t *) malloc (sizeof (cf_callback_t))) == NULL)
522                 return;
523
524         cf_cb->type     = type;
525         cf_cb->callback = callback;
526         cf_cb->keys     = keys;
527         cf_cb->keys_num = keys_num;
528
529         cf_cb->next = first_callback;
530         first_callback = cf_cb;
531 } /* void cf_register */
532
533 int cf_register_complex (const char *type, int (*callback) (oconfig_item_t *))
534 {
535         cf_complex_callback_t *new;
536
537         new = (cf_complex_callback_t *) malloc (sizeof (cf_complex_callback_t));
538         if (new == NULL)
539                 return (-1);
540
541         new->type = strdup (type);
542         if (new->type == NULL)
543         {
544                 sfree (new);
545                 return (-1);
546         }
547
548         new->callback = callback;
549         new->next = NULL;
550
551         if (complex_callback_head == NULL)
552         {
553                 complex_callback_head = new;
554         }
555         else
556         {
557                 cf_complex_callback_t *last = complex_callback_head;
558                 while (last->next != NULL)
559                         last = last->next;
560                 last->next = new;
561         }
562
563         return (0);
564 } /* int cf_register_complex */
565
566 int cf_read (char *filename)
567 {
568         oconfig_item_t *conf;
569         int i;
570
571         conf = cf_read_file (filename, 0 /* depth */);
572         if (conf == NULL)
573         {
574                 ERROR ("Unable to read config file %s.", filename);
575                 return (-1);
576         }
577
578         for (i = 0; i < conf->children_num; i++)
579         {
580                 if (conf->children[i].children == NULL)
581                         dispatch_value (conf->children + i);
582                 else
583                         dispatch_block (conf->children + i);
584         }
585
586         return (0);
587 } /* int cf_read */