Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
tree: 8899a901cf
Fetching contributors…

Cannot retrieve contributors at this time

executable file 83 lines (67 sloc) 1.897 kb
#!/usr/bin/perl -w
#
# file: qad-tcp-checker
# purpose: monitor sas web servers
use strict;
use Data::Dumper;
use IO::Socket;
my @WEB_SERVERS = qw(
10.3.7.105
10.3.7.106
127.0.0.1
);
my $CONNECT_TIMEOUT = 1;
my $SERVER_PORT = 80;
my %connection_stats; # for tracking connection success/failure counts
# -- subroutines --
sub display_stats() {
my @ip_list = keys %connection_stats;
@ip_list = sort
{ $connection_stats{$b}{failure} <=> $connection_stats{$a}{failure} }
@ip_list;
print "\n";
for my $i (@ip_list) {
printf("%16s %7d failures %7d successes\n", $i,
$connection_stats{$i}{failure},
$connection_stats{$i}{success});
}
}
# -- main --
for (@WEB_SERVERS) {
$connection_stats{$_} = {};
$connection_stats{$_}{success} = 0;
$connection_stats{$_}{failure} = 0;
}
# GOAL : give the user a way to read connect stats via the USR1 signal.
# DOC : this signal will interrupt the socket connect call and count as a
# failure
my $USR1_RECEIVED = 0;
$SIG{'USR1'} = sub { $USR1_RECEIVED = 1; };
print $0 ," is starting as pid ", $$ ,
". From another window you may run:\n kill -USR1 ", $$, "\n";
while (1) {
# GOAL : try to connect to the server
SERVER: for my $server_ip (@WEB_SERVERS) {
if ($USR1_RECEIVED) {
$USR1_RECEIVED = 0;
display_stats();
}
my $sock = new IO::Socket::INET (
PeerAddr => $server_ip,
PeerPort => $SERVER_PORT,
Proto => 'tcp',
Timeout => $CONNECT_TIMEOUT,
);
unless ($sock) {
warn "Could not create socket to $server_ip : $!\n";
$connection_stats{$server_ip}{failure} ++;
next SERVER;
}
$connection_stats{$server_ip}{success} ++;
close($sock);
syswrite(STDOUT, '.', 1);
}
# wait a second then check again
#syswrite(STDOUT, "\n", 1);
sleep(1);
}
Jump to Line
Something went wrong with that request. Please try again.