summary | shortlog | log | commit | commitdiff | tree
raw | patch | inline | side by side (parent: 2b83d94)
raw | patch | inline | side by side (parent: 2b83d94)
author | octo <octo> | |
Mon, 19 Dec 2005 22:09:12 +0000 (22:09 +0000) | ||
committer | octo <octo> | |
Mon, 19 Dec 2005 22:09:12 +0000 (22:09 +0000) |
`cf_callback_options_mode': Callback for <Mode> options
`cf_get_mode_option': Function to query above options
`cf_get_mode_option': Function to query above options
src/collectd.c | patch | blob | history | |
src/collectd.h | patch | blob | history | |
src/configfile.c | patch | blob | history | |
src/configfile.h | patch | blob | history |
diff --git a/src/collectd.c b/src/collectd.c
index 247a185f08c753de4c11294b94e84ffd48c5591e..0d9a7db872cf8b714383bff404eeb44e64f43f90 100644 (file)
--- a/src/collectd.c
+++ b/src/collectd.c
struct sigaction sigIntAction;
struct sigaction sigTermAction;
char *configfile = CONFIGFILE;
- char *plugindir = PLUGINDIR;
+ char *plugindir = NULL;
char *datadir = PKGLOCALSTATEDIR;
#if COLLECT_DAEMON
char *pidfile = PIDFILE;
DBG_STARTFILE(logfile, "debug file opened.");
+ /* FIXME this is the wrong place to call this function, because
+ * `cf_read' is called below. We'll need an extra callback for this.
+ * Right now though I'm to tired to do this. G'night. -octo */
+ if ((plugindir == NULL) && ((plugindir = cf_get_mode_option ("PluginDir")) == NULL))
+ plugindir = PLUGINDIR;
+
/*
* Read the config file. This will load any modules automagically.
*/
diff --git a/src/collectd.h b/src/collectd.h
index f20a337a2fc050179e14d5d9ccb1f5d2ef1fbe69..a986c0b3dff8d0dc8d3f74a513f8fb56f313bae0 100644 (file)
--- a/src/collectd.h
+++ b/src/collectd.h
#define MODE_SERVER 0x01
#define MODE_CLIENT 0x02
-#define MODE_LOCAL 0x03
+#define MODE_LOCAL 0x04
extern time_t curtime;
extern int operating_mode;
diff --git a/src/configfile.c b/src/configfile.c
index 14b9dfbc4decc2fccfbc8c10efc70d0fcb2868cf..a0610041257525ba50e6013c473dbb357866262b 100644 (file)
--- a/src/configfile.c
+++ b/src/configfile.c
* 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"
#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
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", NULL, MODE_CLIENT | MODE_SERVER | MODE_LOCAL},
+ {"DataDir", NULL, MODE_SERVER | MODE_LOCAL},
+ {"PluginDir", 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_general (const char *, const char *, const char *,
+int cf_callback_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)
{
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);
}
}
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);
* `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_dispatch,
NULL);
}
else
}
}
+/*
+ * Other query functions
+ */
+char *cf_get_mode_option (const char *key)
+{
+ int i;
+
+ for (i = 0; i < cf_mode_num; i++)
+ {
+ if ((cf_mode_list[i].mode & operating_mode) == 0)
+ continue;
+
+ if (strcasecmp (cf_mode_list[i].key, key) == 0)
+ return (cf_mode_list[i].value);
+ }
+
+ return (NULL);
+}
+
/*
* Functions for the actual parsing
*/
-int cf_callback_general (const char *shortvar, const char *var,
+int cf_callback_dispatch (const char *shortvar, const char *var,
const char *arguments, const char *value, lc_flags_t flags,
void *extra)
{
return (LC_CBRET_OKAY);
}
+int cf_callback_options_mode (const char *shortvar, const char *var,
+ const char *arguments, const char *value, lc_flags_t flags,
+ void *extra)
+{
+ cf_mode_item_t *item;
+
+ 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);
+}
+
int cf_callback_section_mode (const char *shortvar, const char *var,
const char *arguments, const char *value, lc_flags_t flags,
void *extra)
int cf_read (char *filename)
{
+ int i;
+
if (filename == NULL)
filename = CONFIGFILE;
lc_register_callback ("Plugin", SHORTOPT_NONE, LC_VAR_SECTION,
cf_callback_section_module, NULL);
- /*
- * TODO:
- * - Add more directives, such as `DefaultMode', `DataDir', `PIDFile', ...
- */
-
lc_register_callback ("Mode.LoadPlugin", SHORTOPT_NONE,
LC_VAR_STRING, cf_callback_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);
+ }
+
if (lc_process_file ("collectd", filename, LC_CONF_APACHE))
{
syslog (LOG_ERR, "lc_process_file (%s): %s", filename, lc_geterrstr ());
diff --git a/src/configfile.h b/src/configfile.h
index 0802ba5ad60070e2081bf619f65763f91d4ce6b0..13eb82b72ac74fe2857bca89dad2f34f6f63bbfa 100644 (file)
--- a/src/configfile.h
+++ b/src/configfile.h
int (*callback) (char *, char *),
char **keys, int keys_num);
+/*
+ * DESCRIPTION
+ * `cf_get_mode_option' returns options from the <Mode> section(s).
+ *
+ * PARAMETERS
+ * `key' Name of the option to query.
+ *
+ * RETURN VALUE
+ * The pointer returned is part of an internal structure and may not be
+ * changed. If the option is not found for whatever reason (wrong key, option
+ * not allowed for currently selected mode, ...) `NULL' is returned.
+ */
+char *cf_get_mode_option (const char *key);
+
/*
* DESCRIPTION
* `cf_read' reads the config file `filename' and dispatches the read