From 9cb2694409620ae217ede92f78fe45c9eba83504 Mon Sep 17 00:00:00 2001 From: Florian Forster Date: Thu, 15 Nov 2012 13:55:04 +0100 Subject: [PATCH] src/configfile.[ch]: Implement the cf_get_default_interval() function. This should be able to replace "interval_g" in a global (i.e. non-plugin) context, such as the interval in which timeouts are being checked. The default value (10 seconds) is also configurable at compile time using a define. --- src/collectd.c | 31 +++++++------------------------ src/collectd.h | 4 ++++ src/configfile.c | 28 ++++++++++++++++++++++++++-- src/configfile.h | 2 ++ src/plugin.c | 16 +--------------- 5 files changed, 40 insertions(+), 41 deletions(-) diff --git a/src/collectd.c b/src/collectd.c index c3dc7381..cdb0ee89 100644 --- a/src/collectd.c +++ b/src/collectd.c @@ -137,28 +137,10 @@ static int init_hostname (void) static int init_global_variables (void) { - const char *str; - - str = global_option_get ("Interval"); - if (str == NULL) - { - interval_g = TIME_T_TO_CDTIME_T (10); - } - else - { - double tmp; - - tmp = atof (str); - if (tmp <= 0.0) - { - fprintf (stderr, "Cannot set the interval to a " - "correct value.\n" - "Please check your settings.\n"); - return (-1); - } + char const *str; - interval_g = DOUBLE_TO_CDTIME_T (tmp); - } + interval_g = cf_get_default_interval (); + assert (interval_g > 0); DEBUG ("interval_g = %.3f;", CDTIME_T_TO_DOUBLE (interval_g)); str = global_option_get ("Timeout"); @@ -323,9 +305,10 @@ static int do_init (void) static int do_loop (void) { + cdtime_t interval = cf_get_default_interval (); cdtime_t wait_until; - wait_until = cdtime () + interval_g; + wait_until = cdtime () + interval; while (loop == 0) { @@ -345,12 +328,12 @@ static int do_loop (void) WARNING ("Not sleeping because the next interval is " "%.3f seconds in the past!", CDTIME_T_TO_DOUBLE (now - wait_until)); - wait_until = now + interval_g; + wait_until = now + interval; continue; } CDTIME_T_TO_TIMESPEC (wait_until - now, &ts_wait); - wait_until = wait_until + interval_g; + wait_until = wait_until + interval; while ((loop == 0) && (nanosleep (&ts_wait, &ts_wait) != 0)) { diff --git a/src/collectd.h b/src/collectd.h index 4079ad1f..c0994d19 100644 --- a/src/collectd.h +++ b/src/collectd.h @@ -258,6 +258,10 @@ typedef int _Bool; # define COLLECTD_GRP_NAME "collectd" #endif +#ifndef COLLECTD_DEFAULT_INTERVAL +# define COLLECTD_DEFAULT_INTERVAL 10.0 +#endif + #define STATIC_ARRAY_LEN(array) (sizeof (array) / sizeof ((array)[0])) /* Remove GNU specific __attribute__ settings when using another compiler */ diff --git a/src/configfile.c b/src/configfile.c index b09bee8d..e22daf38 100644 --- a/src/configfile.c +++ b/src/configfile.c @@ -98,7 +98,7 @@ static cf_global_option_t cf_global_options[] = {"PIDFile", NULL, PIDFILE}, {"Hostname", NULL, NULL}, {"FQDNLookup", NULL, "true"}, - {"Interval", NULL, "10"}, + {"Interval", NULL, NULL}, {"ReadThreads", NULL, "5"}, {"Timeout", NULL, "2"}, {"PreCacheChain", NULL, "PreCache"}, @@ -265,7 +265,8 @@ static int dispatch_loadplugin (const oconfig_item_t *ci) name = ci->values[0].value.string; /* default to the global interval set before loading this plugin */ - ctx.interval = plugin_get_interval (); + memset (&ctx, 0, sizeof (ctx)); + ctx.interval = cf_get_default_interval (); /* * XXX: Magic at work: @@ -867,6 +868,29 @@ const char *global_option_get (const char *option) : cf_global_options[i].def); } /* char *global_option_get */ +cdtime_t cf_get_default_interval (void) +{ + char const *str = global_option_get ("Interval"); + double interval_double = COLLECTD_DEFAULT_INTERVAL; + + if (str != NULL) + { + char *endptr = NULL; + double tmp = strtod (str, &endptr); + + if ((endptr == NULL) || (endptr == str) || (*endptr != 0)) + ERROR ("cf_get_default_interval: Unable to parse string \"%s\" " + "as number.", str); + else if (tmp <= 0.0) + ERROR ("cf_get_default_interval: Interval must be a positive number. " + "The current number is %g.", tmp); + else + interval_double = tmp; + } + + return (DOUBLE_TO_CDTIME_T (interval_double)); +} /* }}} cdtime_t cf_get_default_interval */ + void cf_unregister (const char *type) { cf_callback_t *this, *prev; diff --git a/src/configfile.h b/src/configfile.h index 59ea5542..b44a738c 100644 --- a/src/configfile.h +++ b/src/configfile.h @@ -87,6 +87,8 @@ int cf_read (char *filename); int global_option_set (const char *option, const char *value); const char *global_option_get (const char *option); +cdtime_t cf_get_default_interval (void); + /* Assures the config option is a string, duplicates it and returns the copy in * "ret_string". If necessary "*ret_string" is freed first. Returns zero upon * success. */ diff --git a/src/plugin.c b/src/plugin.c index 22f89696..f042b125 100644 --- a/src/plugin.c +++ b/src/plugin.c @@ -2074,25 +2074,11 @@ cdtime_t plugin_get_interval (void) { cdtime_t interval; - const char *interval_str; - double interval_dbl; - interval = plugin_get_ctx().interval; if (interval > 0) return interval; - /* this should happen during initialization only */ - interval_str = global_option_get ("Interval"); - if (interval_str != NULL) - { - interval_dbl = atof (interval_str); - if (interval_dbl > 0.0) - interval = DOUBLE_TO_CDTIME_T (interval_dbl); - } - - if (interval > 0) - return interval; - return TIME_T_TO_CDTIME_T (10); + return cf_get_default_interval (); } /* cdtime_t plugin_get_interval */ typedef struct { -- 2.30.2