Code

Added `network.c' which will replace `multicast.c' at some point..
[collectd.git] / src / configfile.c
index a0610041257525ba50e6013c473dbb357866262b..be90dd37437da693dbac11588ab66a67b246cd17 100644 (file)
@@ -20,6 +20,9 @@
  *   Florian octo Forster <octo at verplant.org>
  **/
 
+/* TODO
+ * make internal-only functions `static' */
+
 #include "collectd.h"
 
 #include "libconfig/libconfig.h"
@@ -59,21 +62,24 @@ typedef struct cf_mode_item
        int   mode;
 } cf_mode_item_t;
 
+/* TODO
+ * - LogFile
+ */
 static cf_mode_item_t cf_mode_list[] =
 {
        {"Server",      NULL, MODE_CLIENT                           },
        {"Port",        NULL, MODE_CLIENT | MODE_SERVER             },
        {"PIDFile",     NULL, MODE_CLIENT | MODE_SERVER | MODE_LOCAL},
-       {"DataDir",     NULL, MODE_SERVER |               MODE_LOCAL},
-       {"PluginDir",   NULL, MODE_CLIENT | MODE_SERVER | MODE_LOCAL}
+       {"DataDir",     NULL, MODE_CLIENT | MODE_SERVER | MODE_LOCAL},
+       {"LogFile",     NULL, MODE_CLIENT | MODE_SERVER | MODE_LOCAL}
 };
 static int cf_mode_num = 5;
 
 static int nesting_depth = 0;
 static char *current_module = NULL;
 
-/* cf_register needs this prototype */
-int cf_callback_dispatch (const char *, const char *, const char *,
+/* `cf_register' needs this prototype */
+int cf_callback_plugin_dispatch (const char *, const char *, const char *,
                const char *, lc_flags_t, void *);
 
 /*
@@ -102,6 +108,8 @@ int cf_dispatch (char *type, const char *orig_key, const char *orig_value)
        int ret;
        int i;
 
+       DBG ("type = %s, key = %s, value = %s", type, orig_key, orig_value);
+
        if ((cf_cb = cf_search (type)) == NULL)
        {
                syslog (LOG_WARNING, "Plugin `%s' did not register a callback.\n", type);
@@ -186,7 +194,7 @@ void cf_register (char *type,
                         * `key', but apparently `lc_register_*' can handle
                         * it.. */
                        lc_register_callback (buf, SHORTOPT_NONE,
-                                       LC_VAR_STRING, cf_callback_dispatch,
+                                       LC_VAR_STRING, cf_callback_plugin_dispatch,
                                        NULL);
                }
                else
@@ -199,7 +207,7 @@ void cf_register (char *type,
 /*
  * Other query functions
  */
-char *cf_get_mode_option (const char *key)
+char *cf_get_option (const char *key, char *def)
 {
        int i;
 
@@ -208,42 +216,78 @@ char *cf_get_mode_option (const char *key)
                if ((cf_mode_list[i].mode & operating_mode) == 0)
                        continue;
 
-               if (strcasecmp (cf_mode_list[i].key, key) == 0)
+               if (strcasecmp (cf_mode_list[i].key, key) != 0)
+                       continue;
+
+               if (cf_mode_list[i].value != NULL)
                        return (cf_mode_list[i].value);
+               return (def);
        }
 
        return (NULL);
 }
 
-/* 
- * Functions for the actual parsing
+/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
+ * Functions for the actual parsing                                    *
+ * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
+
+/*
+ * `cf_callback_mode'
+ *   Chose the `operating_mode'
+ *
+ * Mode `value'
  */
