#!/usr/bin/perl use strict; use warnings; use Getopt::Std; use IO::Socket::INET; use Time::HiRes qw(gettimeofday); my ($start,$resptime,$port,$host,$expect,$testing,$threshold,$runcmd); my($portn,$script_name,$devupdcmd,$ind); $| = 1; $port=53; # 80 for http, 25 for SMTp, 53 for DNS - User chooses what they want! $testing=0; # 1 if we are testing , 0 if we're really collecting $threshold = 0.5; # A threshold of 0.5 seconds to respond $runcmd = "cmd /c echo Threshold breached."; # Command to run when threshold is breached $script_name = "time2open"; # The name of this script $devupdcmd = "\"\\Program Files\\HP OpenView\\bin\\devupd\""; # The update command ######################## Probably won't need to change anything below here ################## for($ind=1; $ind< @ARGV; ++$ind){ ($host,$portn)=split /\./,$ARGV[$ind]; $resptime="U"; # Failed by default $port = ($portn eq "dns" ) ? 53 : $portn; # The name 'dns' does not resolve # Start the timer here! # Open the socket $start = now(); my $soket = new IO::Socket::INET->new( PeerAddr=> $host, PeerPort=> $port, Proto=> 'tcp', Timeout=> 30, ); if ( $soket ) { # Everything OK so far $soket->autoflush(1); $resptime = now() - $start; # End the timer here ## while(<$soket>){ print; last;} shutdown($soket,2); close $soket; } # 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) {print `$runcmd`; } } if ($testing > 0) { if($resptime ne "U") { print "Response time of $resptime observered for $host on $portn\n"; ### print`$devupdcmd`; # Just run an update with no arguments - should see the usage } else { # Tell someone we've had a problem print ("Port $portn to $host did not open\n"); } } else { print `$devupdcmd $ARGV[0] $script_name $host.$portn Now $resptime`; } } sub now { my(@tmp) = gettimeofday(); return $tmp[0] + ($tmp[1] / 1000000.0); }