1
0
mirror of https://github.com/c-kr/check_json.git synced 2024-11-23 19:03:49 +01:00

Merge branch 'tentwentyfour-master'

This commit is contained in:
pmoranga 2017-10-18 13:14:55 +02:00
commit 15535d80f8

View File

@ -4,10 +4,13 @@ use warnings;
use strict; use strict;
use HTTP::Request::Common; use HTTP::Request::Common;
use LWP::UserAgent; use LWP::UserAgent;
use URI::URL;
use JSON; use JSON;
use Nagios::Plugin; use Nagios::Plugin;
use Data::Dumper; use Data::Dumper;
my $version = '0.7';
my $np = Nagios::Plugin->new( my $np = Nagios::Plugin->new(
usage => "Usage: %s -u|--url <http://user:pass\@host:port/url> -a|--attributes <attributes> " usage => "Usage: %s -u|--url <http://user:pass\@host:port/url> -a|--attributes <attributes> "
. "[ -c|--critical <thresholds> ] [ -w|--warning <thresholds> ] " . "[ -c|--critical <thresholds> ] [ -w|--warning <thresholds> ] "
@ -18,11 +21,12 @@ my $np = Nagios::Plugin->new(
. "[ -d|--divisor <divisor> ] " . "[ -d|--divisor <divisor> ] "
. "[ -m|--metadata <content> ] " . "[ -m|--metadata <content> ] "
. "[ -T|--contenttype <content-type> ] " . "[ -T|--contenttype <content-type> ] "
. "[ -A|--auth <username:password> ] "
. "[ --ignoressl ] " . "[ --ignoressl ] "
. "[ -A|--hattrib <value> ] " . "[ -A|--hattrib <value> ] "
. "[ -C|--hcon <value> ] " . "[ -C|--hcon <value> ] "
. "[ -h|--help ] ", . "[ -h|--help ] ",
version => '0.51', version => $version,
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 --url http://192.168.5.10:9332/local_stats --attributes '{shares}->{dead}' " . "check_json.pl --url http://192.168.5.10:9332/local_stats --attributes '{shares}->{dead}' "
@ -96,6 +100,12 @@ $np->add_arg(
. "Content-type accepted if different from application/json ", . "Content-type accepted if different from application/json ",
); );
$np->add_arg(
spec => 'auth|A=s',
help => '-A, --auth realm:username:password',
required => 0,
);
$np->add_arg( $np->add_arg(
spec => 'ignoressl', spec => 'ignoressl',
help => "--ignoressl\n Ignore bad ssl certificates", help => "--ignoressl\n Ignore bad ssl certificates",
@ -127,7 +137,7 @@ if ( not $np->opts->hattrib and $np->opts->hcon) {
my $ua = LWP::UserAgent->new; my $ua = LWP::UserAgent->new;
$ua->env_proxy; $ua->env_proxy;
$ua->agent('check_json/0.5'); $ua->agent('check_json/'. $version);
$ua->default_header('Accept' => 'application/json'); $ua->default_header('Accept' => 'application/json');
$ua->default_header($np->opts->hattrib => $np->opts->hcon) if ( $np->opts->hattrib and $np->opts->hcon ); $ua->default_header($np->opts->hattrib => $np->opts->hcon) if ( $np->opts->hattrib and $np->opts->hcon );
$ua->protocols_allowed( [ 'http', 'https'] ); $ua->protocols_allowed( [ 'http', 'https'] );
@ -138,6 +148,12 @@ if ($np->opts->ignoressl) {
$ua->ssl_opts(verify_hostname => 0, SSL_verify_mode => 0x00); $ua->ssl_opts(verify_hostname => 0, SSL_verify_mode => 0x00);
} }
if ($np->opts->auth) {
my @credentials = split(':', $np->opts->auth);
my $url = url $np->opts->url;
$ua->credentials($url->host . ':' . $url->port, $credentials[0], $credentials[1], $credentials[2]);
}
if ($np->opts->verbose) { (print Dumper ($ua))}; if ($np->opts->verbose) { (print Dumper ($ua))};
my $response; my $response;