-int cf_callback_dispatch (const char *shortvar, const char *var,
+int cf_callback_mode (const char *shortvar, const char *var,
                const char *arguments, const char *value, lc_flags_t flags,
                void *extra)
 {
        DBG ("shortvar = %s, var = %s, arguments = %s, value = %s, ...",
                        shortvar, var, arguments, value);
 
-       if ((nesting_depth == 0) || (current_module == NULL))
+       if (strcasecmp (value, "Client") == 0)
+               operating_mode = MODE_CLIENT;
+       else if (strcasecmp (value, "Server") == 0)
+               operating_mode = MODE_SERVER;
+       else if (strcasecmp (value, "Local") == 0)
+               operating_mode = MODE_LOCAL;
+       else
        {
-               fprintf (stderr, ERR_NEEDS_SECTION, shortvar);
+               syslog (LOG_ERR, "Invalid value for config option `Mode': `%s'", value);
                return (LC_CBRET_ERROR);
        }
 
-       /* Send the data to the plugin */
-       if (cf_dispatch (current_module, shortvar, value) < 0)
-               return (LC_CBRET_ERROR);
+       return (LC_CBRET_OKAY);
+}
+
+/*
+ * `cf_callback_mode_plugindir'
+ *   Change the plugin directory
+ *
+ * <Mode xxx>
+ *   PluginDir `value'
+ * </Mode>
+ */
+int cf_callback_mode_plugindir (const char *shortvar, const char *var,
+               const char *arguments, const char *value, lc_flags_t flags,
+               void *extra)
+{
+       DBG ("shortvar = %s, var = %s, arguments = %s, value = %s, ...",
+                       shortvar, var, arguments, value);
+
+       plugin_set_dir (value);
 
        return (LC_CBRET_OKAY);
 }
 
-int cf_callback_options_mode (const char *shortvar, const char *var,
+int cf_callback_mode_option (const char *shortvar, const char *var,
                const char *arguments, const char *value, lc_flags_t flags,
                void *extra)
 {
        cf_mode_item_t *item;
 
+       DBG ("shortvar = %s, var = %s, arguments = %s, value = %s, ...",
+                       shortvar, var, arguments, value);
+
        if (extra == NULL)
        {
                fprintf (stderr, "No extra..?\n");
@@ -279,58 +323,68 @@ int cf_callback_options_mode (const char *shortvar, const char *var,
        return (LC_CBRET_OKAY);
 }
 
-int cf_callback_section_mode (const char *shortvar, const char *var,
+/*
+ * `cf_callback_mode_loadmodule':
+ *   Load a plugin.
+ *
+ * <Mode xxx>
+ *   LoadPlugin `value'
+ * </Mode>
+ */
+int cf_callback_mode_loadmodule (const char *shortvar, const char *var,
                const char *arguments, const char *value, lc_flags_t flags,
                void *extra)
 {
        DBG ("shortvar = %s, var = %s, arguments = %s, value = %s, ...",
                        shortvar, var, arguments, value);
 
-       if (flags == LC_FLAGS_SECTIONSTART)
-       {
-               if (nesting_depth != 0)
-               {
-                       fprintf (stderr, ERR_NOT_NESTED);
-                       return (LC_CBRET_ERROR);
-               }
-
-               if (arguments == NULL)
-               {
-                       fprintf (stderr, ERR_NEEDS_ARG, shortvar);
-                       return (LC_CBRET_ERROR);
-               }
+       if (plugin_load (value))
+               syslog (LOG_ERR, "plugin_load (%s): failed to load plugin", value);
 
-               nesting_depth++;
+       /* Return `okay' even if there was an error, because it's not a syntax
+        * problem.. */
+       return (LC_CBRET_OKAY);
+}
 
-               if (((operating_mode == MODE_CLIENT)
-                                       && (strcasecmp (arguments, "Client") == 0))
-                               || ((operating_mode == MODE_SERVER)
-                                       && (strcasecmp (arguments, "Server") == 0))
-                               || ((operating_mode == MODE_LOCAL)
-                                       && (strcasecmp (arguments, "Local") == 0)))
-               {
-                       return (LC_CBRET_OKAY);
-               }
-               else
-               {
-                       return (LC_CBRET_IGNORESECTION);
-               }
-       }
-       else if (flags == LC_FLAGS_SECTIONEND)
-       {
-               nesting_depth--;
+/*
+ * `cf_callback_mode_switch'
+ *   Change the contents of the global variable `operating_mode'
+ *
+ *   This should be command line options. One *can* do this in the config
+ *   files, but I will not document this. Don't whine abount it not working as
+ *   you expect if you do it anyways.
+ */
+int cf_callback_mode_switch (const char *shortvar, const char *var,
+               const char *arguments, const char *value, lc_flags_t flags,
+               void *extra)
+{
+       DBG ("shortvar = %s, var = %s, arguments = %s, value = %s, ...",
+                       shortvar, var, arguments, value);
 
-               return (LC_CBRET_OKAY);
-       }
+       if (strcasecmp (shortvar, "Client") == 0)
+               operating_mode = MODE_CLIENT;
+       else if (strcasecmp (shortvar, "Local") == 0)
+               operating_mode = MODE_LOCAL;
+       else if (strcasecmp (shortvar, "Server") == 0)
+               operating_mode = MODE_SERVER;
        else
        {
-               fprintf (stderr, ERR_SECTION_ONLY, shortvar);
+               fprintf (stderr, "cf_callback_mode_switch: Wrong mode!\n");
                return (LC_CBRET_ERROR);
        }
 
+       return (LC_CBRET_OKAY);
 }
 
-int cf_callback_section_module (const char *shortvar, const char *var,
+/*
+ * `cf_callback_plugin'
+ *   Start/end section `plugin'
+ *
+ * <Plugin `arguments'>
+ *   ...
+ * </Plugin>
+ */
+int cf_callback_plugin (const char *shortvar, const char *var,
                const char *arguments, const char *value, lc_flags_t flags,
                void *extra)
 {
@@ -383,63 +437,82 @@ int cf_callback_section_module (const char *shortvar, const char *var,
        }
 }
 
-int cf_callback_loadmodule (const char *shortvar, const char *var,
+/*
+ * `cf_callback_plugin_dispatch'
+ *   Send options within `plugin' sections to the plugin that requests it.
+ *
+ * <Plugin `current_module'>
+ *   `var' `value'
+ * </Plugin>
+ */
+int cf_callback_plugin_dispatch (const char *shortvar, const char *var,
                const char *arguments, const char *value, lc_flags_t flags,
                void *extra)
 {
        DBG ("shortvar = %s, var = %s, arguments = %s, value = %s, ...",
                        shortvar, var, arguments, value);
 
-       if (nesting_depth == 0)
+       if ((nesting_depth == 0) || (current_module == NULL))
        {
                fprintf (stderr, ERR_NEEDS_SECTION, shortvar);
                return (LC_CBRET_ERROR);
        }
 
-       if (plugin_load (value))
-               syslog (LOG_ERR, "plugin_load (%s): failed to load plugin", shortvar);
+       /* Send the data to the plugin */
+       if (cf_dispatch (current_module, shortvar, value) < 0)
+               return (LC_CBRET_ERROR);
 
-       /* Return `okay' even if there was an error, because it's not a syntax
-        * problem.. */
        return (LC_CBRET_OKAY);
 }
 
-int cf_read (char *filename)
+void cf_init (void)
 {
+       static int run_once = 0;
        int i;
 
-       if (filename == NULL)
-               filename = CONFIGFILE;
+       if (run_once != 0)
+               return;
+       run_once = 1;
 
-       lc_register_callback ("Mode", SHORTOPT_NONE, LC_VAR_SECTION,
-                       cf_callback_section_mode, NULL);
+       lc_register_callback ("Mode", SHORTOPT_NONE, LC_VAR_STRING,
+                       cf_callback_mode, NULL);
        lc_register_callback ("Plugin", SHORTOPT_NONE, LC_VAR_SECTION,
-                       cf_callback_section_module, NULL);
+                       cf_callback_plugin, NULL);
 
-       lc_register_callback ("Mode.LoadPlugin", SHORTOPT_NONE,
-                       LC_VAR_STRING, cf_callback_loadmodule,
-                       NULL);
+       lc_register_callback ("PluginDir", SHORTOPT_NONE,
+                       LC_VAR_STRING, cf_callback_mode_plugindir, NULL);
+       lc_register_callback ("LoadPlugin", SHORTOPT_NONE,
+                       LC_VAR_STRING, cf_callback_mode_loadmodule, NULL);
 
        for (i = 0; i < cf_mode_num; i++)
        {
-               char            longvar[256];
                cf_mode_item_t *item;
 
                item = &cf_mode_list[i];
 
-               if (snprintf (longvar, 256, "Mode.%s", item->key) >= 256)
-                       continue;
-
-               lc_register_callback (longvar, SHORTOPT_NONE, LC_VAR_STRING,
-                               cf_callback_options_mode, (void *) item);
+               lc_register_callback (item->key, SHORTOPT_NONE, LC_VAR_STRING,
+                               cf_callback_mode_option, (void *) item);
        }
+}
+
+int cf_read (char *filename)
+{
+       cf_init ();
+
+       if (filename == NULL)
+               filename = CONFIGFILE;
+
+       DBG ("Starting to parse file `%s'", filename);
 
+       /* int lc_process_file(const char *appname, const char *pathname, lc_conf_type_t type); */
        if (lc_process_file ("collectd", filename, LC_CONF_APACHE))
        {
                syslog (LOG_ERR, "lc_process_file (%s): %s", filename, lc_geterrstr ());
                return (-1);
        }
 
+       DBG ("Done parsing file `%s'", filename);
+
        /* free memory and stuff */
        lc_cleanup ();