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");
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)
{
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))
{
# 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 */
{"PIDFile", NULL, PIDFILE},
{"Hostname", NULL, NULL},
{"FQDNLookup", NULL, "true"},
- {"Interval", NULL, "10"},
+ {"Interval", NULL, NULL},
{"ReadThreads", NULL, "5"},
{"Timeout", NULL, "2"},
{"PreCacheChain", NULL, "PreCache"},
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:
: 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;
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. */
{
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 {