This commit is contained in:
garnier-quentin 2019-02-20 13:32:18 +01:00
parent 4b8a304e8b
commit 8add7da435

View File

@ -35,51 +35,53 @@ sub new {
bless $self, $class; bless $self, $class;
$self->{version} = '1.2'; $self->{version} = '1.2';
$options{options}->add_options(arguments => $options{options}->add_options(arguments => {
{ "hostname:s" => { name => 'hostname' },
"data:s" => { name => 'data' }, "http-peer-addr:s" => { name => 'http_peer_addr' },
"lookup:s@" => { name => 'lookup' }, "vhost:s" => { name => 'vhost' },
"hostname:s" => { name => 'hostname' }, "port:s" => { name => 'port', },
"http-peer-addr:s" => { name => 'http_peer_addr' }, "proto:s" => { name => 'proto' },
"vhost:s" => { name => 'vhost' }, "urlpath:s" => { name => 'url_path' },
"port:s" => { name => 'port', }, "credentials" => { name => 'credentials' },
"proto:s" => { name => 'proto' }, "basic" => { name => 'basic' },
"urlpath:s" => { name => 'url_path' }, "ntlm" => { name => 'ntlm' }, # Deprecated
"credentials" => { name => 'credentials' }, "ntlmv2" => { name => 'ntlmv2' },
"basic" => { name => 'basic' }, "username:s" => { name => 'username' },
"ntlm" => { name => 'ntlm' }, # Deprecated "password:s" => { name => 'password' },
"ntlmv2" => { name => 'ntlmv2' }, "proxyurl:s" => { name => 'proxyurl' },
"username:s" => { name => 'username' }, "proxypac:s" => { name => 'proxypac' },
"password:s" => { name => 'password' }, "header:s@" => { name => 'header' },
"proxyurl:s" => { name => 'proxyurl' }, "get-param:s@" => { name => 'get_param' },
"proxypac:s" => { name => 'proxypac' }, "timeout:s" => { name => 'timeout', default => 10 },
"header:s@" => { name => 'header' }, "ssl-opt:s@" => { name => 'ssl_opt' },
"get-param:s@" => { name => 'get_param' }, "ssl:s" => { name => 'ssl', },
"timeout:s" => { name => 'timeout', default => 10 }, "cert-file:s" => { name => 'cert_file' },
"ssl-opt:s@" => { name => 'ssl_opt' }, "key-file:s" => { name => 'key_file' },
"ssl:s" => { name => 'ssl', }, "cacert-file:s" => { name => 'cacert_file' },
"cert-file:s" => { name => 'cert_file' }, "cert-pwd:s" => { name => 'cert_pwd' },
"key-file:s" => { name => 'key_file' }, "cert-pkcs12" => { name => 'cert_pkcs12' },
"cacert-file:s" => { name => 'cacert_file' }, "unknown-status:s" => { name => 'unknown_status' },
"cert-pwd:s" => { name => 'cert_pwd' }, "warning-status:s" => { name => 'warning_status' },
"cert-pkcs12" => { name => 'cert_pkcs12' }, "critical-status:s" => { name => 'critical_status' },
"unknown-status:s" => { name => 'unknown_status' }, "warning-numeric:s" => { name => 'warning_numeric' },
"warning-status:s" => { name => 'warning_status' }, "critical-numeric:s" => { name => 'critical_numeric' },
"critical-status:s" => { name => 'critical_status' }, "warning-string:s" => { name => 'warning_string' },
"warning-numeric:s" => { name => 'warning_numeric' }, "critical-string:s" => { name => 'critical_string' },
"critical-numeric:s" => { name => 'critical_numeric' }, "unknown-string:s" => { name => 'unknown_string' },
"warning-string:s" => { name => 'warning_string' }, "warning-time:s" => { name => 'warning_time' },
"critical-string:s" => { name => 'critical_string' }, "critical-time:s" => { name => 'critical_time' },
"unknown-string:s" => { name => 'unknown_string' }, "threshold-value:s" => { name => 'threshold_value', default => 'count' },
"warning-time:s" => { name => 'warning_time' }, "format-ok:s" => { name => 'format_ok', default => '%{count} element(s) found' },
"critical-time:s" => { name => 'critical_time' }, "format-warning:s" => { name => 'format_warning', default => '%{count} element(s) found' },
"threshold-value:s" => { name => 'threshold_value', default => 'count' }, "format-critical:s" => { name => 'format_critical', default => '%{count} element(s) found' },
"format-ok:s" => { name => 'format_ok', default => '%{count} element(s) found' }, "format-unknown:s" => { name => 'format_unknown', default => '%{count} element(s) found' },
"format-warning:s" => { name => 'format_warning', default => '%{count} element(s) found' }, "format-lookup:s" => { name => 'format_lookup'},
"format-critical:s" => { name => 'format_critical', default => '%{count} element(s) found' }, "values-separator:s" => { name => 'values_separator', default => ', ' },
"format-unknown:s" => { name => 'format_unknown', default => '%{count} element(s) found' }, "lookup-perfdatas-nagios:s" => { name => 'lookup_perfdatas_nagios'},
"values-separator:s" => { name => 'values_separator', default => ', ' }, "data:s" => { name => 'data' },
}); "lookup:s@" => { name => 'lookup' },
});
$self->{count} = 0; $self->{count} = 0;
$self->{count_ok} = 0; $self->{count_ok} = 0;
$self->{count_warning} = 0; $self->{count_warning} = 0;
@ -154,7 +156,12 @@ sub display_output {
foreach my $severity (('ok', 'warning', 'critical', 'unknown')) { foreach my $severity (('ok', 'warning', 'critical', 'unknown')) {
next if (scalar(@{$self->{'values_' . $severity}}) == 0 && scalar(@{$self->{'values_string_' . $severity}}) == 0); next if (scalar(@{$self->{'values_' . $severity}}) == 0 && scalar(@{$self->{'values_string_' . $severity}}) == 0);
my $format = $self->{option_results}->{'format_' . $severity}; my $format = '';
if(defined($self->{option_results}->{format_lookup}) && $self->{option_results}->{format_lookup} ne '') {
$format = $self->{format_from_json};
} else {
$format = $self->{option_results}->{'format_' . $severity};
}
while ($format =~ /%\{(.*?)\}/g) { while ($format =~ /%\{(.*?)\}/g) {
my $replace = ''; my $replace = '';
if (ref($self->{$1}) eq 'ARRAY') { if (ref($self->{$1}) eq 'ARRAY') {
@ -245,9 +252,57 @@ sub lookup {
} }
} }
if (defined($self->{option_results}->{format_lookup}) && $self->{option_results}->{format_lookup} ne '') {
my $xpath_find = $self->{option_results}->{format_lookup};
eval {
my $jpath = JSON::Path->new($xpath_find);
$self->{format_from_json} = $jpath->value($content);
};
if ($@) {
$self->{output}->add_option_msg(short_msg => "Cannot lookup output message: $@");
$self->{output}->option_exit();
}
$self->{output}->output_add(long_msg => "Lookup perfdatas XPath $xpath_find:");
}
$self->display_output(); $self->display_output();
} }
sub lookup_perfdata_nagios {
my ($self, %options) = @_;
return if (!defined($self->{option_results}->{lookup_perfdatas_nagios}) || $self->{option_results}->{lookup_perfdatas_nagios} eq '');
my $perfdata_string;
my $xpath_find = $self->{option_results}->{lookup_perfdatas_nagios};
eval {
my $jpath = JSON::Path->new($xpath_find);
$perfdata_string = $jpath->value($content);
};
if ($@) {
$self->{output}->add_option_msg(short_msg => "Cannot lookup perfdatas: $@");
$self->{output}->option_exit();
}
$self->{output}->output_add(long_msg => "Lookup perfdatas XPath $xpath_find:");
my @metrics = split(/ /, $perfdata_string);
foreach my $single_metric (@metrics) {
my ($label, $perfdatas) = split(/=/, $single_metric);
my ($value_w_unit, $warn, $crit, $min, $max) = split(/;/, $perfdatas);
# 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);
}
}
sub run { sub run {
my ($self, %options) = @_; my ($self, %options) = @_;
@ -279,6 +334,8 @@ sub run {
critical => $self->{perfdata}->get_perfdata_for_output(label => 'critical-time'), critical => $self->{perfdata}->get_perfdata_for_output(label => 'critical-time'),
min => 0); min => 0);
$self->lookup_perfdata_nagios();
$self->{output}->display(); $self->{output}->display();
$self->{output}->exit(); $self->{output}->exit();
} }
@ -306,12 +363,23 @@ Set file with JSON request
What to lookup in JSON response (JSON XPath string) (can be multiple) What to lookup in JSON response (JSON XPath string) (can be multiple)
See: http://goessner.net/articles/JsonPath/ See: http://goessner.net/articles/JsonPath/
=item B<--lookup-perfdatas-nagios>
Take perfdatas from the JSON response (JSON XPath string)
Chain must be formated in Nagios format.
Ex : "rta=10.752ms;50.000;100.000;0; pl=0%;20;40;; rtmax=10.802ms;;;;"
=back =back
FORMAT OPTIONS: FORMAT OPTIONS:
=over 8 =over 8
=item B<--format-lookup>
Take the output message from the JSON response (JSON XPath string)
Override all the format options but substitute are still applied.
=item B<--format-ok> =item B<--format-ok>
Output format (Default: '%{count} element(s) found') Output format (Default: '%{count} element(s) found')