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