Code

Moved `exit_usage' from `collectd.c' to `configfile.c' and renamed it to `cf_callback...
[collectd.git] / src / configfile.c
index 14b9dfbc4decc2fccfbc8c10efc70d0fcb2868cf..9b1b83a163533bb3e3848ece22997914d6324f68 100644 (file)
  *   Florian octo Forster <octo at verplant.org>
  **/
 
-/*
- * FIXME:
- * - remove all (I mean *ALL*) calls to `fprintf': `stderr' will have been
- *   closed.
- */
-
 #include "collectd.h"
 
 #include "libconfig/libconfig.h"
@@ -44,7 +38,7 @@
 #ifdef HAVE_LIBRRD
 extern int operating_mode;
 #else
-static int operating_mode = MODE_LOCAL;
+static int operating_mode = MODE_CLIENT;
 #endif
 
 typedef struct cf_callback
@@ -58,15 +52,32 @@ typedef struct cf_callback
 
 static cf_callback_t *first_callback = NULL;
 
+typedef struct cf_mode_item
+{
+       char *key;
+       char *value;
+       int   mode;
+} cf_mode_item_t;
+
+static cf_mode_item_t cf_mode_list[] =
+{
+       {"Server",      NULL,             MODE_CLIENT                           },
+       {"Port",        NULL,             MODE_CLIENT | MODE_SERVER             },
+       {"PIDFile",     PIDFILE,          MODE_CLIENT | MODE_SERVER | MODE_LOCAL},
+       {"DataDir",     PKGLOCALSTATEDIR, MODE_SERVER |               MODE_LOCAL}
+};
+static int cf_mode_num = 4;
+
 static int nesting_depth = 0;
 static char *current_module = NULL;
 
-/* cf_register needs this prototype */
-int cf_callback_general (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 *);
 
 /*
- * Functions to handle register/unregister, search, ...
+ * Functions to handle register/unregister, search, and other plugin related
+ * stuff
  */
 cf_callback_t *cf_search (char *type)
 {
@@ -90,9 +101,11 @@ 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)
        {
-               fprintf (stderr, "Plugin `%s' did not register a callback.\n", type);
+               syslog (LOG_WARNING, "Plugin `%s' did not register a callback.\n", type);
                return (-1);
        }
 
@@ -116,7 +129,7 @@ int cf_dispatch (char *type, const char *orig_key, const char *orig_value)
        }
 
        if (i >= cf_cb->keys_num)
-               fprintf (stderr, "Plugin `%s' did not register for value `%s'.\n", type, key);
+               syslog (LOG_WARNING, "Plugin `%s' did not register for value `%s'.\n", type, key);
 
        free (key);
        free (value);
@@ -174,7 +187,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_general,
+                                       LC_VAR_STRING, cf_callback_plugin_dispatch,
                                        NULL);
                }
                else
@@ -184,30 +197,79 @@ void cf_register (char *type,
        }
 }
 
-/* 
- * Functions for the actual parsing
+/*
+ * Other query functions
  */
-int cf_callback_general (const char *shortvar, const char *var,
-               const char *arguments, const char *value, lc_flags_t flags,
-               void *extra)
+char *cf_get_mode_option (const char *key)
 {
-       DBG ("shortvar = %s, var = %s, arguments = %s, value = %s, ...",
-                       shortvar, var, arguments, value);
+       int i;
 
-       if ((nesting_depth == 0) || (current_module == NULL))
+       for (i = 0; i < cf_mode_num; i++)
        {
-               fprintf (stderr, ERR_NEEDS_SECTION, shortvar);
-               return (LC_CBRET_ERROR);
-       }
+               if ((cf_mode_list[i].mode & operating_mode) == 0)
+                       continue;
 
-       /* Send the data to the plugin */
-       if (cf_dispatch (current_module, shortvar, value) < 0)
-               return (LC_CBRET_ERROR);
+               if (strcasecmp (cf_mode_list[i].key, key) == 0)
+                       return (cf_mode_list[i].value);
+       }
 
-       return (LC_CBRET_OKAY);
+       return (NULL);
 }
 
