This commit is contained in:
garnier-quentin 2019-07-03 09:08:54 +02:00
parent 810c9ca791
commit 7c802d4975
1 changed files with 32 additions and 22 deletions

View File

@ -34,16 +34,16 @@ sub new {
bless $self, $class; bless $self, $class;
if (defined($options{options})) { if (defined($options{options})) {
$options{options}->add_options(arguments => $options{options}->add_options(arguments => {
{ 'memcached:s' => { name => 'memcached' },
"memcached:s" => { name => 'memcached' }, 'redis-server:s' => { name => 'redis_server' },
"redis-server:s" => { name => 'redis_server' },
'redis-attribute:s%' => { name => 'redis_attribute' }, 'redis-attribute:s%' => { name => 'redis_attribute' },
"memexpiration:s" => { name => 'memexpiration', default => 86400 }, 'redis-db:s' => { name => 'redis_db' },
"statefile-dir:s" => { name => 'statefile_dir', default => $default_dir }, 'memexpiration:s' => { name => 'memexpiration', default => 86400 },
"statefile-suffix:s" => { name => 'statefile_suffix', default => '' }, 'statefile-dir:s' => { name => 'statefile_dir', default => $default_dir },
"statefile-concat-cwd" => { name => 'statefile_concat_cwd' }, 'statefile-suffix:s' => { name => 'statefile_suffix', default => '' },
"statefile-storable" => { name => 'statefile_storable' }, 'statefile-concat-cwd' => { name => 'statefile_concat_cwd' },
'statefile-storable' => { name => 'statefile_storable' },
}); });
$options{options}->add_help(package => __PACKAGE__, sections => 'RETENTION OPTIONS', once => 1); $options{options}->add_help(package => __PACKAGE__, sections => 'RETENTION OPTIONS', once => 1);
} }
@ -76,7 +76,7 @@ sub check_options {
$self->{redis_attributes} = ''; $self->{redis_attributes} = '';
if (defined($options{option_results}->{redis_attribute})) { if (defined($options{option_results}->{redis_attribute})) {
foreach (keys %{$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 { eval {
$self->{redis_cnx} = Redis->new(server => $options{option_results}->{redis_server}, $self->{redis_cnx} = Redis->new(server => $options{option_results}->{redis_server},
eval $self->{redis_attributes}); 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; 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}) { if (! -w $self->{statefile_dir} || ! -x $self->{statefile_dir}) {
$self->error(1); $self->error(1);
$self->{output}->add_option_msg(short_msg => "Cannot write statefile '" . $self->{statefile_dir} . "/" . $self->{statefile} . "'. Need write/exec permissions on directory."); $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; return 0;
} elsif (! -w $self->{statefile_dir} . "/" . $self->{statefile}) { } elsif (! -w $self->{statefile_dir} . '/' . $self->{statefile}) {
$self->error(1); $self->error(1);
$self->{output}->add_option_msg(short_msg => "Cannot write statefile '" . $self->{statefile_dir} . "/" . $self->{statefile} . "'. Need write permissions on file."); $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) { if ($self->{no_quit} == 0) {
$self->{output}->option_exit(); $self->{output}->option_exit();
} }
return 1; 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 # Empty file. Not a problem. Maybe plugin not manage not values
return 0; return 0;
} }
if ($self->{storable} == 1) { if ($self->{storable} == 1) {
open FILE, $self->{statefile_dir} . "/" . $self->{statefile}; open FILE, $self->{statefile_dir} . '/' . $self->{statefile};
eval { eval {
$self->{datas} = Storable::fd_retrieve(*FILE); $self->{datas} = Storable::fd_retrieve(*FILE);
}; };
@ -180,7 +186,7 @@ sub read {
} }
close FILE; close FILE;
} else { } 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 # File is corrupted surely. We'll reset it
return 0; return 0;
#if ($@) { #if ($@) {
@ -221,21 +227,21 @@ sub write {
my ($self, %options) = @_; my ($self, %options) = @_;
if ($self->{memcached_ok} == 1) { if ($self->{memcached_ok} == 1) {
Memcached::libmemcached::memcached_set($self->{memcached}, $self->{statefile_dir} . "/" . $self->{statefile}, Memcached::libmemcached::memcached_set($self->{memcached}, $self->{statefile_dir} . '/' . $self->{statefile},
Data::Dumper->Dump([$options{data}], ["datas"]), $self->{memexpiration}); Data::Dumper->Dump([$options{data}], ['datas']), $self->{memexpiration});
if (defined($self->{memcached}->errstr) && $self->{memcached}->errstr =~ /^SUCCESS$/i) { if (defined($self->{memcached}->errstr) && $self->{memcached}->errstr =~ /^SUCCESS$/i) {
return ; return ;
} }
} }
if (defined($self->{redis_cnx})) { 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}))); 'EX', $self->{memexpiration})));
} }
open FILE, ">", $self->{statefile_dir} . "/" . $self->{statefile}; open FILE, '>', $self->{statefile_dir} . '/' . $self->{statefile};
if ($self->{storable} == 1) { if ($self->{storable} == 1) {
Storable::store_fd($options{data}, *FILE); Storable::store_fd($options{data}, *FILE);
} else { } else {
print FILE Data::Dumper->Dump([$options{data}], ["datas"]); print FILE Data::Dumper->Dump([$options{data}], ['datas']);
} }
close FILE; close FILE;
} }
@ -268,6 +274,10 @@ Redis server to use (only one server).
Set Redis Options (--redis-attribute="cnx_timeout=5"). Set Redis Options (--redis-attribute="cnx_timeout=5").
=item B<--redis-db>
Set Redis database index.
=item B<--memexpiration> =item B<--memexpiration>
Time to keep data in seconds (Default: 86400). Time to keep data in seconds (Default: 86400).