#!/usr/local/bin/perl # # Use try to connect to a http server. # For use with "mon". # # Arguments are "-p port host [host...]" # # Jon Meek # American Cyanamid Company # Princeton, NJ # # $Id: http.plugin,v 1.2.2.1 2002/07/22 15:21:48 paul-cs Exp $ # # Copyright (C) 1998, Jim Trocki # # 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 # # Modified by Paul van Tilburg. Speedup needed for Checkservice... # (removed multiple host support and changed default values) use Getopt::Std; use English; use CS::Functions; getopts ("p:t:u:h:"); $Port = $opt_p || 80; $TIMEOUT = $opt_t || 5; $Host = $opt_h || "localhost"; $URL = $opt_u || "/"; exit &httpGET($Host, $Port); sub httpGET { my ($Server, $Port) = @_; my ($ServerOK, $TheContent) = (1); eval { local $SIG{ALRM} = sub { die "Timeout Alarm" }; alarm $TIMEOUT; my $sock = opensocket($Server, $Port); # Open a connection to the server if (!$sock) { # Failure to open the socket return 1; } print $sock "GET $URL HTTP/1.0\r\n"; print $sock "Host: $Server\r\n"; print $sock "User-Agent: Checkservice/http.plugin\r\n\r\n"; while ($in = <$sock>) { $TheContent .= $in; # Store data for later processing } # HTTP/1.1 200 OK if ($TheContent =~ /^HTTP\/([\d\.]+)\s+200\b/) { $ServerOK = 0; } else { print STDERR "Could not find HTTP/1.? OK\n"; $ServerOK = 1; } close($sock); alarm 0; # Cancel the alarm }; if ($EVAL_ERROR and ($EVAL_ERROR =~ /Timeout Alarm/)) { return 2; } return $ServerOK; }