This commit is contained in:
qgarnier 2017-11-22 13:10:07 +01:00
parent 0a0b919afc
commit e9a884c041
1 changed files with 51 additions and 1 deletions

View File

@ -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').