- 论坛徽章:
- 0
|
如题.
代码如下
- #!/usr/bin/perl
- #ver 1.1.0
- #auth FB
- #lastModify 20070606 18:20
- #date,name,ip,size,min,avg,max,loss
- #%date,%name,%size,%min,%avg,%max,%loss
- use strict;
- no strict "refs" ;
- use Data::Dumper;
- my %name;
- #my $dir="/root/ping/";`mkdir -p $dir`;
- my $config="/root/scripts/data.ini";
- #read config start
- open DATA,"$config";
- foreach my $data(<DATA>){
- chomp($data);
- if ($data !~ /^#/){
- my ($tmpname,$tmpip)=split/\=/,$data;
- $name{$tmpip}=$tmpname;
- }
- }
- close DATA;
- #read config end
- sub ping(@_) {
- my $ip=shift;
- my $times=shift;
- my $size=shift;
- my $dt=time;
- my $loss;
- my $tmpresult=`/bin/ping -c $times -s $size $ip`;
- $tmpresult =~ m/(.*min\/avg\/max\/.*\=)(.*) (ms|usec)/;
- ($$ip{min},$$ip{avg},$$ip{max})=split(/\//,$2);
- #printf ("%s%s%s\n",$min,$avg,$max,$tmp);
- $tmpresult =~ m/(.*\,)(.*) (packet loss)/;
- $loss=$2;
- $loss=~ s/\%//g;
- #print $loss."\n";
- ${$ip}{dt}=$dt;
- ${$ip}{size}=$size;
- ${$ip}{times}=$times;
- ${$ip}{loss}=$loss;
- return \%$ip;
- }
- #foreach (keys %name){
- #ping($_,"2","16");
- #}
- my %pid_to_host;
- foreach my $tmpip (keys %name){
- if (my $pid = fork){
- $pid_to_host{$pid} = $tmpip;
- warn "$pid is processing $tmpip\n";
- } else {
- ping($tmpip,"2","16");
- exit;
- }
- }
- while (keys %pid_to_host){
- my $pid = wait;
- last if $pid <0;
- my $host = delete $pid_to_host{$pid}
- or warn("why did I see $pid($?)\n"),next;
- warn "reaping $pid for $host\n";
- }
- foreach (keys %name){
- printf("%s,%s,%s,%s,%s,%s\n",$$_{dt},$$_{min},$$_{max},$$_{avg},$$_{loss},$$_{size});
- }
复制代码
如果不用多进程.单一进行,可以出结果.隐约知道问题在哪.但不是很明确.一开始以为多维hash传递出问题了.
应该是fork后出的问题,初步怀疑标准输入输出的问题.调试看到错误为得不到TREM. |
|