src/configfile.c: s/TypesDS/TypesDB/;
[collectd.git] / src / configfile.c
index 6aafc56..4b6803e 100644 (file)
@@ -27,8 +27,6 @@
 #include "common.h"
 #include "plugin.h"
 #include "configfile.h"
-#include "network.h"
-#include "utils_debug.h"
 
 #define ESCAPE_NULL(str) ((str) == NULL ? "(null)" : (str))
 
@@ -60,7 +58,6 @@ typedef struct cf_global_option_s
 /*
  * Prototypes of callback functions
  */
-static int dispatch_value_pidfile (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);
 
@@ -71,7 +68,6 @@ static cf_callback_t *first_callback = NULL;
 
 static cf_value_map_t cf_value_map[] =
 {
-       {"PIDFile",    dispatch_value_pidfile},
        {"PluginDir",  dispatch_value_plugindir},
        {"LoadPlugin", dispatch_value_loadplugin}
 };
@@ -79,9 +75,12 @@ static int cf_value_map_num = STATIC_ARRAY_LEN (cf_value_map);
 
 static cf_global_option_t cf_global_options[] =
 {
-       {"BaseDir", NULL, PKGLOCALSTATEDIR},
-       {"LogFile", NULL, LOGFILE},
-       {"PIDFile", NULL, PIDFILE}
+       {"BaseDir",   NULL, PKGLOCALSTATEDIR},
+       {"PIDFile",   NULL, PIDFILE},
+       {"Hostname",  NULL, NULL},
+       {"Interval",  NULL, "10"},
+       {"ReadThreads", NULL, "5"},
+       {"TypesDB",   NULL, PLUGINDIR"/types.db"} /* FIXME: Configure path */
 };
 static int cf_global_options_num = STATIC_ARRAY_LEN (cf_global_options);
 
@@ -112,14 +111,14 @@ static int cf_dispatch (const char *type, const char *orig_key,
        int ret;
        int i;
 
-       DBG ("type = %s, key = %s, value = %s",
+       DEBUG ("type = %s, key = %s, value = %s",
                        ESCAPE_NULL(type),
                        ESCAPE_NULL(orig_key),
                        ESCAPE_NULL(orig_value));
 
        if ((cf_cb = cf_search (type)) == NULL)
        {
-               syslog (LOG_WARNING, "Plugin `%s' did not register a callback.", type);
+               WARNING ("Plugin `%s' did not register a callback.", type);
                return (-1);
        }
 
@@ -143,27 +142,32 @@ static int cf_dispatch (const char *type, const char *orig_key,
        }
 
        if (i >= cf_cb->keys_num)
-               syslog (LOG_WARNING, "Plugin `%s' did not register for value `%s'.", type, key);
+               WARNING ("Plugin `%s' did not register for value `%s'.", type, key);
 
        free (key);
        free (value);
 
-       DBG ("return (%i)", ret);
+       DEBUG ("return (%i)", ret);
 
        return (ret);
-}
+} /* int cf_dispatch */
 
-static int dispatch_value_pidfile (const oconfig_item_t *ci)
+static int dispatch_global_option (const oconfig_item_t *ci)
 {
-       assert (strcasecmp (ci->key, "PIDFile") == 0);
-       
        if (ci->values_num != 1)
                return (-1);
-       if (ci->values[0].type != OCONFIG_TYPE_STRING)
-               return (-1);
+       if (ci->values[0].type == OCONFIG_TYPE_STRING)
+               return (global_option_set (ci->key, ci->values[0].value.string));
+       else if (ci->values[0].type == OCONFIG_TYPE_NUMBER)
+       {
+               char tmp[128];
+               snprintf (tmp, sizeof (tmp), "%lf", ci->values[0].value.number);
+               tmp[127] = '\0';
+               return (global_option_set (ci->key, tmp));
+       }
 
-       return (global_option_set ("PIDFile", ci->values[0].value.string));
-}
+       return (-1);
+} /* int dispatch_global_option */
 
 static int dispatch_value_plugindir (const oconfig_item_t *ci)
 {
@@ -238,6 +242,13 @@ static int dispatch_value (const oconfig_item_t *ci)
                        break;
                }
 
+       for (i = 0; i < cf_global_options_num; i++)
+               if (strcasecmp (cf_global_options[i].key, ci->key) == 0)
+               {
+                       ret = dispatch_global_option (ci);
+                       break;
+               }
+
        return (ret);
 } /* int dispatch_value */
 
@@ -260,7 +271,7 @@ static int dispatch_block_plugin (oconfig_item_t *ci)
                if (ci->children[i].children == NULL)
                        dispatch_value_plugin (name, ci->children + i);
                else
-                       {DBG ("No nested config blocks allow for plugins. Yet.");}
+                       {DEBUG ("No nested config blocks allow for plugins. Yet.");}
        }
 
        return (0);
@@ -282,6 +293,8 @@ int global_option_set (const char *option, const char *value)
 {
        int i;
 
+       DEBUG ("option = %s; value = %s;", option, value);
+
        for (i = 0; i < cf_global_options_num; i++)
                if (strcasecmp (cf_global_options[i].key, option) == 0)
                        break;
@@ -289,8 +302,7 @@ int global_option_set (const char *option, const char *value)
        if (i >= cf_global_options_num)
                return (-1);
 
-       if (cf_global_options[i].value != NULL)
-               free (cf_global_options[i].value);
+       sfree (cf_global_options[i].value);
 
        if (value != NULL)
                cf_global_options[i].value = strdup (value);
@@ -333,7 +345,7 @@ void cf_unregister (const char *type)
                        free (this);
                        break;
                }
-}
+} /* void cf_unregister */
 
 void cf_register (const char *type,
                int (*callback) (const char *, const char *),
@@ -365,7 +377,7 @@ int cf_read (char *filename)
        conf = oconfig_parse_file (filename);
        if (conf == NULL)
        {
-               syslog (LOG_ERR, "Unable to read config file %s.", filename);
+               ERROR ("Unable to read config file %s.", filename);
                return (-1);
        }