From 7c802d4975ce2ac7032921bb8f800afc8706d78c Mon Sep 17 00:00:00 2001 From: garnier-quentin Date: Wed, 3 Jul 2019 09:08:54 +0200 Subject: [PATCH] Fix #1562 --- centreon/plugins/statefile.pm | 54 +++++++++++++++++++++-------------- 1 file changed, 32 insertions(+), 22 deletions(-) diff --git a/centreon/plugins/statefile.pm b/centreon/plugins/statefile.pm index e93d28c57..012eb0418 100644 --- a/centreon/plugins/statefile.pm +++ b/centreon/plugins/statefile.pm @@ -34,17 +34,17 @@ sub new { bless $self, $class; 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' }, - "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' }, - }); + $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' }, + }); $options{options}->add_help(package => __PACKAGE__, sections => 'RETENTION OPTIONS', once => 1); } @@ -76,7 +76,7 @@ sub check_options { $self->{redis_attributes} = ''; if (defined($options{option_results}->{redis_attribute})) { foreach (keys %{$options{option_results}->{redis_attribute}}) { - $self->{redis_attributes} .= "$_ => " . $options{option_results}->{redis_attribute}->{$_} . ", "; + $self->{redis_attributes} .= "$_ => " . $options{option_results}->{redis_attribute}->{$_} . ', '; } } @@ -85,6 +85,12 @@ sub check_options { eval { $self->{redis_cnx} = Redis->new(server => $options{option_results}->{redis_server}, eval $self->{redis_attributes}); + if (defined($self->{redis_cnx}) && + defined($options{option_results}->{redis_db}) && + $options{option_results}->{redis_db} ne '' + ) { + $self->{redis_cnx}->select($options{option_results}->{redis_db}); + } }; } @@ -147,7 +153,7 @@ sub read { return 0; } - if (! -e $self->{statefile_dir} . "/" . $self->{statefile}) { + if (! -e $self->{statefile_dir} . '/' . $self->{statefile}) { if (! -w $self->{statefile_dir} || ! -x $self->{statefile_dir}) { $self->error(1); $self->{output}->add_option_msg(short_msg => "Cannot write statefile '" . $self->{statefile_dir} . "/" . $self->{statefile} . "'. Need write/exec permissions on directory."); @@ -156,20 +162,20 @@ sub read { } } return 0; - } elsif (! -w $self->{statefile_dir} . "/" . $self->{statefile}) { + } elsif (! -w $self->{statefile_dir} . '/' . $self->{statefile}) { $self->error(1); $self->{output}->add_option_msg(short_msg => "Cannot write statefile '" . $self->{statefile_dir} . "/" . $self->{statefile} . "'. Need write permissions on file."); if ($self->{no_quit} == 0) { $self->{output}->option_exit(); } return 1; - } elsif (! -s $self->{statefile_dir} . "/" . $self->{statefile}) { + } elsif (! -s $self->{statefile_dir} . '/' . $self->{statefile}) { # Empty file. Not a problem. Maybe plugin not manage not values return 0; } if ($self->{storable} == 1) { - open FILE, $self->{statefile_dir} . "/" . $self->{statefile}; + open FILE, $self->{statefile_dir} . '/' . $self->{statefile}; eval { $self->{datas} = Storable::fd_retrieve(*FILE); }; @@ -180,7 +186,7 @@ sub read { } close FILE; } else { - unless (my $return = do $self->{statefile_dir} . "/" . $self->{statefile}) { + unless (my $return = do $self->{statefile_dir} . '/' . $self->{statefile}) { # File is corrupted surely. We'll reset it return 0; #if ($@) { @@ -221,21 +227,21 @@ sub write { my ($self, %options) = @_; if ($self->{memcached_ok} == 1) { - Memcached::libmemcached::memcached_set($self->{memcached}, $self->{statefile_dir} . "/" . $self->{statefile}, - Data::Dumper->Dump([$options{data}], ["datas"]), $self->{memexpiration}); + Memcached::libmemcached::memcached_set($self->{memcached}, $self->{statefile_dir} . '/' . $self->{statefile}, + Data::Dumper->Dump([$options{data}], ['datas']), $self->{memexpiration}); if (defined($self->{memcached}->errstr) && $self->{memcached}->errstr =~ /^SUCCESS$/i) { return ; } } if (defined($self->{redis_cnx})) { - return if (defined($self->{redis_cnx}->set($self->{statefile_dir} . "/" . $self->{statefile}, Data::Dumper->Dump([$options{data}], ["datas"]), + return if (defined($self->{redis_cnx}->set($self->{statefile_dir} . '/' . $self->{statefile}, Data::Dumper->Dump([$options{data}], ['datas']), 'EX', $self->{memexpiration}))); } - open FILE, ">", $self->{statefile_dir} . "/" . $self->{statefile}; + open FILE, '>', $self->{statefile_dir} . '/' . $self->{statefile}; if ($self->{storable} == 1) { Storable::store_fd($options{data}, *FILE); } else { - print FILE Data::Dumper->Dump([$options{data}], ["datas"]); + print FILE Data::Dumper->Dump([$options{data}], ['datas']); } close FILE; } @@ -268,6 +274,10 @@ Redis server to use (only one server). Set Redis Options (--redis-attribute="cnx_timeout=5"). +=item B<--redis-db> + +Set Redis database index. + =item B<--memexpiration> Time to keep data in seconds (Default: 86400).