1
0
mirror of https://github.com/c-kr/check_json.git synced 2024-11-23 10:53:47 +01:00

Merge pull request #3 from bensinober/master

Improvements made by bensinober
This commit is contained in:
Christopher Kreft 2014-01-24 01:38:57 -08:00
commit 422dac69d2
2 changed files with 81 additions and 32 deletions

View File

@ -3,12 +3,22 @@ check_json
Nagios plugin to check JSON attributes via http(s). Nagios plugin to check JSON attributes via http(s).
This Plugin is a fork of the existing JSON Plugin from https://github.com/bbuchalter/check_json with the enhancements of using the Nagios::Plugin Perl Module, allowing to use thresholds and performancedata collection from various json attributes. This Plugin is a fork of the existing JSON Plugin from https://github.com/bbuchalter/check_json with the enhancements of using the Nagios::Plugin Perl Module, allowing to use thresholds and performance data collection from various json attributes.
Usage: `check_json -U <URL> -a <attribute> [ -v|--verbose ] [-t <timeout>] [ -c|--critical <threshold> ] [ -w|--warning <threshold> ]` Performance data is also enhanced to extract performance data compliant to Nagios and Graphite standards. One attribute is selected for thresholds check, multiple others can be added for extracting performance data. This plugin is aimed at simplifying Nagios, Icinga & Icinga2 polling of JSON status APIs.
Usage:
```
check_json -u|--url <URL> -a|--attribute <attribute> [ -c|--critical <threshold> ] [ -w|--warning <threshold> ] [ -p|--perfvars <fields> ] [ -t|--timeout <timeout> ] [ -d|--divisor <divisor> ] [ -h|--help ]
```
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 -U http://192.168.5.10:9332/local_stats -a '{shares}->{dead}' -w :5 -c :10` 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
```
JSON OK - 2 | value=2;;

View File

@ -3,27 +3,32 @@
use warnings; use warnings;
use strict; use strict;
use LWP::UserAgent; use LWP::UserAgent;
use JSON 'decode_json'; use JSON;
use Nagios::Plugin; use Nagios::Plugin;
use Data::Dumper; use Data::Dumper;
my $np = Nagios::Plugin->new( my $np = Nagios::Plugin->new(
usage => "Usage: %s [ -v|--verbose ] [-U <URL>] [-t <timeout>] " usage => "Usage: %s -u|--url <URL> -a|--attribute <attribute> "
. "[ -c|--critical <threshold> ] [ -w|--warning <threshold> ] " . "[ -c|--critical <threshold> ] [ -w|--warning <threshold> ] "
. "[ -a | --attribute ] <attribute>", . "[ -p|--perfvars <fields> ] "
version => '0.1', . "[ -t|--timeout <timeout> ] "
. "[ -d|--divisor <divisor> ] "
. "[ -h|--help ] ",
version => '0.2',
blurb => 'Nagios plugin to check JSON attributes via http(s)', blurb => 'Nagios plugin to check JSON attributes via http(s)',
extra => "\nExample: \n" extra => "\nExample: \n"
. "check_json.pl -U http://192.168.5.10:9332/local_stats -a '{shares}->{dead}' -w :5 -c :10", . "check_json.pl --url http://192.168.5.10:9332/local_stats --attribute '{shares}->{dead}' "
. "--warning :5 --critical :10 --perfvars '{shares}->{dead},{shares}->{live}'",
url => 'https://github.com/c-kr/check_json', url => 'https://github.com/c-kr/check_json',
plugin => 'check_json', plugin => 'check_json',
timeout => 15, timeout => 15,
shortname => "Check JSON status API",
); );
# add valid command line options and build them into your usage/help documentation. # add valid command line options and build them into your usage/help documentation.
$np->add_arg( $np->add_arg(
spec => 'URL|U=s', spec => 'url|u=s',
help => '-U, --URL http://192.168.5.10:9332/local_stats', help => '-u, --url (eg. http://192.168.5.10:9332/local_stats}',
required => 1, required => 1,
); );
$np->add_arg( $np->add_arg(
@ -32,36 +37,42 @@ $np->add_arg(
required => 1, required => 1,
); );
$np->add_arg( $np->add_arg(
spec => 'divisor|D=i', spec => 'divisor|d=i',
help => '-D, --divisor 1000000', help => '-d, --divisor 1000000',
); );
$np->add_arg( $np->add_arg(
spec => 'warning|w=s', spec => 'warning|w=s',
help => '-w, --warning INTEGER:INTEGER . See ' help => '-w, --warning INTEGER:INTEGER . See '
. 'http://nagiosplug.sourceforge.net/developer-guidelines.html#THRESHOLDFORMAT ' . 'http://nagiosplug.sourceforge.net/developer-guidelines.html#THRESHOLDFORMAT '
. 'for the threshold format. ', . 'for the threshold format. ',
); );
$np->add_arg( $np->add_arg(
spec => 'critical|c=s', spec => 'critical|c=s',
help => '-c, --critical INTEGER:INTEGER . See ' help => '-c, --critical INTEGER:INTEGER . See '
. 'http://nagiosplug.sourceforge.net/developer-guidelines.html#THRESHOLDFORMAT ' . 'http://nagiosplug.sourceforge.net/developer-guidelines.html#THRESHOLDFORMAT '
. 'for the threshold format. ', . 'for the threshold format. ',
); );
$np->add_arg(
spec => 'perfvars|p=s',
help => '-p, --perfvars . CSV list of fields from JSON response to include in perfdata '
. '{shares}->{dead},{shares}->{live}',
);
# Parse @ARGV and process standard arguments (e.g. usage, help, version) ## Parse @ARGV and process standard arguments (e.g. usage, help, version)
$np->getopts; $np->getopts;
if ($np->opts->verbose) { (print Dumper ($np))};
## GET URL ## GET URL
my $ua = LWP::UserAgent->new; my $ua = LWP::UserAgent->new;
$ua->agent('check_json/0.1'); $ua->agent('check_json/0.2');
$ua->default_header('Accept' => 'application/json'); $ua->default_header('Accept' => 'application/json');
$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);
if ($np->opts->verbose) { (print Dumper ($ua))};
my $response = ($ua->get($np->opts->URL)); my $response = ($ua->get($np->opts->url));
if ($response->is_success) { if ($response->is_success) {
if (!($response->header("content-type") =~ 'application/json')) { if (!($response->header("content-type") =~ 'application/json')) {
@ -71,32 +82,60 @@ if ($response->is_success) {
$np->nagios_exit(CRITICAL, "Connection failed: ".$response->status_line); $np->nagios_exit(CRITICAL, "Connection failed: ".$response->status_line);
} }
## Parse JSON
my $json_response = decode_json($response->content); my $json_response = decode_json($response->content);
if ($np->opts->verbose) { (print Dumper ($json_response))}; if ($np->opts->verbose) { (print Dumper ($json_response))};
my $value; my $check_value;
my $exec = '$value = $json_response->'.$np->opts->attribute; my $check_value_str = '$check_value = $json_response->'.$np->opts->attribute;
if ($np->opts->verbose) {print "EXEC is: $exec \n"};
eval $exec;
if (!defined $value) { if ($np->opts->verbose) { (print Dumper ($check_value_str))};
eval $check_value_str;
if (!defined $check_value) {
$np->nagios_exit(UNKNOWN, "No value received"); $np->nagios_exit(UNKNOWN, "No value received");
} }
if (defined $np->opts->divisor) { if (defined $np->opts->divisor) {
$value = $value/$np->opts->divisor; $check_value = $check_value/$np->opts->divisor;
} }
my $result = $np->check_threshold($value); my $result = $np->check_threshold($check_value);
$np->add_perfdata( my @statusmsg;
label => 'value',
value => $value, # routine to add perfdata from JSON response based on a loop of keys given in perfvals (csv)
threshold => $np->threshold(), if ($np->opts->perfvars) {
); foreach my $key (split(',', $np->opts->perfvars)) {
# use last element of key as label
my $label = (split('->', $key))[-1];
# make label ascii compatible
$label =~ s/[^a-zA-Z0-9_-]//g ;
my $perf_value;
$perf_value = eval '$json_response->'.$key;
if ($np->opts->verbose) { print Dumper ("JSON key: ".$label.", JSON val: " . $perf_value) };
if ( defined($perf_value) ) {
# add threshold if attribute option matches key
if ($key eq $np->opts->attribute) {
push(@statusmsg, "$label: $check_value");
$np->add_perfdata(
label => lc $label,
value => $check_value,
threshold => $np->threshold(),
);
} else {
push(@statusmsg, "$label: $perf_value");
$np->add_perfdata(
label => lc $label,
value => $perf_value,
);
}
}
}
}
$np->nagios_exit( $np->nagios_exit(
return_code => $result, return_code => $result,
message => $value, message => join(', ', @statusmsg),
); );