Here's the beast I talked about writing earlier - a https.monitor with
regexp matching capabilities.
It takes a -r 'regexp' argument for ensuring the response conforms to what
you hope, and a -n 'regexp' for ensuring the response does _not_ contain a
certain string, like for example an error message included into an
otherwise fine page.
Thanks to Andrew Ryan for https.monitor and Gilles Lamiral for
phttp.monitor.
Patches and comments welcome. It Works For Me (TM).
--
Erik I. Bols�, Triangel Maritech Software AS | Skybert AS
Tlf: 712 41 694 Mobil: 915 79 512
#!/usr/bin/perl
#
# $Id: https-rx.monitor,v 1.3 2001/11/20 11:04:09 erik Exp $
#
# https-rx.monitor by Erik Inge Bols�
#
# Modified from https.monitor by Andrew Ryan and phttp.monitor by Gilles Lamiral
#
# License: GPL v2
#
=head1 NAME
http-rx.monitor - https monitor with regexps.
=head1 SYNOPSIS
type this:
https-rx.monitor -h
and read.
=head1 DESCRIPTION
https-rx.monitor checks https servers. The response can be parsed using
an arbitrary regular expression, and another "should not occur" regular expression.
=head1 RETURN STATUS
O on success for all hosts, or usage demand (--help option)
1 on failure of any host
=cut
use strict;
use Socket;
use Net::SSLeay qw(die_now die_if_ssl_error) ;
use Getopt::Std;
#
use English;
use vars qw($opt_p $opt_t $opt_u $opt_v $opt_r $opt_n $opt_h);
getopts ("vhp:t:u:r:n:");
my $PORT = $opt_p || 443;
my $TIMEOUT = $opt_t || 30;
my $URL = $opt_u || "/";
my $perl = "/usr/bin/perl"; # where you keep perl
my $field_delim = "<>"; # html field delimiter
my $regex = $opt_r || '^HTTP/([\d\.]+)\s+200\b';
my $notregex = $opt_n || '^HTTP/([\d\.]+)\s+500\b';
my @failures = ();
my @detail = ();
my ($host, $OK, $default_header, $auth_header, $end_header, $request_header, $msg);
my ($dest_ip, $dest_serv, $sockaddr_template, $dest_serv_params, $ctx, $ssl, $res,
$reply, $got, $ServerOK);
if ($opt_h)
{
usage();
exit 0;
}
foreach $host (@ARGV) {
$OK = &httpsGET($host, $PORT, $URL);
if (!defined ($OK) || $OK == 0) {
push (@failures, $host);
}
}
if (@failures == 0) {
exit 0;
}
print "@failures\n";
print join(";",@detail);
exit 1;
# Main function begins here
sub httpsGET {
my ($site, $port, $path) = (@_);
my $total_bytes = 0; #set total bytes transferred to 0
my ($page, $result, %headers);
# print "attempting to contact site $site on port $port with path $path\n";
eval {
local $SIG{ALRM} = sub { die "Timeout Alarm" };
alarm $TIMEOUT;
$result = `$perl -e'use Net::SSLeay ; Net::SSLeay::SSLeay_add_ssl_algorithms()
; print join("$field_delim",Net::SSLeay::get_https("$site", "$port", "$path"))'
2>/dev/null`;
alarm 0; #cancel the alarm
($page, $result, %headers) = split ("<>",$result);
print "Result was `$result'\n" if $opt_v;
foreach my $h (sort keys %headers) {
print "Header `$h'\tvalue `$headers{$h}'\n" if $opt_v;
}
if ($result =~ m~$regex~) {
if ($result =~ m~$notregex~) {
$ServerOK = 0;
push(@detail,"$result");
}
else {
$ServerOK = 1;
}
} else {
$ServerOK = 0;
push(@detail,"$result");
}
};
if ($EVAL_ERROR and ($EVAL_ERROR eq 'Timeout Alarm')) {
print "**** Time Out\n";
return 0;
}
return $ServerOK;
}
sub usage {
print <<EOF;
USAGE : $0 [options] host1 host2 ...
OPTIONS
-h : print the command usage (this message).
-p <int> : the https port to connect to.
default is 443.
-t <int> : timeout in seconds (default 30)
-u <string> : the url to test (default / )
-r <string> : regular expression to match for a good http response.
default is :
^HTTP/([\\d\\.]+)\\s+200\\b
CAVEAT: Do not forget to quote the string.
-n <string> : regular expression to match for a known bad http response.
i.e. database error message in a friendly non-500 error page.
default, rather useless one is :
^HTTP/([\\d\\.]+)\\s+500\\b
CAVEAT: Do not forget to quote the string.
ARGUMENTS
host1 host2 ... : list of hosts to check
DEFAULT
default behaviour with no options specified, is the same as:
$0 \\
-p 443 \\
-t 30 \\
-u / \\
-r '^HTTP/([\\d\\.]+)\\s+200\\b'
-n '^HTTP/([\\d\\.]+)\\s+500\\b'
EOF
}