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