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

View File

@ -35,10 +35,7 @@ sub new {
bless $self, $class;
$self->{version} = '1.2';
$options{options}->add_options(arguments =>
{
"data:s" => { name => 'data' },
"lookup:s@" => { name => 'lookup' },
$options{options}->add_options(arguments => {
"hostname:s" => { name => 'hostname' },
"http-peer-addr:s" => { name => 'http_peer_addr' },
"vhost:s" => { name => 'vhost' },
@ -78,8 +75,13 @@ sub new {
"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') {
@ -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();
}
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')