TODO: Updated the todo file.
[collectd.git] / src / configfile.c
index f4e9c60..706e2d1 100644 (file)
@@ -1,6 +1,6 @@
 /**
  * collectd - src/configfile.c
- * Copyright (C) 2005,2006  Florian octo Forster
+ * Copyright (C) 2005-2008  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
@@ -27,6 +27,8 @@
 #include "common.h"
 #include "plugin.h"
 #include "configfile.h"
+#include "types_list.h"
+#include "utils_threshold.h"
 
 #define ESCAPE_NULL(str) ((str) == NULL ? "(null)" : (str))
 
@@ -65,6 +67,7 @@ typedef struct cf_global_option_s
 /*
  * Prototypes of callback functions
  */
+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);
 
@@ -76,6 +79,7 @@ static cf_complex_callback_t *complex_callback_head = NULL;
 
 static cf_value_map_t cf_value_map[] =
 {
+       {"TypesDB",    dispatch_value_typesdb},
        {"PluginDir",  dispatch_value_plugindir},
        {"LoadPlugin", dispatch_value_loadplugin}
 };
@@ -83,15 +87,17 @@ static int cf_value_map_num = STATIC_ARRAY_LEN (cf_value_map);
 
 static cf_global_option_t cf_global_options[] =
 {
-       {"BaseDir",   NULL, PKGLOCALSTATEDIR},
-       {"PIDFile",   NULL, PIDFILE},
-       {"Hostname",  NULL, NULL},
-       {"Interval",  NULL, "10"},
-       {"ReadThreads", NULL, "5"},
-       {"TypesDB",   NULL, PLUGINDIR"/types.db"} /* FIXME: Configure path */
+       {"BaseDir",     NULL, PKGLOCALSTATEDIR},
+       {"PIDFile",     NULL, PIDFILE},
+       {"Hostname",    NULL, NULL},
+       {"FQDNLookup",  NULL, "false"},
+       {"Interval",    NULL, "10"},
+       {"ReadThreads", NULL, "5"}
 };
 static int cf_global_options_num = STATIC_ARRAY_LEN (cf_global_options);
 
+static int cf_default_typesdb = 1;
+
 /*
  * Functions to handle register/unregister, search, and other plugin related
  * stuff
@@ -126,7 +132,9 @@ static int cf_dispatch (const char *type, const char *orig_key,
 
        if ((cf_cb = cf_search (type)) == NULL)
        {
-               WARNING ("Plugin `%s' did not register a callback.", type);
+               WARNING ("Found a configuration for the `%s' plugin, but "
+                               "the plugin isn't loaded or didn't register "
+                               "a configuration callback.", type);
                return (-1);
        }
 
@@ -173,10 +181,38 @@ static int dispatch_global_option (const oconfig_item_t *ci)
                tmp[127] = '\0';
                return (global_option_set (ci->key, tmp));
        }
+       else if (ci->values[0].type == OCONFIG_TYPE_BOOLEAN)
+       {
+               if (ci->values[0].value.boolean)
+                       return (global_option_set (ci->key, "true"));
+               else
+                       return (global_option_set (ci->key, "false"));
+       }
 
        return (-1);
 } /* int dispatch_global_option */
 
