add data option for expected content

This commit is contained in:
garnier-quentin 2019-11-25 17:23:29 +01:00
parent 9cbc3133fa
commit 3836dfd0be
3 changed files with 121 additions and 63 deletions

View File

@ -35,7 +35,6 @@ sub custom_content_threshold {
return $self->{instance_mode}->{content_status}; return $self->{instance_mode}->{content_status};
} }
sub custom_content_output { sub custom_content_output {
my ($self, %options) = @_; my ($self, %options) = @_;
@ -108,45 +107,76 @@ sub new {
bless $self, $class; bless $self, $class;
$options{options}->add_options(arguments => { $options{options}->add_options(arguments => {
"hostname:s" => { name => 'hostname' }, 'hostname:s' => { name => 'hostname' },
"port:s" => { name => 'port', }, 'port:s' => { name => 'port', },
"method:s" => { name => 'method' }, 'method:s' => { name => 'method' },
"proto:s" => { name => 'proto' }, 'proto:s' => { name => 'proto' },
"urlpath:s" => { name => 'url_path' }, 'urlpath:s' => { name => 'url_path' },
"credentials" => { name => 'credentials' }, 'credentials' => { name => 'credentials' },
"basic" => { name => 'basic' }, 'basic' => { name => 'basic' },
"ntlmv2" => { name => 'ntlmv2' }, 'ntlmv2' => { name => 'ntlmv2' },
"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' }, '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' },
"key-file:s" => { name => 'key_file' }, 'key-file:s' => { name => 'key_file' },
"cacert-file:s" => { name => 'cacert_file' }, 'cacert-file:s' => { name => 'cacert_file' },
"cert-pwd:s" => { name => 'cert_pwd' }, 'cert-pwd:s' => { name => 'cert_pwd' },
"cert-pkcs12" => { name => 'cert_pkcs12' }, 'cert-pkcs12' => { name => 'cert_pkcs12' },
"header:s@" => { name => 'header' }, 'data:s' => { name => 'data' },
"get-param:s@" => { name => 'get_param' }, 'header:s@' => { name => 'header' },
"post-param:s@" => { name => 'post_param' }, 'get-param:s@' => { name => 'get_param' },
"cookies-file:s" => { name => 'cookies_file' }, 'post-param:s@' => { name => 'post_param' },
"unknown-status:s" => { name => 'unknown_status' }, 'cookies-file:s' => { name => 'cookies_file' },
"warning-status:s" => { name => 'warning_status' }, 'unknown-status:s' => { name => 'unknown_status' },
"critical-status:s" => { name => 'critical_status' }, 'warning-status:s' => { name => 'warning_status' },
"unknown-content:s" => { name => 'unknown_content', default => '' }, 'critical-status:s' => { name => 'critical_status' },
"warning-content:s" => { name => 'warning_content', default => '' }, 'unknown-content:s' => { name => 'unknown_content', default => '' },
"critical-content:s" => { name => 'critical_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);
return $self; return $self;
} }
sub load_request {
my ($self, %options) = @_;
$self->{options_request} = {};
if (defined($self->{option_results}->{data}) && $self->{option_results}->{data} ne '') {
$self->{option_results}->{method} = 'POST';
if (-f $self->{option_results}->{data} and -r $self->{option_results}->{data}) {
local $/ = undef;
my $fh;
if (!open($fh, "<:encoding(UTF-8)", $self->{option_results}->{data})) {
$self->{output}->output_add(
severity => 'UNKNOWN',
short_msg => sprintf("Could not read file '%s': %s", $self->{option_results}->{data}, $!)
);
$self->{output}->display();
$self->{output}->exit();
}
my $file_content = <$fh>;
close $fh;
$/ = "\n";
chomp $file_content;
$self->{options_request}->{query_form_post} = $file_content;
} else {
$self->{options_request}->{query_form_post} = $self->{option_results}->{data};
}
}
}
sub check_options { sub check_options {
my ($self, %options) = @_; my ($self, %options) = @_;
$self->SUPER::check_options(%options); $self->SUPER::check_options(%options);
$self->load_request();
# Legacy compat # Legacy compat
if (defined($self->{option_results}->{expected_string}) && $self->{option_results}->{expected_string} ne '') { if (defined($self->{option_results}->{expected_string}) && $self->{option_results}->{expected_string} ne '') {
$self->{option_results}->{critical_content} = "%{content} !~ /$self->{option_results}->{expected_string}/mi"; $self->{option_results}->{critical_content} = "%{content} !~ /$self->{option_results}->{expected_string}/mi";
@ -160,7 +190,7 @@ sub manage_selection {
my ($self, %options) = @_; my ($self, %options) = @_;
my $timing0 = [gettimeofday]; my $timing0 = [gettimeofday];
my $webcontent = $self->{http}->request(); my $webcontent = $self->{http}->request(%{$self->{options_request}});
my $timeelapsed = tv_interval($timing0, [gettimeofday]); my $timeelapsed = tv_interval($timing0, [gettimeofday]);
$self->{global} = { $self->{global} = {
@ -272,6 +302,10 @@ Specify certificate's password
Specify type of certificate (PKCS12) Specify type of certificate (PKCS12)
=item B<--data>
Set POST data request (For a JSON data, add following option: --header='Content-Type: application/json')
=item B<--header> =item B<--header>
Set HTTP headers (Multiple option) Set HTTP headers (Multiple option)

View File

@ -164,8 +164,10 @@ sub display_output {
} }
$format =~ s/%\{$1\}/$replace/g; $format =~ s/%\{$1\}/$replace/g;
} }
$self->{output}->output_add(severity => $severity, $self->{output}->output_add(
short_msg => $format); severity => $severity,
short_msg => $format
);
} }
} }
@ -207,32 +209,44 @@ sub lookup {
} }
if ($self->{option_results}->{threshold_value} eq 'count') { if ($self->{option_results}->{threshold_value} eq 'count') {
my $exit = lc($self->{perfdata}->threshold_check(value => $self->{count}, my $exit = lc(
threshold => [ { label => 'critical-numeric', exit_litteral => 'critical' }, { label => 'warning-numeric', exit_litteral => 'warning' } ])); $self->{perfdata}->threshold_check(
value => $self->{count},
threshold => [ { label => 'critical-numeric', exit_litteral => 'critical' }, { label => 'warning-numeric', exit_litteral => 'warning' } ]
)
);
push @{$self->{'values_' . $exit}}, $self->{count}; push @{$self->{'values_' . $exit}}, $self->{count};
$self->{'count_' . $exit}++; $self->{'count_' . $exit}++;
} }
$self->{output}->perfdata_add(label => 'count', $self->{output}->perfdata_add(
value => $self->{count}, label => 'count',
warning => $self->{option_results}->{threshold_value} eq 'count' ? $self->{perfdata}->get_perfdata_for_output(label => 'warning-numeric') : undef, value => $self->{count},
critical => $self->{option_results}->{threshold_value} eq 'count' ? $self->{perfdata}->get_perfdata_for_output(label => 'critical-numeric') : undef, warning => $self->{option_results}->{threshold_value} eq 'count' ? $self->{perfdata}->get_perfdata_for_output(label => 'warning-numeric') : undef,
min => 0); critical => $self->{option_results}->{threshold_value} eq 'count' ? $self->{perfdata}->get_perfdata_for_output(label => 'critical-numeric') : undef,
min => 0
);
my $count = 0; my $count = 0;
foreach my $value (@{$self->{values}}) { foreach my $value (@{$self->{values}}) {
$count++; $count++;
if ($value =~ /^[0-9.]+$/) { if ($value =~ /^[0-9.]+$/) {
if ($self->{option_results}->{threshold_value} eq 'values') { if ($self->{option_results}->{threshold_value} eq 'values') {
my $exit = lc($self->{perfdata}->threshold_check(value => $value, my $exit = lc(
threshold => [ { label => 'critical-numeric', exit_litteral => 'critical' }, { label => 'warning-numeric', exit_litteral => 'warning' } ])); $self->{perfdata}->threshold_check(
value => $value,
threshold => [ { label => 'critical-numeric', exit_litteral => 'critical' }, { label => 'warning-numeric', exit_litteral => 'warning' } ]
)
);
push @{$self->{'values_' . $exit}}, $value; push @{$self->{'values_' . $exit}}, $value;
$self->{'count_' . $exit}++ $self->{'count_' . $exit}++
} }
$self->{output}->perfdata_add(label => 'element_' . $count, $self->{output}->perfdata_add(
value => $value, label => 'element_' . $count,
warning => $self->{option_results}->{threshold_value} eq 'values' ? $self->{perfdata}->get_perfdata_for_output(label => 'warning-numeric') : undef, value => $value,
critical => $self->{option_results}->{threshold_value} eq 'values' ? $self->{perfdata}->get_perfdata_for_output(label => 'critical-numeric') : undef); warning => $self->{option_results}->{threshold_value} eq 'values' ? $self->{perfdata}->get_perfdata_for_output(label => 'warning-numeric') : undef,
critical => $self->{option_results}->{threshold_value} eq 'values' ? $self->{perfdata}->get_perfdata_for_output(label => 'critical-numeric') : undef
);
} else { } else {
if (defined($self->{option_results}->{critical_string}) && $self->{option_results}->{critical_string} ne '' && if (defined($self->{option_results}->{critical_string}) && $self->{option_results}->{critical_string} ne '' &&
$value =~ /$self->{option_results}->{critical_string}/) { $value =~ /$self->{option_results}->{critical_string}/) {
@ -294,12 +308,14 @@ sub lookup_perfdata_nagios {
# separate the value from the unit # separate the value from the unit
my ($value, $unit) = $value_w_unit =~ /(^[0-9]+\.*\,*[0-9]*)(.*)/g; my ($value, $unit) = $value_w_unit =~ /(^[0-9]+\.*\,*[0-9]*)(.*)/g;
$self->{output}->perfdata_add(label => $label, unit => $unit, $self->{output}->perfdata_add(
value => $value, label => $label, unit => $unit,
warning => $warn, value => $value,
critical => $crit, warning => $warn,
min => $min, critical => $crit,
max => $max); min => $min,
max => $max
);
} }
} }
@ -314,25 +330,31 @@ sub run {
$self->{output}->output_add(long_msg => $self->{json_response}, debug => 1); $self->{output}->output_add(long_msg => $self->{json_response}, debug => 1);
if (!defined($self->{option_results}->{lookup}) || scalar(@{$self->{option_results}->{lookup}}) == 0) { if (!defined($self->{option_results}->{lookup}) || scalar(@{$self->{option_results}->{lookup}}) == 0) {
$self->{output}->output_add(severity => 'OK', $self->{output}->output_add(
short_msg => "JSON webservice request success"); severity => 'OK',
short_msg => "JSON webservice request success"
);
} else { } else {
$self->lookup(); $self->lookup();
} }
my $exit = $self->{perfdata}->threshold_check(value => $timeelapsed, my $exit = $self->{perfdata}->threshold_check(
threshold => [ { label => 'critical-time', exit_litteral => 'critical' }, { label => 'warning-time', exit_litteral => 'warning' } ]); value => $timeelapsed,
threshold => [ { label => 'critical-time', exit_litteral => 'critical' }, { label => 'warning-time', exit_litteral => 'warning' } ]
);
if (!$self->{output}->is_status(value => $exit, compare => 'ok', litteral => 1)) { if (!$self->{output}->is_status(value => $exit, compare => 'ok', litteral => 1)) {
$self->{output}->output_add(severity => $exit, $self->{output}->output_add(severity => $exit,
short_msg => sprintf("Response time %.3fs", $timeelapsed)); short_msg => sprintf("Response time %.3fs", $timeelapsed));
} else { } else {
$self->{output}->output_add(long_msg => sprintf("Response time %.3fs", $timeelapsed)); $self->{output}->output_add(long_msg => sprintf("Response time %.3fs", $timeelapsed));
} }
$self->{output}->perfdata_add(label => "time", unit => 's', $self->{output}->perfdata_add(
value => sprintf('%.3f', $timeelapsed), label => "time", unit => 's',
warning => $self->{perfdata}->get_perfdata_for_output(label => 'warning-time'), value => sprintf('%.3f', $timeelapsed),
critical => $self->{perfdata}->get_perfdata_for_output(label => 'critical-time'), warning => $self->{perfdata}->get_perfdata_for_output(label => 'warning-time'),
min => 0); critical => $self->{perfdata}->get_perfdata_for_output(label => 'critical-time'),
min => 0
);
$self->lookup_perfdata_nagios(); $self->lookup_perfdata_nagios();

View File

@ -83,8 +83,10 @@ sub set_proxy {
my ($self, %options) = @_; my ($self, %options) = @_;
if (defined($options{request}->{proxypac}) && $options{request}->{proxypac} ne '') { if (defined($options{request}->{proxypac}) && $options{request}->{proxypac} ne '') {
centreon::plugins::misc::mymodule_load(output => $self->{output}, module => 'HTTP::ProxyPAC', centreon::plugins::misc::mymodule_load(
error_msg => "Cannot load module 'HTTP::ProxyPAC'."); output => $self->{output}, module => 'HTTP::ProxyPAC',
error_msg => "Cannot load module 'HTTP::ProxyPAC'."
);
my ($pac, $pac_uri); my ($pac, $pac_uri);
eval { eval {
if ($options{request}->{proxypac} =~ /^(http|https):\/\//) { if ($options{request}->{proxypac} =~ /^(http|https):\/\//) {