Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Fetching contributors…

Cannot retrieve contributors at this time

executable file 190 lines (155 sloc) 3.141 kb
#!/usr/bin/env perl
## Copyright © 2011-2013 by Daniel Friesel <derf@finalrewind.org>
## License: WTFPL:
## 0. You just DO WHAT THE FUCK YOU WANT TO.
use strict;
use warnings;
use 5.010;
no if $] >= 5.018, warnings => "experimental::smartmatch";
use LWP::UserAgent;
use Nagios::Plugin;
my $ua = LWP::UserAgent->new(
max_size => 4096,
requests_redirectable => [],
timeout => 10,
);
my ($test, @passed, @failed);
my $np = Nagios::Plugin->new(
usage => 'Usage: %s [-v|--verbose] [-H|--host <host>] '
. '[-t|--timeout <timeout>]',
);
$np->add_arg(
spec => 'auth|a=s@',
help => 'Verify that URL requires auth (HTTP 401)',
);
$np->add_arg(
spec => 'ok|o=s@',
help => 'Verify that URL exists (HTTP 200)',
);
$np->add_arg(
spec => 'redirect|r=s@',
help => 'Verify that FROM is a redirect (HTTP 301 by default) to TO',
label => '"FROM TO [HTTP CODE]"',
);
sub test {
($test) = @_;
if ($np->opts->verbose) {
say "check ${test}";
}
}
sub pass {
my ($msg) = @_;
my $extra = $msg ? " -- got $msg" : q{};
push(@passed, $test . $extra);
if ($np->opts->verbose) {
say ' passed';
}
}
sub fail {
my ($msg) = @_;
my $extra = $msg ? " -- got $msg" : q{};
push(@failed, $test . $extra);
if ($np->opts->verbose) {
say ' failed';
}
}
sub check_redirect {
my ($code, %pairs) = @_;
while (my ($from, $to) = each(%pairs)) {
my $response = $ua->get($from);
test("Redirect (HTTP $code) $from => $to");
if (
$response->code == $code
and $response->header('Location') eq $to
) {
pass();
}
else {
fail(($response->code // 'undef')
. "; location " . ($response->header('Location') // 'undef'));
}
}
}
sub check_ok {
my (@sites) = @_;
for my $site (@sites) {
my $response = $ua->get($site);
test("$site exists (HTTP 200)");
if ($response->code == 200) {
pass();
}
else {
fail($response->code);
}
}
}
sub check_exist {
my (@urls) = @_;
for my $url (@urls) {
my $response = $ua->get($url);
test("$url exists (no 404)");
if ($response->code ~~ [qw[200 301]]) {
pass();
}
else {
fail($response->code);
}
}
}
sub check_auth_required {
my (@sites) = @_;
for my $site (@sites) {
my $response = $ua->get($site);
test("$site requires auth (HTTP 401)");
if ($response->code == 401) {
pass();
}
else {
fail($response->code);
}
}
}
sub finish_tests {
if (@failed == 0) {
$np->nagios_exit(
OK,
sprintf(
'All %d subchecks passed',
scalar @passed,
),
);
}
else {
$np->nagios_exit(
WARNING,
sprintf(
"%d/%d subchecks failed\n%s",
scalar @failed,
(@passed + @failed),
join("\n", @failed),
),
);
exit 1;
}
}
$np->getopts;
if ($np->opts->auth) {
for my $url (@{ $np->opts->auth }) {
check_auth_required($url);
}
}
if ($np->opts->ok) {
for my $url (@{ $np->opts->ok }) {
check_ok($url);
}
}
if ($np->opts->redirect) {
for my $pair (@{ $np->opts->redirect }) {
$pair =~ s{ ^ ["'] }{}ox;
$pair =~ s{ ["'] $ }{}ox;
my ($from, $to, $code) = split(qr{\s+}o, $pair);
$code //= 301;
check_redirect($code, $from => $to);
}
}
finish_tests();
Jump to Line
Something went wrong with that request. Please try again.