Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

Re: trouble parsing log file...

by inman (Curate)
on Nov 20, 2006 at 19:48 UTC ( #585118=note: print w/replies, xml ) Need Help??


in reply to trouble parsing log file...

The line @logarray=<LOG>;   # dumps all of $logfile into @logarray is reading all of the lines into an array. The test @logarray eq $error is in scalar context. It is comparing the number of lines in the file to the text. This will bever succeed.

Ignore the list building and just work through the file line by line and use a regular expression to test.

Take a look at the following as an example.

use strict; # Set the button to green initially my $button = "perlgreenblink"; # test the file line by line. # The line gets read into $_ # I am testing on the DATA segment to illustrate the point while (<DATA>){ # test with a regex and end the # while loop if there is a problem if (/DOWN/){ $button = "perlredblink2"; last; } if (/PROBLEM/){ $button = "perlyellowblink"; last; } } print "HTML for <img src=\"$button.gif\" />\n"; __DATA__ nothing here going smoothly Its all going DOWN no PROBLEM at all

Replies are listed 'Best First'.
Re^2: trouble parsing log file...
by perl_geoff (Acolyte) on Nov 20, 2006 at 20:25 UTC
    Ok, I tried this, but could only get it to display the green button...also, I'm not sure I understand the logic of setting the button to green first. When I run your script it seems to ignore everything but the last line. Also, I don't believe any of my log files are above about 5 mb or so.
      The logic is that you are testing the log file for a specific condition. You create a starting condition that assumes that everything has gone well and would result in a green button. The idea is that if you get to the end of the file without hitting one of your two tests then everything was OK.

      You read the file one line at a time looking for either DOWN or PROBLEM. When one of these tests work, you set the button response accordingly and use last to leave the while loop and do something with the outcome.

      You may only be reading 5Mb of files but this translates into a much larger use of memory. It also involves the computer reading the file line by line anyway as it puts it into memory. If your match is on line 20 of a 2000 line file, your script only needs to read 20 lines and you are done.

      If you continue to have trouble, post your code in the replies.

        Ok, now I understand. Here's what I tried, sorry I wasn't more specific. Unfortunately it only displays green:
        $logfile="log.txt"; $error=(/DOWN/); $warn=(/PROBLEM/); $redbutton="\<img src\=\'default_files/perlredblink2\.gif'>"; $greenbutton="\<img src\=\'default_files/perlgreenblink\.gif'>"; $yellowbutton="\<img src\=\'default_files/perlyellowblink\.gif'>"; open LOG, $logfile or die "Cannot open $logfile for read :$!"; # @logarray=<LOG>; # dumps all of $logfile into @logarray use strict; # Set the button to green initially my $button = "perlgreenblink"; # test the file line by line. # The line gets read into $_ # I am testing on the DATA segment to illustrate the point while (<DATA>){ # test with a regex and end the # while loop if there is a problem if (/DOWN/){ $button = "perlredblink2"; last; } if (/PROBLEM/){ $button = "perlyellowblink"; last; } } print "HTML for <img src=\"$button.gif\">\n";

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://585118]
help
Chatterbox?
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others browsing the Monastery: (12)
As of 2018-07-23 17:41 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    It has been suggested to rename Perl 6 in order to boost its marketing potential. Which name would you prefer?















    Results (472 votes). Check out past polls.

    Notices?