#!/usr/local/bin/perl # Web server checking looks like this: $port=80; # 80 for http - User chooses what they want! $send = "GET /index.html\n"; # What do you want sent $expect="\<\/html\>"; # What you expect to be returned if it worked. $testing=1; # 1 if we are testing , 0 if we're really collecting $threshold = 0.5; # A threshold of 0.5 seconds to respond $runcmd = "echo Threshold breached."; # Command to run when threshold is breached $script_name = "http_time"; # The name of this script ######################## Probably won't need to change anything below here ################## use IO::Socket; use Time::HiRes qw(gettimeofday); for($ind=1; $ind< @ARGV; ++$ind){ $resptime = "U"; # Start the timer here! # Open the socket $start = now(); $soket = IO::Socket::INET->new(Proto => "tcp", PeerAddr => $ARGV[$ind] ,PeerPort => $port, Timeout => 2, ); $resptime="U"; # Failed by default if ( $! eq '' ) { # Everything OK so far $soket->autoflush(1); # Send some protocol dependant data to the server print $soket "$send"; # Send something to the port for it to respond # Read back the results while(<$soket>){ if($testing > 0){ print; }#Print all the responses if testing is set greater than 1 # Check for something in the result? if(/$expect/) { $resptime = now() - $start; if ($testing) { print "Expected response of '$expect' was seen in $resptime seconds for $ARGV[$ind]\n"; } } # Success } close $soket; } else { # Tell someone we've had a problem print ("Port $port to $ARGV[$ind] did not open\n"); } # End the timer here # if $resptime=="U" then the time is not valid so don't check the thresholds. # If the threshold has been breached, run a command! if($resptime ne "U") { if($resptime > $threshold) {`$runcmd`; } } elsif ($testing) { print "Expected response of '$expect' was NOT seen for $ARGV[$ind]\n"; } if ($testing > 0) { `"/opt/OV/bin/devupd"`; # Just run an update with no arguments - should see the usage } else { `"/opt/OV/bin/devupd" $ARGV[0] $script_name $ARGV[$ind] Now $resptime`; } } sub now { my(@tmp) = gettimeofday(); return $tmp[0] + ($tmp[1] / 1000000.0); }