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};
}
sub custom_content_output {
my ($self, %options) = @_;
@ -108,45 +107,76 @@ sub new {
bless $self, $class;
$options{options}->add_options(arguments => {
"hostname:s" => { name => 'hostname' },
"port:s" => { name => 'port', },
"method:s" => { name => 'method' },
"proto:s" => { name => 'proto' },
"urlpath:s" => { name => 'url_path' },
"credentials" => { name => 'credentials' },
"basic" => { name => 'basic' },
"ntlmv2" => { name => 'ntlmv2' },
"username:s" => { name => 'username' },
"password:s" => { name => 'password' },
"expected-string:s" => { name => 'expected_string' },
"extracted-pattern:s" => { name => 'extracted_pattern' },
"timeout:s" => { name => 'timeout' },
"no-follow" => { name => 'no_follow', },
"cert-file:s" => { name => 'cert_file' },
"key-file:s" => { name => 'key_file' },
"cacert-file:s" => { name => 'cacert_file' },
"cert-pwd:s" => { name => 'cert_pwd' },
"cert-pkcs12" => { name => 'cert_pkcs12' },
"header:s@" => { name => 'header' },
"get-param:s@" => { name => 'get_param' },
"post-param:s@" => { name => 'post_param' },
"cookies-file:s" => { name => 'cookies_file' },
"unknown-status:s" => { name => 'unknown_status' },
"warning-status:s" => { name => 'warning_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 => '' },
'hostname:s' => { name => 'hostname' },
'port:s' => { name => 'port', },
'method:s' => { name => 'method' },
'proto:s' => { name => 'proto' },
'urlpath:s' => { name => 'url_path' },
'credentials' => { name => 'credentials' },
'basic' => { name => 'basic' },
'ntlmv2' => { name => 'ntlmv2' },
'username:s' => { name => 'username' },
'password:s' => { name => 'password' },
'expected-string:s' => { name => 'expected_string' },
'extracted-pattern:s' => { name => 'extracted_pattern' },
'timeout:s' => { name => 'timeout' },
'no-follow' => { name => 'no_follow', },
'cert-file:s' => { name => 'cert_file' },
'key-file:s' => { name => 'key_file' },
'cacert-file:s' => { name => 'cacert_file' },
'cert-pwd:s' => { name => 'cert_pwd' },
'cert-pkcs12' => { name => 'cert_pkcs12' },
'data:s' => { name => 'data' },
'header:s@' => { name => 'header' },
'get-param:s@' => { name => 'get_param' },
'post-param:s@' => { name => 'post_param' },
'cookies-file:s' => { name => 'cookies_file' },
'unknown-status:s' => { name => 'unknown_status' },
'warning-status:s' => { name => 'warning_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);
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 {
my ($self, %options) = @_;
$self->SUPER::check_options(%options);
$self->load_request();
# Legacy compat
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";
@ -160,7 +190,7 @@ sub manage_selection {
my ($self, %options) = @_;
my $timing0 = [gettimeofday];
my $webcontent = $self->{http}->request();
my $webcontent = $self->{http}->request(%{$self->{options_request}});
my $timeelapsed = tv_interval($timing0, [gettimeofday]);
$self->{global} = {
@ -272,6 +302,10 @@ Specify certificate's password
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>
Set HTTP headers (Multiple option)

View File

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

View File

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