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