到目前为止,我有一些工作代码进入服务器列表并执行一些正则表达式从日志文件中获取数据。我想要做的是如果没有数据从特定服务器上的正则表达式捕获,则表明“没有报告此服务器”的状态。
现在,它遍历每个服务器,如果数据与正则表达式匹配,它将打印出来。 我添加了一个else语句来打印出上面的语句来处理这个问题,但现在它为每个不匹配的实例打印出来。
以下是输出的副本,因为它现在有效之前我添加了更改:
========================================================================
REPORTING SUMMARY for BACKUP SERVER : hostname1
========================================================================
REPORTING SUMMARY for BACKUP SERVER : hostname2
Mon Jul 05 00:30:02 2010: hostname2:backup:INFO: backup-date=20100705003002
Mon Jul 05 00:30:02 2010: hostname2:backup:INFO: host=hostname2
Mon Jul 05 00:55:25 2010: hostname2:backup:INFO: backup-size=49.75 GB
Mon Jul 05 00:55:25 2010: hostname2:backup:INFO: backup-time=00:25:23
Mon Jul 05 00:55:25 2010: hostname2:backup:INFO: backup-status=Backup succeeded
========================================================================
现在是输出的副本, AFTER 我添加了“此日期没有报告”:(在while循环中)。基本上,它每次打印出这个声明都不匹配。我真的只想给它一个声明。
========================================================================
REPORTING SUMMARY for BACKUP SERVER : hostname1
NOTHING TO REPORT FOR THIS DATE...
NOTHING TO REPORT FOR THIS DATE...
NOTHING TO REPORT FOR THIS DATE...
...
...
========================================================================
NOTHING TO REPORT FOR THIS DATE...
Mon Jul 05 00:30:02 2010: hostname2:backup:INFO: backup-date=20100705003002
Mon Jul 05 00:30:02 2010: hostname2:backup:INFO: host=hostname2
NOTHING TO REPORT FOR THIS DATE...
NOTHING TO REPORT FOR THIS DATE...
Mon Jul 05 00:55:25 2010: hostname2:backup:INFO: backup-size=49.75 GB
Mon Jul 05 00:55:25 2010: hostname2:backup:INFO: backup-time=00:25:23
Mon Jul 05 00:55:25 2010: hostname2:backup:INFO: backup-status=Backup succeeded
以下是代码:
# Usage: ./test.pl Ju1 05 2010 <logfilepath> hostname1 hostname2 hostname3
use strict;
use warnings;
my($mon,$day,$year,$file) = @ARGV;
splice(@ARGV, 0, 4, ());
foreach my $server ( @ARGV ) {
print "========================================================================\n";
print "REPORTING SUMMARY for BACKUP SERVER : $server\n";
open(my $fh,"ssh $server cat $file |") or die "can't open log $server:$file: $!\n";
while (my $line = <$fh>) {
if ($line =~ m/.* $mon $day \d{2}:\d{2}:\d{2} $year:.*(host=|ERROR:|backup-date=|backup-size=|backup-time=|backup-status)/) {
print $line;
# adding else statement here
} else {
print "NOTHING TO REPORT FOR THIS DATE... \n";
}
}
close($fh);
}
答案 0 :(得分:8)
答案 1 :(得分:1)
现在可以使用:
use strict;
use warnings;
my($mon,$day,$year,$file) = @ARGV;
my $has_data = 0;
splice(@ARGV, 0, 4, ());
foreach my $server ( @ARGV ) {
print "========================================================================\n";
print "REPORTING SUMMARY for BACKUP SERVER : $server\n";
open(my $fh,"ssh $server cat $file |") or die "can't open log $server:$file: $!\n";
while (my $line = <$fh>) {
if ($line =~ m/.* $mon $day \d{2}:\d{2}:\d{2} $year:.*(host=|ERROR:|backup-date=|backup-size=|backup-time=|backup-status)/) {
print $line;
$has_data=1; #set to true
#print "Boolean is set to $has_data \n";
}
} #end while loop
if ($has_data==0) {
print "Boolean is set to $has_data \n";
print "NOTHING TO REPORT FOR THIS DATE... \n";
}
$has_data=0;
print "Boolean is reset to $has_data \n";
close($fh);
}
答案 2 :(得分:0)
我不认为,我说得对。我尝试了建议(@mcandre)并放置在while循环之外,仍然得到相同的行为。我应该把这张支票放在哪里?
use strict;
use warnings;
my($mon,$day,$year,$file) = @ARGV;
my $boolean = 0;
splice(@ARGV, 0, 4, ());
foreach my $server ( @ARGV ) {
print "========================================================================\n";
print "REPORTING SUMMARY for BACKUP SERVER : $server\n";
open(my $fh,"ssh $server cat $file |") or die "can't open log $server:$file: $!\n";
while (my $line = <$fh>) {
if ($line =~ m/.* $mon $day \d{2}:\d{2}:\d{2} $year:.*(host=|ERROR:|backup-date=|backup-size=|backup-time=|backup-status)/) {
print $line;
$boolean=1; #set to true
#print "Boolean is set to $boolean \n";
}
} #end while loop
if ($boolean=0) {
print "Boolean is set to $boolean \n";
print "NOTHING TO REPORT FOR THIS DATE... \n";
}
close($fh);
}