Fix #1443
This commit is contained in:
parent
34534d8daa
commit
4c44743620
|
@ -26,6 +26,36 @@ use strict;
|
||||||
use warnings;
|
use warnings;
|
||||||
use centreon::plugins::http;
|
use centreon::plugins::http;
|
||||||
use Time::HiRes qw(gettimeofday tv_interval);
|
use Time::HiRes qw(gettimeofday tv_interval);
|
||||||
|
use centreon::plugins::templates::catalog_functions qw(catalog_status_threshold);
|
||||||
|
|
||||||
|
sub custom_content_threshold {
|
||||||
|
my ($self, %options) = @_;
|
||||||
|
|
||||||
|
$self->{instance_mode}->{content_status} = catalog_status_threshold($self, %options);
|
||||||
|
return $self->{instance_mode}->{content_status};
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
sub custom_content_output {
|
||||||
|
my ($self, %options) = @_;
|
||||||
|
|
||||||
|
my $msg = 'content test';
|
||||||
|
if (!$self->{output}->is_status(value => $self->{instance_mode}->{content_status}, compare => 'ok', litteral => 1)) {
|
||||||
|
my $filter = $self->{instance_mode}->{option_results}->{lc($self->{instance_mode}->{content_status}) . '_content'};
|
||||||
|
$filter =~ s/\$self->\{result_values\}->/%/g;
|
||||||
|
$msg = sprintf("content test [filter: '%s']", $filter);
|
||||||
|
}
|
||||||
|
|
||||||
|
return $msg;
|
||||||
|
}
|
||||||
|
|
||||||
|
sub custom_content_calc {
|
||||||
|
my ($self, %options) = @_;
|
||||||
|
|
||||||
|
$self->{result_values}->{content} = $options{new_datas}->{$self->{instance} . '_content'};
|
||||||
|
$self->{result_values}->{code} = $options{new_datas}->{$self->{instance} . '_code'};
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
sub set_counters {
|
sub set_counters {
|
||||||
my ($self, %options) = @_;
|
my ($self, %options) = @_;
|
||||||
|
@ -35,6 +65,14 @@ sub set_counters {
|
||||||
];
|
];
|
||||||
|
|
||||||
$self->{maps_counters}->{global} = [
|
$self->{maps_counters}->{global} = [
|
||||||
|
{ label => 'content', threshold => 0, set => {
|
||||||
|
key_values => [ { name => 'content' }, { name => 'code' } ],
|
||||||
|
closure_custom_output => $self->can('custom_content_output'),
|
||||||
|
closure_custom_calc => $self->can('custom_content_calc'),
|
||||||
|
closure_custom_perfdata => sub { return 0; },
|
||||||
|
closure_custom_threshold_check => $self->can('custom_content_threshold'),
|
||||||
|
}
|
||||||
|
},
|
||||||
{ label => 'size', display_ok => 0, set => {
|
{ label => 'size', display_ok => 0, set => {
|
||||||
key_values => [ { name => 'size' } ],
|
key_values => [ { name => 'size' } ],
|
||||||
output_template => 'Content size : %s',
|
output_template => 'Content size : %s',
|
||||||
|
@ -80,6 +118,7 @@ sub new {
|
||||||
"username:s" => { name => 'username' },
|
"username:s" => { name => 'username' },
|
||||||
"password:s" => { name => 'password' },
|
"password:s" => { name => 'password' },
|
||||||
"expected-string:s" => { name => 'expected_string' },
|
"expected-string:s" => { name => 'expected_string' },
|
||||||
|
"extracted-pattern:s" => { name => 'extracted_pattern' },
|
||||||
"timeout:s" => { name => 'timeout' },
|
"timeout:s" => { name => 'timeout' },
|
||||||
"no-follow" => { name => 'no_follow', },
|
"no-follow" => { name => 'no_follow', },
|
||||||
"cert-file:s" => { name => 'cert_file' },
|
"cert-file:s" => { name => 'cert_file' },
|
||||||
|
@ -94,6 +133,9 @@ sub new {
|
||||||
"unknown-status:s" => { name => 'unknown_status' },
|
"unknown-status:s" => { name => 'unknown_status' },
|
||||||
"warning-status:s" => { name => 'warning_status' },
|
"warning-status:s" => { name => 'warning_status' },
|
||||||
"critical-status:s" => { name => 'critical_status' },
|
"critical-status:s" => { name => 'critical_status' },
|
||||||
|
"unknown-content:s" => { name => 'unknown_content', default => '' },
|
||||||
|
"warning-content:s" => { name => 'warning_content', default => '' },
|
||||||
|
"critical-content:s" => { name => 'critical_content', default => '' },
|
||||||
});
|
});
|
||||||
|
|
||||||
$self->{http} = centreon::plugins::http->new(%options);
|
$self->{http} = centreon::plugins::http->new(%options);
|
||||||
|
@ -104,11 +146,13 @@ sub check_options {
|
||||||
my ($self, %options) = @_;
|
my ($self, %options) = @_;
|
||||||
$self->SUPER::check_options(%options);
|
$self->SUPER::check_options(%options);
|
||||||
|
|
||||||
if (!defined($self->{option_results}->{expected_string})) {
|
# Legacy compat
|
||||||
$self->{output}->add_option_msg(short_msg => "You need to specify --expected-string option.");
|
if (defined($self->{option_results}->{expected_string}) && $self->{option_results}->{expected_string} ne '') {
|
||||||
$self->{output}->option_exit();
|
$self->{option_results}->{critical_content} = "%{content} =~ /$self->{option_results}->{expected_string}/mi";
|
||||||
}
|
}
|
||||||
|
|
||||||
$self->{http}->set_options(%{$self->{option_results}});
|
$self->{http}->set_options(%{$self->{option_results}});
|
||||||
|
$self->change_macros(macros => ['warning_content', 'critical_content', 'unknown_content']);
|
||||||
}
|
}
|
||||||
|
|
||||||
sub manage_selection {
|
sub manage_selection {
|
||||||
|
@ -118,25 +162,19 @@ sub manage_selection {
|
||||||
my $webcontent = $self->{http}->request();
|
my $webcontent = $self->{http}->request();
|
||||||
my $timeelapsed = tv_interval($timing0, [gettimeofday]);
|
my $timeelapsed = tv_interval($timing0, [gettimeofday]);
|
||||||
|
|
||||||
$self->{global} = { time => $timeelapsed };
|
$self->{global} = { time => $timeelapsed, content => $webcontent, code => $self->{http}->get_code() };
|
||||||
|
|
||||||
$self->{output}->output_add(long_msg => $webcontent);
|
|
||||||
|
|
||||||
if ($webcontent =~ /$self->{option_results}->{expected_string}/mi) {
|
if (defined($self->{option_results}->{extracted_pattern}) && $self->{option_results}->{extracted_pattern} ne '' &&
|
||||||
$self->{output}->output_add(severity => 'OK',
|
$webcontent =~ /$self->{option_results}->{extracted_pattern}/mi) {
|
||||||
short_msg => sprintf("'%s' is present in content.", $self->{option_results}->{expected_string}));
|
my $extracted = $1;
|
||||||
} else {
|
if (defined($extracted) && $extracted =~ /(\d+([\.,]\d+)?)/) {
|
||||||
$self->{output}->output_add(severity => 'CRITICAL',
|
$extracted =~ s/,/\./;
|
||||||
short_msg => sprintf("'%s' is not present in content.", $self->{option_results}->{expected_string}));
|
$self->{global}->{extracted} = $extracted,
|
||||||
}
|
}
|
||||||
|
|
||||||
my $extracted = $1;
|
|
||||||
if (defined($extracted) && $extracted =~ /(\d+([\.,]\d+)?)/) {
|
|
||||||
$extracted = $1;
|
|
||||||
$extracted =~ s/,/\./;
|
|
||||||
$self->{global}->{extracted} = $extracted;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
$self->{output}->output_add(long_msg => $webcontent);
|
||||||
|
|
||||||
# Size check
|
# Size check
|
||||||
{
|
{
|
||||||
require bytes;
|
require bytes;
|
||||||
|
@ -243,6 +281,10 @@ Set POST params (Multiple option. Example: --post-param='key=value')
|
||||||
|
|
||||||
Save cookies in a file (Example: '/tmp/lwp_cookies.dat')
|
Save cookies in a file (Example: '/tmp/lwp_cookies.dat')
|
||||||
|
|
||||||
|
=item B<--extracted-pattern>
|
||||||
|
|
||||||
|
Set pattern to extracted a number (used --warning-extracted and --critical-extracted option).
|
||||||
|
|
||||||
=item B<--unknown-status>
|
=item B<--unknown-status>
|
||||||
|
|
||||||
Threshold warning for http response code (Default: '%{http_code} < 200 or %{http_code} >= 300')
|
Threshold warning for http response code (Default: '%{http_code} < 200 or %{http_code} >= 300')
|
||||||
|
@ -279,9 +321,20 @@ Threshold warning for extracted value
|
||||||
|
|
||||||
Threshold critical for extracted value
|
Threshold critical for extracted value
|
||||||
|
|
||||||
=item B<--expected-string>
|
=item B<--unknown-content>
|
||||||
|
|
||||||
Specify String to check on the Webpage
|
Set warning threshold for content page (Default: '').
|
||||||
|
Can used special variables like: %{content}, %{code}
|
||||||
|
|
||||||
|
=item B<--warning-content>
|
||||||
|
|
||||||
|
Set warning threshold for status (Default: '').
|
||||||
|
Can used special variables like: %{content}, %{code}
|
||||||
|
|
||||||
|
=item B<--critical-content>
|
||||||
|
|
||||||
|
Set critical threshold for content page (Default: '').
|
||||||
|
Can used special variables like: %{content}, %{code}
|
||||||
|
|
||||||
=back
|
=back
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue