1 /**
2 * collectd - src/configfile.c
3 * Copyright (C) 2005-2011 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 collectd.org>
21 * Sebastian tokkee Harl <sh at tokkee.org>
22 **/
24 #include "collectd.h"
26 #include "liboconfig/oconfig.h"
28 #include "common.h"
29 #include "plugin.h"
30 #include "configfile.h"
31 #include "types_list.h"
32 #include "filter_chain.h"
34 #if HAVE_WORDEXP_H
35 # include <wordexp.h>
36 #endif /* HAVE_WORDEXP_H */
38 #if HAVE_FNMATCH_H
39 # include <fnmatch.h>
40 #endif /* HAVE_FNMATCH_H */
42 #if HAVE_LIBGEN_H
43 # include <libgen.h>
44 #endif /* HAVE_LIBGEN_H */
46 #define ESCAPE_NULL(str) ((str) == NULL ? "(null)" : (str))
48 /*
49 * Private types
50 */
51 typedef struct cf_callback
52 {
53 const char *type;
54 int (*callback) (const char *, const char *);
55 const char **keys;
56 int keys_num;
57 plugin_ctx_t ctx;
58 struct cf_callback *next;
59 } cf_callback_t;
61 typedef struct cf_complex_callback_s
62 {
63 char *type;
64 int (*callback) (oconfig_item_t *);
65 plugin_ctx_t ctx;
66 struct cf_complex_callback_s *next;
67 } cf_complex_callback_t;
69 typedef struct cf_value_map_s
70 {
71 char *key;
72 int (*func) (const oconfig_item_t *);
73 } cf_value_map_t;
75 typedef struct cf_global_option_s
76 {
77 char *key;
78 char *value;
79 char *def;
80 } cf_global_option_t;
82 /*
83 * Prototypes of callback functions
84 */
85 static int dispatch_value_typesdb (const oconfig_item_t *ci);
86 static int dispatch_value_plugindir (const oconfig_item_t *ci);
87 static int dispatch_loadplugin (const oconfig_item_t *ci);
89 /*
90 * Private variables
91 */
92 static cf_callback_t *first_callback = NULL;
93 static cf_complex_callback_t *complex_callback_head = NULL;
95 static cf_value_map_t cf_value_map[] =
96 {
97 {"TypesDB", dispatch_value_typesdb},
98 {"PluginDir", dispatch_value_plugindir},
99 {"LoadPlugin", dispatch_loadplugin}
100 };
101 static int cf_value_map_num = STATIC_ARRAY_SIZE (cf_value_map);
103 static cf_global_option_t cf_global_options[] =
104 {
105 {"BaseDir", NULL, PKGLOCALSTATEDIR},
106 {"PIDFile", NULL, PIDFILE},
107 {"Hostname", NULL, NULL},
108 {"FQDNLookup", NULL, "true"},
109 {"Interval", NULL, NULL},
110 {"ReadThreads", NULL, "5"},
111 {"WriteThreads", NULL, "5"},
112 {"Timeout", NULL, "2"},
113 {"PreCacheChain", NULL, "PreCache"},
114 {"PostCacheChain", NULL, "PostCache"}
115 };
116 static int cf_global_options_num = STATIC_ARRAY_SIZE (cf_global_options);
118 static int cf_default_typesdb = 1;
120 /*
121 * Functions to handle register/unregister, search, and other plugin related
122 * stuff
123 */
124 static cf_callback_t *cf_search (const char *type)
125 {
126 cf_callback_t *cf_cb;
128 if (type == NULL)
129 return (NULL);
131 for (cf_cb = first_callback; cf_cb != NULL; cf_cb = cf_cb->next)
132 if (strcasecmp (cf_cb->type, type) == 0)
133 break;
135 return (cf_cb);
136 }
138 static int cf_dispatch (const char *type, const char *orig_key,
139 const char *orig_value)
140 {
141 cf_callback_t *cf_cb;
142 plugin_ctx_t old_ctx;
143 char *key;
144 char *value;
145 int ret;
146 int i;
148 DEBUG ("type = %s, key = %s, value = %s",
149 ESCAPE_NULL(type),
150 ESCAPE_NULL(orig_key),
151 ESCAPE_NULL(orig_value));
153 if ((cf_cb = cf_search (type)) == NULL)
154 {
155 WARNING ("Found a configuration for the `%s' plugin, but "
156 "the plugin isn't loaded or didn't register "
157 "a configuration callback.", type);
158 return (-1);
159 }
161 if ((key = strdup (orig_key)) == NULL)
162 return (1);
163 if ((value = strdup (orig_value)) == NULL)
164 {
165 free (key);
166 return (2);
167 }
169 ret = -1;
171 old_ctx = plugin_set_ctx (cf_cb->ctx);
173 for (i = 0; i < cf_cb->keys_num; i++)
174 {
175 if ((cf_cb->keys[i] != NULL)
176 && (strcasecmp (cf_cb->keys[i], key) == 0))
177 {
178 ret = (*cf_cb->callback) (key, value);
179 break;
180 }
181 }
183 plugin_set_ctx (old_ctx);
185 if (i >= cf_cb->keys_num)
186 WARNING ("Plugin `%s' did not register for value `%s'.", type, key);
188 free (key);
189 free (value);
191 DEBUG ("cf_dispatch: return (%i)", ret);
193 return (ret);
194 } /* int cf_dispatch */
196 static int dispatch_global_option (const oconfig_item_t *ci)
197 {
198 if (ci->values_num != 1)
199 return (-1);
200 if (ci->values[0].type == OCONFIG_TYPE_STRING)
201 return (global_option_set (ci->key, ci->values[0].value.string));
202 else if (ci->values[0].type == OCONFIG_TYPE_NUMBER)
203 {
204 char tmp[128];
205 ssnprintf (tmp, sizeof (tmp), "%lf", ci->values[0].value.number);
206 return (global_option_set (ci->key, tmp));
207 }
208 else if (ci->values[0].type == OCONFIG_TYPE_BOOLEAN)
209 {
210 if (ci->values[0].value.boolean)
211 return (global_option_set (ci->key, "true"));
212 else
213 return (global_option_set (ci->key, "false"));
214 }
216 return (-1);
217 } /* int dispatch_global_option */
219 static int dispatch_value_typesdb (const oconfig_item_t *ci)
220 {
221 int i = 0;
223 assert (strcasecmp (ci->key, "TypesDB") == 0);
225 cf_default_typesdb = 0;
227 if (ci->values_num < 1) {
228 ERROR ("configfile: `TypesDB' needs at least one argument.");
229 return (-1);
230 }
232 for (i = 0; i < ci->values_num; ++i)
233 {
234 if (OCONFIG_TYPE_STRING != ci->values[i].type) {
235 WARNING ("configfile: TypesDB: Skipping %i. argument which "
236 "is not a string.", i + 1);
237 continue;
238 }
240 read_types_list (ci->values[i].value.string);
241 }
242 return (0);
243 } /* int dispatch_value_typesdb */
245 static int dispatch_value_plugindir (const oconfig_item_t *ci)
246 {
247 assert (strcasecmp (ci->key, "PluginDir") == 0);
249 if (ci->values_num != 1)
250 return (-1);
251 if (ci->values[0].type != OCONFIG_TYPE_STRING)
252 return (-1);
254 plugin_set_dir (ci->values[0].value.string);
255 return (0);
256 }
258 static int dispatch_loadplugin (const oconfig_item_t *ci)
259 {
260 int i;
261 const char *name;
262 unsigned int flags = 0;
263 plugin_ctx_t ctx;
264 plugin_ctx_t old_ctx;
265 int ret_val;
267 assert (strcasecmp (ci->key, "LoadPlugin") == 0);
269 if (ci->values_num != 1)
270 return (-1);
271 if (ci->values[0].type != OCONFIG_TYPE_STRING)
272 return (-1);
274 name = ci->values[0].value.string;
276 /* default to the global interval set before loading this plugin */
277 memset (&ctx, 0, sizeof (ctx));
278 ctx.interval = cf_get_default_interval ();
280 for (i = 0; i < ci->children_num; ++i) {
281 if (strcasecmp("Globals", ci->children[i].key) == 0)
282 cf_util_get_flag (ci->children + i, &flags, PLUGIN_FLAGS_GLOBAL);
283 else if (strcasecmp ("Interval", ci->children[i].key) == 0) {
284 double interval = 0.0;
286 if (cf_util_get_double (ci->children + i, &interval) != 0) {
287 /* cf_util_get_double will log an error */
288 continue;
289 }
291 ctx.interval = DOUBLE_TO_CDTIME_T (interval);
292 }
293 else {
294 WARNING("Ignoring unknown LoadPlugin option \"%s\" "
295 "for plugin \"%s\"",
296 ci->children[i].key, ci->values[0].value.string);
297 }
298 }
300 old_ctx = plugin_set_ctx (ctx);
301 ret_val = plugin_load (name, (uint32_t) flags);
302 /* reset to the "global" context */
303 plugin_set_ctx (old_ctx);
305 return (ret_val);
306 } /* int dispatch_value_loadplugin */
308 static int dispatch_value_plugin (const char *plugin, oconfig_item_t *ci)
309 {
310 char buffer[4096];
311 char *buffer_ptr;
312 int buffer_free;
313 int i;
315 buffer_ptr = buffer;
316 buffer_free = sizeof (buffer);
318 for (i = 0; i < ci->values_num; i++)
319 {
320 int status = -1;
322 if (ci->values[i].type == OCONFIG_TYPE_STRING)
323 status = ssnprintf (buffer_ptr, buffer_free, " %s",
324 ci->values[i].value.string);
325 else if (ci->values[i].type == OCONFIG_TYPE_NUMBER)
326 status = ssnprintf (buffer_ptr, buffer_free, " %lf",
327 ci->values[i].value.number);
328 else if (ci->values[i].type == OCONFIG_TYPE_BOOLEAN)
329 status = ssnprintf (buffer_ptr, buffer_free, " %s",
330 ci->values[i].value.boolean
331 ? "true" : "false");
333 if ((status < 0) || (status >= buffer_free))
334 return (-1);
335 buffer_free -= status;
336 buffer_ptr += status;
337 }
338 /* skip the initial space */
339 buffer_ptr = buffer + 1;
341 return (cf_dispatch (plugin, ci->key, buffer_ptr));
342 } /* int dispatch_value_plugin */
344 static int dispatch_value (const oconfig_item_t *ci)
345 {
346 int ret = -2;
347 int i;
349 for (i = 0; i < cf_value_map_num; i++)
350 if (strcasecmp (cf_value_map[i].key, ci->key) == 0)
351 {
352 ret = cf_value_map[i].func (ci);
353 break;
354 }
356 for (i = 0; i < cf_global_options_num; i++)
357 if (strcasecmp (cf_global_options[i].key, ci->key) == 0)
358 {
359 ret = dispatch_global_option (ci);
360 break;
361 }
363 return (ret);
364 } /* int dispatch_value */
366 static int dispatch_block_plugin (oconfig_item_t *ci)
367 {
368 int i;
369 char *name;
371 cf_complex_callback_t *cb;
373 if (strcasecmp (ci->key, "Plugin") != 0)
374 return (-1);
375 if (ci->values_num < 1)
376 return (-1);
377 if (ci->values[0].type != OCONFIG_TYPE_STRING)
378 return (-1);
380 name = ci->values[0].value.string;
382 /* Check for a complex callback first */
383 for (cb = complex_callback_head; cb != NULL; cb = cb->next)
384 {
385 if (strcasecmp (name, cb->type) == 0)
386 {
387 plugin_ctx_t old_ctx;
388 int ret_val;
390 old_ctx = plugin_set_ctx (cb->ctx);
391 ret_val = (cb->callback (ci));
392 plugin_set_ctx (old_ctx);
393 return (ret_val);
394 }
395 }
397 /* Hm, no complex plugin found. Dispatch the values one by one */
398 for (i = 0; i < ci->children_num; i++)
399 {
400 if (ci->children[i].children == NULL)
401 dispatch_value_plugin (name, ci->children + i);
402 else
403 {
404 WARNING ("There is a `%s' block within the "
405 "configuration for the %s plugin. "
406 "The plugin either only expects "
407 "\"simple\" configuration statements "
408 "or wasn't loaded using `LoadPlugin'."
409 " Please check your configuration.",
410 ci->children[i].key, name);
411 }
412 }
414 return (0);
415 }
418 static int dispatch_block (oconfig_item_t *ci)
419 {
420 if (strcasecmp (ci->key, "LoadPlugin") == 0)
421 return (dispatch_loadplugin (ci));
422 else if (strcasecmp (ci->key, "Plugin") == 0)
423 return (dispatch_block_plugin (ci));
424 else if (strcasecmp (ci->key, "Chain") == 0)
425 return (fc_configure (ci));
427 return (0);
428 }
430 static int cf_ci_replace_child (oconfig_item_t *dst, oconfig_item_t *src,
431 int offset)
432 {
433 oconfig_item_t *temp;
434 int i;
436 assert (offset >= 0);
437 assert (dst->children_num > offset);
439 /* Free the memory used by the replaced child. Usually that's the
440 * `Include "blah"' statement. */
441 temp = dst->children + offset;
442 for (i = 0; i < temp->values_num; i++)
443 {
444 if (temp->values[i].type == OCONFIG_TYPE_STRING)
445 {
446 sfree (temp->values[i].value.string);
447 }
448 }
449 sfree (temp->values);
450 temp = NULL;
452 /* If (src->children_num == 0) the array size is decreased. If offset
453 * is _not_ the last element, (offset < (dst->children_num - 1)), then
454 * we need to move the trailing elements before resizing the array. */
455 if ((src->children_num == 0) && (offset < (dst->children_num - 1)))
456 {
457 int nmemb = dst->children_num - (offset + 1);
458 memmove (dst->children + offset, dst->children + offset + 1,
459 sizeof (oconfig_item_t) * nmemb);
460 }
462 /* Resize the memory containing the children to be big enough to hold
463 * all children. */
464 temp = (oconfig_item_t *) realloc (dst->children,
465 sizeof (oconfig_item_t)
466 * (dst->children_num + src->children_num - 1));
467 if (temp == NULL)
468 {
469 ERROR ("configfile: realloc failed.");
470 return (-1);
471 }
472 dst->children = temp;
474 /* If there are children behind the include statement, and they have
475 * not yet been moved because (src->children_num == 0), then move them
476 * to the end of the list, so that the new children have room before
477 * them. */
478 if ((src->children_num > 0)
479 && ((dst->children_num - (offset + 1)) > 0))
480 {
481 int nmemb = dst->children_num - (offset + 1);
482 int old_offset = offset + 1;
483 int new_offset = offset + src->children_num;
485 memmove (dst->children + new_offset,
486 dst->children + old_offset,
487 sizeof (oconfig_item_t) * nmemb);
488 }
490 /* Last but not least: If there are new children, copy them to the
491 * memory reserved for them. */
492 if (src->children_num > 0)
493 {
494 memcpy (dst->children + offset,
495 src->children,
496 sizeof (oconfig_item_t) * src->children_num);
497 }
499 /* Update the number of children. */
500 dst->children_num += (src->children_num - 1);
502 return (0);
503 } /* int cf_ci_replace_child */
505 static int cf_ci_append_children (oconfig_item_t *dst, oconfig_item_t *src)
506 {
507 oconfig_item_t *temp;
509 if ((src == NULL) || (src->children_num == 0))
510 return (0);
512 temp = (oconfig_item_t *) realloc (dst->children,
513 sizeof (oconfig_item_t)
514 * (dst->children_num + src->children_num));
515 if (temp == NULL)
516 {
517 ERROR ("configfile: realloc failed.");
518 return (-1);
519 }
520 dst->children = temp;
522 memcpy (dst->children + dst->children_num,
523 src->children,
524 sizeof (oconfig_item_t)
525 * src->children_num);
526 dst->children_num += src->children_num;
528 return (0);
529 } /* int cf_ci_append_children */
531 #define CF_MAX_DEPTH 8
532 static oconfig_item_t *cf_read_generic (const char *path,
533 const char *pattern, int depth);
535 static int cf_include_all (oconfig_item_t *root, int depth)
536 {
537 int i;
539 for (i = 0; i < root->children_num; i++)
540 {
541 oconfig_item_t *new;
542 oconfig_item_t *old;
544 char *pattern = NULL;
546 int j;
548 if (strcasecmp (root->children[i].key, "Include") != 0)
549 continue;
551 old = root->children + i;
553 if ((old->values_num != 1)
554 || (old->values[0].type != OCONFIG_TYPE_STRING))
555 {
556 ERROR ("configfile: `Include' needs exactly one string argument.");
557 continue;
558 }
560 for (j = 0; j < old->children_num; ++j)
561 {
562 oconfig_item_t *child = old->children + j;
564 if (strcasecmp (child->key, "Filter") == 0)
565 cf_util_get_string (child, &pattern);
566 else
567 ERROR ("configfile: Option `%s' not allowed in <Include> block.",
568 child->key);
569 }
571 new = cf_read_generic (old->values[0].value.string, pattern, depth + 1);
572 sfree (pattern);
574 if (new == NULL)
575 continue;
577 /* Now replace the i'th child in `root' with `new'. */
578 cf_ci_replace_child (root, new, i);
580 /* ... and go back to the new i'th child. */
581 --i;
583 sfree (new->values);
584 sfree (new);
585 } /* for (i = 0; i < root->children_num; i++) */
587 return (0);
588 } /* int cf_include_all */
590 static oconfig_item_t *cf_read_file (const char *file,
591 const char *pattern, int depth)
592 {
593 oconfig_item_t *root;
595 assert (depth < CF_MAX_DEPTH);
597 if (pattern != NULL) {
598 #if HAVE_FNMATCH_H && HAVE_LIBGEN_H
599 char *tmp = sstrdup (file);
600 char *filename = basename (tmp);
602 if ((filename != NULL) && (fnmatch (pattern, filename, 0) != 0)) {
603 DEBUG ("configfile: Not including `%s' because it "
604 "does not match pattern `%s'.",
605 filename, pattern);
606 free (tmp);
607 return (NULL);
608 }
610 free (tmp);
611 #else
612 ERROR ("configfile: Cannot apply pattern filter '%s' "
613 "to file '%s': functions basename() and / or "
614 "fnmatch() not available.", pattern, file);
615 #endif /* HAVE_FNMATCH_H && HAVE_LIBGEN_H */
616 }
618 root = oconfig_parse_file (file);
619 if (root == NULL)
620 {
621 ERROR ("configfile: Cannot read file `%s'.", file);
622 return (NULL);
623 }
625 cf_include_all (root, depth);
627 return (root);
628 } /* oconfig_item_t *cf_read_file */
630 static int cf_compare_string (const void *p1, const void *p2)
631 {
632 return strcmp (*(const char **) p1, *(const char **) p2);
633 }
635 static oconfig_item_t *cf_read_dir (const char *dir,
636 const char *pattern, int depth)
637 {
638 oconfig_item_t *root = NULL;
639 DIR *dh;
640 struct dirent *de;
641 char **filenames = NULL;
642 int filenames_num = 0;
643 int status;
644 int i;
646 assert (depth < CF_MAX_DEPTH);
648 dh = opendir (dir);
649 if (dh == NULL)
650 {
651 char errbuf[1024];
652 ERROR ("configfile: opendir failed: %s",
653 sstrerror (errno, errbuf, sizeof (errbuf)));
654 return (NULL);
655 }
657 root = (oconfig_item_t *) malloc (sizeof (oconfig_item_t));
658 if (root == NULL)
659 {
660 ERROR ("configfile: malloc failed.");
661 return (NULL);
662 }
663 memset (root, 0, sizeof (oconfig_item_t));
665 while ((de = readdir (dh)) != NULL)
666 {
667 char name[1024];
668 char **tmp;
670 if ((de->d_name[0] == '.') || (de->d_name[0] == 0))
671 continue;
673 status = ssnprintf (name, sizeof (name), "%s/%s",
674 dir, de->d_name);
675 if ((status < 0) || ((size_t) status >= sizeof (name)))
676 {
677 ERROR ("configfile: Not including `%s/%s' because its"
678 " name is too long.",
679 dir, de->d_name);
680 for (i = 0; i < filenames_num; ++i)
681 free (filenames[i]);
682 free (filenames);
683 free (root);
684 return (NULL);
685 }
687 ++filenames_num;
688 tmp = (char **) realloc (filenames,
689 filenames_num * sizeof (*filenames));
690 if (tmp == NULL) {
691 ERROR ("configfile: realloc failed.");
692 for (i = 0; i < filenames_num - 1; ++i)
693 free (filenames[i]);
694 free (filenames);
695 free (root);
696 return (NULL);
697 }
698 filenames = tmp;
700 filenames[filenames_num - 1] = sstrdup (name);
701 }
703 qsort ((void *) filenames, filenames_num, sizeof (*filenames),
704 cf_compare_string);
706 for (i = 0; i < filenames_num; ++i)
707 {
708 oconfig_item_t *temp;
709 char *name = filenames[i];
711 temp = cf_read_generic (name, pattern, depth);
712 if (temp == NULL)
713 {
714 /* An error should already have been reported. */
715 sfree (name);
716 continue;
717 }
719 cf_ci_append_children (root, temp);
720 sfree (temp->children);
721 sfree (temp);
723 free (name);
724 }
726 free(filenames);
727 return (root);
728 } /* oconfig_item_t *cf_read_dir */
730 /*
731 * cf_read_generic
732 *
733 * Path is stat'ed and either cf_read_file or cf_read_dir is called
734 * accordingly.
735 *
736 * There are two versions of this function: If `wordexp' exists shell wildcards
737 * will be expanded and the function will include all matches found. If
738 * `wordexp' (or, more precisely, it's header file) is not available the
739 * simpler function is used which does not do any such expansion.
740 */
741 #if HAVE_WORDEXP_H
742 static oconfig_item_t *cf_read_generic (const char *path,
743 const char *pattern, int depth)
744 {
745 oconfig_item_t *root = NULL;
746 int status;
747 const char *path_ptr;
748 wordexp_t we;
749 size_t i;
751 if (depth >= CF_MAX_DEPTH)
752 {
753 ERROR ("configfile: Not including `%s' because the maximum "
754 "nesting depth has been reached.", path);
755 return (NULL);
756 }
758 status = wordexp (path, &we, WRDE_NOCMD);
759 if (status != 0)
760 {
761 ERROR ("configfile: wordexp (%s) failed.", path);
762 return (NULL);
763 }
765 root = (oconfig_item_t *) malloc (sizeof (oconfig_item_t));
766 if (root == NULL)
767 {
768 ERROR ("configfile: malloc failed.");
769 return (NULL);
770 }
771 memset (root, '\0', sizeof (oconfig_item_t));
773 /* wordexp() might return a sorted list already. That's not
774 * documented though, so let's make sure we get what we want. */
775 qsort ((void *) we.we_wordv, we.we_wordc, sizeof (*we.we_wordv),
776 cf_compare_string);
778 for (i = 0; i < we.we_wordc; i++)
779 {
780 oconfig_item_t *temp;
781 struct stat statbuf;
783 path_ptr = we.we_wordv[i];
785 status = stat (path_ptr, &statbuf);
786 if (status != 0)
787 {
788 char errbuf[1024];
789 WARNING ("configfile: stat (%s) failed: %s",
790 path_ptr,
791 sstrerror (errno, errbuf, sizeof (errbuf)));
792 continue;
793 }
795 if (S_ISREG (statbuf.st_mode))
796 temp = cf_read_file (path_ptr, pattern, depth);
797 else if (S_ISDIR (statbuf.st_mode))
798 temp = cf_read_dir (path_ptr, pattern, depth);
799 else
800 {
801 WARNING ("configfile: %s is neither a file nor a "
802 "directory.", path);
803 continue;
804 }
806 if (temp == NULL) {
807 oconfig_free (root);
808 return (NULL);
809 }
811 cf_ci_append_children (root, temp);
812 sfree (temp->children);
813 sfree (temp);
814 }
816 wordfree (&we);
818 if (root->children == NULL)
819 {
820 oconfig_free (root);
821 return (NULL);
822 }
824 return (root);
825 } /* oconfig_item_t *cf_read_generic */
826 /* #endif HAVE_WORDEXP_H */
828 #else /* if !HAVE_WORDEXP_H */
829 static oconfig_item_t *cf_read_generic (const char *path,
830 const char *pattern, int depth)
831 {
832 struct stat statbuf;
833 int status;
835 if (depth >= CF_MAX_DEPTH)
836 {
837 ERROR ("configfile: Not including `%s' because the maximum "
838 "nesting depth has been reached.", path);
839 return (NULL);
840 }
842 status = stat (path, &statbuf);
843 if (status != 0)
844 {
845 char errbuf[1024];
846 ERROR ("configfile: stat (%s) failed: %s",
847 path,
848 sstrerror (errno, errbuf, sizeof (errbuf)));
849 return (NULL);
850 }
852 if (S_ISREG (statbuf.st_mode))
853 return (cf_read_file (path, pattern, depth));
854 else if (S_ISDIR (statbuf.st_mode))
855 return (cf_read_dir (path, pattern, depth));
857 ERROR ("configfile: %s is neither a file nor a directory.", path);
858 return (NULL);
859 } /* oconfig_item_t *cf_read_generic */
860 #endif /* !HAVE_WORDEXP_H */
862 /*
863 * Public functions
864 */
865 int global_option_set (const char *option, const char *value)
866 {
867 int i;
869 DEBUG ("option = %s; value = %s;", option, value);
871 for (i = 0; i < cf_global_options_num; i++)
872 if (strcasecmp (cf_global_options[i].key, option) == 0)
873 break;
875 if (i >= cf_global_options_num)
876 return (-1);
878 sfree (cf_global_options[i].value);
880 if (value != NULL)
881 cf_global_options[i].value = strdup (value);
882 else
883 cf_global_options[i].value = NULL;
885 return (0);
886 }
888 const char *global_option_get (const char *option)
889 {
890 int i;
892 for (i = 0; i < cf_global_options_num; i++)
893 if (strcasecmp (cf_global_options[i].key, option) == 0)
894 break;
896 if (i >= cf_global_options_num)
897 return (NULL);
899 return ((cf_global_options[i].value != NULL)
900 ? cf_global_options[i].value
901 : cf_global_options[i].def);
902 } /* char *global_option_get */
904 cdtime_t cf_get_default_interval (void)
905 {
906 char const *str = global_option_get ("Interval");
907 double interval_double = COLLECTD_DEFAULT_INTERVAL;
909 if (str != NULL)
910 {
911 char *endptr = NULL;
912 double tmp = strtod (str, &endptr);
914 if ((endptr == NULL) || (endptr == str) || (*endptr != 0))
915 ERROR ("cf_get_default_interval: Unable to parse string \"%s\" "
916 "as number.", str);
917 else if (tmp <= 0.0)
918 ERROR ("cf_get_default_interval: Interval must be a positive number. "
919 "The current number is %g.", tmp);
920 else
921 interval_double = tmp;
922 }
924 return (DOUBLE_TO_CDTIME_T (interval_double));
925 } /* }}} cdtime_t cf_get_default_interval */
927 void cf_unregister (const char *type)
928 {
929 cf_callback_t *this, *prev;
931 for (prev = NULL, this = first_callback;
932 this != NULL;
933 prev = this, this = this->next)
934 if (strcasecmp (this->type, type) == 0)
935 {
936 if (prev == NULL)
937 first_callback = this->next;
938 else
939 prev->next = this->next;
941 free (this);
942 break;
943 }
944 } /* void cf_unregister */
946 void cf_unregister_complex (const char *type)
947 {
948 cf_complex_callback_t *this, *prev;
950 for (prev = NULL, this = complex_callback_head;
951 this != NULL;
952 prev = this, this = this->next)
953 if (strcasecmp (this->type, type) == 0)
954 {
955 if (prev == NULL)
956 complex_callback_head = this->next;
957 else
958 prev->next = this->next;
960 sfree (this->type);
961 sfree (this);
962 break;
963 }
964 } /* void cf_unregister */
966 void cf_register (const char *type,
967 int (*callback) (const char *, const char *),
968 const char **keys, int keys_num)
969 {
970 cf_callback_t *cf_cb;
972 /* Remove this module from the list, if it already exists */
973 cf_unregister (type);
975 /* This pointer will be free'd in `cf_unregister' */
976 if ((cf_cb = (cf_callback_t *) malloc (sizeof (cf_callback_t))) == NULL)
977 return;
979 cf_cb->type = type;
980 cf_cb->callback = callback;
981 cf_cb->keys = keys;
982 cf_cb->keys_num = keys_num;
983 cf_cb->ctx = plugin_get_ctx ();
985 cf_cb->next = first_callback;
986 first_callback = cf_cb;
987 } /* void cf_register */
989 int cf_register_complex (const char *type, int (*callback) (oconfig_item_t *))
990 {
991 cf_complex_callback_t *new;
993 new = (cf_complex_callback_t *) malloc (sizeof (cf_complex_callback_t));
994 if (new == NULL)
995 return (-1);
997 new->type = strdup (type);
998 if (new->type == NULL)
999 {
1000 sfree (new);
1001 return (-1);
1002 }
1004 new->callback = callback;
1005 new->next = NULL;
1007 new->ctx = plugin_get_ctx ();
1009 if (complex_callback_head == NULL)
1010 {
1011 complex_callback_head = new;
1012 }
1013 else
1014 {
1015 cf_complex_callback_t *last = complex_callback_head;
1016 while (last->next != NULL)
1017 last = last->next;
1018 last->next = new;
1019 }
1021 return (0);
1022 } /* int cf_register_complex */
1024 int cf_read (char *filename)
1025 {
1026 oconfig_item_t *conf;
1027 int i;
1029 conf = cf_read_generic (filename, /* pattern = */ NULL, /* depth = */ 0);
1030 if (conf == NULL)
1031 {
1032 ERROR ("Unable to read config file %s.", filename);
1033 return (-1);
1034 }
1036 for (i = 0; i < conf->children_num; i++)
1037 {
1038 if (conf->children[i].children == NULL)
1039 dispatch_value (conf->children + i);
1040 else
1041 dispatch_block (conf->children + i);
1042 }
1044 oconfig_free (conf);
1046 /* Read the default types.db if no `TypesDB' option was given. */
1047 if (cf_default_typesdb)
1048 read_types_list (PKGDATADIR"/types.db");
1050 return (0);
1051 } /* int cf_read */
1053 /* Assures the config option is a string, duplicates it and returns the copy in
1054 * "ret_string". If necessary "*ret_string" is freed first. Returns zero upon
1055 * success. */
1056 int cf_util_get_string (const oconfig_item_t *ci, char **ret_string) /* {{{ */
1057 {
1058 char *string;
1060 if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_STRING))
1061 {
1062 ERROR ("cf_util_get_string: The %s option requires "
1063 "exactly one string argument.", ci->key);
1064 return (-1);
1065 }
1067 string = strdup (ci->values[0].value.string);
1068 if (string == NULL)
1069 return (-1);
1071 if (*ret_string != NULL)
1072 sfree (*ret_string);
1073 *ret_string = string;
1075 return (0);
1076 } /* }}} int cf_util_get_string */
1078 /* Assures the config option is a string and copies it to the provided buffer.
1079 * Assures null-termination. */
1080 int cf_util_get_string_buffer (const oconfig_item_t *ci, char *buffer, /* {{{ */
1081 size_t buffer_size)
1082 {
1083 if ((ci == NULL) || (buffer == NULL) || (buffer_size < 1))
1084 return (EINVAL);
1086 if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_STRING))
1087 {
1088 ERROR ("cf_util_get_string_buffer: The %s option requires "
1089 "exactly one string argument.", ci->key);
1090 return (-1);
1091 }
1093 strncpy (buffer, ci->values[0].value.string, buffer_size);
1094 buffer[buffer_size - 1] = 0;
1096 return (0);
1097 } /* }}} int cf_util_get_string_buffer */
1099 /* Assures the config option is a number and returns it as an int. */
1100 int cf_util_get_int (const oconfig_item_t *ci, int *ret_value) /* {{{ */
1101 {
1102 if ((ci == NULL) || (ret_value == NULL))
1103 return (EINVAL);
1105 if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_NUMBER))
1106 {
1107 ERROR ("cf_util_get_int: The %s option requires "
1108 "exactly one numeric argument.", ci->key);
1109 return (-1);
1110 }
1112 *ret_value = (int) ci->values[0].value.number;
1114 return (0);
1115 } /* }}} int cf_util_get_int */
1117 int cf_util_get_double (const oconfig_item_t *ci, double *ret_value) /* {{{ */
1118 {
1119 if ((ci == NULL) || (ret_value == NULL))
1120 return (EINVAL);
1122 if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_NUMBER))
1123 {
1124 ERROR ("cf_util_get_double: The %s option requires "
1125 "exactly one numeric argument.", ci->key);
1126 return (-1);
1127 }
1129 *ret_value = ci->values[0].value.number;
1131 return (0);
1132 } /* }}} int cf_util_get_double */
1134 int cf_util_get_boolean (const oconfig_item_t *ci, _Bool *ret_bool) /* {{{ */
1135 {
1136 if ((ci == NULL) || (ret_bool == NULL))
1137 return (EINVAL);
1139 if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_BOOLEAN))
1140 {
1141 ERROR ("cf_util_get_boolean: The %s option requires "
1142 "exactly one boolean argument.", ci->key);
1143 return (-1);
1144 }
1146 *ret_bool = ci->values[0].value.boolean ? 1 : 0;
1148 return (0);
1149 } /* }}} int cf_util_get_boolean */
1151 int cf_util_get_flag (const oconfig_item_t *ci, /* {{{ */
1152 unsigned int *ret_value, unsigned int flag)
1153 {
1154 int status;
1155 _Bool b;
1157 if (ret_value == NULL)
1158 return (EINVAL);
1160 b = 0;
1161 status = cf_util_get_boolean (ci, &b);
1162 if (status != 0)
1163 return (status);
1165 if (b)
1166 {
1167 *ret_value |= flag;
1168 }
1169 else
1170 {
1171 *ret_value &= ~flag;
1172 }
1174 return (0);
1175 } /* }}} int cf_util_get_flag */
1177 /* Assures that the config option is a string or a number if the correct range
1178 * of 1-65535. The string is then converted to a port number using
1179 * `service_name_to_port_number' and returned.
1180 * Returns the port number in the range [1-65535] or less than zero upon
1181 * failure. */
1182 int cf_util_get_port_number (const oconfig_item_t *ci) /* {{{ */
1183 {
1184 int tmp;
1186 if ((ci->values_num != 1)
1187 || ((ci->values[0].type != OCONFIG_TYPE_STRING)
1188 && (ci->values[0].type != OCONFIG_TYPE_NUMBER)))
1189 {
1190 ERROR ("cf_util_get_port_number: The \"%s\" option requires "
1191 "exactly one string argument.", ci->key);
1192 return (-1);
1193 }
1195 if (ci->values[0].type == OCONFIG_TYPE_STRING)
1196 return (service_name_to_port_number (ci->values[0].value.string));
1198 assert (ci->values[0].type == OCONFIG_TYPE_NUMBER);
1199 tmp = (int) (ci->values[0].value.number + 0.5);
1200 if ((tmp < 1) || (tmp > 65535))
1201 {
1202 ERROR ("cf_util_get_port_number: The \"%s\" option requires "
1203 "a service name or a port number. The number "
1204 "you specified, %i, is not in the valid "
1205 "range of 1-65535.",
1206 ci->key, tmp);
1207 return (-1);
1208 }
1210 return (tmp);
1211 } /* }}} int cf_util_get_port_number */
1213 int cf_util_get_service (const oconfig_item_t *ci, char **ret_string) /* {{{ */
1214 {
1215 int port;
1216 char *service;
1217 int status;
1219 if (ci->values_num != 1)
1220 {
1221 ERROR ("cf_util_get_service: The %s option requires exactly "
1222 "one argument.", ci->key);
1223 return (-1);
1224 }
1226 if (ci->values[0].type == OCONFIG_TYPE_STRING)
1227 return (cf_util_get_string (ci, ret_string));
1228 if (ci->values[0].type != OCONFIG_TYPE_NUMBER)
1229 {
1230 ERROR ("cf_util_get_service: The %s option requires "
1231 "exactly one string or numeric argument.",
1232 ci->key);
1233 }
1235 port = 0;
1236 status = cf_util_get_int (ci, &port);
1237 if (status != 0)
1238 return (status);
1239 else if ((port < 1) || (port > 65535))
1240 {
1241 ERROR ("cf_util_get_service: The port number given "
1242 "for the %s option is out of "
1243 "range (%i).", ci->key, port);
1244 return (-1);
1245 }
1247 service = malloc (6);
1248 if (service == NULL)
1249 {
1250 ERROR ("cf_util_get_service: Out of memory.");
1251 return (-1);
1252 }
1253 ssnprintf (service, 6, "%i", port);
1255 sfree (*ret_string);
1256 *ret_string = service;
1258 return (0);
1259 } /* }}} int cf_util_get_service */
1261 int cf_util_get_cdtime (const oconfig_item_t *ci, cdtime_t *ret_value) /* {{{ */
1262 {
1263 if ((ci == NULL) || (ret_value == NULL))
1264 return (EINVAL);
1266 if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_NUMBER))
1267 {
1268 ERROR ("cf_util_get_cdtime: The %s option requires "
1269 "exactly one numeric argument.", ci->key);
1270 return (-1);
1271 }
1273 if (ci->values[0].value.number < 0.0)
1274 {
1275 ERROR ("cf_util_get_cdtime: The numeric argument of the %s "
1276 "option must not be negative.", ci->key);
1277 return (-1);
1278 }
1280 *ret_value = DOUBLE_TO_CDTIME_T (ci->values[0].value.number);
1282 return (0);
1283 } /* }}} int cf_util_get_cdtime */