+static int dispatch_value_typesdb (const oconfig_item_t *ci)
+{
+       int i = 0;
+
+       assert (strcasecmp (ci->key, "TypesDB") == 0);
+
+       cf_default_typesdb = 0;
+
+       if (ci->values_num < 1)
+               return (-1);
+
+       for (i = 0; i < ci->values_num; ++i)
+       {
+               if (OCONFIG_TYPE_STRING != ci->values[i].type)
+                       continue;
+
+               read_types_list (ci->values[i].value.string);
+       }
+       return (0);
+} /* int dispatch_value_typesdb */
+
 static int dispatch_value_plugindir (const oconfig_item_t *ci)
 {
        assert (strcasecmp (ci->key, "PluginDir") == 0);
@@ -298,10 +334,134 @@ static int dispatch_block (oconfig_item_t *ci)
 {
        if (strcasecmp (ci->key, "Plugin") == 0)
                return (dispatch_block_plugin (ci));
+       else if (strcasecmp (ci->key, "Threshold") == 0)
+               return (ut_config (ci));
 
        return (0);
 }
 
+#define CF_MAX_DEPTH 8
+static oconfig_item_t *cf_read_file (const char *file, int depth);
+
+static int cf_include_all (oconfig_item_t *root, int depth)
+{
+       int i;
+
+       for (i = 0; i < root->children_num; i++)
+       {
+               oconfig_item_t *new;
+               oconfig_item_t *old;
+
+               /* Ignore all blocks, including `Include' blocks. */
+               if (root->children[i].children_num != 0)
+                       continue;
+
+               if (strcasecmp (root->children[i].key, "Include") != 0)
+                       continue;
+
+               old = root->children + i;
+
+               if ((old->values_num != 1)
+                               || (old->values[0].type != OCONFIG_TYPE_STRING))
+               {
+                       ERROR ("configfile: `Include' needs exactly one string argument.");
+                       continue;
+               }
+
+               new = cf_read_file (old->values[0].value.string, depth + 1);
+               if (new == NULL)
+                       continue;
+
+               /* There are more children now. We need to expand
+                * root->children. */
+               if (new->children_num > 1)
+               {
+                       oconfig_item_t *temp;
+
+                       DEBUG ("configfile: Resizing root-children from %i to %i elements.",
+                                       root->children_num,
+                                       root->children_num + new->children_num - 1);
+
+                       temp = (oconfig_item_t *) realloc (root->children,
+                                       sizeof (oconfig_item_t)
+                                       * (root->children_num + new->children_num - 1));
+                       if (temp == NULL)
+                       {
+                               ERROR ("configfile: realloc failed.");
+                               oconfig_free (new);
+                               continue;
+                       }
+                       root->children = temp;
+               }
+
+               /* Clean up the old include directive while we still have a
+                * valid pointer */
+               DEBUG ("configfile: Cleaning up `old'");
+               /* sfree (old->values[0].value.string); */
+               sfree (old->values);
+
+               /* If there are trailing children and the number of children
+                * changes, we need to move the trailing ones either one to the
+                * front or (new->num - 1) to the back */
+               if (((root->children_num - i) > 1)
+                               && (new->children_num != 1))
+               {
+                       DEBUG ("configfile: Moving trailing children.");
+                       memmove (root->children + i + new->children_num,
+                                       root->children + i + 1,
+                                       sizeof (oconfig_item_t)
+                                       * (root->children_num - (i + 1)));
+               }
+
+               /* Now copy the new children to where the include statement was */
+               if (new->children_num > 0)
+               {
+                       DEBUG ("configfile: Copying new children.");
+                       memcpy (root->children + i,
+                                       new->children,
+                                       sizeof (oconfig_item_t)
+                                       * new->children_num);
+               }
+
+               /* Adjust the number of children and the position in the list. */
+               root->children_num = root->children_num + new->children_num - 1;
+               i = i + new->children_num - 1;
+
+               /* Clean up the `new' struct. We set `new->children' to NULL so
+                * the stuff we've just copied pointers to isn't freed by
+                * `oconfig_free' */
+               DEBUG ("configfile: Cleaning up `new'");
+               sfree (new->values); /* should be NULL anyway */
+               sfree (new);
+               new = NULL;
+       } /* for (i = 0; i < root->children_num; i++) */
+
+       return (0);
+} /* int cf_include_all */
+
+static oconfig_item_t *cf_read_file (const char *file, int depth)
+{
+       oconfig_item_t *root;
+
+       if (depth >= CF_MAX_DEPTH)
+       {
+               ERROR ("configfile: Not including `%s' because the maximum nesting depth has been reached.",
+                               file);
+               return (NULL);
+       }
+
+       root = oconfig_parse_file (file);
+       if (root == NULL)
+       {
+               ERROR ("configfile: Cannot read file `%s'.", file);
+               return (NULL);
+       }
+
+       cf_include_all (root, depth);
+
+       return (root);
+} /* oconfig_item_t *cf_read_file */
+
 /* 
  * Public functions
  */
@@ -443,7 +603,7 @@ int cf_read (char *filename)
        oconfig_item_t *conf;
        int i;
 
-       conf = oconfig_parse_file (filename);
+       conf = cf_read_file (filename, 0 /* depth */);
        if (conf == NULL)
        {
                ERROR ("Unable to read config file %s.", filename);
@@ -458,5 +618,7 @@ int cf_read (char *filename)
                        dispatch_block (conf->children + i);
        }
 
+       if (cf_default_typesdb)
+               read_types_list (PLUGINDIR"/types.db"); /* FIXME: Configure path */
        return (0);
 } /* int cf_read */