From baeddb24e445f6e42968e324cf0f824793994783 Mon Sep 17 00:00:00 2001 From: David Raison Date: Sun, 8 Oct 2017 15:22:31 +0200 Subject: [PATCH] Add support for supplying basic auth parameters in a separate argument --- check_json.pl | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) diff --git a/check_json.pl b/check_json.pl index fb253c9..45c9679 100755 --- a/check_json.pl +++ b/check_json.pl @@ -4,6 +4,7 @@ use warnings; use strict; use HTTP::Request::Common; use LWP::UserAgent; +use URI::URL; use JSON; use Nagios::Plugin; use Data::Dumper; @@ -18,9 +19,10 @@ my $np = Nagios::Plugin->new( . "[ -d|--divisor ] " . "[ -m|--metadata ] " . "[ -T|--contenttype ] " + . "[ -A|--auth ] " . "[ --ignoressl ] " . "[ -h|--help ] ", - version => '0.5', + version => '0.6', blurb => 'Nagios plugin to check JSON attributes via http(s)', extra => "\nExample: \n" . "check_json.pl --url http://192.168.5.10:9332/local_stats --attributes '{shares}->{dead}' " @@ -94,6 +96,12 @@ $np->add_arg( . "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( spec => 'ignoressl', help => "--ignoressl\n Ignore bad ssl certificates", @@ -107,7 +115,7 @@ if ($np->opts->verbose) { (print Dumper ($np))}; my $ua = LWP::UserAgent->new; $ua->env_proxy; -$ua->agent('check_json/0.5'); +$ua->agent('check_json/0.6'); $ua->default_header('Accept' => 'application/json'); $ua->protocols_allowed( [ 'http', 'https'] ); $ua->parse_head(0); @@ -117,6 +125,12 @@ if ($np->opts->ignoressl) { $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))}; my $response;