-int cf_callback_section_mode (const char *shortvar, const char *var,
+int cf_callback_usage (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);
+
+       printf ("Usage: "PACKAGE" [OPTIONS]\n\n"
+                       
+                       "Available options:\n"
+#if COLLECT_DAEMON
+                       "    -P <file>       PID file.\n"
+                       "                    Default: "PIDFILE"\n"
+#endif
+                       "    -M <dir>        Module/Plugin directory.\n"
+                       "                    Default: "PLUGINDIR"\n"
+                       "    -D <dir>        Data storage directory.\n"
+                       "                    Default: "PKGLOCALSTATEDIR"\n"
+#if COLLECT_DEBUG
+                       "    -L <file>       Log file.\n"
+                       "                    Default: "LOGFILE"\n"
+#endif
+#if COLLECT_DAEMON
+                       "    -f              Don't fork to the background.\n"
+#endif
+#if HAVE_LIBRRD
+                       "    -l              Start in local mode (no network).\n"
+                       "    -c              Start in client (sender) mode.\n"
+                       "    -s              Start in server (listener) mode.\n"
+#endif /* HAVE_LIBRRD */
+#if COLLECT_PING
+                       "  Ping:\n"
+                       "    -p <host>       Host to ping periodically, may be repeated to ping\n"
+                       "                    more than one host.\n"
+#endif /* COLLECT_PING */
+                       "\n"PACKAGE" "VERSION", http://verplant.org/collectd/\n"
+                       "by Florian octo Forster <octo@verplant.org>\n"
+                       "for contributions see `AUTHORS'\n");
+       exit (0);
+} /* exit_usage */
+
+/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
+ * Functions for the actual parsing                                    *
+ * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
+
+/*
+ * `cf_callback_mode'
+ *   Start/end the `mode' section
+ *
+ * <Mode `arguments'>
+ *   ...
+ * </Mode>
+ */
+int cf_callback_mode (const char *shortvar, const char *var,
                const char *arguments, const char *value, lc_flags_t flags,
                void *extra)
 {
@@ -258,7 +320,131 @@ int cf_callback_section_mode (const char *shortvar, const char *var,
 
 }
 
-int cf_callback_section_module (const char *shortvar, const char *var,
+/*
+ * `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_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");
+               return (LC_CBRET_ERROR);
+       }
+
+       item = (cf_mode_item_t *) extra;
+
+       if (strcasecmp (item->key, shortvar))
+       {
+               fprintf (stderr, "Wrong extra..\n");
+               return (LC_CBRET_ERROR);
+       }
+
+       if ((operating_mode & item->mode) == 0)
+       {
+               fprintf (stderr, "Option `%s' is not valid in this mode!\n", shortvar);
+               return (LC_CBRET_ERROR);
+       }
+
+       if (item->value != NULL)
+       {
+               free (item->value);
+               item->value = NULL;
+       }
+
+       if ((item->value = strdup (value)) == NULL)
+       {
+               perror ("strdup");
+               return (LC_CBRET_ERROR);
+       }
+
+       return (LC_CBRET_OKAY);
+}
+
+/*
+ * `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 (nesting_depth == 0)
+       {
+               fprintf (stderr, ERR_NEEDS_SECTION, shortvar);
+               return (LC_CBRET_ERROR);
+       }
+
+       if (plugin_load (value))
+               syslog (LOG_ERR, "plugin_load (%s): failed to load plugin", value);
+
+       /* Return `okay' even if there was an error, because it's not a syntax
+        * problem.. */
+       return (LC_CBRET_OKAY);
+}
+
+/* XXX think about how to do the command line stuff */
+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);
+
+       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, "cf_callback_mode_switch: Wrong mode!\n");
+               return (LC_CBRET_ERROR);
+       }
+
+       return (LC_CBRET_OKAY);
+}
+
+/*
+ * `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)
 {
@@ -311,47 +497,86 @@ 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)
 {
-       if (filename == NULL)
-               filename = CONFIGFILE;
+       static int run_once = 0;
+       int i;
+
+       if (run_once != 0)
+               return;
+       run_once = 1;
+
+       lc_register_callback ("Help", 'h', LC_VAR_NONE,
+                       cf_callback_usage, NULL);
+
+       lc_register_callback ("Client", 'c', LC_VAR_NONE,
+                       cf_callback_mode_switch, NULL);
+       lc_register_callback ("Local", 'l', LC_VAR_NONE,
+                       cf_callback_mode_switch, NULL);
+       lc_register_callback ("Server", 's', LC_VAR_NONE,
+                       cf_callback_mode_switch, NULL);
 
        lc_register_callback ("Mode", SHORTOPT_NONE, LC_VAR_SECTION,
-                       cf_callback_section_mode, NULL);
+                       cf_callback_mode, NULL);
        lc_register_callback ("Plugin", SHORTOPT_NONE, LC_VAR_SECTION,
-                       cf_callback_section_module, NULL);
-
-       /*
-        * TODO:
-        * - Add more directives, such as `DefaultMode', `DataDir', `PIDFile', ...
-        */
+                       cf_callback_plugin, NULL);
 
+       lc_register_callback ("Mode.PluginDir", 'P',
+                       LC_VAR_STRING, cf_callback_mode_plugindir, NULL);
        lc_register_callback ("Mode.LoadPlugin", SHORTOPT_NONE,
-                       LC_VAR_STRING, cf_callback_loadmodule,
-                       NULL);
+                       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_mode_option, (void *) item);
+       }
+}
+
+int cf_read (int argc, char **argv, char *filename)
+{
+       cf_init ();
+
+       if (filename == NULL)
+               filename = CONFIGFILE;
 
-       if (lc_process_file ("collectd", filename, LC_CONF_APACHE))
+       if (lc_process (argc, argv, "collectd", LC_CONF_APACHE, filename))
        {
                syslog (LOG_ERR, "lc_process_file (%s): %s", filename, lc_geterrstr ());
                return (-1);