/**
* collectd - src/configfile.c
- * Copyright (C) 2005-2008 Florian octo Forster
+ * Copyright (C) 2005-2009 Florian octo Forster
*
* This program is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by the
#include "configfile.h"
#include "types_list.h"
#include "utils_threshold.h"
+#include "filter_chain.h"
#if HAVE_WORDEXP_H
# include <wordexp.h>
*/
static int dispatch_value_typesdb (const oconfig_item_t *ci);
static int dispatch_value_plugindir (const oconfig_item_t *ci);
-static int dispatch_value_loadplugin (const oconfig_item_t *ci);
+static int dispatch_loadplugin (const oconfig_item_t *ci);
/*
* Private variables
{
{"TypesDB", dispatch_value_typesdb},
{"PluginDir", dispatch_value_plugindir},
- {"LoadPlugin", dispatch_value_loadplugin}
+ {"LoadPlugin", dispatch_loadplugin}
};
static int cf_value_map_num = STATIC_ARRAY_LEN (cf_value_map);
{"Hostname", NULL, NULL},
{"FQDNLookup", NULL, "false"},
{"Interval", NULL, "10"},
- {"ReadThreads", NULL, "5"}
+ {"ReadThreads", NULL, "5"},
+ {"PreCacheChain", NULL, "PreCache"},
+ {"PostCacheChain", NULL, "PostCache"}
};
static int cf_global_options_num = STATIC_ARRAY_LEN (cf_global_options);
for (i = 0; i < cf_cb->keys_num; i++)
{
- if (strcasecmp (cf_cb->keys[i], key) == 0)
+ if ((cf_cb->keys[i] != NULL)
+ && (strcasecmp (cf_cb->keys[i], key) == 0))
{
ret = (*cf_cb->callback) (key, value);
break;
free (key);
free (value);
- DEBUG ("return (%i)", ret);
+ DEBUG ("cf_dispatch: return (%i)", ret);
return (ret);
} /* int cf_dispatch */
cf_default_typesdb = 0;
- if (ci->values_num < 1)
+ if (ci->values_num < 1) {
+ ERROR ("configfile: `TypesDB' needs at least one argument.");
return (-1);
+ }
for (i = 0; i < ci->values_num; ++i)
{
- if (OCONFIG_TYPE_STRING != ci->values[i].type)
+ if (OCONFIG_TYPE_STRING != ci->values[i].type) {
+ WARNING ("configfile: TypesDB: Skipping %i. argument which "
+ "is not a string.", i + 1);
continue;
+ }
read_types_list (ci->values[i].value.string);
}
return (0);
}
-static int dispatch_value_loadplugin (const oconfig_item_t *ci)
+static int dispatch_loadplugin (const oconfig_item_t *ci)
{
+ int i;
+ uint32_t flags = 0;
assert (strcasecmp (ci->key, "LoadPlugin") == 0);
if (ci->values_num != 1)
if (ci->values[0].type != OCONFIG_TYPE_STRING)
return (-1);
- return (plugin_load (ci->values[0].value.string));
+ for (i = 0; i < ci->children_num; ++i) {
+ if (ci->children[i].values_num != 1 ||
+ ci->children[i].values[0].type != OCONFIG_TYPE_BOOLEAN) {
+ WARNING("Ignoring unknown LoadPlugin option %s for plugin %s", ci->children[i].key, ci->values[0].value.string);
+ continue;
+ }
+ if (strcasecmp(ci->children[i].key, "globals") == 0) {
+ flags |= PLUGIN_FLAGS_GLOBAL;
+ } else {
+ WARNING("Ignoring unknown LoadPlugin option %s for plugin %s", ci->children[i].key, ci->values[0].value.string);
+ }
+ }
+ return (plugin_load (ci->values[0].value.string, flags));
} /* int dispatch_value_loadplugin */
static int dispatch_value_plugin (const char *plugin, oconfig_item_t *ci)
buffer_ptr = buffer + 1;
return (cf_dispatch (plugin, ci->key, buffer_ptr));
-} /* int plugin_conf_dispatch */
+} /* int dispatch_value_plugin */
static int dispatch_value (const oconfig_item_t *ci)
{
if (ci->children[i].children == NULL)
dispatch_value_plugin (name, ci->children + i);
else
- {DEBUG ("No nested config blocks allowed for this plugin.");}
+ {
+ WARNING ("There is a `%s' block within the "
+ "configuration for the %s plugin. "
+ "The plugin either only expects "
+ "\"simple\" configuration statements "
+ "or wasn't loaded using `LoadPlugin'."
+ " Please check your configuration.",
+ ci->children[i].key, name);
+ }
}
return (0);
static int dispatch_block (oconfig_item_t *ci)
{
- if (strcasecmp (ci->key, "Plugin") == 0)
+ if (strcasecmp (ci->key, "LoadPlugin") == 0)
+ return (dispatch_loadplugin (ci));
+ else if (strcasecmp (ci->key, "Plugin") == 0)
return (dispatch_block_plugin (ci));
else if (strcasecmp (ci->key, "Threshold") == 0)
return (ut_config (ci));
+ else if (strcasecmp (ci->key, "Chain") == 0)
+ return (fc_configure (ci));
return (0);
}
temp = NULL;
/* If (src->children_num == 0) the array size is decreased. If offset
- * is _not_ the last element, (offset < (src->children_num - 1)), then
+ * is _not_ the last element, (offset < (dst->children_num - 1)), then
* we need to move the trailing elements before resizing the array. */
- if ((src->children_num == 0) && (offset < (src->children_num - 1)))
+ if ((src->children_num == 0) && (offset < (dst->children_num - 1)))
{
- int nmemb = src->children_num - (offset + 1);
- memmove (src->children + offset, src->children + offset + 1,
+ int nmemb = dst->children_num - (offset + 1);
+ memmove (dst->children + offset, dst->children + offset + 1,
sizeof (oconfig_item_t) * nmemb);
}
sizeof (oconfig_item_t) * nmemb);
}
- /* Last but not least: If there are new childrem, copy them to the
+ /* Last but not least: If there are new children, copy them to the
* memory reserved for them. */
if (src->children_num > 0)
{
/* Now replace the i'th child in `root' with `new'. */
cf_ci_replace_child (root, new, i);
+ /* ... and go back to the new i'th child. */
+ --i;
+
sfree (new->values);
sfree (new);
} /* for (i = 0; i < root->children_num; i++) */
status = ssnprintf (name, sizeof (name), "%s/%s",
dir, de->d_name);
- if (status >= sizeof (name))
+ if ((status < 0) || ((size_t) status >= sizeof (name)))
{
ERROR ("configfile: Not including `%s/%s' because its"
" name is too long.",
int status;
const char *path_ptr;
wordexp_t we;
- int i;
+ size_t i;
if (depth >= CF_MAX_DEPTH)
{
/* Read the default types.db if no `TypesDB' option was given. */
if (cf_default_typesdb)
- read_types_list (PLUGINDIR"/types.db");
+ read_types_list (PKGDATADIR"/types.db");
return (0);
} /* int cf_read */
+
+/* Assures the config option is a string, duplicates it and returns the copy in
+ * "ret_string". If necessary "*ret_string" is freed first. Returns zero upon
+ * success. */
+int cf_util_get_string (const oconfig_item_t *ci, char **ret_string) /* {{{ */
+{
+ char *string;
+
+ if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_STRING))
+ {
+ ERROR ("cf_util_get_string: The %s option requires "
+ "exactly one string argument.", ci->key);
+ return (-1);
+ }
+
+ string = strdup (ci->values[0].value.string);
+ if (string == NULL)
+ return (-1);
+
+ if (*ret_string != NULL)
+ sfree (*ret_string);
+ *ret_string = string;
+
+ return (0);
+} /* }}} int cf_util_get_string */
+
+int cf_util_get_boolean (const oconfig_item_t *ci, _Bool *ret_bool) /* {{{ */
+{
+ if ((ci == NULL) || (ret_bool == NULL))
+ return (EINVAL);
+
+ if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_BOOLEAN))
+ {
+ ERROR ("cf_util_get_boolean: The %s option requires "
+ "exactly one string argument.", ci->key);
+ return (-1);
+ }
+
+ *ret_bool = ci->values[0].value.boolean ? true : false;
+
+ return (0);
+} /* }}} int cf_util_get_boolean */
+
+/* Assures that the config option is a string. The string is then converted to
+ * a port number using `service_name_to_port_number' and returned. Returns the
+ * port number in the range [1-65535] or less than zero upon failure. */
+int cf_util_get_port_number (const oconfig_item_t *ci) /* {{{ */
+{
+ if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_STRING))
+ {
+ ERROR ("cf_util_get_port_number: The %s option requires "
+ "exactly one string argument.", ci->key);
+ return (-1);
+ }
+
+ return (service_name_to_port_number (ci->values[0].value.string));
+} /* }}} int cf_util_get_port_number */