So far, I have some working code that goes into a list of servers and does some regex to grab data from a log file. What I want to do is to pring out a status of "NOTHING TO REPORT FOR THIS SERVER" if there is NO data to capture from the regex on a particular server.
Right now, it goes through each server and if data matches the regex, it will print out. I added an else statement to print out the above statement to handle this, but now its printing it out for every instance it doesn't match.
Here is a copy of the output as it works now BEFORE I added the change:
========================================================================
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
========================================================================
Here is a copy of the output now, AFTER I added the "NOTHING TO REPORT FOR THIS DATE": (within the while loop). Basically, its printing out this statement everytime it doesn't match. I really just want it to give me one statement.
========================================================================
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
Here is the code:
# 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);
}