From 9d590c2cd19cf010c06c844f8f475a4ca32e757c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rodolphe=20Qui=C3=A9deville?= Date: Tue, 6 May 2014 13:15:50 +0200 Subject: [PATCH] Add option -T|contenttype to specify alternative content-type if the server does not answer application/json --- README.md | 8 +++++++- check_json.pl | 11 ++++++++++- 2 files changed, 17 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 92814a8..96b71dc 100644 --- a/README.md +++ b/README.md @@ -9,7 +9,7 @@ Performance data is also enhanced to extract performance data compliant to Nagio Usage: ``` -check_json -u|--url -a|--attribute [ -c|--critical ] [ -w|--warning ] [ -p|--perfvars ] [ -t|--timeout ] [ -d|--divisor ] [ -h|--help ] +check_json -u|--url -a|--attribute [ -c|--critical ] [ -w|--warning ] [ -p|--perfvars ] [ -t|--timeout ] [ -d|--divisor ] [ -T|--contenttype ] [ -h|--help ] ``` Example: @@ -22,3 +22,9 @@ 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 ``` +Requirements +============ + +Perl JSON package + +* Debian : libjson-perl diff --git a/check_json.pl b/check_json.pl index 638e880..2566382 100755 --- a/check_json.pl +++ b/check_json.pl @@ -13,6 +13,7 @@ my $np = Nagios::Plugin->new( . "[ -p|--perfvars ] " . "[ -t|--timeout ] " . "[ -d|--divisor ] " + . "[ -T|--contenttype ] " . "[ -h|--help ] ", version => '0.2', blurb => 'Nagios plugin to check JSON attributes via http(s)', @@ -52,12 +53,20 @@ $np->add_arg( . 'http://nagiosplug.sourceforge.net/developer-guidelines.html#THRESHOLDFORMAT ' . '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}', ); +$np->add_arg( + spec => 'contenttype|T=s', + default => 'application/json', + help => '-T, --contenttype . content-type accepted if different from application/json ', +); + + ## Parse @ARGV and process standard arguments (e.g. usage, help, version) $np->getopts; if ($np->opts->verbose) { (print Dumper ($np))}; @@ -75,7 +84,7 @@ if ($np->opts->verbose) { (print Dumper ($ua))}; my $response = ($ua->get($np->opts->url)); if ($response->is_success) { - if (!($response->header("content-type") =~ 'application/json')) { + if (!($response->header("content-type") =~ $np->opts->contenttype)) { $np->nagios_exit(UNKNOWN,"Content type is not JSON: ".$response->header("content-type")); } } else {