enhance http nlabel

This commit is contained in:
garnier-quentin 2021-01-25 10:30:24 +01:00
parent 717dcf859f
commit f6af415daa
5 changed files with 92 additions and 84 deletions

View File

@ -48,30 +48,19 @@ sub custom_content_output {
return $msg; 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'};
$self->{result_values}->{header} = $options{new_datas}->{$self->{instance} . '_header'};
$self->{result_values}->{first_header} = $options{new_datas}->{$self->{instance} . '_first_header'};
return 0;
}
sub set_counters { sub set_counters {
my ($self, %options) = @_; my ($self, %options) = @_;
$self->{maps_counters_type} = [ $self->{maps_counters_type} = [
{ name => 'global', type => 0, skipped_code => { -10 => 1 } }, { name => 'global', type => 0, skipped_code => { -10 => 1 } }
]; ];
$self->{maps_counters}->{global} = [ $self->{maps_counters}->{global} = [
{ label => 'content', threshold => 0, set => { { label => 'content', type => 2, set => {
key_values => [ { name => 'content' }, { name => 'code' }, { name => 'first_header' }, { name => 'header' } ], key_values => [ { name => 'content' }, { name => 'code' }, { name => 'first_header' }, { name => 'header' } ],
closure_custom_output => $self->can('custom_content_output'), closure_custom_output => $self->can('custom_content_output'),
closure_custom_calc => $self->can('custom_content_calc'),
closure_custom_perfdata => sub { return 0; }, closure_custom_perfdata => sub { return 0; },
closure_custom_threshold_check => $self->can('custom_content_threshold'), closure_custom_threshold_check => $self->can('custom_content_threshold')
} }
}, },
{ label => 'size', nlabel => 'http.content.size.bytes', display_ok => 0, set => { { label => 'size', nlabel => 'http.content.size.bytes', display_ok => 0, set => {
@ -90,7 +79,7 @@ sub set_counters {
] ]
} }
}, },
{ label => 'extracted', nlabel => 'http.extracted.value', display_ok => 0, set => { { label => 'extracted', nlabel => 'http.extracted.value.count', display_ok => 0, set => {
key_values => [ { name => 'extracted' } ], key_values => [ { name => 'extracted' } ],
output_template => 'Extracted value : %s', output_template => 'Extracted value : %s',
perfdatas => [ perfdatas => [
@ -133,12 +122,9 @@ sub new {
'cookies-file:s' => { name => 'cookies_file' }, 'cookies-file:s' => { name => 'cookies_file' },
'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);
return $self; return $self;
} }
@ -181,7 +167,6 @@ sub check_options {
} }
$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 {
@ -190,7 +175,7 @@ sub manage_selection {
my $timing0 = [gettimeofday]; my $timing0 = [gettimeofday];
my $webcontent = $self->{http}->request(%{$self->{options_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} = {
time => $timeelapsed, time => $timeelapsed,
content => $webcontent, content => $webcontent,

View File

@ -36,44 +36,44 @@ 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' },
"vhost:s" => { name => 'vhost' }, 'vhost:s' => { name => 'vhost' },
"port:s" => { name => 'port', }, 'port:s' => { name => 'port', },
"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' },
"header:s@" => { name => 'header' }, 'header:s@' => { name => 'header' },
"get-param:s@" => { name => 'get_param' }, 'get-param:s@' => { name => 'get_param' },
"timeout:s" => { name => 'timeout', default => 10 }, 'timeout:s' => { name => 'timeout', default => 10 },
"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' },
"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' },
"warning-numeric:s" => { name => 'warning_numeric' }, 'warning-numeric:s' => { name => 'warning_numeric' },
"critical-numeric:s" => { name => 'critical_numeric' }, 'critical-numeric:s' => { name => 'critical_numeric' },
"warning-string:s" => { name => 'warning_string' }, 'warning-string:s' => { name => 'warning_string' },
"critical-string:s" => { name => 'critical_string' }, 'critical-string:s' => { name => 'critical_string' },
"unknown-string:s" => { name => 'unknown_string' }, 'unknown-string:s' => { name => 'unknown_string' },
"warning-time:s" => { name => 'warning_time' }, 'warning-time:s' => { name => 'warning_time' },
"critical-time:s" => { name => 'critical_time' }, 'critical-time:s' => { name => 'critical_time' },
"threshold-value:s" => { name => 'threshold_value', default => 'count' }, 'threshold-value:s' => { name => 'threshold_value', default => 'count' },
"format-ok:s" => { name => 'format_ok', default => '%{count} element(s) found' }, 'format-ok:s' => { name => 'format_ok', default => '%{count} element(s) found' },
"format-warning:s" => { name => 'format_warning', default => '%{count} element(s) found' }, 'format-warning:s' => { name => 'format_warning', default => '%{count} element(s) found' },
"format-critical:s" => { name => 'format_critical', default => '%{count} element(s) found' }, 'format-critical:s' => { name => 'format_critical', default => '%{count} element(s) found' },
"format-unknown:s" => { name => 'format_unknown', default => '%{count} element(s) found' }, 'format-unknown:s' => { name => 'format_unknown', default => '%{count} element(s) found' },
"format-lookup:s" => { name => 'format_lookup'}, 'format-lookup:s' => { name => 'format_lookup'},
"values-separator:s" => { name => 'values_separator', default => ', ' }, 'values-separator:s' => { name => 'values_separator', default => ', ' },
"lookup-perfdatas-nagios:s" => { name => 'lookup_perfdatas_nagios'}, 'lookup-perfdatas-nagios:s' => { name => 'lookup_perfdatas_nagios'},
"data:s" => { name => 'data' }, 'data:s' => { name => 'data' },
"lookup:s@" => { name => 'lookup' }, 'lookup:s@' => { name => 'lookup' }
}); });
$self->{count} = 0; $self->{count} = 0;
@ -222,6 +222,7 @@ sub lookup {
$self->{output}->perfdata_add( $self->{output}->perfdata_add(
label => 'count', label => 'count',
nlabel => 'json.match.total.count',
value => $self->{count}, value => $self->{count},
warning => $self->{option_results}->{threshold_value} eq 'count' ? $self->{perfdata}->get_perfdata_for_output(label => 'warning-numeric') : undef, 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, critical => $self->{option_results}->{threshold_value} eq 'count' ? $self->{perfdata}->get_perfdata_for_output(label => 'critical-numeric') : undef,
@ -245,6 +246,7 @@ sub lookup {
} }
$self->{output}->perfdata_add( $self->{output}->perfdata_add(
label => 'element_' . $count, label => 'element_' . $count,
nlabel => 'json.match.element.' . $count . '.count',
value => $value_expand, value => $value_expand,
warning => $self->{option_results}->{threshold_value} eq 'values' ? $self->{perfdata}->get_perfdata_for_output(label => 'warning-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 critical => $self->{option_results}->{threshold_value} eq 'values' ? $self->{perfdata}->get_perfdata_for_output(label => 'critical-numeric') : undef
@ -311,7 +313,9 @@ sub lookup_perfdata_nagios {
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( $self->{output}->perfdata_add(
label => $label, unit => $unit, label => $label,
nlabel => $label,
unit => $unit,
value => $value, value => $value,
warning => $warn, warning => $warn,
critical => $crit, critical => $crit,
@ -345,13 +349,17 @@ sub run {
threshold => [ { label => 'critical-time', exit_litteral => 'critical' }, { label => 'warning-time', exit_litteral => 'warning' } ] 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(
short_msg => sprintf("Response time %.3fs", $timeelapsed)); severity => $exit,
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( $self->{output}->perfdata_add(
label => "time", unit => 's', label => 'time',
nlabel => 'http.response.time.seconds',
unit => 's',
value => sprintf('%.3f', $timeelapsed), value => sprintf('%.3f', $timeelapsed),
warning => $self->{perfdata}->get_perfdata_for_output(label => 'warning-time'), warning => $self->{perfdata}->get_perfdata_for_output(label => 'warning-time'),
critical => $self->{perfdata}->get_perfdata_for_output(label => 'critical-time'), critical => $self->{perfdata}->get_perfdata_for_output(label => 'critical-time'),

View File

@ -24,7 +24,7 @@ use base qw(centreon::plugins::templates::counter);
use strict; use strict;
use warnings; use warnings;
use centreon::plugins::templates::catalog_functions qw(catalog_status_threshold_ng catalog_status_calc); use centreon::plugins::templates::catalog_functions qw(catalog_status_threshold_ng);
use Time::HiRes qw(gettimeofday tv_interval); use Time::HiRes qw(gettimeofday tv_interval);
use centreon::plugins::http; use centreon::plugins::http;
@ -46,7 +46,6 @@ sub set_counters {
key_values => [ key_values => [
{ name => 'http_code' }, { name => 'message' } { name => 'http_code' }, { name => 'message' }
], ],
closure_custom_calc => \&catalog_status_calc,
closure_custom_output => $self->can('custom_status_output'), closure_custom_output => $self->can('custom_status_output'),
closure_custom_perfdata => sub { return 0; }, closure_custom_perfdata => sub { return 0; },
closure_custom_threshold_check => \&catalog_status_threshold_ng closure_custom_threshold_check => \&catalog_status_threshold_ng
@ -56,7 +55,7 @@ sub set_counters {
key_values => [ { name => 'time' } ], key_values => [ { name => 'time' } ],
output_template => 'Response time %.3fs', output_template => 'Response time %.3fs',
perfdatas => [ perfdatas => [
{ label => 'time', value => 'time', template => '%.3f', min => 0, unit => 's' } { label => 'time', template => '%.3f', min => 0, unit => 's' }
] ]
} }
}, },
@ -64,7 +63,7 @@ sub set_counters {
key_values => [ { name => 'size' } ], key_values => [ { name => 'size' } ],
output_template => 'Content size : %s', output_template => 'Content size : %s',
perfdatas => [ perfdatas => [
{ label => 'size', value => 'size', template => '%s', min => 0, unit => 'B' } { label => 'size', template => '%s', min => 0, unit => 'B' }
] ]
} }
} }

View File

@ -67,7 +67,7 @@ sub new {
'format-ok:s' => { name => 'format_ok', default => '%{count} element(s) found' }, 'format-ok:s' => { name => 'format_ok', default => '%{count} element(s) found' },
'format-warning:s' => { name => 'format_warning', default => '%{count} element(s) found' }, 'format-warning:s' => { name => 'format_warning', default => '%{count} element(s) found' },
'format-critical:s' => { name => 'format_critical', default => '%{count} element(s) found' }, 'format-critical:s' => { name => 'format_critical', default => '%{count} element(s) found' },
'values-separator:s' => { name => 'values_separator', default => ', ' }, 'values-separator:s' => { name => 'values_separator', default => ', ' }
}); });
$self->{count} = 0; $self->{count} = 0;
$self->{count_ok} = 0; $self->{count_ok} = 0;
@ -148,8 +148,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
);
} }
} }
@ -210,14 +212,17 @@ 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($self->{perfdata}->threshold_check(
threshold => [ { label => 'critical-numeric', exit_litteral => 'critical' }, { label => 'warning-numeric', exit_litteral => 'warning' } ])); 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( $self->{output}->perfdata_add(
label => 'count', label => 'count',
nlabel => 'xml.match.total.count',
value => $self->{count}, value => $self->{count},
warning => $self->{option_results}->{threshold_value} eq 'count' ? $self->{perfdata}->get_perfdata_for_output(label => 'warning-numeric') : undef, 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, critical => $self->{option_results}->{threshold_value} eq 'count' ? $self->{perfdata}->get_perfdata_for_output(label => 'critical-numeric') : undef,
@ -229,13 +234,16 @@ sub lookup {
$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($self->{perfdata}->threshold_check(
threshold => [ { label => 'critical-numeric', exit_litteral => 'critical' }, { label => 'warning-numeric', exit_litteral => 'warning' } ])); 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( $self->{output}->perfdata_add(
label => 'element_' . $count, label => 'element_' . $count,
nlabel => 'xml.match.element.' . $count . '.count',
value => $value, value => $value,
warning => $self->{option_results}->{threshold_value} eq 'values' ? $self->{perfdata}->get_perfdata_for_output(label => 'warning-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 critical => $self->{option_results}->{threshold_value} eq 'values' ? $self->{perfdata}->get_perfdata_for_output(label => 'critical-numeric') : undef
@ -270,22 +278,30 @@ sub run {
scalar(@{$self->{option_results}->{lookup}}) == 0 || scalar(@{$self->{option_results}->{lookup}}) == 0 ||
$self->{option_results}->{lookup}->[0] eq '' $self->{option_results}->{lookup}->[0] eq ''
) { ) {
$self->{output}->output_add(severity => 'OK', $self->{output}->output_add(
short_msg => "SOAP request success"); severity => 'OK',
short_msg => 'SOAP 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(
short_msg => sprintf("Response time %.3fs", $timeelapsed)); severity => $exit,
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( $self->{output}->perfdata_add(
label => "time", unit => 's', label => 'time',
nlabel => 'http.response.time.seconds',
unit => 's',
value => sprintf('%.3f', $timeelapsed), value => sprintf('%.3f', $timeelapsed),
warning => $self->{perfdata}->get_perfdata_for_output(label => 'warning-time'), warning => $self->{perfdata}->get_perfdata_for_output(label => 'warning-time'),
critical => $self->{perfdata}->get_perfdata_for_output(label => 'critical-time'), critical => $self->{perfdata}->get_perfdata_for_output(label => 'critical-time'),

View File

@ -30,7 +30,7 @@ sub new {
bless $self, $class; bless $self, $class;
$self->{version} = '0.1'; $self->{version} = '0.1';
$self->{modes} = ( $self->{modes} = {
'expected-content' => 'apps::protocols::http::mode::expectedcontent', 'expected-content' => 'apps::protocols::http::mode::expectedcontent',
'json-content' => 'apps::protocols::http::mode::jsoncontent', 'json-content' => 'apps::protocols::http::mode::jsoncontent',
'response' => 'apps::protocols::http::mode::response', 'response' => 'apps::protocols::http::mode::response',