mirror of
https://github.com/c-kr/check_json.git
synced 2024-11-23 19:03:49 +01:00
Fix call to json node when deeper that 1 level, also added support for wildcard in the middle of the xpath.
This commit is contained in:
parent
a59fc0a279
commit
1315c5b8d6
@ -14,7 +14,7 @@ check_json -u|--url <URL> -a|--attribute <attribute> [ -c|--critical <threshold>
|
|||||||
|
|
||||||
Example:
|
Example:
|
||||||
```
|
```
|
||||||
./check_json.pl --url http://192.168.5.10:9332/local_stats --attribute '{shares}->{dead_shares}' --warning :5 --critical :10 --perfvars '{shares}->{dead_shares},{shares}->{live_shares},{clients}->{clients_connected}'
|
./check_json.pl --url http://192.168.5.10:9332/local_stats --attribute '{shares}->{dead_shares}' --warning :5 --critical :10 --perfvars '{shares}->{dead_shares},{shares}->{live_shares},{clients}->{clients_connected}' -o '{shares}->*->{name},'
|
||||||
```
|
```
|
||||||
|
|
||||||
Result:
|
Result:
|
||||||
@ -22,9 +22,12 @@ Result:
|
|||||||
Check JSON status API OK - dead_shares: 2, live_shares: 12, clients_connected: 234 | dead_shares=2;5;10 live_shares=12 clients_connected=234
|
Check JSON status API OK - dead_shares: 2, live_shares: 12, clients_connected: 234 | dead_shares=2;5;10 live_shares=12 clients_connected=234
|
||||||
```
|
```
|
||||||
|
|
||||||
|
Outputvars: It accepts also wildcards in the middle of the tree.
|
||||||
|
|
||||||
Requirements
|
Requirements
|
||||||
============
|
============
|
||||||
|
|
||||||
Perl JSON package
|
Perl JSON package
|
||||||
|
|
||||||
* Debian / Ubuntu : libjson-perl libnagios-plugin-perl libwww-perl
|
* Debian / Ubuntu : libjson-perl libnagios-plugin-perl libwww-perl
|
||||||
|
* RHEL / CentOS : perl-Nagios-Plugin perl-JSON perl-HTTP-Parser
|
||||||
|
@ -116,13 +116,20 @@ $np->add_arg(
|
|||||||
$np->getopts;
|
$np->getopts;
|
||||||
if ($np->opts->verbose) { (print Dumper ($np))};
|
if ($np->opts->verbose) { (print Dumper ($np))};
|
||||||
|
|
||||||
|
if ($np->opts->hattrib and not $np->opts->hcon) {
|
||||||
|
$np->nagios_exit(UNKNOWN,"Additional Header attribute provided without Additional Header content");
|
||||||
|
}
|
||||||
|
if ( not $np->opts->hattrib and $np->opts->hcon) {
|
||||||
|
$np->nagios_exit(UNKNOWN,"Additional Header content provided without Additional Header attribule");
|
||||||
|
}
|
||||||
|
|
||||||
## GET URL
|
## GET URL
|
||||||
my $ua = LWP::UserAgent->new;
|
my $ua = LWP::UserAgent->new;
|
||||||
|
|
||||||
$ua->env_proxy;
|
$ua->env_proxy;
|
||||||
$ua->agent('check_json/0.5');
|
$ua->agent('check_json/0.5');
|
||||||
$ua->default_header('Accept' => 'application/json');
|
$ua->default_header('Accept' => 'application/json');
|
||||||
$ua->default_header($np->opts->hattrib => $np->opts->hcon);
|
$ua->default_header($np->opts->hattrib => $np->opts->hcon) if ( $np->opts->hattrib and $np->opts->hcon );
|
||||||
$ua->protocols_allowed( [ 'http', 'https'] );
|
$ua->protocols_allowed( [ 'http', 'https'] );
|
||||||
$ua->parse_head(0);
|
$ua->parse_head(0);
|
||||||
$ua->timeout($np->opts->timeout);
|
$ua->timeout($np->opts->timeout);
|
||||||
@ -153,8 +160,14 @@ my $json_response = decode_json($response->content);
|
|||||||
if ($np->opts->verbose) { (print Dumper ($json_response))};
|
if ($np->opts->verbose) { (print Dumper ($json_response))};
|
||||||
|
|
||||||
my @attributes = split(',', $np->opts->attributes);
|
my @attributes = split(',', $np->opts->attributes);
|
||||||
my @warning = split(',', $np->opts->warning);
|
my @warning;
|
||||||
my @critical = split(',', $np->opts->critical);
|
if ($np->opts->warning) {
|
||||||
|
@warning = split(',', $np->opts->warning);
|
||||||
|
}
|
||||||
|
my @critical;
|
||||||
|
if ($np->opts->critical) {
|
||||||
|
@critical = split(',', $np->opts->critical);
|
||||||
|
}
|
||||||
my @divisor = $np->opts->divisor ? split(',',$np->opts->divisor) : () ;
|
my @divisor = $np->opts->divisor ? split(',',$np->opts->divisor) : () ;
|
||||||
my %attributes = map { $attributes[$_] => { warning => $warning[$_] , critical => $critical[$_], divisor => ($divisor[$_] or 0) } } 0..$#attributes;
|
my %attributes = map { $attributes[$_] => { warning => $warning[$_] , critical => $critical[$_], divisor => ($divisor[$_] or 0) } } 0..$#attributes;
|
||||||
|
|
||||||
@ -232,7 +245,7 @@ if ($np->opts->perfvars) {
|
|||||||
# make label ascii compatible
|
# make label ascii compatible
|
||||||
$label =~ s/[^a-zA-Z0-9_-]//g ;
|
$label =~ s/[^a-zA-Z0-9_-]//g ;
|
||||||
my $perf_value;
|
my $perf_value;
|
||||||
$perf_value = $json_response->{$key};
|
$perf_value = eval('$json_response->'.$key);
|
||||||
if ($np->opts->verbose) { print Dumper ("JSON key: ".$label.", JSON val: " . $perf_value) };
|
if ($np->opts->verbose) { print Dumper ("JSON key: ".$label.", JSON val: " . $perf_value) };
|
||||||
if ( defined($perf_value) ) {
|
if ( defined($perf_value) ) {
|
||||||
# add threshold if attribute option matches key
|
# add threshold if attribute option matches key
|
||||||
@ -254,20 +267,51 @@ if ($np->opts->perfvars) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
sub process_wildcard{
|
||||||
|
my($key,$json_response) = @_;
|
||||||
|
|
||||||
|
if ($np->opts->verbose) { (print "DEBUG: handling wildcard on key ". $key. "\n"); }
|
||||||
|
my @parts;
|
||||||
|
my @result;
|
||||||
|
if ($key !~ /\*/) {
|
||||||
|
$Data::Dumper::Terse = 1;
|
||||||
|
$Data::Dumper::Indent = 0;
|
||||||
|
if ($np->opts->verbose) { (print "DEBUG: Now checking: ". $key. " against: ". Dumper($json_response)."\n")};
|
||||||
|
my $output_value = Dumper(eval('$json_response->'.$key));
|
||||||
|
$Data::Dumper::Terse = 0;
|
||||||
|
$Data::Dumper::Indent = 1;
|
||||||
|
return $output_value;
|
||||||
|
} else {
|
||||||
|
@parts = split(/->[\{\[]?\*[\}\]]?->/,$key,2);
|
||||||
|
my $part1 = $parts[0];
|
||||||
|
my $part2 = $parts[1];
|
||||||
|
if ($np->opts->verbose) { (print "DEBUG: processing first part before wildcard: ". $part1 . "\n"); }
|
||||||
|
my $c = eval('$json_response->'.$part1);
|
||||||
|
return if (! $c);
|
||||||
|
if ($np->opts->verbose) { (print "DEBUG: process_wildcard: Fount childs: ".Dumper($c)."\n")};
|
||||||
|
foreach my $v (values($c)){
|
||||||
|
my $r = process_wildcard($part2,$v);
|
||||||
|
push(@result,$r) if $r;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return @result;
|
||||||
|
}
|
||||||
|
|
||||||
# output some vars in message
|
# output some vars in message
|
||||||
if ($np->opts->outputvars) {
|
if ($np->opts->outputvars) {
|
||||||
foreach my $key ($np->opts->outputvars eq '*' ? map { "{$_}"} sort keys %$json_response : split(',', $np->opts->outputvars)) {
|
foreach my $key ($np->opts->outputvars eq '*' ? map { "{$_}"} sort keys %$json_response : split(',', $np->opts->outputvars)) {
|
||||||
# use last element of key as label
|
# use last element of key as label
|
||||||
my $label = (split('->', $key))[-1];
|
my $label = (split('->', $key))[-1];
|
||||||
# make label ascii compatible
|
# make label ascii compatible i.e. remove the { and }
|
||||||
$label =~ s/[^a-zA-Z0-9_-]//g;
|
$label =~ s/[^a-zA-Z0-9_-]//g;
|
||||||
my $output_value;
|
my $output_value;
|
||||||
$output_value = $json_response->{$key};
|
## Handle case of wildcard in the middle of the tree: {data}->*->{description}
|
||||||
|
$output_value = join(", ",process_wildcard($key,$json_response));
|
||||||
push(@statusmsg, "$label: $output_value");
|
push(@statusmsg, "$label: $output_value");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
$np->nagios_exit(
|
$np->nagios_exit(
|
||||||
return_code => $result,
|
return_code => $result,
|
||||||
message => join(', ', @statusmsg),
|
message => join('; ', @statusmsg),
|
||||||
);
|
);
|
||||||
|
Loading…
Reference in New Issue
Block a user