views:

121

answers:

3

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);
}
+8  A: 
  1. Set a boolean to false.
  2. Inside the while loop, if the regex matches, set the boolean to true.
  3. Outside the while loop, if the boolean is still false, print NOTHING TO REPORT.
mcandre
Thanks for the helping me with this logic.
jda6one9
I use this pattern all the time :)
mcandre
A: 

I don't think, I'm getting this right. I tried as suggested (@mcandre) and placed outside of the while loop and still getting the same behavior. Where should I place this check?

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);
}
jda6one9
@jaedre619: In `if ($boolean=0)`, you're assigning 0 to `$boolean`. You want `if ($boolean == 0)`. You also want to reset `$boolean` back to 0 inside your `foreach` loop, probably. Also you should rename `$boolean` to something meaningful, like `$has_data`.
indiv
Also, you're using strict and warnings, so you should have seen something like this when you compiled and ran it: `Found = in conditional, should be == at...`. Don't ignore those warnings, they tell you something is wrong.
indiv
@indiv: Thank you!! It know works, as I would like it too...Also, I did not ignore warning. I would have tried to resolve that. Thanks.
jda6one9
+1  A: 

This works now:

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);
}
jda6one9
you don't need to reset the `$has_data=0;` at the end, since you don't use it anymore, if anything you can `undef $has_data`. Also `if ($has_data==0)` can just become `if ($has_data)`.
vol7ron