程序调用远端服务,为了确保可用性最好在使用前进行健康检测,将异常的服务排除。
#!/usr/bin/perl
#
use strict;
use IO::Socket;
my @servers;
$servers[0]=['180.153.100.93',2222];
$servers[1]=['180.153.100.93',22];
$servers[2]=['180.153.100.93',80];
my $i=0;
my $interval=3;
my $count=int(60/$interval);
while($i < $count){
foreach (@servers){
if(fetch_server_status(@$_)){
print $$_[0] . ':' . $$_[1] ."ok\n";
}else{
print $$_[0] . ':' . $$_[1] ."bad\n";
}
}
$i++;
sleep $interval;
}
sub fetch_server_status(){
my($ip,$port)=@_;
my $socket=IO::Socket::INET->new(
PeerAddr => $ip,
PeerPort => $port,
Timeout => 1,
);
if(!$socket){
return 0;
}else{
return 1;
}
$socket->close;
}