0

So I am having an issue with the array @cpuAll losing its value after $pm->finish.. This is just SSHing to a bunch of servers and bringing back some stats which works fine. But the array won't print after the last loop is done. I don't want to write everything to files because I get a 90% performance increase from just loading it into the array.

my @cpuAll = ();
my @memAll = ();
$pm->run_on_finish(sub{
        my ($pid,$exit_code,$ident,$exit_signal,$core_dump,$data)=@_;
        push(@data,$data);
});
for(@servers)
{
    next if $_ =~ "10.1.4.52";
    next if $_ =~ "10.1.4.106";
    my $pid = $pm->start and next;
    chomp;
    my @output_cpu  = `/usr/bin/ssh $_ \"/root/scripts/punkbuster.cpu|sed 's/ (//g'|sed 's/)//g'|sed s'/ //g'\"`;
    for(@output_cpu)
    {
        chomp;
        my ($server,$username,$cpu,$process)=(split /:/, $_)[0,1,2,3];
#       push(@cpuAll,"$server\,$username\,$cpu\,$process\,$date\,$time\n");
    }
$pm->finish(0, [$server,$username,$cpu,$process]);
}

print $_ for @data;
print "OK\n";
$pm->wait_all_children;
4

2 回答 2

2

我过去也遇到过类似的问题,相信您会在数据结构检索的文档中找到解决方案。您需要传递数据以完成$pm->finish(0, \@cpuAll),然后使用回调$pm->run_on_finish循环遍历您的数组并打印您需要的任何内容。我提供的链接显示了一个代码示例,应该非常清楚如何检索数据。如果没有,请告诉我,我会在我的答案中添加更多内容。

于 2013-10-03T20:35:19.667 回答
1

使用Net::OpenSSH::Parallel

my $pssh = Net::OpenSSH::Parallel->new;
for my $server (@servers) {
    $pssh->add_host($server);
}
$pssh->push(*, cmd => { stdout_file => "%LABEL%.out" },
            "/root/scripts/punkbuster.cpu|sed 's/ (//g'|sed 's/)//g'|sed s'/ //g'");
$pssh->run;

my @cpuAll;
for my $server (@servers) {
    if (open my $fh, '<', "$server.out") {
        my ($server,$username,$cpu,$process) = split /:/;
        push @cpuAll, join ',', (split /:/)[0..3], $date, $time;
    }
    else {
        warn "unable to retrieve data for $server\n";
    }
}

print "$_\n" for @cpuAll;

我还将用sedperl 中完成的一些本地后处理来替换替换。

于 2013-10-04T07:59:32.433 回答