From 1b5a65328d230599511a5459109019f6f3bb39a0 Mon Sep 17 00:00:00 2001 From: Doug MacEachern Date: Fri, 2 Jan 2009 13:52:35 -0800 Subject: [PATCH] collectd: Add the -T option. The -T switch: - implies -f - does not start any read threads - invokes callback() foreach list_read and will exit 1 on failure, 0 otherwise Makes for a nicer dryrun than -f + ^C, also makes it possible to wrap with a test harness such as Test.pm Signed-off-by: Doug MacEachern Signed-off-by: Florian Forster --- src/collectd.c | 19 +++++++++++++++++-- src/collectd.pod | 5 +++++ src/plugin.c | 32 +++++++++++++++++++++++++++++++- src/plugin.h | 1 + 4 files changed, 54 insertions(+), 3 deletions(-) diff --git a/src/collectd.c b/src/collectd.c index 9526ec91..57701c0c 100644 --- a/src/collectd.c +++ b/src/collectd.c @@ -253,6 +253,7 @@ static void exit_usage (int status) " -C Configuration file.\n" " Default: "CONFIGFILE"\n" " -t Test config and exit.\n" + " -T Test plugin read and exit.\n" " -P PID-file.\n" " Default: "PIDFILE"\n" #if COLLECT_DAEMON @@ -398,6 +399,7 @@ int main (int argc, char **argv) struct sigaction sig_pipe_action; char *configfile = CONFIGFILE; int test_config = 0; + int test_readall = 0; const char *basedir; #if COLLECT_DAEMON struct sigaction sig_chld_action; @@ -410,7 +412,7 @@ int main (int argc, char **argv) { int c; - c = getopt (argc, argv, "htC:" + c = getopt (argc, argv, "htTC:" #if COLLECT_DAEMON "fP:" #endif @@ -427,6 +429,13 @@ int main (int argc, char **argv) case 't': test_config = 1; break; + case 'T': + test_readall = 1; + global_option_set ("ReadThreads", "-1"); +#if COLLECT_DAEMON + daemonize = 0; +#endif /* COLLECT_DAEMON */ + break; #if COLLECT_DAEMON case 'P': global_option_set ("PIDFile", optarg); @@ -580,7 +589,13 @@ int main (int argc, char **argv) * run the actual loops */ do_init (); - do_loop (); + if (test_readall) + { + if (plugin_read_all_once ()) + return (1); + } + else + do_loop (); /* close syslog */ INFO ("Exiting normally"); diff --git a/src/collectd.pod b/src/collectd.pod index c10ae780..e36dcdf3 100644 --- a/src/collectd.pod +++ b/src/collectd.pod @@ -31,6 +31,11 @@ directory. Test the configuration only. The program immediately exits after parsing the config file. A return code not equal to zero indicates an error. +=item B<-T> + +Test the plugin read callbacks only. The program immediately exits after invoking +the read callbacks once. A return code not equal to zero indicates an error. + =item B<-P> Ipid-fileE> Specify an alternative pid file. This overwrites any settings in the config diff --git a/src/plugin.c b/src/plugin.c index 4ad7366c..510f92b1 100644 --- a/src/plugin.c +++ b/src/plugin.c @@ -276,6 +276,9 @@ static void stop_threads (void) { int i; + if (read_threads == NULL) + return; + pthread_mutex_lock (&read_lock); read_loop = 0; DEBUG ("plugin: stop_threads: Signalling `read_cond'"); @@ -635,7 +638,8 @@ void plugin_init_all (void) int num; rt = global_option_get ("ReadThreads"); num = atoi (rt); - start_threads ((num > 0) ? num : 5); + if (num != -1) + start_threads ((num > 0) ? num : 5); } } /* void plugin_init_all */ @@ -678,6 +682,32 @@ void plugin_read_all (void) pthread_mutex_unlock (&read_lock); } /* void plugin_read_all */ +int plugin_read_all_once (void) +{ + llentry_t *le; + read_func_t *rf; + int status; + + if (list_read == NULL) + return (0); + + for (le = llist_head (list_read); + le != NULL; + le = le->next) + { + rf = (read_func_t *) le->value; + status = rf->callback (); + if (status != 0) + { + NOTICE ("read-function of plugin `%s' failed.", + le->key); + return status; + } + } + + return (0); +} /* void plugin_read_all_once */ + int plugin_write (const char *plugin, /* {{{ */ const data_set_t *ds, const value_list_t *vl) { diff --git a/src/plugin.h b/src/plugin.h index a6f89a0e..4f4a3602 100644 --- a/src/plugin.h +++ b/src/plugin.h @@ -181,6 +181,7 @@ int plugin_load (const char *name); void plugin_init_all (void); void plugin_read_all (void); +int plugin_read_all_once (void); void plugin_shutdown_all (void); /* -- 2.30.2