1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224
|
#!/usr/bin/perl
#
# Try to connect to a http server.
# For use with "mon".
#
# Version to measure and log http file transfer speed and use a proxy
#
# Use: "-l log_file -T alarm_seconds -t timeout_seconds host [host:/path_to_doc ...]"
# -d for interactive commandline debugging
#
# Configuration file example:
#
# hostgroup internet_web www.ama-assn.org
# www.gartner.com
# test.mysite.com:/~meekj/ca_zip.txt
# ot.myweb.com/ca_zip.txt@proxy.mysite.com
#
# watch internet_web
# service internet_web
# interval 5m
# monitor http_tp.monitor -l /usr/local/mon/logs/internet_web_YYYYMM.log -T 15 -t 100
# period wd {Sun-Sat}
# alert mail.alert firewall_admin
# alertevery 1h summary
#
# TODO: Allow port number to be specified in URL
#
#
# Jon Meek
# Lawrenceville, NJ
# meekj@ieee.org
#
# $Id: http_tp.monitor 1.2 Sat, 30 Jun 2001 14:44:29 -0400 trockij $
#
# Copyright (C) 1998, Jon Meek
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
#
use Getopt::Std;
use English;
use Time::HiRes qw( gettimeofday tv_interval );
use LWP::UserAgent;
use HTTP::Request::Common;
getopts ("dt:T:l:");
#$PORT = $opt_p || 80;
$TIMEOUT = $opt_t || 30;
@failures = ();
$TimeOfDay = time;
foreach $host_path (@ARGV) { # Build host and path lists
print "ARG: $host_path\n" if $opt_d;
undef $proxy_server;
if ($host_path =~ /@/) {
($host_path, $proxy_server) = split(/@/, $host_path, 2);
}
($host, $Path) = split(/\//, $host_path, 2);
if (defined $proxy_server) {
$ProxyServer = $proxy_server;
} else {
$ProxyServer = 'noproxy';
}
if ($opt_d) {
print "$host - $ProxyServer - $Path\n";
}
push(@Hosts, $host);
push(@Proxies, $ProxyServer);
push(@Paths, $Path);
}
# Open logfile, if -l
if ($opt_l) {
open(LOG, ">>$opt_l") || warn "$0 Can't open logfile: $opt_l\n";
}
# Do a dummy httpGET to initialize the code, if we don't do this there
# will be 250 ms extra added to the first time. This dummy call
# still does not completely clear this problem, there may still be an
# extra 10ms on the first test.
$host = '';
$path = '';
$proxy = '';
($OK, $ByteCount, $GetTime) = &httpGET($host, $path, $proxy);
for ($i = 0; $i <= $#Hosts; $i++) {
$host = $Hosts[$i];
$proxy = $Proxies[$i];
$path = $Paths[$i];
print "$host - $path - $proxy\n\n" if ($opt_d);
($OK, $ByteCount, $GetTime) = &httpGET($host, $path, $proxy);
if (!defined (OK) || $OK == 0) {
push (@failures, $host);
}
if (length($path) < 1) { # Set the path for logging purposes
$path = '/'; # we don't want an empty, space separated, field
}
$ResultString{$i} = sprintf "%d %s %s %s %d %8.3f",
$TimeOfDay, $proxy, $host, $path, $ByteCount, $GetTime;
# print "$ResultString{$i}\n";
}
# Write results to logfile, if -l
if ($opt_l) {
$LogFile = $opt_l;
($sec,$min,$hour,$mday,$Month,$Year,$wday,$yday,$isdst) =
localtime($TimeOfDay);
$Month++; $Year += 1900;
$YYYYMM = sprintf('%04d%02d', $Year, $Month);
$LogFile =~ s/YYYYMM/$YYYYMM/; # Fill in current year and month
if (-e $LogFile) { # Check for existing log file
$NewLogFile = 0;
} else {
$NewLogFile = 1;
}
open(LOG, ">>$LogFile") || warn "$0 Can't open logfile: $LogFile\n";
foreach $host (sort keys %ResultString) {
print LOG "$ResultString{$host}\n";
}
close LOG;
}
if (@failures == 0) {
exit 0;
}
print join (" ", sort @failures), "\n";
foreach $host (sort keys %ResultString) {
print "$ResultString{$host}\n";
}
exit 1;
sub httpGET {
my($Server, $Path, $Proxy) = @_;
my($ServerOK, $TheContent, $ByteCount, $URL,
$dt, $t0, $t1, $ResultCode, $TheContent, $ua);
$URL = "http://$Server/$Path";
$ua = new LWP::UserAgent;
$ua->timeout($TIMEOUT); # Set timeout for LWP, used to do an eval/alarm
print "URL: $URL Proxy: $Proxy\n" if $opt_d;
$TheContent = '';
if ($Proxy ne 'noproxy') {
$ua->proxy('http', "http://$Proxy");
}
$dt = 0;
$t0 = [gettimeofday]; # Get start time
$response = $ua->request(GET $URL);
$t1 = [gettimeofday]; # Get end time
$dt = tv_interval($t0, $t1);
$ResultCode = $response->code();
$TheContent = $response->content();
$ByteCount = length($TheContent);
if ($ResultCode == 200) { # HTTP/1.1 200 OK
$ServerOK = 1;
} else {
$ServerOK = 0;
}
if ($ServerOK) { # Success, otherwise set time to -1 to indicate time-out
# $t1 = [gettimeofday];
# $dt = tv_interval($t0, $t1);
} else {
$dt = -1;
}
if (($opt_T) && ($dt >= 0)) { # Check time limit for alarm
if ($dt > $opt_T) {
$ServerOK = 0;
} else {
$ServerOK = 1;
}
}
print "ServerOK: $ServerOK - dt: $dt - ResultCode $ResultCode\n" if $opt_d;
return $ServerOK, $ByteCount, $dt;
}
|