From f6cc6f07f01f56aa8b87b8fd505fffc7c69ebe70 Mon Sep 17 00:00:00 2001 From: garnier-quentin Date: Mon, 15 Jun 2020 09:20:24 +0200 Subject: [PATCH] Fix #2051 --- .../centreon/plugins/statefile.pm | 30 ++++++++++++------- 1 file changed, 20 insertions(+), 10 deletions(-) diff --git a/centreon-plugins/centreon/plugins/statefile.pm b/centreon-plugins/centreon/plugins/statefile.pm index f70227e7f..6efcb2aeb 100644 --- a/centreon-plugins/centreon/plugins/statefile.pm +++ b/centreon-plugins/centreon/plugins/statefile.pm @@ -35,15 +35,16 @@ sub new { if (defined($options{options})) { $options{options}->add_options(arguments => { - 'memcached:s' => { name => 'memcached' }, - 'redis-server:s' => { name => 'redis_server' }, - 'redis-attribute:s%' => { name => 'redis_attribute' }, - 'redis-db:s' => { name => 'redis_db' }, - 'memexpiration:s' => { name => 'memexpiration', default => 86400 }, - 'statefile-dir:s' => { name => 'statefile_dir', default => $default_dir }, - 'statefile-suffix:s' => { name => 'statefile_suffix', default => '' }, - 'statefile-concat-cwd' => { name => 'statefile_concat_cwd' }, - 'statefile-storable' => { name => 'statefile_storable' }, + 'memcached:s' => { name => 'memcached' }, + 'redis-server:s' => { name => 'redis_server' }, + 'redis-attribute:s%' => { name => 'redis_attribute' }, + 'redis-db:s' => { name => 'redis_db' }, + 'memexpiration:s' => { name => 'memexpiration', default => 86400 }, + 'statefile-dir:s' => { name => 'statefile_dir', default => $default_dir }, + 'statefile-suffix:s' => { name => 'statefile_suffix', default => '' }, + 'statefile-concat-cwd' => { name => 'statefile_concat_cwd' }, + 'statefile-storable' => { name => 'statefile_storable' }, + 'failback-file' => { name => 'failback_file' } }); $options{options}->add_help(package => __PACKAGE__, sections => 'RETENTION OPTIONS', once => 1); } @@ -89,6 +90,7 @@ sub check_options { error_msg => "Cannot load module 'Redis'." ); eval { + $options{option_results}->{redis_server} .= ':6379' if ($options{option_results}->{redis_server} !~ /:\d+$/); $self->{redis_cnx} = Redis->new( server => $options{option_results}->{redis_server}, eval $self->{redis_attributes} @@ -100,6 +102,10 @@ sub check_options { $self->{redis_cnx}->select($options{option_results}->{redis_db}); } }; + if (!defined($self->{redis_cnx}) && !defined($options{option_results}->{failback_file})) { + $self->{output}->add_option_msg(short_msg => "redis connection issue: $@"); + $self->{output}->option_exit(); + } } $self->{statefile_dir} = $options{option_results}->{statefile_dir}; @@ -285,7 +291,7 @@ Memcached server to use (only one server). =item B<--redis-server> -Redis server to use (only one server). +Redis server to use (only one server). SYntax: address[:port] =item B<--redis-attribute> @@ -295,6 +301,10 @@ Set Redis Options (--redis-attribute="cnx_timeout=5"). Set Redis database index. +=item B<--failback-file> + +Failback on a local file if redis connection failed. + =item B<--memexpiration> Time to keep data in seconds (Default: 86400).