Hi,
Here's a little piece of code I wrote as part of a generic tcp monitoring
program. It handles sites reachable directly and sites reached through a
proxy. Since I've hacked it from that program I make no guarantees that
it'll work "out of the box". For anyone who doesn't recognise it, it's
perl :-).
On Thu, 1 Nov 2001, Deb Heller-Evans wrote:
> Hi,
>
> I need to set up a monitor to tell me whether the port on
> my proxy service is alive or non-responsive.
First is the sub "http" that does the HTTP query
------------------------------------------------------------------------
sub http {
    my %args = @_;
    # Make a connection to the specifed server or proxy and send:
    # $command $scheme://$host$port$path $protocol/$version\r\n\r\n
    # Expect a HTTP response code.
    # set up parameters from %args or "sensible" defaults
    my $command = $args{Command} || 'HEAD';
    my $scheme = $args{Scheme} || 'http';
    my $host = $args{Hostname} || 'localhost';
    my $server = $host;
    my $port = $args{Port} || 80;
    my $server_port = $port;
    my $path = $args{Path} || '/';
    my $protocol = $args{Protocol} || 'HTTP';
    my $version = $args{Version} || '1.0';
    my $match = $args{Match} || "HTTP\/\d\.\d\s+(200|30[12]|401)\b";
    # default command - talking direct to server
    my $cmd = "$command $path $protocol/$version\r\n\r\n";
    # handle proxy requirements
    if (defined $args{Proxy}) {
        $server = $args{Proxy};
        $server_port = $args{ProxyPort} || 80;
        print "connect via proxy $server, port $port\n" if $DEBUG;
        $port = ($port == 80) ? "" : ":$port";
        $cmd = "$command $scheme://$host$port$path $protocol/$version\r\n\r\n";
    }
    my $sock = make_connection($server, $server_port);
    my $sock = IO::Socket::INET->new(PeerAddr => $server,
                PeerPort => $server_port,
                Proto    => "tcp",
                Type     => SOCK_STREAM);
    if ($sock) {
        $sock->autoflush(1);
    }
    else {
        die "$args{Hostname}: connection to $server:$server_port failed: $!";
    }
    my $in;
    my $out;
    $sock->send($cmd);
    print "sent: $cmd\n" if $DEBUG;
    print "want: ", $match, "\n" if $DEBUG;
    $in = <$sock>;
    print "rcvd: $in" if $DEBUG;
    if ($in !~ /$match/) {
        die "$args{Hostname}: unexpected response to $command: $in";
    }
    $sock->close();
}
------------------------------------------------------------------------
The http subroutine could be called as follows to check a site
through a proxy. Note that this checks both the proxy and the remote
site. You could check the proxy simply by treating it as a "direct"
server. As long as it responds (probably with "HTTP/1.0 400 Bad
Request") you can be happy it's working.
http (Hostname  => "www.some.domain",
      Path      => "/",
      Command   => "HEAD",
      Protocol  => "HTTP",
      Version   => "1.0",
      Proxy     => "proxy.my.domain",
      ProxyPort => 80,
      Scheme    => "http");
And here's one that just checks the intranet, directly:
http (Hostname  => "intranet");
Colin
Received on Thu Nov 01 2001 - 16:45:53 MST
This archive was generated by hypermail pre-2.1.9 : Tue Dec 09 2003 - 17:03:50 MST