diff --git a/centreon-plugins/apps/apache/serverstatus/mode/requests.pm b/centreon-plugins/apps/apache/serverstatus/mode/requests.pm index 4e056a3db..c62bf1709 100644 --- a/centreon-plugins/apps/apache/serverstatus/mode/requests.pm +++ b/centreon-plugins/apps/apache/serverstatus/mode/requests.pm @@ -129,6 +129,8 @@ sub run { $self->{output}->option_exit(); } $rPerSec = '0' . $rPerSec if ($rPerSec =~ /^\./); + $avg_bPerSec = '0' . $avg_bPerSec if ($avg_bPerSec =~ /^\./); + $bPerReq = '0' . $bPerReq if ($bPerReq =~ /^\./); $self->{statefile_value}->read(statefile => 'apache_' . $self->{option_results}->{hostname} . '_' . centreon::plugins::httplib::get_port($self) . '_' . $self->{mode}); my $old_timestamp = $self->{statefile_value}->get(name => 'last_timestamp'); diff --git a/centreon-plugins/centreon/plugins/output.pm b/centreon-plugins/centreon/plugins/output.pm index 18d776fee..e844faeb0 100644 --- a/centreon-plugins/centreon/plugins/output.pm +++ b/centreon-plugins/centreon/plugins/output.pm @@ -564,7 +564,8 @@ sub is_litteral_status { sub create_json_document { my ($self) = @_; - require JSON; + centreon::plugins::misc::mymodule_load(output => $self->{output}, module => 'JSON', + error_msg => "Cannot load module 'JSON'."); $self->{is_output_json} = 1; $self->{json_output} = JSON->new->utf8(); } @@ -572,7 +573,8 @@ sub create_json_document { sub create_xml_document { my ($self) = @_; - require XML::LibXML; + centreon::plugins::misc::mymodule_load(output => $self->{output}, module => 'XML::LibXML', + error_msg => "Cannot load module 'XML::LibXML'."); $self->{is_output_xml} = 1; $self->{xml_output} = XML::LibXML::Document->new('1.0', 'utf-8'); } @@ -678,7 +680,7 @@ sub add_disco_entry { sub is_disco_format { my ($self) = @_; - if (defined($self->{option_results}->{disco_format}) ) { + if (defined($self->{option_results}->{disco_format})) { return 1; } return 0; @@ -687,7 +689,7 @@ sub is_disco_format { sub is_disco_show { my ($self) = @_; - if ( defined($self->{option_results}->{disco_show}) ) { + if (defined($self->{option_results}->{disco_show})) { return 1; } return 0; diff --git a/centreon-plugins/centreon/plugins/statefile.pm b/centreon-plugins/centreon/plugins/statefile.pm index a4379a963..125ab9bf4 100644 --- a/centreon-plugins/centreon/plugins/statefile.pm +++ b/centreon-plugins/centreon/plugins/statefile.pm @@ -39,6 +39,7 @@ use strict; use warnings; use Data::Dumper; use vars qw($datas); +use centreon::plugins::misc; my $default_dir = '/var/lib/centreon/centplugins'; @@ -50,15 +51,18 @@ sub new { if (defined($options{options})) { $options{options}->add_options(arguments => { - "memcached:s" => { name => 'memcached' }, + "memcached:s" => { name => 'memcached' }, "statefile-dir:s" => { name => 'statefile_dir', default => $default_dir }, "statefile-concat-cwd" => { name => 'statefile_concat_cwd' }, + "statefile-storable" => { name => 'statefile_storable' }, }); $options{options}->add_help(package => __PACKAGE__, sections => 'RETENTION OPTIONS', once => 1); } $self->{output} = $options{output}; $self->{datas} = {}; + $self->{storable} = 0; + $self->{memcached_ok} = 0; $self->{memcached} = undef; $self->{statefile_dir} = undef; @@ -70,15 +74,22 @@ sub check_options { my ($self, %options) = @_; if (defined($options{option_results}) && defined($options{option_results}->{memcached})) { - require Memcached::libmemcached; + centreon::plugins::misc::mymodule_load(output => $self->{output}, module => 'Memcached::libmemcached', + error_msg => "Cannot load module 'Memcached::libmemcached'."); $self->{memcached} = Memcached::libmemcached->new(); Memcached::libmemcached::memcached_server_add($self->{memcached}, $options{option_results}->{memcached}); } $self->{statefile_dir} = $options{option_results}->{statefile_dir}; if ($self->{statefile_dir} ne $default_dir && defined($options{option_results}->{statefile_concat_cwd})) { - require Cwd; + centreon::plugins::misc::mymodule_load(output => $self->{output}, module => 'Cwd', + error_msg => "Cannot load module 'Cwd'."); $self->{statefile_dir} = Cwd::cwd() . '/' . $self->{statefile_dir}; } + if (defined($options{option_results}->{statefile_storable})) { + centreon::plugins::misc::mymodule_load(output => $self->{output}, module => 'Storable', + error_msg => "Cannot load module 'Storable'."); + $self->{storable} = 1; + } } sub read { @@ -90,6 +101,7 @@ sub read { # if "SUCCESS" or "NOT FOUND" is ok. Other with use the file my $val = Memcached::libmemcached::memcached_get($self->{memcached}, $self->{statefile_dir} . "/" . $self->{statefile}); if (defined($self->{memcached}->errstr) && $self->{memcached}->errstr =~ /^SUCCESS|NOT FOUND$/i) { + $self->{memcached_ok} = 1; if (defined($val)) { eval( $val ); $self->{datas} = $datas; @@ -98,7 +110,6 @@ sub read { } return 0; } - $self->{memcached_ok} = 0; } if (! -e $self->{statefile_dir} . "/" . $self->{statefile}) { @@ -115,22 +126,36 @@ sub read { return 0; } - unless (my $return = do $self->{statefile_dir} . "/" . $self->{statefile}) { + if ($self->{storable} == 1) { + open FILE, $self->{statefile_dir} . "/" . $self->{statefile}; + eval { + $self->{datas} = Storable::fd_retrieve(*FILE); + }; + # File is corrupted surely. We'll reset it if ($@) { - $self->{output}->add_option_msg(short_msg => "Couldn't parse '" . $self->{statefile_dir} . "/" . $self->{statefile} . "': $@"); - $self->{output}->option_exit(); + close FILE; + return 0; } - unless (defined($return)) { - $self->{output}->add_option_msg(short_msg => "Couldn't do '" . $self->{statefile_dir} . "/" . $self->{statefile} . "': $!"); - $self->{output}->option_exit(); - } - unless ($return) { - $self->{output}->add_option_msg(short_msg => "Couldn't run '" . $self->{statefile_dir} . "/" . $self->{statefile} . "': $!"); - $self->{output}->option_exit(); + close FILE; + } else { + unless (my $return = do $self->{statefile_dir} . "/" . $self->{statefile}) { + # File is corrupted surely. We'll reset it + return 0; + #if ($@) { + # $self->{output}->add_option_msg(short_msg => "Couldn't parse '" . $self->{statefile_dir} . "/" . $self->{statefile} . "': $@"); + # $self->{output}->option_exit(); + #} + #unless (defined($return)) { + # $self->{output}->add_option_msg(short_msg => "Couldn't do '" . $self->{statefile_dir} . "/" . $self->{statefile} . "': $!"); + # $self->{output}->option_exit(); + #} + #unless ($return) { + # $self->{output}->add_option_msg(short_msg => "Couldn't run '" . $self->{statefile_dir} . "/" . $self->{statefile} . "': $!"); + # $self->{output}->option_exit(); } + $self->{datas} = $datas; + $datas = {}; } - $self->{datas} = $datas; - $datas = {}; return 1; } @@ -153,7 +178,7 @@ sub get { sub write { my ($self, %options) = @_; - if (defined($self->{memcached})) { + if ($self->{memcached_ok} == 1) { Memcached::libmemcached::memcached_set($self->{memcached}, $self->{statefile_dir} . "/" . $self->{statefile}, Data::Dumper->Dump([$options{data}], ["datas"])); if (defined($self->{memcached}->errstr) && $self->{memcached}->errstr =~ /^SUCCESS$/i) { @@ -161,7 +186,11 @@ sub write { } } open FILE, ">", $self->{statefile_dir} . "/" . $self->{statefile}; - print FILE Data::Dumper->Dump([$options{data}], ["datas"]); + if ($self->{storable} == 1) { + Storable::store_fd($options{data}, *FILE); + } else { + print FILE Data::Dumper->Dump([$options{data}], ["datas"]); + } close FILE; } @@ -194,6 +223,10 @@ Directory for statefile (Default: '/var/lib/centreon/centplugins'). Concat current working directory with option '--statefile-dir'. Useful on Windows when plugin is compiled. +=item B<--statefile-storable> + +Use Perl Module 'Storable' (instead Data::Dumper) to store datas. + =back =head1 DESCRIPTION