From 04a60b603a7be956463acd046d02992273108a35 Mon Sep 17 00:00:00 2001 From: Christopher Kreft Date: Sun, 28 Apr 2013 18:39:35 +0200 Subject: [PATCH] initial commit --- README.md | 28 +++++++++++++++- check_json.pl | 88 +++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 115 insertions(+), 1 deletion(-) create mode 100755 check_json.pl diff --git a/README.md b/README.md index 48074a4..14b7d76 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,30 @@ 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 ] [-t ] [ -c|--critical ] [ -w|--warning ] [ -a | --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 + diff --git a/check_json.pl b/check_json.pl new file mode 100755 index 0000000..c447274 --- /dev/null +++ b/check_json.pl @@ -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 ] [-t ] " + . "[ -c|--critical ] [ -w|--warning ] " + . "[ -a | --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 +); +