From 5ad9b3ea78ef30b2ec93ebcb3794d21b6f2390aa Mon Sep 17 00:00:00 2001 From: qgarnier Date: Wed, 22 Nov 2017 13:10:07 +0100 Subject: [PATCH] fix #439 --- .../centreon/plugins/statefile.pm | 52 ++++++++++++++++++- 1 file changed, 51 insertions(+), 1 deletion(-) diff --git a/centreon-plugins/centreon/plugins/statefile.pm b/centreon-plugins/centreon/plugins/statefile.pm index 0275fa778..0915ee50a 100644 --- a/centreon-plugins/centreon/plugins/statefile.pm +++ b/centreon-plugins/centreon/plugins/statefile.pm @@ -37,6 +37,9 @@ sub new { $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' }, @@ -67,6 +70,24 @@ sub check_options { $self->{memcached} = Memcached::libmemcached->new(); Memcached::libmemcached::memcached_server_add($self->{memcached}, $options{option_results}->{memcached}); } + + # Check redis + if (defined($options{option_results}->{redis_server})) { + $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}->{$_} . ", "; + } + } + + centreon::plugins::misc::mymodule_load(output => $self->{output}, module => 'Redis', + error_msg => "Cannot load module 'Redis'."); + eval { + $self->{redis_cnx} = Redis->new(server => $options{option_results}->{redis_server}, + eval $self->{redis_attributes}); + }; + } + $self->{statefile_dir} = $options{option_results}->{statefile_dir}; if ($self->{statefile_dir} ne $default_dir && defined($options{option_results}->{statefile_concat_cwd})) { centreon::plugins::misc::mymodule_load(output => $self->{output}, module => 'Cwd', @@ -79,6 +100,7 @@ sub check_options { $self->{storable} = 1; } $self->{statefile_suffix} = $options{option_results}->{statefile_suffix}; + $self->{memexpiration} = $options{option_results}->{memexpiration}; } sub error { @@ -113,6 +135,18 @@ sub read { } } + if (defined($self->{redis_cnx})) { + my $val = $self->{redis_cnx}->get($self->{statefile_dir} . "/" . $self->{statefile}); + if (defined($val)) { + eval($val); + $self->{datas} = $datas; + $datas = {}; + return 1; + } + + return 0; + } + if (! -e $self->{statefile_dir} . "/" . $self->{statefile}) { if (! -w $self->{statefile_dir}) { $self->error(1); @@ -188,11 +222,15 @@ sub write { if ($self->{memcached_ok} == 1) { Memcached::libmemcached::memcached_set($self->{memcached}, $self->{statefile_dir} . "/" . $self->{statefile}, - Data::Dumper->Dump([$options{data}], ["datas"])); + 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"]), + 'EX', $self->{memexpiration}))); + } open FILE, ">", $self->{statefile_dir} . "/" . $self->{statefile}; if ($self->{storable} == 1) { Storable::store_fd($options{data}, *FILE); @@ -222,6 +260,18 @@ Statefile class Memcached server to use (only one server). +=item B<--redis-server> + +Redis server to use (only one server). + +=item B<--redis-attribute> + +Set Redis Options (--redis-attribute="cnx_timeout=5"). + +=item B<--memexpiration> + +Time to keep data in seconds (Default: 86400). + =item B<--statefile-dir> Directory for statefile (Default: '/var/lib/centreon/centplugins').