부하 분산 장치의 오류를 테스트하고 트래픽을 해석하는 빠른 스크립트를 작성하고 있습니다. 한 호스트 또는 다른 호스트에 연결할 수 없으면 연결을 계속 시도하고 싶습니다. 현재 스크립트는 mkcnct 하위의 eval 블록을 실행하는 것처럼 보이지 않으므로 이유를 파악할 수 없습니다. 아무도 내가 잘못하고있는 것을 발견 할 수 있습니까?평가/알람이 실행 중입니까?
#!/usr/bin/perl
use strict;
use Net::HTTP;
use Getopt::Std;
my %opts;
getopts('ht:',\%opts);
my @hostlist ("www.foo.com","www1.foo.com","www2.foo.com");
my $timeout;
if ($opts{t} =~ /\d+/) {
$timeout = $opts{t} + time();
} else {
$timeout = 3600 + time();
}
while ($timeout < time()) {
foreach my $host (@hostlist) {
my $cnct = mkcnct($host);
if ($cnct) { mkreq($cnct) };
}
}
sub mkreq {
my $cnct = shift;
my $time = gettime();
my $out;
$cnct->write_request(GET => "/index.html");
($out->{code},$out->{message},%{$out->{headers}}) = $cnct->read_response_headers;
printf "%s\t%s - Size %d\tLast modified %s\n", $time, $out->{message}, $out->{headers}{'Content-Length'}, $out->{headers}{'Last-Modified'};
$out = "";
$cnct->write_request(GET => "/pki/ca.crl");
($out->{code},$out->{message},%{$out->{headers}}) = $cnct->read_response_headers;
printf "%s\t%s - Size %d\tLast modified %s\n", $time, $out->{message}, $out->{headers}{'Content-Length'}, $out->{headers}{'Last-Modified'};
}
sub mkcnct {
my $host = shift;
my $time = gettime();
my $cnct;
eval{
local $SIG{ALRM} = sub { print "$time\tCannot connect to $host\n"};
alarm(2);
$cnct = Net::HTTP->new(Host => $host);
alarm(0);
};
alarm(0);
return($cnct);
}
sub gettime {
my @time = localtime(time);
my $out;
$out = sprintf "%d\/%d\/%d %d:%d", ($time[4] + 1), $time[3], ($time[5] % 100), $time[2], $time[1];
return($out);
}
'if ($ @) {print $ @;를 사용하여'eval'의 반환 값을 항상 확인해야합니다. }'직후에. – CanSpice
[LWPx :: ParanoidAgent] (http://p3rl.org/LWPx::ParanoidAgent)를 사용하고'timeout' 속성을 설정하는 것이 더 강력한 접근 방법 일 것입니다. – daxim