网络
来源:互联网 发布:qq三国70js橙鬼武器 编辑:程序博客网 时间:2024/04/29 09:56
use strict;
use warnings;
use Net::Ping;
my @host_array = qw(127.0.0.1);
my $last_isOk = 1; #上次的测试结果
my $curr_isOk = 1; #最终测试结果
my $normalT = 5; #正常情况下测试间隔
my $failedT = 1;
my $is_runing = 0;
&setLock(1);
while (1) {
$last_isOk = $curr_isOk;
foreach my $host (@host_array)
{
$curr_isOk = (&test_site($host)) ? 1 : 0;
last if $curr_isOk; #如果有成功,则不继续测
}
#本地网络有问题
unless ($curr_isOk) {
if (1 == $last_isOk) {#上次成功,本次失败,需要修改失败锁
&setLock(0);
#写日志#---待完善
}
}
elsif(0 == $last_isOk) { #上次失败,本次成功,需要修改失败锁
&setLock(1);
#写日志#---待完善
}
unless ($is_runing) {
#启动日志,---待完善
print "-----start ok-----/n";
$is_runing = 1;
}
sleep ($curr_isOk ? $normalT : $failedT);
}
##====================================主程序到此结束========================
#设置文件锁
#入参:1设置为成功,0设置为失败
sub setLock
{
#---待完善
print "set ",shift, "/n";
}
#失败返回0,成功返回1
sub test_site
{
my $host = shift;
my $timeOut = 5; #超时时间
my $max_ms = 200/1000; #ping 允许的时延,200毫秒
my $tmpIs = 1; #测试结果
my $p = Net::Ping->new("icmp");
$p->hires();
my ($ret, $duration, $ip) = $p->ping($host, $timeOut); #进行 icmp 测试
unless ($ret and ($duration < $max_ms)){#icmp 失败,进行 tcp 测试
#写日志---待完善,记录返回时间
sleep 1;
my $tcp_p = Net::Ping->new("tcp", 2);
$tcp_p->port_number(getservbyname("http", "tcp"));
$tmpIs = 0 unless $tcp_p->ping($host);
$tcp_p->close();
}
$p->close();
return $tmpIs;
}