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 **/
23 #include "collectd.h"
25 #include "liboconfig/oconfig.h"
27 #include "common.h"
28 #include "plugin.h"
29 #include "configfile.h"
30 #include "types_list.h"
31 #include "utils_threshold.h"
33 #if HAVE_WORDEXP_H
34 # include <wordexp.h>
35 #endif /* HAVE_WORDEXP_H */
37 #define ESCAPE_NULL(str) ((str) == NULL ? "(null)" : (str))
39 /*
40 * Private types
41 */
42 typedef struct cf_callback
43 {
44 const char *type;
45 int (*callback) (const char *, const char *);
46 const char **keys;
47 int keys_num;
48 struct cf_callback *next;
49 } cf_callback_t;
51 typedef struct cf_complex_callback_s
52 {
53 char *type;
54 int (*callback) (oconfig_item_t *);
55 struct cf_complex_callback_s *next;
56 } cf_complex_callback_t;
58 typedef struct cf_value_map_s
59 {
60 char *key;
61 int (*func) (const oconfig_item_t *);
62 } cf_value_map_t;
64 typedef struct cf_global_option_s
65 {
66 char *key;
67 char *value;
68 char *def;
69 } cf_global_option_t;
71 /*
72 * Prototypes of callback functions
73 */
74 static int dispatch_value_typesdb (const oconfig_item_t *ci);
75 static int dispatch_value_plugindir (const oconfig_item_t *ci);
76 static int dispatch_value_loadplugin (const oconfig_item_t *ci);
78 /*
79 * Private variables
80 */
81 static cf_callback_t *first_callback = NULL;
82 static cf_complex_callback_t *complex_callback_head = NULL;
84 static cf_value_map_t cf_value_map[] =
85 {
86 {"TypesDB", dispatch_value_typesdb},
87 {"PluginDir", dispatch_value_plugindir},
88 {"LoadPlugin", dispatch_value_loadplugin}
89 };
90 static int cf_value_map_num = STATIC_ARRAY_LEN (cf_value_map);
92 static cf_global_option_t cf_global_options[] =
93 {
94 {"BaseDir", NULL, PKGLOCALSTATEDIR},
95 {"PIDFile", NULL, PIDFILE},
96 {"Hostname", NULL, NULL},
97 {"FQDNLookup", NULL, "false"},
98 {"Interval", NULL, "10"},
99 {"ReadThreads", NULL, "5"}
100 };
101 static int cf_global_options_num = STATIC_ARRAY_LEN (cf_global_options);
103 static int cf_default_typesdb = 1;
105 /*
106 * Functions to handle register/unregister, search, and other plugin related
107 * stuff
108 */
109 static cf_callback_t *cf_search (const char *type)
110 {
111 cf_callback_t *cf_cb;
113 if (type == NULL)
114 return (NULL);
116 for (cf_cb = first_callback; cf_cb != NULL; cf_cb = cf_cb->next)
117 if (strcasecmp (cf_cb->type, type) == 0)
118 break;
120 return (cf_cb);
121 }
123 static int cf_dispatch (const char *type, const char *orig_key,
124 const char *orig_value)
125 {
126 cf_callback_t *cf_cb;
127 char *key;
128 char *value;
129 int ret;
130 int i;
132 DEBUG ("type = %s, key = %s, value = %s",
133 ESCAPE_NULL(type),
134 ESCAPE_NULL(orig_key),
135 ESCAPE_NULL(orig_value));
137 if ((cf_cb = cf_search (type)) == NULL)
138 {
139 WARNING ("Found a configuration for the `%s' plugin, but "
140 "the plugin isn't loaded or didn't register "
141 "a configuration callback.", type);
142 return (-1);
143 }
145 if ((key = strdup (orig_key)) == NULL)
146 return (1);
147 if ((value = strdup (orig_value)) == NULL)
148 {
149 free (key);
150 return (2);
151 }
153 ret = -1;
155 for (i = 0; i < cf_cb->keys_num; i++)
156 {
157 if (strcasecmp (cf_cb->keys[i], key) == 0)
158 {
159 ret = (*cf_cb->callback) (key, value);
160 break;
161 }
162 }
164 if (i >= cf_cb->keys_num)
165 WARNING ("Plugin `%s' did not register for value `%s'.", type, key);
167 free (key);
168 free (value);
170 DEBUG ("return (%i)", ret);
172 return (ret);
173 } /* int cf_dispatch */
175 static int dispatch_global_option (const oconfig_item_t *ci)
176 {
177 if (ci->values_num != 1)
178 return (-1);
179 if (ci->values[0].type == OCONFIG_TYPE_STRING)
180 return (global_option_set (ci->key, ci->values[0].value.string));
181 else if (ci->values[0].type == OCONFIG_TYPE_NUMBER)
182 {
183 char tmp[128];
184 snprintf (tmp, sizeof (tmp), "%lf", ci->values[0].value.number);
185 tmp[127] = '\0';
186 return (global_option_set (ci->key, tmp));
187 }
188 else if (ci->values[0].type == OCONFIG_TYPE_BOOLEAN)
189 {
190 if (ci->values[0].value.boolean)
191 return (global_option_set (ci->key, "true"));
192 else
193 return (global_option_set (ci->key, "false"));
194 }
196 return (-1);
197 } /* int dispatch_global_option */
199 static int dispatch_value_typesdb (const oconfig_item_t *ci)
200 {
201 int i = 0;
203 assert (strcasecmp (ci->key, "TypesDB") == 0);
205 cf_default_typesdb = 0;
207 if (ci->values_num < 1)
208 return (-1);
210 for (i = 0; i < ci->values_num; ++i)
211 {
212 if (OCONFIG_TYPE_STRING != ci->values[i].type)
213 continue;
215 read_types_list (ci->values[i].value.string);
216 }
217 return (0);
218 } /* int dispatch_value_typesdb */
220 static int dispatch_value_plugindir (const oconfig_item_t *ci)
221 {
222 assert (strcasecmp (ci->key, "PluginDir") == 0);
224 if (ci->values_num != 1)
225 return (-1);
226 if (ci->values[0].type != OCONFIG_TYPE_STRING)
227 return (-1);
229 plugin_set_dir (ci->values[0].value.string);
230 return (0);
231 }
233 static int dispatch_value_loadplugin (const oconfig_item_t *ci)
234 {
235 assert (strcasecmp (ci->key, "LoadPlugin") == 0);
237 if (ci->values_num != 1)
238 return (-1);
239 if (ci->values[0].type != OCONFIG_TYPE_STRING)
240 return (-1);
242 return (plugin_load (ci->values[0].value.string));
243 } /* int dispatch_value_loadplugin */
245 static int dispatch_value_plugin (const char *plugin, oconfig_item_t *ci)
246 {
247 char buffer[4096];
248 char *buffer_ptr;
249 int buffer_free;
250 int i;
252 buffer_ptr = buffer;
253 buffer_free = sizeof (buffer);
255 for (i = 0; i < ci->values_num; i++)
256 {
257 int status = -1;
259 if (ci->values[i].type == OCONFIG_TYPE_STRING)
260 status = snprintf (buffer_ptr, buffer_free, " %s",
261 ci->values[i].value.string);
262 else if (ci->values[i].type == OCONFIG_TYPE_NUMBER)
263 status = snprintf (buffer_ptr, buffer_free, " %lf",
264 ci->values[i].value.number);
265 else if (ci->values[i].type == OCONFIG_TYPE_BOOLEAN)
266 status = snprintf (buffer_ptr, buffer_free, " %s",
267 ci->values[i].value.boolean
268 ? "true" : "false");
270 if ((status < 0) || (status >= buffer_free))
271 return (-1);
272 buffer_free -= status;
273 buffer_ptr += status;
274 }
275 /* skip the initial space */
276 buffer_ptr = buffer + 1;
278 return (cf_dispatch (plugin, ci->key, buffer_ptr));
279 } /* int plugin_conf_dispatch */
281 static int dispatch_value (const oconfig_item_t *ci)
282 {
283 int ret = -2;
284 int i;
286 for (i = 0; i < cf_value_map_num; i++)
287 if (strcasecmp (cf_value_map[i].key, ci->key) == 0)
288 {
289 ret = cf_value_map[i].func (ci);
290 break;
291 }
293 for (i = 0; i < cf_global_options_num; i++)
294 if (strcasecmp (cf_global_options[i].key, ci->key) == 0)
295 {
296 ret = dispatch_global_option (ci);
297 break;
298 }
300 return (ret);
301 } /* int dispatch_value */
303 static int dispatch_block_plugin (oconfig_item_t *ci)
304 {
305 int i;
306 char *name;
308 cf_complex_callback_t *cb;
310 if (strcasecmp (ci->key, "Plugin") != 0)
311 return (-1);
312 if (ci->values_num < 1)
313 return (-1);
314 if (ci->values[0].type != OCONFIG_TYPE_STRING)
315 return (-1);
317 name = ci->values[0].value.string;
319 /* Check for a complex callback first */
320 for (cb = complex_callback_head; cb != NULL; cb = cb->next)
321 if (strcasecmp (name, cb->type) == 0)
322 return (cb->callback (ci));
324 /* Hm, no complex plugin found. Dispatch the values one by one */
325 for (i = 0; i < ci->children_num; i++)
326 {
327 if (ci->children[i].children == NULL)
328 dispatch_value_plugin (name, ci->children + i);
329 else
330 {DEBUG ("No nested config blocks allow for this plugin.");}
331 }
333 return (0);
334 }
337 static int dispatch_block (oconfig_item_t *ci)
338 {
339 if (strcasecmp (ci->key, "Plugin") == 0)
340 return (dispatch_block_plugin (ci));
341 else if (strcasecmp (ci->key, "Threshold") == 0)
342 return (ut_config (ci));
344 return (0);
345 }
347 static int cf_ci_replace_child (oconfig_item_t *dst, oconfig_item_t *src,
348 int offset)
349 {
350 oconfig_item_t *temp;
351 int i;
353 assert (offset >= 0);
354 assert (dst->children_num > offset);
356 /* Resize the memory containing the children to be big enough to hold
357 * all children. */
358 temp = (oconfig_item_t *) realloc (dst->children,
359 sizeof (oconfig_item_t)
360 * (dst->children_num + src->children_num - 1));
361 if (temp == NULL)
362 {
363 ERROR ("configfile: realloc failed.");
364 return (-1);
365 }
366 dst->children = temp;
368 /* Free the memory used by the replaced child. Usually that's the
369 * `Include "blah"' statement. */
370 temp = dst->children + offset;
371 for (i = 0; i < temp->values_num; i++)
372 {
373 if (temp->values[i].type == OCONFIG_TYPE_STRING)
374 {
375 sfree (temp->values[i].value.string);
376 }
377 }
378 sfree (temp->values);
379 temp = NULL;
381 /* If there are children behind the include statement, move them to the
382 * end of the list, so that the new children have room before them. */
383 if ((dst->children_num - (offset + 1)) > 0)
384 {
385 int nmemb = dst->children_num - (offset + 1);
386 int old_offset = offset + 1;
387 int new_offset = offset + src->children_num;
389 memmove (dst->children + new_offset,
390 dst->children + old_offset,
391 sizeof (oconfig_item_t) * nmemb);
392 }
394 /* Last but not least: If there are new childrem, copy them to the
395 * memory reserved for them. */
396 if (src->children_num > 0)
397 {
398 memcpy (dst->children + offset,
399 src->children,
400 sizeof (oconfig_item_t) * src->children_num);
401 }
403 /* Update the number of children. */
404 dst->children_num += (src->children_num - 1);
406 return (0);
407 } /* int cf_ci_replace_child */
409 static int cf_ci_append_children (oconfig_item_t *dst, oconfig_item_t *src)
410 {
411 oconfig_item_t *temp;
413 temp = (oconfig_item_t *) realloc (dst->children,
414 sizeof (oconfig_item_t)
415 * (dst->children_num + src->children_num));
416 if (temp == NULL)
417 {
418 ERROR ("configfile: realloc failed.");
419 return (-1);
420 }
421 dst->children = temp;
423 memcpy (dst->children + dst->children_num,
424 src->children,
425 sizeof (oconfig_item_t)
426 * src->children_num);
427 dst->children_num += src->children_num;
429 return (0);
430 } /* int cf_ci_append_children */
432 #define CF_MAX_DEPTH 8
433 static oconfig_item_t *cf_read_generic (const char *path, int depth);
435 static int cf_include_all (oconfig_item_t *root, int depth)
436 {
437 int i;
439 for (i = 0; i < root->children_num; i++)
440 {
441 oconfig_item_t *new;
442 oconfig_item_t *old;
444 /* Ignore all blocks, including `Include' blocks. */
445 if (root->children[i].children_num != 0)
446 continue;
448 if (strcasecmp (root->children[i].key, "Include") != 0)
449 continue;
451 old = root->children + i;
453 if ((old->values_num != 1)
454 || (old->values[0].type != OCONFIG_TYPE_STRING))
455 {
456 ERROR ("configfile: `Include' needs exactly one string argument.");
457 continue;
458 }
460 new = cf_read_generic (old->values[0].value.string, depth + 1);
461 if (new == NULL)
462 continue;
464 /* Now replace the i'th child in `root' with `new'. */
465 cf_ci_replace_child (root, new, i);
467 sfree (new->values);
468 sfree (new);
469 } /* for (i = 0; i < root->children_num; i++) */
471 return (0);
472 } /* int cf_include_all */
474 static oconfig_item_t *cf_read_file (const char *file, int depth)
475 {
476 oconfig_item_t *root;
478 assert (depth < CF_MAX_DEPTH);
480 root = oconfig_parse_file (file);
481 if (root == NULL)
482 {
483 ERROR ("configfile: Cannot read file `%s'.", file);
484 return (NULL);
485 }
487 cf_include_all (root, depth);
489 return (root);
490 } /* oconfig_item_t *cf_read_file */
492 static oconfig_item_t *cf_read_dir (const char *dir, int depth)
493 {
494 oconfig_item_t *root = NULL;
495 DIR *dh;
496 struct dirent *de;
497 char name[1024];
498 int status;
500 assert (depth < CF_MAX_DEPTH);
502 dh = opendir (dir);
503 if (dh == NULL)
504 {
505 char errbuf[1024];
506 ERROR ("configfile: opendir failed: %s",
507 sstrerror (errno, errbuf, sizeof (errbuf)));
508 return (NULL);
509 }
511 root = (oconfig_item_t *) malloc (sizeof (oconfig_item_t));
512 if (root == NULL)
513 {
514 ERROR ("configfile: malloc failed.");
515 return (NULL);
516 }
517 memset (root, '\0', sizeof (oconfig_item_t));
519 while ((de = readdir (dh)) != NULL)
520 {
521 oconfig_item_t *temp;
523 if ((de->d_name[0] == '.') || (de->d_name[0] == '\0'))
524 continue;
526 status = snprintf (name, sizeof (name), "%s/%s",
527 dir, de->d_name);
528 if (status >= sizeof (name))
529 {
530 ERROR ("configfile: Not including `%s/%s' because its"
531 " name is too long.",
532 dir, de->d_name);
533 continue;
534 }
536 temp = cf_read_generic (name, depth);
537 if (temp == NULL)
538 continue;
540 cf_ci_append_children (root, temp);
541 sfree (temp->children);
542 sfree (temp);
543 }
545 return (root);
546 } /* oconfig_item_t *cf_read_dir */
548 /*
549 * cf_read_generic
550 *
551 * Path is stat'ed and either cf_read_file or cf_read_dir is called
552 * accordingly.
553 *
554 * There are two versions of this function: If `wordexp' exists shell wildcards
555 * will be expanded and the function will include all matches found. If
556 * `wordexp' (or, more precisely, it's header file) is not available the
557 * simpler function is used which does not do any such expansion.
558 */
559 #if HAVE_WORDEXP_H
560 static oconfig_item_t *cf_read_generic (const char *path, int depth)
561 {
562 oconfig_item_t *root = NULL;
563 int status;
564 const char *path_ptr;
565 wordexp_t we;
566 int i;
568 if (depth >= CF_MAX_DEPTH)
569 {
570 ERROR ("configfile: Not including `%s' because the maximum "
571 "nesting depth has been reached.", path);
572 return (NULL);
573 }
575 status = wordexp (path, &we, WRDE_NOCMD);
576 if (status != 0)
577 {
578 ERROR ("configfile: wordexp (%s) failed.", path);
579 return (NULL);
580 }
582 root = (oconfig_item_t *) malloc (sizeof (oconfig_item_t));
583 if (root == NULL)
584 {
585 ERROR ("configfile: malloc failed.");
586 return (NULL);
587 }
588 memset (root, '\0', sizeof (oconfig_item_t));
590 for (i = 0; i < we.we_wordc; i++)
591 {
592 oconfig_item_t *temp;
593 struct stat statbuf;
595 path_ptr = we.we_wordv[i];
597 status = stat (path_ptr, &statbuf);
598 if (status != 0)
599 {
600 char errbuf[1024];
601 ERROR ("configfile: stat (%s) failed: %s",
602 path_ptr,
603 sstrerror (errno, errbuf, sizeof (errbuf)));
604 return (NULL);
605 }
607 if (S_ISREG (statbuf.st_mode))
608 temp = cf_read_file (path_ptr, depth);
609 else if (S_ISDIR (statbuf.st_mode))
610 temp = cf_read_dir (path_ptr, depth);
611 else
612 {
613 ERROR ("configfile: %s is neither a file nor a "
614 "directory.", path);
615 continue;
616 }
618 cf_ci_append_children (root, temp);
619 sfree (temp->children);
620 sfree (temp);
621 }
623 wordfree (&we);
625 return (root);
626 } /* oconfig_item_t *cf_read_generic */
627 /* #endif HAVE_WORDEXP_H */
629 #else /* if !HAVE_WORDEXP_H */
630 static oconfig_item_t *cf_read_generic (const char *path, int depth)
631 {
632 struct stat statbuf;
633 int status;
635 if (depth >= CF_MAX_DEPTH)
636 {
637 ERROR ("configfile: Not including `%s' because the maximum "
638 "nesting depth has been reached.", path);
639 return (NULL);
640 }
642 status = stat (path, &statbuf);
643 if (status != 0)
644 {
645 char errbuf[1024];
646 ERROR ("configfile: stat (%s) failed: %s",
647 path,
648 sstrerror (errno, errbuf, sizeof (errbuf)));
649 return (NULL);
650 }
652 if (S_ISREG (statbuf.st_mode))
653 return (cf_read_file (path, depth));
654 else if (S_ISDIR (statbuf.st_mode))
655 return (cf_read_dir (path, depth));
657 ERROR ("configfile: %s is neither a file nor a directory.", path);
658 return (NULL);
659 } /* oconfig_item_t *cf_read_generic */
660 #endif /* !HAVE_WORDEXP_H */
662 /*
663 * Public functions
664 */
665 int global_option_set (const char *option, const char *value)
666 {
667 int i;
669 DEBUG ("option = %s; value = %s;", option, value);
671 for (i = 0; i < cf_global_options_num; i++)
672 if (strcasecmp (cf_global_options[i].key, option) == 0)
673 break;
675 if (i >= cf_global_options_num)
676 return (-1);
678 sfree (cf_global_options[i].value);
680 if (value != NULL)
681 cf_global_options[i].value = strdup (value);
682 else
683 cf_global_options[i].value = NULL;
685 return (0);
686 }
688 const char *global_option_get (const char *option)
689 {
690 int i;
692 for (i = 0; i < cf_global_options_num; i++)
693 if (strcasecmp (cf_global_options[i].key, option) == 0)
694 break;
696 if (i >= cf_global_options_num)
697 return (NULL);
699 return ((cf_global_options[i].value != NULL)
700 ? cf_global_options[i].value
701 : cf_global_options[i].def);
702 } /* char *global_option_get */
704 void cf_unregister (const char *type)
705 {
706 cf_callback_t *this, *prev;
708 for (prev = NULL, this = first_callback;
709 this != NULL;
710 prev = this, this = this->next)
711 if (strcasecmp (this->type, type) == 0)
712 {
713 if (prev == NULL)
714 first_callback = this->next;
715 else
716 prev->next = this->next;
718 free (this);
719 break;
720 }
721 } /* void cf_unregister */
723 void cf_unregister_complex (const char *type)
724 {
725 cf_complex_callback_t *this, *prev;
727 for (prev = NULL, this = complex_callback_head;
728 this != NULL;
729 prev = this, this = this->next)
730 if (strcasecmp (this->type, type) == 0)
731 {
732 if (prev == NULL)
733 complex_callback_head = this->next;
734 else
735 prev->next = this->next;
737 sfree (this->type);
738 sfree (this);
739 break;
740 }
741 } /* void cf_unregister */
743 void cf_register (const char *type,
744 int (*callback) (const char *, const char *),
745 const char **keys, int keys_num)
746 {
747 cf_callback_t *cf_cb;
749 /* Remove this module from the list, if it already exists */
750 cf_unregister (type);
752 /* This pointer will be free'd in `cf_unregister' */
753 if ((cf_cb = (cf_callback_t *) malloc (sizeof (cf_callback_t))) == NULL)
754 return;
756 cf_cb->type = type;
757 cf_cb->callback = callback;
758 cf_cb->keys = keys;
759 cf_cb->keys_num = keys_num;
761 cf_cb->next = first_callback;
762 first_callback = cf_cb;
763 } /* void cf_register */
765 int cf_register_complex (const char *type, int (*callback) (oconfig_item_t *))
766 {
767 cf_complex_callback_t *new;
769 new = (cf_complex_callback_t *) malloc (sizeof (cf_complex_callback_t));
770 if (new == NULL)
771 return (-1);
773 new->type = strdup (type);
774 if (new->type == NULL)
775 {
776 sfree (new);
777 return (-1);
778 }
780 new->callback = callback;
781 new->next = NULL;
783 if (complex_callback_head == NULL)
784 {
785 complex_callback_head = new;
786 }
787 else
788 {
789 cf_complex_callback_t *last = complex_callback_head;
790 while (last->next != NULL)
791 last = last->next;
792 last->next = new;
793 }
795 return (0);
796 } /* int cf_register_complex */
798 int cf_read (char *filename)
799 {
800 oconfig_item_t *conf;
801 int i;
803 conf = cf_read_generic (filename, 0 /* depth */);
804 if (conf == NULL)
805 {
806 ERROR ("Unable to read config file %s.", filename);
807 return (-1);
808 }
810 for (i = 0; i < conf->children_num; i++)
811 {
812 if (conf->children[i].children == NULL)
813 dispatch_value (conf->children + i);
814 else
815 dispatch_block (conf->children + i);
816 }
818 if (cf_default_typesdb)
819 read_types_list (PLUGINDIR"/types.db"); /* FIXME: Configure path */
820 return (0);
821 } /* int cf_read */