X-Git-Url: https://git.octo.it/?a=blobdiff_plain;f=src%2Fconfigfile.c;h=e7eb981d2b44b8162f6402a3205410270390b8d0;hb=97d20120d513c69ed5ce11445e93c184ca29ac5e;hp=0d1569f9d0c83af6c0313e9c657c5b1cf7207fe7;hpb=ae338f19a27a1a560ac8a41ce2de0e63f281f583;p=collectd.git diff --git a/src/configfile.c b/src/configfile.c index 0d1569f9..e7eb981d 100644 --- a/src/configfile.c +++ b/src/configfile.c @@ -20,18 +20,14 @@ * Florian octo Forster **/ -/* - * FIXME: - * - remove all (I mean *ALL*) calls to `fprintf': `stderr' will have been - * closed. - */ - #include "collectd.h" #include "libconfig/libconfig.h" +#include "common.h" #include "plugin.h" #include "configfile.h" +#include "network.h" #include "utils_debug.h" #define SHORTOPT_NONE 0 @@ -44,7 +40,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,17 +54,37 @@ 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; + +/* TODO + * - LogFile + */ +static cf_mode_item_t cf_mode_list[] = +{ + {"TimeToLive", NULL, MODE_CLIENT }, + {"PIDFile", 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 = 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 *, - const char *, lc_flags_t, void *); +/* `cf_register' needs this prototype */ +static 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) +static cf_callback_t *cf_search (char *type) { cf_callback_t *cf_cb; @@ -82,7 +98,7 @@ cf_callback_t *cf_search (char *type) return (cf_cb); } -int cf_dispatch (char *type, const char *orig_key, const char *orig_value) +static int cf_dispatch (char *type, const char *orig_key, const char *orig_value) { cf_callback_t *cf_cb; char *key; @@ -90,9 +106,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.", type); return (-1); } @@ -109,15 +127,20 @@ int cf_dispatch (char *type, const char *orig_key, const char *orig_value) for (i = 0; i < cf_cb->keys_num; i++) { if (strcasecmp (cf_cb->keys[i], key) == 0) + { ret = (*cf_cb->callback) (key, value); + break; + } } 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'.", type, key); free (key); free (value); + DBG ("return (%i)", ret); + return (ret); } @@ -165,13 +188,13 @@ void cf_register (char *type, for (i = 0; i < keys_num; i++) { - if (snprintf (buf, 64, "Module.%s", keys[i]) < 64) + if (snprintf (buf, 64, "Plugin.%s", keys[i]) < 64) { /* This may be called multiple times for the same * `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 @@ -181,81 +204,200 @@ void cf_register (char *type, } } -/* - * Functions for the actual parsing +/* + * Other query functions + */ +char *cf_get_option (const char *key, char *def) +{ + 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) + continue; + + if (cf_mode_list[i].value != NULL) + return (cf_mode_list[i].value); + return (def); + } + + return (NULL); +} + +/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * + * Functions for the actual parsing * + * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */ + +/* + * `cf_callback_mode' + * Chose the `operating_mode' + * + * Mode `value' */ -int cf_callback_general (const char *shortvar, const char *var, +static 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 + * + * + * PluginDir `value' + * + */ +static 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_section_mode (const char *shortvar, const char *var, +static 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 (flags == LC_FLAGS_SECTIONSTART) + if (extra == NULL) { - if (nesting_depth != 0) - { - fprintf (stderr, ERR_NOT_NESTED); - return (LC_CBRET_ERROR); - } + fprintf (stderr, "No extra..?\n"); + return (LC_CBRET_ERROR); + } - if (arguments == NULL) - { - fprintf (stderr, ERR_NEEDS_ARG, shortvar); - return (LC_CBRET_ERROR); - } + item = (cf_mode_item_t *) extra; - nesting_depth++; + if (strcasecmp (item->key, shortvar)) + { + fprintf (stderr, "Wrong extra..\n"); + return (LC_CBRET_ERROR); + } - 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); - } + if ((operating_mode & item->mode) == 0) + { + fprintf (stderr, "Option `%s' is not valid in this mode!\n", shortvar); + return (LC_CBRET_ERROR); } - else if (flags == LC_FLAGS_SECTIONEND) + + if (item->value != NULL) { - nesting_depth--; + free (item->value); + item->value = NULL; + } - return (LC_CBRET_OKAY); + if ((item->value = strdup (value)) == NULL) + { + perror ("strdup"); + return (LC_CBRET_ERROR); } - else + + return (LC_CBRET_OKAY); +} + +/* + * `cf_callback_mode_loadmodule': + * Load a plugin. + * + * + * LoadPlugin `value' + * + */ +static 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 (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); +} + +static int cf_callback_socket (const char *shortvar, const char *var, + const char *arguments, const char *value, lc_flags_t flags, + void *extra) +{ + char *buffer; + + char *fields[3]; + int numfields; + + char *node; + char *service = NET_DEFAULT_PORT; + + DBG ("shortvar = %s, var = %s, arguments = %s, value = %s, ...", + shortvar, var, arguments, value); + + buffer = strdup (value); + if (buffer == NULL) + return (LC_CBRET_ERROR); + + numfields = strsplit (buffer, fields, 3); + + if ((numfields != 1) && (numfields != 2)) { - fprintf (stderr, ERR_SECTION_ONLY, shortvar); + syslog (LOG_ERR, "Invalid number of arguments to `%s'", + shortvar); + free (buffer); return (LC_CBRET_ERROR); } + node = fields[0]; + if (numfields == 2) + service = fields[1]; + + /* Still return `LC_CBRET_OKAY' because this is not an syntax error */ + if (network_create_socket (node, service) < 1) + syslog (LOG_ERR, "network_create_socket (%s, %s) failed", + node, service); + + free (buffer); + + return (LC_CBRET_OKAY); } -int cf_callback_section_module (const char *shortvar, const char *var, +/* + * `cf_callback_plugin' + * Start/end section `plugin' + * + * + * ... + * + */ +static int cf_callback_plugin (const char *shortvar, const char *var, const char *arguments, const char *value, lc_flags_t flags, void *extra) { @@ -294,7 +436,7 @@ int cf_callback_section_module (const char *shortvar, const char *var, if (current_module != NULL) { free (current_module); - current_module == NULL; + current_module = NULL; } nesting_depth--; @@ -308,52 +450,87 @@ 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. + * + * + * `var' `value' + * + */ +static 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 (shortvar)) - 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); } +static void cf_init (void) +{ + static int run_once = 0; + int i; + + if (run_once != 0) + return; + run_once = 1; + + lc_register_callback ("Mode", SHORTOPT_NONE, LC_VAR_STRING, + cf_callback_mode, NULL); + lc_register_callback ("Plugin", SHORTOPT_NONE, LC_VAR_SECTION, + cf_callback_plugin, 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); + + lc_register_callback ("Listen", SHORTOPT_NONE, + LC_VAR_STRING, cf_callback_socket, NULL); + lc_register_callback ("Server", SHORTOPT_NONE, + LC_VAR_STRING, cf_callback_socket, NULL); + + for (i = 0; i < cf_mode_num; i++) + { + cf_mode_item_t *item; + + item = &cf_mode_list[i]; + + 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; - lc_register_callback ("Mode", SHORTOPT_NONE, LC_VAR_SECTION, - cf_callback_section_mode, NULL); - lc_register_callback ("Module", SHORTOPT_NONE, LC_VAR_SECTION, - cf_callback_section_module, NULL); - - /* - * TODO: - * - Add more directives, such as `DefaultMode', `DataDir', `PIDFile', ... - */ - - lc_register_callback ("Mode.LoadModule", SHORTOPT_NONE, - LC_VAR_STRING, cf_callback_loadmodule, - NULL); + 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 ();