3b6cb6b3a1b93505d994a91181bcf6e0e6d9b08b
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 #define ESCAPE_NULL(str) ((str) == NULL ? "(null)" : (str))
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;
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;
54 typedef struct cf_value_map_s
55 {
56 char *key;
57 int (*func) (const oconfig_item_t *);
58 } cf_value_map_t;
60 typedef struct cf_global_option_s
61 {
62 char *key;
63 char *value;
64 char *def;
65 } cf_global_option_t;
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);
74 /*
75 * Private variables
76 */
77 static cf_callback_t *first_callback = NULL;
78 static cf_complex_callback_t *complex_callback_head = NULL;
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);
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);
99 static int cf_default_typesdb = 1;
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;
109 if (type == NULL)
110 return (NULL);
112 for (cf_cb = first_callback; cf_cb != NULL; cf_cb = cf_cb->next)
113 if (strcasecmp (cf_cb->type, type) == 0)
114 break;
116 return (cf_cb);
117 }
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;
128 DEBUG ("type = %s, key = %s, value = %s",
129 ESCAPE_NULL(type),
130 ESCAPE_NULL(orig_key),
131 ESCAPE_NULL(orig_value));
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 }
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 }
149 ret = -1;
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 }
160 if (i >= cf_cb->keys_num)
161 WARNING ("Plugin `%s' did not register for value `%s'.", type, key);
163 free (key);
164 free (value);
166 DEBUG ("return (%i)", ret);
168 return (ret);
169 } /* int cf_dispatch */
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 }
192 return (-1);
193 } /* int dispatch_global_option */
195 static int dispatch_value_typesdb (const oconfig_item_t *ci)
196 {
197 int i = 0;
199 assert (strcasecmp (ci->key, "TypesDB") == 0);
201 cf_default_typesdb = 0;
203 if (ci->values_num < 1)
204 return (-1);
206 for (i = 0; i < ci->values_num; ++i)
207 {
208 if (OCONFIG_TYPE_STRING != ci->values[i].type)
209 continue;
211 read_types_list (ci->values[i].value.string);
212 }
213 return (0);
214 } /* int dispatch_value_typesdb */
216 static int dispatch_value_plugindir (const oconfig_item_t *ci)
217 {
218 assert (strcasecmp (ci->key, "PluginDir") == 0);
220 if (ci->values_num != 1)
221 return (-1);
222 if (ci->values[0].type != OCONFIG_TYPE_STRING)
223 return (-1);
225 plugin_set_dir (ci->values[0].value.string);
226 return (0);
227 }
229 static int dispatch_value_loadplugin (const oconfig_item_t *ci)
230 {
231 assert (strcasecmp (ci->key, "LoadPlugin") == 0);
233 if (ci->values_num != 1)
234 return (-1);
235 if (ci->values[0].type != OCONFIG_TYPE_STRING)
236 return (-1);
238 return (plugin_load (ci->values[0].value.string));
239 } /* int dispatch_value_loadplugin */
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;
248 buffer_ptr = buffer;
249 buffer_free = sizeof (buffer);
251 for (i = 0; i < ci->values_num; i++)
252 {
253 int status = -1;
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");
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;
274 return (cf_dispatch (plugin, ci->key, buffer_ptr));
275 } /* int plugin_conf_dispatch */
277 static int dispatch_value (const oconfig_item_t *ci)
278 {
279 int ret = -2;
280 int i;
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 }
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 }
296 return (ret);
297 } /* int dispatch_value */
299 static int dispatch_block_plugin (oconfig_item_t *ci)
300 {
301 int i;
302 char *name;
304 cf_complex_callback_t *cb;
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);
313 name = ci->values[0].value.string;
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));
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 }
329 return (0);
330 }
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));
340 return (0);
341 }
343 static int cf_ci_replace_child (oconfig_item_t *dst, oconfig_item_t *src,
344 int offset)
345 {
346 oconfig_item_t *temp;
347 int i;
349 assert (offset >= 0);
350 assert (dst->children_num > offset);
352 /* Resize the memory containing the children to be big enough to hold
353 * all children. */
354 temp = (oconfig_item_t *) realloc (dst->children,
355 sizeof (oconfig_item_t)
356 * (dst->children_num + src->children_num - 1));
357 if (temp == NULL)
358 {
359 ERROR ("configfile: realloc failed.");
360 return (-1);
361 }
362 dst->children = temp;
364 /* Free the memory used by the replaced child. Usually that's the
365 * `Include "blah"' statement. */
366 temp = dst->children + offset;
367 for (i = 0; i < temp->values_num; i++)
368 {
369 if (temp->values[i].type == OCONFIG_TYPE_STRING)
370 {
371 sfree (temp->values[i].value.string);
372 }
373 }
374 sfree (temp->values);
375 temp = NULL;
377 /* If there are children behind the include statement, move them to the
378 * end of the list, so that the new children have room before them. */
379 if ((dst->children_num - (offset + 1)) > 0)
380 {
381 int nmemb = dst->children_num - (offset + 1);
382 int old_offset = offset + 1;
383 int new_offset = offset + src->children_num;
385 memmove (dst->children + new_offset,
386 dst->children + old_offset,
387 sizeof (oconfig_item_t) * nmemb);
388 }
390 /* Last but not least: If there are new childrem, copy them to the
391 * memory reserved for them. */
392 if (src->children_num > 0)
393 {
394 memcpy (dst->children + offset,
395 src->children,
396 sizeof (oconfig_item_t) * src->children_num);
397 }
399 /* Update the number of children. */
400 dst->children_num += (src->children_num - 1);
402 return (0);
403 } /* int cf_ci_replace_child */
405 static int cf_ci_append_children (oconfig_item_t *dst, oconfig_item_t *src)
406 {
407 oconfig_item_t *temp;
409 temp = (oconfig_item_t *) realloc (dst->children,
410 sizeof (oconfig_item_t)
411 * (dst->children_num + src->children_num));
412 if (temp == NULL)
413 {
414 ERROR ("configfile: realloc failed.");
415 return (-1);
416 }
417 dst->children = temp;
419 memcpy (dst->children + dst->children_num,
420 src->children,
421 sizeof (oconfig_item_t)
422 * src->children_num);
423 dst->children_num += src->children_num;
425 return (0);
426 } /* int cf_ci_append_children */
428 #define CF_MAX_DEPTH 8
429 static oconfig_item_t *cf_read_generic (const char *path, int depth);
431 static int cf_include_all (oconfig_item_t *root, int depth)
432 {
433 int i;
435 for (i = 0; i < root->children_num; i++)
436 {
437 oconfig_item_t *new;
438 oconfig_item_t *old;
440 /* Ignore all blocks, including `Include' blocks. */
441 if (root->children[i].children_num != 0)
442 continue;
444 if (strcasecmp (root->children[i].key, "Include") != 0)
445 continue;
447 old = root->children + i;
449 if ((old->values_num != 1)
450 || (old->values[0].type != OCONFIG_TYPE_STRING))
451 {
452 ERROR ("configfile: `Include' needs exactly one string argument.");
453 continue;
454 }
456 new = cf_read_generic (old->values[0].value.string, depth + 1);
457 if (new == NULL)
458 continue;
460 /* Now replace the i'th child in `root' with `new'. */
461 cf_ci_replace_child (root, new, i);
463 sfree (new->values);
464 sfree (new);
465 } /* for (i = 0; i < root->children_num; i++) */
467 return (0);
468 } /* int cf_include_all */
470 static oconfig_item_t *cf_read_file (const char *file, int depth)
471 {
472 oconfig_item_t *root;
474 assert (depth < CF_MAX_DEPTH);
476 root = oconfig_parse_file (file);
477 if (root == NULL)
478 {
479 ERROR ("configfile: Cannot read file `%s'.", file);
480 return (NULL);
481 }
483 cf_include_all (root, depth);
485 return (root);
486 } /* oconfig_item_t *cf_read_file */
488 static oconfig_item_t *cf_read_dir (const char *dir, int depth)
489 {
490 oconfig_item_t *root = NULL;
491 DIR *dh;
492 struct dirent *de;
493 char name[1024];
494 int status;
496 assert (depth < CF_MAX_DEPTH);
498 dh = opendir (dir);
499 if (dh == NULL)
500 {
501 char errbuf[1024];
502 ERROR ("configfile: opendir failed: %s",
503 sstrerror (errno, errbuf, sizeof (errbuf)));
504 return (NULL);
505 }
507 root = (oconfig_item_t *) malloc (sizeof (oconfig_item_t));
508 if (root == NULL)
509 {
510 ERROR ("configfile: malloc failed.");
511 return (NULL);
512 }
513 memset (root, '\0', sizeof (oconfig_item_t));
515 while ((de = readdir (dh)) != NULL)
516 {
517 oconfig_item_t *temp;
519 if ((de->d_name[0] == '.') || (de->d_name[0] == '\0'))
520 continue;
522 status = snprintf (name, sizeof (name), "%s/%s",
523 dir, de->d_name);
524 if (status >= sizeof (name))
525 {
526 ERROR ("configfile: Not including `%s/%s' because its"
527 " name is too long.",
528 dir, de->d_name);
529 continue;
530 }
532 temp = cf_read_generic (name, depth);
533 if (temp == NULL)
534 continue;
536 cf_ci_append_children (root, temp);
537 sfree (temp->children);
538 sfree (temp);
539 }
541 return (root);
542 } /* oconfig_item_t *cf_read_dir */
544 /*
545 * cf_read_generic
546 *
547 * Path is stat'ed and either cf_read_file or cf_read_dir is called
548 * accordingly.
549 */
550 static oconfig_item_t *cf_read_generic (const char *path, int depth)
551 {
552 struct stat statbuf;
553 int status;
555 if (depth >= CF_MAX_DEPTH)
556 {
557 ERROR ("configfile: Not including `%s' because the maximum "
558 "nesting depth has been reached.", path);
559 return (NULL);
560 }
562 fprintf (stderr, "cf_read_generic (path = %s, depth = %i);", path, depth);
564 status = stat (path, &statbuf);
565 if (status != 0)
566 {
567 char errbuf[1024];
568 ERROR ("configfile: stat (%s) failed: %s",
569 path,
570 sstrerror (errno, errbuf, sizeof (errbuf)));
571 return (NULL);
572 }
574 if (S_ISREG (statbuf.st_mode))
575 return (cf_read_file (path, depth));
576 else if (S_ISDIR (statbuf.st_mode))
577 return (cf_read_dir (path, depth));
579 ERROR ("configfile: %s is neither a file nor a directory.", path);
580 return (NULL);
581 } /* oconfig_item_t *cf_read_generic */
583 /*
584 * Public functions
585 */
586 int global_option_set (const char *option, const char *value)
587 {
588 int i;
590 DEBUG ("option = %s; value = %s;", option, value);
592 for (i = 0; i < cf_global_options_num; i++)
593 if (strcasecmp (cf_global_options[i].key, option) == 0)
594 break;
596 if (i >= cf_global_options_num)
597 return (-1);
599 sfree (cf_global_options[i].value);
601 if (value != NULL)
602 cf_global_options[i].value = strdup (value);
603 else
604 cf_global_options[i].value = NULL;
606 return (0);
607 }
609 const char *global_option_get (const char *option)
610 {
611 int i;
613 for (i = 0; i < cf_global_options_num; i++)
614 if (strcasecmp (cf_global_options[i].key, option) == 0)
615 break;
617 if (i >= cf_global_options_num)
618 return (NULL);
620 return ((cf_global_options[i].value != NULL)
621 ? cf_global_options[i].value
622 : cf_global_options[i].def);
623 } /* char *global_option_get */
625 void cf_unregister (const char *type)
626 {
627 cf_callback_t *this, *prev;
629 for (prev = NULL, this = first_callback;
630 this != NULL;
631 prev = this, this = this->next)
632 if (strcasecmp (this->type, type) == 0)
633 {
634 if (prev == NULL)
635 first_callback = this->next;
636 else
637 prev->next = this->next;
639 free (this);
640 break;
641 }
642 } /* void cf_unregister */
644 void cf_unregister_complex (const char *type)
645 {
646 cf_complex_callback_t *this, *prev;
648 for (prev = NULL, this = complex_callback_head;
649 this != NULL;
650 prev = this, this = this->next)
651 if (strcasecmp (this->type, type) == 0)
652 {
653 if (prev == NULL)
654 complex_callback_head = this->next;
655 else
656 prev->next = this->next;
658 sfree (this->type);
659 sfree (this);
660 break;
661 }
662 } /* void cf_unregister */
664 void cf_register (const char *type,
665 int (*callback) (const char *, const char *),
666 const char **keys, int keys_num)
667 {
668 cf_callback_t *cf_cb;
670 /* Remove this module from the list, if it already exists */
671 cf_unregister (type);
673 /* This pointer will be free'd in `cf_unregister' */
674 if ((cf_cb = (cf_callback_t *) malloc (sizeof (cf_callback_t))) == NULL)
675 return;
677 cf_cb->type = type;
678 cf_cb->callback = callback;
679 cf_cb->keys = keys;
680 cf_cb->keys_num = keys_num;
682 cf_cb->next = first_callback;
683 first_callback = cf_cb;
684 } /* void cf_register */
686 int cf_register_complex (const char *type, int (*callback) (oconfig_item_t *))
687 {
688 cf_complex_callback_t *new;
690 new = (cf_complex_callback_t *) malloc (sizeof (cf_complex_callback_t));
691 if (new == NULL)
692 return (-1);
694 new->type = strdup (type);
695 if (new->type == NULL)
696 {
697 sfree (new);
698 return (-1);
699 }
701 new->callback = callback;
702 new->next = NULL;
704 if (complex_callback_head == NULL)
705 {
706 complex_callback_head = new;
707 }
708 else
709 {
710 cf_complex_callback_t *last = complex_callback_head;
711 while (last->next != NULL)
712 last = last->next;
713 last->next = new;
714 }
716 return (0);
717 } /* int cf_register_complex */
719 int cf_read (char *filename)
720 {
721 oconfig_item_t *conf;
722 int i;
724 conf = cf_read_generic (filename, 0 /* depth */);
725 if (conf == NULL)
726 {
727 ERROR ("Unable to read config file %s.", filename);
728 return (-1);
729 }
731 for (i = 0; i < conf->children_num; i++)
732 {
733 if (conf->children[i].children == NULL)
734 dispatch_value (conf->children + i);
735 else
736 dispatch_block (conf->children + i);
737 }
739 if (cf_default_typesdb)
740 read_types_list (PLUGINDIR"/types.db"); /* FIXME: Configure path */
741 return (0);
742 } /* int cf_read */