Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

Re^4: trouble parsing log file...

by perl_geoff (Acolyte)
on Nov 22, 2006 at 14:21 UTC ( #585524=note: print w/ replies, xml ) Need Help??


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

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;


Comment on Re^4: trouble parsing log file...
Download Code
Re^5: trouble parsing log file...
by McDarren (Abbot) on Nov 22, 2006 at 19:19 UTC
    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 :)

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://585524]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others cooling their heels in the Monastery: (7)
As of 2014-07-26 06:04 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My favorite superfluous repetitious redundant duplicative phrase is:









    Results (175 votes), past polls