enhance http nlabel

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

View File

@ -48,30 +48,19 @@ sub custom_content_output {
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 {
my ($self, %options) = @_;
$self->{maps_counters_type} = [
{ name => 'global', type => 0, skipped_code => { -10 => 1 } },
{ name => 'global', type => 0, skipped_code => { -10 => 1 } }
];
$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' } ],
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'),
closure_custom_threshold_check => $self->can('custom_content_threshold')
}
},
{ 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' } ],
output_template => 'Extracted value : %s',
perfdatas => [
@ -133,12 +122,9 @@ sub new {
'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 => '' },
'critical-status:s' => { name => 'critical_status' }
});
$self->{http} = centreon::plugins::http->new(%options);
return $self;
}
@ -181,7 +167,6 @@ sub check_options {
}
$self->{http}->set_options(%{$self->{option_results}});
$self->change_macros(macros => ['warning_content', 'critical_content', 'unknown_content']);
}
sub manage_selection {
@ -190,7 +175,7 @@ sub manage_selection {
my $timing0 = [gettimeofday];
my $webcontent = $self->{http}->request(%{$self->{options_request}});
my $timeelapsed = tv_interval($timing0, [gettimeofday]);
$self->{global} = {
time => $timeelapsed,
content => $webcontent,

View File

@ -36,44 +36,44 @@ sub new {
bless $self, $class;
$options{options}->add_options(arguments => {
"hostname:s" => { name => 'hostname' },
"vhost:s" => { name => 'vhost' },
"port:s" => { name => 'port', },
"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' },
"header:s@" => { name => 'header' },
"get-param:s@" => { name => 'get_param' },
"timeout:s" => { name => 'timeout', default => 10 },
"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' },
"unknown-status:s" => { name => 'unknown_status' },
"warning-status:s" => { name => 'warning_status' },
"critical-status:s" => { name => 'critical_status' },
"warning-numeric:s" => { name => 'warning_numeric' },
"critical-numeric:s" => { name => 'critical_numeric' },
"warning-string:s" => { name => 'warning_string' },
"critical-string:s" => { name => 'critical_string' },
"unknown-string:s" => { name => 'unknown_string' },
"warning-time:s" => { name => 'warning_time' },
"critical-time:s" => { name => 'critical_time' },
"threshold-value:s" => { name => 'threshold_value', default => 'count' },
"format-ok:s" => { name => 'format_ok', 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-unknown:s" => { name => 'format_unknown', default => '%{count} element(s) found' },
"format-lookup:s" => { name => 'format_lookup'},
"values-separator:s" => { name => 'values_separator', default => ', ' },
"lookup-perfdatas-nagios:s" => { name => 'lookup_perfdatas_nagios'},
"data:s" => { name => 'data' },
"lookup:s@" => { name => 'lookup' },
'hostname:s' => { name => 'hostname' },
'vhost:s' => { name => 'vhost' },
'port:s' => { name => 'port', },
'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' },
'header:s@' => { name => 'header' },
'get-param:s@' => { name => 'get_param' },
'timeout:s' => { name => 'timeout', default => 10 },
'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' },
'unknown-status:s' => { name => 'unknown_status' },
'warning-status:s' => { name => 'warning_status' },
'critical-status:s' => { name => 'critical_status' },
'warning-numeric:s' => { name => 'warning_numeric' },
'critical-numeric:s' => { name => 'critical_numeric' },
'warning-string:s' => { name => 'warning_string' },
'critical-string:s' => { name => 'critical_string' },
'unknown-string:s' => { name => 'unknown_string' },
'warning-time:s' => { name => 'warning_time' },
'critical-time:s' => { name => 'critical_time' },
'threshold-value:s' => { name => 'threshold_value', default => 'count' },
'format-ok:s' => { name => 'format_ok', 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-unknown:s' => { name => 'format_unknown', default => '%{count} element(s) found' },
'format-lookup:s' => { name => 'format_lookup'},
'values-separator:s' => { name => 'values_separator', default => ', ' },
'lookup-perfdatas-nagios:s' => { name => 'lookup_perfdatas_nagios'},
'data:s' => { name => 'data' },
'lookup:s@' => { name => 'lookup' }
});
$self->{count} = 0;
@ -222,6 +222,7 @@ sub lookup {
$self->{output}->perfdata_add(
label => 'count',
nlabel => 'json.match.total.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,
@ -245,6 +246,7 @@ sub lookup {
}
$self->{output}->perfdata_add(
label => 'element_' . $count,
nlabel => 'json.match.element.' . $count . '.count',
value => $value_expand,
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
@ -311,7 +313,9 @@ sub lookup_perfdata_nagios {
my ($value, $unit) = $value_w_unit =~ /(^[0-9]+\.*\,*[0-9]*)(.*)/g;
$self->{output}->perfdata_add(
label => $label, unit => $unit,
label => $label,
nlabel => $label,
unit => $unit,
value => $value,
warning => $warn,
critical => $crit,
@ -345,13 +349,17 @@ sub run {
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));
$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',
label => 'time',
nlabel => 'http.response.time.seconds',
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'),

View File

@ -24,7 +24,7 @@ use base qw(centreon::plugins::templates::counter);
use strict;
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 centreon::plugins::http;
@ -46,7 +46,6 @@ sub set_counters {
key_values => [
{ name => 'http_code' }, { name => 'message' }
],
closure_custom_calc => \&catalog_status_calc,
closure_custom_output => $self->can('custom_status_output'),
closure_custom_perfdata => sub { return 0; },
closure_custom_threshold_check => \&catalog_status_threshold_ng
@ -56,7 +55,7 @@ sub set_counters {
key_values => [ { name => 'time' } ],
output_template => 'Response time %.3fs',
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' } ],
output_template => 'Content size : %s',
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-warning:s' => { name => 'format_warning', 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_ok} = 0;
@ -148,8 +148,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
);
}
}
@ -210,14 +212,17 @@ 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',
nlabel => 'xml.match.total.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,
@ -229,13 +234,16 @@ sub lookup {
$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,
nlabel => 'xml.match.element.' . $count . '.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
@ -270,22 +278,30 @@ sub run {
scalar(@{$self->{option_results}->{lookup}}) == 0 ||
$self->{option_results}->{lookup}->[0] eq ''
) {
$self->{output}->output_add(severity => 'OK',
short_msg => "SOAP request success");
$self->{output}->output_add(
severity => 'OK',
short_msg => 'SOAP 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));
$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}->output_add(long_msg => sprintf('Response time %.3fs', $timeelapsed));
}
$self->{output}->perfdata_add(
label => "time", unit => 's',
label => 'time',
nlabel => 'http.response.time.seconds',
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'),

View File

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