This commit is contained in:
parent
4b8a304e8b
commit
8add7da435
|
@ -35,51 +35,53 @@ sub new {
|
|||
bless $self, $class;
|
||||
|
||||
$self->{version} = '1.2';
|
||||
$options{options}->add_options(arguments =>
|
||||
{
|
||||
"data:s" => { name => 'data' },
|
||||
"lookup:s@" => { name => 'lookup' },
|
||||
"hostname:s" => { name => 'hostname' },
|
||||
"http-peer-addr:s" => { name => 'http_peer_addr' },
|
||||
"vhost:s" => { name => 'vhost' },
|
||||
"port:s" => { name => 'port', },
|
||||
"proto:s" => { name => 'proto' },
|
||||
"urlpath:s" => { name => 'url_path' },
|
||||
"credentials" => { name => 'credentials' },
|
||||
"basic" => { name => 'basic' },
|
||||
"ntlm" => { name => 'ntlm' }, # Deprecated
|
||||
"ntlmv2" => { name => 'ntlmv2' },
|
||||
"username:s" => { name => 'username' },
|
||||
"password:s" => { name => 'password' },
|
||||
"proxyurl:s" => { name => 'proxyurl' },
|
||||
"proxypac:s" => { name => 'proxypac' },
|
||||
"header:s@" => { name => 'header' },
|
||||
"get-param:s@" => { name => 'get_param' },
|
||||
"timeout:s" => { name => 'timeout', default => 10 },
|
||||
"ssl-opt:s@" => { name => 'ssl_opt' },
|
||||
"ssl:s" => { name => 'ssl', },
|
||||
"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' },
|
||||
"values-separator:s" => { name => 'values_separator', default => ', ' },
|
||||
});
|
||||
$options{options}->add_options(arguments => {
|
||||
"hostname:s" => { name => 'hostname' },
|
||||
"http-peer-addr:s" => { name => 'http_peer_addr' },
|
||||
"vhost:s" => { name => 'vhost' },
|
||||
"port:s" => { name => 'port', },
|
||||
"proto:s" => { name => 'proto' },
|
||||
"urlpath:s" => { name => 'url_path' },
|
||||
"credentials" => { name => 'credentials' },
|
||||
"basic" => { name => 'basic' },
|
||||
"ntlm" => { name => 'ntlm' }, # Deprecated
|
||||
"ntlmv2" => { name => 'ntlmv2' },
|
||||
"username:s" => { name => 'username' },
|
||||
"password:s" => { name => 'password' },
|
||||
"proxyurl:s" => { name => 'proxyurl' },
|
||||
"proxypac:s" => { name => 'proxypac' },
|
||||
"header:s@" => { name => 'header' },
|
||||
"get-param:s@" => { name => 'get_param' },
|
||||
"timeout:s" => { name => 'timeout', default => 10 },
|
||||
"ssl-opt:s@" => { name => 'ssl_opt' },
|
||||
"ssl:s" => { name => 'ssl', },
|
||||
"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;
|
||||
$self->{count_ok} = 0;
|
||||
$self->{count_warning} = 0;
|
||||
|
@ -154,7 +156,12 @@ sub display_output {
|
|||
|
||||
foreach my $severity (('ok', 'warning', 'critical', 'unknown')) {
|
||||
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) {
|
||||
my $replace = '';
|
||||
if (ref($self->{$1}) eq 'ARRAY') {
|
||||
|
@ -244,10 +251,58 @@ 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();
|
||||
}
|
||||
|
||||
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 {
|
||||
my ($self, %options) = @_;
|
||||
|
||||
|
@ -279,6 +334,8 @@ sub run {
|
|||
critical => $self->{perfdata}->get_perfdata_for_output(label => 'critical-time'),
|
||||
min => 0);
|
||||
|
||||
$self->lookup_perfdata_nagios();
|
||||
|
||||
$self->{output}->display();
|
||||
$self->{output}->exit();
|
||||
}
|
||||
|
@ -306,12 +363,23 @@ Set file with JSON request
|
|||
What to lookup in JSON response (JSON XPath string) (can be multiple)
|
||||
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
|
||||
|
||||
FORMAT OPTIONS:
|
||||
|
||||
=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>
|
||||
|
||||
Output format (Default: '%{count} element(s) found')
|
||||
|
|
Loading…
Reference in New Issue