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