From: octo Date: Sun, 15 Jan 2006 12:20:33 +0000 (+0000) Subject: Fixed a but in the mysql module, so it builds as write-only module, too. X-Git-Tag: collectd-3.6.0~11 X-Git-Url: https://git.tokkee.org/?a=commitdiff_plain;h=3cf46d9689f6fae30e19f3898852083003862552;p=collectd.git Fixed a but in the mysql module, so it builds as write-only module, too. Shorted the command line options to `help' and `dont fork' only. Brought `getopt' back: `libconfig' doesn't do what I want: Command line options are evaluated after config files. So I don't know what mode the daemon will be in when parsing the file. That's why I moved everything into the config file. Removed `cf_callback_usage' again. Changed `cf_read' to not need `argc' and `argv' anymore.. --- diff --git a/src/collectd.c b/src/collectd.c index 71c11677..190e043a 100644 --- a/src/collectd.c +++ b/src/collectd.c @@ -122,6 +122,26 @@ static void update_kstat (void) } /* static void update_kstat (void) */ #endif /* HAVE_LIBKSTAT */ +/* TODO + * Remove all settings but `-f' and `-C' + */ +static void exit_usage (char *name) +{ + printf ("Usage: "PACKAGE" [OPTIONS]\n\n" + + "Available options:\n" + " General:\n" + " -C Configuration file.\n" + " Default: "CONFIGFILE"\n" +#if COLLECT_DAEMON + " -f Don't fork to the background.\n" +#endif + "\n"PACKAGE" "VERSION", http://verplant.org/collectd/\n" + "by Florian octo Forster \n" + "for contributions see `AUTHORS'\n"); + exit (0); +} /* static void exit_usage (char *name) */ + static int start_client (void) { int sleepingtime; @@ -223,13 +243,12 @@ static int pidfile_remove (const char *file) int main (int argc, char **argv) { -#if COLLECT_DAEMON - struct sigaction sigChldAction; -#endif struct sigaction sigIntAction; struct sigaction sigTermAction; - char *datadir; + char *datadir = PKGLOCALSTATEDIR; + char *configfile = CONFIGFILE; #if COLLECT_DAEMON + struct sigaction sigChldAction; char *pidfile = PIDFILE; pid_t pid; int daemonize = 1; @@ -245,7 +264,41 @@ int main (int argc, char **argv) /* open syslog */ openlog (PACKAGE, LOG_CONS | LOG_PID, LOG_DAEMON); - DBG_STARTFILE(logfile, "Debug file opened."); + /* read options */ + while (1) + { + int c; + + /* FIXME */ + c = getopt (argc, argv, "C:" +#if COLLECT_DAEMON + "f" +#endif + ); + + if (c == -1) + break; + + switch (c) + { + case 'C': + configfile = optarg; + break; +#if COLLECT_DAEMON + case 'f': + daemonize = 0; + break; +#endif /* COLLECT_DAEMON */ + case 'h': + default: + exit_usage (argv[0]); + } /* switch (c) */ + } /* while (1) */ + +#if COLLECT_DEBUG + if ((logfile = cf_get_mode_option ("LogFile")) != NULL) + DBG_STARTFILE (logfile, "Debug file opened."); +#endif /* * Read options from the config file, the environment and the command @@ -253,7 +306,7 @@ int main (int argc, char **argv) * general). * Also, this will automatically load modules. */ - if (cf_read (argc, argv, CONFIGFILE)) + if (cf_read (configfile)) { fprintf (stderr, "Error: Reading the config file failed!\n" "Read the syslog for details.\n"); @@ -352,7 +405,10 @@ int main (int argc, char **argv) #endif start_client (); - DBG_STOPFILE("debug file closed."); +#if COLLECT_DEBUG + if (logfile != NULL) + DBG_STOPFILE("debug file closed."); +#endif /* close syslog */ syslog (LOG_INFO, "Exiting normally"); diff --git a/src/collectd.h b/src/collectd.h index a986c0b3..1697df70 100644 --- a/src/collectd.h +++ b/src/collectd.h @@ -181,6 +181,10 @@ #define PIDFILE PREFIX "/var/run/" PACKAGE_NAME ".pid" #endif +#ifndef LOGFILE +#define LOGFILE PREFIX"/var/log/"PACKAGE_NAME"/"PACKAGE_NAME".log" +#endif + #ifndef PLUGINDIR #define PLUGINDIR PREFIX "/lib/" PACKAGE_NAME #endif diff --git a/src/configfile.c b/src/configfile.c index e3341dd4..21fcc8d3 100644 --- a/src/configfile.c +++ b/src/configfile.c @@ -20,6 +20,9 @@ * Florian octo Forster **/ +/* TODO + * make internal-only functions `static' */ + #include "collectd.h" #include "libconfig/libconfig.h" @@ -61,14 +64,14 @@ typedef struct cf_mode_item /* TODO * - LogFile - * - DontFork */ 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} + {"DataDir", PKGLOCALSTATEDIR, MODE_SERVER | MODE_LOCAL}, + {"LogFile", LOGFILE, MODE_SERVER | MODE_SERVER | MODE_LOCAL}, }; static int cf_mode_num = 4; @@ -220,58 +223,15 @@ char *cf_get_mode_option (const char *key) return (NULL); } -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 PID file.\n" - " Default: "PIDFILE"\n" -#endif - " -M Module/Plugin directory.\n" - " Default: "PLUGINDIR"\n" - " -D Data storage directory.\n" - " Default: "PKGLOCALSTATEDIR"\n" -#if COLLECT_DEBUG - " -L 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 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 \n" - "for contributions see `AUTHORS'\n"); - exit (0); -} /* exit_usage */ - /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * Functions for the actual parsing * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */ /* * `cf_callback_mode' - * Start/end the `mode' section + * Chose the `operating_mode' * - * - * ... - * + * Mode `value' */ int cf_callback_mode (const char *shortvar, const char *var, const char *arguments, const char *value, lc_flags_t flags, @@ -280,48 +240,19 @@ int cf_callback_mode (const char *shortvar, const char *var, 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); - } - - nesting_depth++; - - 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--; - - return (LC_CBRET_OKAY); - } + 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_SECTION_ONLY, shortvar); + syslog (LOG_ERR, "Invalid value for config option `Mode': `%s'", value); return (LC_CBRET_ERROR); } + return (LC_CBRET_OKAY); } /* @@ -403,12 +334,6 @@ int cf_callback_mode_loadmodule (const char *shortvar, const char *var, 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); @@ -545,24 +470,21 @@ void cf_init (void) return; run_once = 1; - lc_register_callback ("Help", 'h', LC_VAR_NONE, - cf_callback_usage, NULL); - - lc_register_callback ("Client", 'c', LC_VAR_NONE, + lc_register_callback ("Client", SHORTOPT_NONE, LC_VAR_NONE, cf_callback_mode_switch, NULL); - lc_register_callback ("Local", 'l', LC_VAR_NONE, + lc_register_callback ("Local", SHORTOPT_NONE, LC_VAR_NONE, cf_callback_mode_switch, NULL); - lc_register_callback ("Server", 's', LC_VAR_NONE, + lc_register_callback ("Server", SHORTOPT_NONE, LC_VAR_NONE, cf_callback_mode_switch, NULL); - lc_register_callback ("Mode", SHORTOPT_NONE, LC_VAR_SECTION, + 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 ("Mode.PluginDir", 'P', + lc_register_callback ("PluginDir", SHORTOPT_NONE, LC_VAR_STRING, cf_callback_mode_plugindir, NULL); - lc_register_callback ("Mode.LoadPlugin", SHORTOPT_NONE, + lc_register_callback ("LoadPlugin", SHORTOPT_NONE, LC_VAR_STRING, cf_callback_mode_loadmodule, NULL); for (i = 0; i < cf_mode_num; i++) @@ -580,19 +502,24 @@ void cf_init (void) } } -int cf_read (int argc, char **argv, char *filename) +int cf_read (char *filename) { cf_init (); if (filename == NULL) filename = CONFIGFILE; - if (lc_process (argc, argv, "collectd", LC_CONF_APACHE, filename)) + 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 (); diff --git a/src/configfile.h b/src/configfile.h index 7d89fadb..e9d31104 100644 --- a/src/configfile.h +++ b/src/configfile.h @@ -82,8 +82,6 @@ char *cf_get_mode_option (const char *key); * to load specific plugins, depending on the current mode of operation. * * PARAMETERS - * `argc' Same as `argc' passed to `main' - * `argv' Same as `argv' passed to `main' * `filename' An additional filename to look for. This function calls * `lc_process' which already searches many standard locations.. * If set to NULL will use the `CONFIGFILE' define. @@ -92,6 +90,6 @@ char *cf_get_mode_option (const char *key); * Returns zero upon success and non-zero otherwise. A error-message will have * been printed in this case. */ -int cf_read (int argc, char **argv, char *filename); +int cf_read (char *filename); #endif /* defined(CONFIGFILE_H) */ diff --git a/src/mysql.c b/src/mysql.c index 42429e98..83163d58 100644 --- a/src/mysql.c +++ b/src/mysql.c @@ -44,7 +44,9 @@ static char *user; static char *pass; static char *db = NULL; +#if MYSQL_HAVE_READ static char init_suceeded = 0; +#endif static char *commands_file = "mysql/mysql_commands-%s.rrd"; static char *handler_file = "mysql/mysql_handler-%s.rrd";