一個非常幸福的新一年所有生成的連續平記錄:dPerl代碼重新設計了多線程每臺主機
我寫一段代碼中正常工作的Perl,但我期待給予它加強的是代碼利用多線程和進程分叉等功能。代碼被設計用於以下任務:
- 連續記錄列表中提供的多個服務器的ping。
- 分別爲每個ping主機保留和寫入日誌。
- 在實時命令窗口中顯示實時狀態。
完整代碼:
#!/user/perl/bin
use strict;
use Net::Ping;
use Time::HiRes;
use IO::Handle;
use threads;
use threads::shared;
# AUTHOR : Avi Mehenwal
# PURPOSE : Continuous ping check with PERL iThreads (interpreted threads)
# DATE : 10th-Jan-2013
sub Ping {
#Filer handle generation and open log file for writing in append mode.
#creating ping object to check ping response
#Writing Ping results in logs and command window for Real time analysis
#Close file after logs are witten
my ($host , $protocol) = @_;
chop $host;
open my $log, '>>', "Ping_$host.txt" or die "Cannot create log file: $!";
$log->autoflush; #autoflusing to stop output stream buffering.
my $p = Net::Ping->new($protocol) or die "Cannot create new Net::Ping object : $!";
$p->hires;
my ($ret, $duration, $ip) = $p->ping($host);
if ($ret=='1')
{ my $event = sprintf "%s\t%s is alive $protocol (packet RTT: %.3fms)\n",
scalar localtime, $host, $duration;
print STDOUT $event;
print $log $event;
sleep(1); #sleep only when ping is continuous
}
else
{ my $event = sprintf "%s\t%s is UNAVAILABLE (Timedout/lost $protocol request)\n",
scalar localtime, $host;
print STDOUT $event;
print $log $event;
}
close $log;
}
my @hostip = qw(10.98.10.253, 10.112.114.10, 10.112.114.11);
STDOUT->autoflush;
print "****************************************************************************\n\ncPING-CHECK -v2.0 By-Avi Mehenwal\n\n";
print "Kindly enter the protocol to be used by cPing-Probe : tcp/udp/icmp ...\n";
my $protocol = <STDIN>;
chomp $protocol;
if($protocol eq "icmp") {
print "\nWARNING:For cPing-Probe to use icmp protocol kindly run the program in administrative command prompt\ncPing-Probe v2.0will now exit ...";
sleep(10);
exit;
}
while(1) {
foreach my $thost (@hostip) {
Ping($thost,$protocol);
}
}
#END
現在我想用每個IP主機有一個線程這將檢查ping和不影響其他主機的IP線程寫日誌,以提高我的代碼。對於這個我tryed幾個代碼,但我發現了一些成功的下面這段代碼:
my @hostip = qw(10.98.10.253, 10.112.114.10, 10.112.114.11);
STDOUT->autoflush;
print "****************************************************************************\n\ncPING-CHECK -v2.0 By-Avi Mehenwal\n\n";
print "Kindly enter the protocol to be used by cPing-Probe : tcp/udp/icmp ...\n";
my $protocol = <STDIN>;
chomp $protocol;
if($protocol eq "icmp") {
print "\nWARNING:For cPing-Probe to use icmp protocol kindly run the program in administrative command prompt\ncPing-Probe v2.0will now exit ...";
sleep(10);
exit;
}
while(1)
{ my $thr;
foreach my $thost (@hostip)
{ $thr = threads->new(Ping,$thost,$protocol) or die "Cannot Create thread : $!"; #seperate thread for each IP
Ping($thost,$protocol);
}
if($thr->is_joinable())
{ $thr->join();
print STDOUT "Thread joined\n"; }
else
{ $thr->detach();
print STDOUT "Thread detached\n"; }
}
#END
但這段代碼是從我需要高手幫忙一些不需要的症狀的人。
- 爲什麼我會生成一個名爲Ping_的日誌文件並將日誌寫入它!理想情況下,每臺主機需要記錄文件。我無法理解這裏的訣竅。
- 我的代碼是否真的在爲每個主機並行運行線程執行多任務處理?我該如何檢查或確定?
- 有沒有更好的工作方式?
- 有什麼額外的功能,你可以認爲我可以添加到我的代碼?
謝謝大家提前:D 乾杯!
-Avi Mehenwal
是否使用平檢測你的平安?您是否檢查過Ping-Probe文檔以查看它所做的記錄? –
不,我沒有使用Ping-Probe進行ping操作。我使用Perl模塊Net :: Ping發送icmp迴應請求。 是的,先生我已閱讀其文檔:D –