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


in reply to trouble parsing log file...

You can't wrap a while loop around that to just have it continue to read the logfile either. Have a look at File::Tail for continuous logfile reading. Something like the following should work for you:
use File::Tail; tie *LOG, 'File::Tail', (name => $logfile, tail => -1) or die("log open error: $!"); while (my $line = <LOG>) { if ($line eq $error) { print $redbutton; } # etc } close (LOG) or die("log close error: $!");

Replies are listed 'Best First'.
Re^2: trouble parsing log file...
by perl_geoff (Acolyte) on Nov 20, 2006 at 20:36 UTC
    Hi, I am new to using modules, can you give me more information on how to install/get File::Tail? Thanks!
      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 :)

        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;
        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;