Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for supplying basic auth parameters in a separate argument #34

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 16 additions & 2 deletions check_json.pl
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
use strict;
use HTTP::Request::Common;
use LWP::UserAgent;
use URI::URL;
use JSON;
use Nagios::Plugin;
use Data::Dumper;
Expand All @@ -18,9 +19,10 @@
. "[ -d|--divisor <divisor> ] "
. "[ -m|--metadata <content> ] "
. "[ -T|--contenttype <content-type> ] "
. "[ -A|--auth <username:password> ] "
. "[ --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}' "
Expand Down Expand Up @@ -94,6 +96,12 @@
. "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",
Expand All @@ -107,7 +115,7 @@
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);
Expand All @@ -117,6 +125,12 @@
$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;
Expand Down