mirror of
https://github.com/c-kr/check_json.git
synced 2024-11-23 10:53:47 +01:00
initial commit
This commit is contained in:
parent
14afb9664f
commit
04a60b603a
28
README.md
28
README.md
@ -1,4 +1,30 @@
|
|||||||
check_json
|
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 Nagis::Plugins Perl Module, allowing to use thresholds and performancedata collection from various json attributes.
|
||||||
|
|
||||||
|
Usage: check_json [ -v|--verbose ] [-U <URL>] [-t <timeout>] [ -c|--critical <threshold> ] [ -w|--warning <threshold> ] [ -a | --attribute ] <attribute>
|
||||||
|
|
||||||
|
-?, --usage
|
||||||
|
Print usage information
|
||||||
|
-h, --help
|
||||||
|
Print detailed help screen
|
||||||
|
-V, --version
|
||||||
|
Print version information
|
||||||
|
--extra-opts=[section][@file]
|
||||||
|
Read options from an ini file. See http://nagiosplugins.org/extra-opts
|
||||||
|
for usage and examples.
|
||||||
|
-U, --URL http://192.168.5.10:9332/local_stats
|
||||||
|
-a, --attribute {shares}->{dead}
|
||||||
|
-w, --warning INTEGER:INTEGER . See http://nagiosplug.sourceforge.net/developer-guidelines.html#THRESHOLDFORMAT for the threshold format.
|
||||||
|
-c, --critical INTEGER:INTEGER . See http://nagiosplug.sourceforge.net/developer-guidelines.html#THRESHOLDFORMAT for the threshold format.
|
||||||
|
-t, --timeout=INTEGER
|
||||||
|
Seconds before plugin times out (default: 15)
|
||||||
|
-v, --verbose
|
||||||
|
Show details for command-line debugging (can repeat up to 3 times)
|
||||||
|
|
||||||
|
Example:
|
||||||
|
check_json.pl -U http://192.168.5.10:9332/local_stats -a '{shares}->{dead}' -w :5 -c :10
|
||||||
|
|
||||||
|
88
check_json.pl
Executable file
88
check_json.pl
Executable file
@ -0,0 +1,88 @@
|
|||||||
|
#!/usr/bin/env perl
|
||||||
|
|
||||||
|
use warnings;
|
||||||
|
use strict;
|
||||||
|
use LWP::UserAgent;
|
||||||
|
use JSON 'decode_json';
|
||||||
|
use Nagios::Plugin;
|
||||||
|
use Data::Dumper;
|
||||||
|
|
||||||
|
my $np = Nagios::Plugin->new(
|
||||||
|
usage => "Usage: %s [ -v|--verbose ] [-U <URL>] [-t <timeout>] "
|
||||||
|
. "[ -c|--critical <threshold> ] [ -w|--warning <threshold> ] "
|
||||||
|
. "[ -a | --attribute ] <attribute>",
|
||||||
|
version => '0.1',
|
||||||
|
blurb => 'Nagios plugin to check JSON attributes via http(s)',
|
||||||
|
extra => "\nExample: \n"
|
||||||
|
. "check_json.pl -U http://192.168.5.10:9332/local_stats -a '{shares}->{dead}' -w :5 -c :10",
|
||||||
|
url => 'url',
|
||||||
|
license => 'GPLv2',
|
||||||
|
plugin => 'check_json',
|
||||||
|
timeout => 15,
|
||||||
|
);
|
||||||
|
|
||||||
|
# add valid command line options and build them into your usage/help documentation.
|
||||||
|
$np->add_arg(
|
||||||
|
spec => 'URL|U=s',
|
||||||
|
help => '-U, --URL http://192.168.5.10:9332/local_stats',
|
||||||
|
required => 1,
|
||||||
|
);
|
||||||
|
$np->add_arg(
|
||||||
|
spec => 'attribute|a=s',
|
||||||
|
help => '-a, --attribute {shares}->{dead}',
|
||||||
|
required => 1,
|
||||||
|
);
|
||||||
|
$np->add_arg(
|
||||||
|
spec => 'warning|w=s',
|
||||||
|
help => '-w, --warning INTEGER:INTEGER . See '
|
||||||
|
. 'http://nagiosplug.sourceforge.net/developer-guidelines.html#THRESHOLDFORMAT '
|
||||||
|
. 'for the threshold format. ',
|
||||||
|
);
|
||||||
|
$np->add_arg(
|
||||||
|
spec => 'critical|c=s',
|
||||||
|
help => '-c, --critical INTEGER:INTEGER . See '
|
||||||
|
. 'http://nagiosplug.sourceforge.net/developer-guidelines.html#THRESHOLDFORMAT '
|
||||||
|
. 'for the threshold format. ',
|
||||||
|
);
|
||||||
|
|
||||||
|
# Parse @ARGV and process standard arguments (e.g. usage, help, version)
|
||||||
|
$np->getopts;
|
||||||
|
|
||||||
|
|
||||||
|
## GET URL
|
||||||
|
my $ua = LWP::UserAgent->new;
|
||||||
|
|
||||||
|
$ua->agent('Redirect Bot');
|
||||||
|
$ua->protocols_allowed( [ 'http', 'https'] );
|
||||||
|
$ua->parse_head(0);
|
||||||
|
$ua->timeout($np->opts->timeout);
|
||||||
|
|
||||||
|
my $response = $ua->get($np->opts->URL);
|
||||||
|
|
||||||
|
if ($response->header("content-type") ne 'application/json') {
|
||||||
|
$np->nagios_exit(UNKNOWN,"Content type is not JSON: ".$response->header("content-type"));
|
||||||
|
}
|
||||||
|
|
||||||
|
my $json_response = decode_json($response->content) ; #NAG-EXIT;
|
||||||
|
if ($np->opts->verbose) { (print Dumper ($json_response))};
|
||||||
|
|
||||||
|
my $value;
|
||||||
|
my $exec = '$value = $json_response->'.$np->opts->attribute;
|
||||||
|
if ($np->opts->verbose) {print "EXEC is: $exec \n"};
|
||||||
|
eval $exec;
|
||||||
|
|
||||||
|
if (!defined $value) {
|
||||||
|
$np->nagios_exit(UNKNOWN, "No value received");
|
||||||
|
}
|
||||||
|
|
||||||
|
$np->add_perfdata(
|
||||||
|
label => 'value',
|
||||||
|
value => $value,
|
||||||
|
#threshold => $threshold,
|
||||||
|
);
|
||||||
|
|
||||||
|
$np->nagios_exit(
|
||||||
|
return_code => $np->check_threshold($value),
|
||||||
|
message => $value
|
||||||
|
);
|
||||||
|
|
Loading…
Reference in New Issue
Block a user