http://www.perlmonks.org?node_id=585166


in reply to Re^2: trouble parsing log file...
in thread trouble parsing log file...

A Guide to Installing Modules

One other small point that others seem to have missed....
You probably don't want to break out of your while loop if you find a "PROBLEM". That is because there may be a "DOWN" further on in the logfile which you would then miss.

ie. you should not have a last; in the if ($line =~ /$warn/) condition (or however you end up writing it).

Cheers,
Darren :)

Replies are listed 'Best First'.
Re^4: trouble parsing log file...
by perl_geoff (Acolyte) on Nov 22, 2006 at 14:21 UTC
    The way this should work is on a priority basis; 1st priority is errors, 2nd is warns and last is the greenlight. If the program parses my log and finds a red, it stops and prints a red light; that's it. Or, if there are no reds in the file, but the script snags a warning, it will show a yellow light and end. If the script sees neither warnings nor errors in the log file, the script will print one green light and stop. What would you suggest would be the best way to do this? Here is my latest:
    use strict; use warnings; my $logfile="log.txt"; my $error="DOWN"; my $warn="PROBLEM"; my $redbutton="\<img src\=\'default_files/perlredblink\.gif'>"; my $greenbutton="\<img src\=\'default_files/perlgreenblink\.gif'>"; my $yellowbutton="\<img src\=\'default_files/perlyellowblink\.gif'>"; open LOG, $logfile or die "Cannot open $logfile for read :$!"; my $button = $greenbutton; while ($_ = <LOG>) { if ($_ =~ /$error/i) { $button = $redbutton; print "<!--Content-type: text/html-->\n\n"; print "$button"; last; } elsif ($_ =~ /$warn/i) { $button = $yellowbutton; print "<!--Content-type: text/html-->\n\n"; print "$button"; last; } else { print "<!--Content-type: text/html-->\n\n"; print "$button"; last; } } close LOG;
      Okay, most important comment first:

      What you have there will only ever read one line of your logfile, and then exit. That is because you finish each of your conditional blocks with a last;.

      I suspect that you are somewhat confused about what the while ($_ = <LOG>) { line does. From the code you have written, it appears that you are under the impression that it will read the entire file in a single iteration of the while loop. This is not the case. It reads one line at a time. Therefore - because you've included a last in each conditional block - it's going to exit the while loop after reading one line only!

      To make the above work like you want it to, you simply need to remove the last from the 2nd and 3rd conditionals. ie. Only use last to exit the loop if you find an "$error". Update: actually, even then it wont work - because you'll be printing your HTML header and a button for EVERY line in your logfile. So you really need to make some changes as I've outlined below.

      Now, a couple of other comments:

      • The $_ in while ($_ = <LOG>) { is redundant. That could be written as while (<LOG>) { and the effect would be exactly the same. Personally, I prefer to use an explicitly named variable, and so I would write it as: while (my $line = <LOG>) {, and then operate on $line within the loop.
      • Because you only need to print once, you can remove your print statements from the loop and save yourself a few lines of code.
      • print "<!--Content-type: text/html-->\n\n" sounds a bit of warning bell. If this is part of a larger script that is outputting HTML, then you really should be using the CGI module. Or at the very least, move that line (you only need to do it once) to near the top of your code.
      • In your assignments to the "buttons", you are again repeating code un-necessarily. Also, you can use the quoting operators (q,qq), which will save you having to do all that escaping.
      • It is generally a much better practise to use the three argument form of open

      So, re-writing your code and implementing those few changes:

      use strict; use warnings; use CGI qw(standard); my $logfile="log.txt"; my $error="DOWN"; my $warn="PROBLEM"; my $imagedir = 'default_files'; my ($redbutton, $greenbutton, $yellowbutton) = q(perlredblink.gif perlyellowblink.gif perlgreenblink.gif); my $button = $greenbutton; print header(); open LOG, '<', $logfile or die "Cannot open $logfile for read :$!"; while (my $line = <LOG>) { if ($line =~ /$error/i) { $button = $redbutton; last; } elsif ($line =~ /$warn/i) { $button = $yellowbutton; } } close LOG; print qq(<img src="$imagedir/$button">);

      Disclaimer: The above is untested, and has been written at 3am after I've just gotten off a flight from Hong Kong to Manila - so it is almost certainly buggy. But I hope it helps anyway ;)

      Cheers,
      Darren :)

Re^4: trouble parsing log file...
by perl_geoff (Acolyte) on Nov 25, 2006 at 19:23 UTC
    Hey, I tried using this module, but I'm still running into the same problem as my other script...can you show me where I'm going wrong?
    use strict; use warnings; my $logfile="log.txt"; my $error="DOWN"; my $warn="PROBLEM"; my $redbutton="\<img src\=\'default_files/perlredblink\.gif'>"; my $greenbutton="\<img src\=\'default_files/perlgreenblink\.gif'>"; my $yellowbutton="\<img src\=\'default_files/perlyellowblink\.gif'>"; my $button = $greenbutton; # open LOG, $logfile or die "Cannot open $logfile for read :$!"; use File::Tail; tie *LOG, 'File::Tail', (name => $logfile, tail => -1) or die("log open error: $!"); while (my $line = <LOG>) { if ($line eq $error) { $button = $redbutton; print "<!--Content-type: text/html-->\n\n"; print $button; last; } elsif ($line eq $warn) { $button = $yellowbutton; print "<!--Content-type: text/html-->\n\n"; print $button; last; } else { print "<!--Content-type: text/html-->\n\n"; print $button; last; } } close LOG;