--- lwp-http.mon 2002/09/02 13:59:54 1.1 +++ lwp-http.mon 2003/06/23 21:48:53 1.4 @@ -1,4 +1,5 @@ #!/usr/local/bin/perl +# # File: lwp-http.mon # Author: Daniel Hagerty, hag@linnaean.org # Date: Sun Mar 19 22:06:02 2000 @@ -6,23 +7,22 @@ # Lots of options. # # $Id: lwp-http.mon,v 1.3 2000/03/20 05:55:48 hag Exp $ +# +# 2002-09-02 Dobrica Pavlinusic +# added option -o which will return success if ANY of server responded with +# success (so that you can ignore alerts if backup servers are working) use strict; use LWP::UserAgent; use HTTP::Cookies; use HTTP::Request; -use Getopt::Std; +use Getopt::Long qw(:config pass_through); # leave ARGV use File::Basename; use URI; ### -use vars qw($opt_h $opt_p $opt_t $opt_z $opt_d $opt_r $opt_s $opt_P - $opt_v $opt_c); - -## - # Configure this. my $maintainer = 'youremailhere@localhost'; @@ -33,6 +33,13 @@ my $regex; my $proto = "http"; my $timeout = 60; +my $invert; +my $nozero; +my $one; +my $envproxy; +my $proxy; +my $cookies; +my $extended_help; my $version = "0.1"; my $agent = "Yet Another Monitor Bot/$version"; @@ -42,14 +49,7 @@ ### sub main { - do_usage() if(@_ == 0); - - $directory = $opt_d if($opt_d); - $port = $opt_p if($opt_p); - $timeout = $opt_t if($opt_t); - $regex = $opt_r if($opt_r); - $proto = "https" if ($opt_s); - $proto = $opt_P if($opt_P); + do_usage() if(! @_); $directory =~ s/^\///; # Nuke leading slash $u_proto = $proto; $u_proto =~ tr/[a-z]/[A-Z]/; @@ -58,8 +58,11 @@ $user_agent->agent($agent); $user_agent->from($maintainer); $user_agent->timeout($timeout); + $user_agent->proxy(['http', 'ftp'], $proxy) if ($proxy); + $user_agent->env_proxy() if ($envproxy); my @failed; + my @available; my %failure; host: foreach my $host (@_) { @@ -71,7 +74,7 @@ next host; }; - if($opt_c) { + if($cookies) { # Generate new cookies for each host. my $cookies = HTTP::Cookies->new() || &{$ht_lose}("HTTP::Cookies create failure"); @@ -95,26 +98,31 @@ } my $strref = $response->content_ref; - if(!$opt_z && length($$strref) == 0) { + if(!$nozero && length($$strref) == 0) { &{$ht_lose}("Empty document"); } if(defined($regex)) { my $winning; map {$winning++ if(/$regex/);} split("\n", $$strref); - if($opt_v) { + if($invert) { &{$ht_lose}("Failure regex matches:", $winning) if($winning); } elsif(!$winning) { &{$ht_lose}("Regex not found"); } } + push(@available, $host); } if(@failed) { print "$u_proto Failures: " . join(" ", @failed) . "\n"; foreach my $fail (@failed) { print "$fail: $failure{$fail}\n"; } - exit(1); + if ($one && ($#available+1) > 0) { + print "$u_proto Available: ".join(" ", @available)."\n"; + } else { + exit(1); + } } exit; } @@ -124,22 +132,24 @@ } sub do_usage { - my $extended = shift; my $base = basename $0; print STDERR "Usage: $base [options...] hosts ...\n"; - if($extended) { + if($extended_help) { print <<'EOF'; --h Help. You're reading it. --d URL URL to test on the remote host. Default is /. --p PORT Port to connect to. Default is proto specific. --P PROTO Protocol to fetch. Default is http. --s Fetch via https. Equivalent to -P https. --t TIMEOUT Timeout for the fetch. Default is 60 seconds. --r REGEX A regular expression that the retrieved content must match. --v Invert the regular expression. Content must NOT match. --z Supress zero-length check. --c Enable Cookies. +-h Help. You're reading it. +-d|--url URL URL to test on the remote host. Default is /. +-p|--port PORT Port to connect to. Default is proto specific. +-P|--proto PROTO Protocol to fetch. Default is http. +-s|--https Fetch via https. Equivalent to -P https. +-t|--timeout TIMEOUT Timeout for the fetch. Default is 60 seconds. +-r|--regex REGEX A regexp that the retrieved content must match. +-v|--invert Invert the regular expression. Content must NOT match. +-z|--nozero Supress zero-length check. +-c|--cookies Enable Cookies. +-o|--one Return success if at least One server is available. +--envproxy User proxy server from env http_proxy and friends. +--proxy PROXY Set proxy explicitly EOF } exit 1; @@ -147,8 +157,21 @@ ### -getopts("hszvcp:t:d:r:P:") || do_usage(); -do_usage($opt_h) if($opt_h); +GetOptions( + 'url|d=s' => \$directory, + 'port|p=i' => \$port, + 'timeout|t=i' => \$timeout, + 'regex|r=s' => \$regex, + 'https|s=s' => sub { $proto = "https" }, + 'proto|P=s' => \$proto, + 'invert|v' => \$invert, + 'nozero|z' => \$nozero, + 'cookies|c' => \$cookies, + 'one|o' => \$one, + 'envproxy' => \$envproxy, + 'proxy=s' => \$proxy, + 'help|h' => \$extended_help + ) || do_usage(); &main(@ARGV);