Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

Re: trouble parsing log file...

by liverpole (Monsignor)
on Nov 20, 2006 at 19:52 UTC ( #585121=note: print w/ replies, xml ) Need Help??


in reply to trouble parsing log file...

Hi perl_geoff,

You are trying to compare a list against a scalar in this line:

if (@logarray eq $error) {

What I suspect you want to do instead is to iterate through the list, and break out of the loop when you find the condition you're looking for (note that you don't need to quote variable names to print them):

chomp @logfile; my $got_button = 0; foreach my $line (@logarray) { if ($line eq $error) { $got_button = 1; print $redbutton; last; } elsif ($line eq $warn) { $got_button = 1; print $yellowbutton; last; } } if (not $got_button) { print $greenbutton; }

The above will look through each line read in from the file, and print $redbutton or $yellowbutton if the appropriate string is matched.

I also did a chomp @logfile to remove the newline from each logfile line, so that the match doesn't have to explicitly declare it.

Also, the variable got_button was used so that, when you've gone through all the lines from the file, if the match wasn't found, you know to display the $greenbutton.

Hope that helps!


s''(q.S:$/9=(T1';s;(..)(..);$..=substr+crypt($1,$2),2,3;eg;print$..$/


Comment on Re: trouble parsing log file...
Select or Download Code
Re^2: trouble parsing log file...
by perl_geoff (Acolyte) on Nov 20, 2006 at 20:29 UTC
    This looks good logically, but I can only get it to display green again...my logfile specifically says "server is DOWN." Oh well, I will keep hackin around... :)
Re^2: trouble parsing log file...
by liverpole (Monsignor) on Nov 20, 2006 at 20:46 UTC
    Well, if you're looking for a pattern instead of an exact string, you can look into regular expressions.

    For example, to match the string "server is DOWN" anywhere in the line:

    if ($line =~ /server is DOWN/) { $got_button = 1; print $redbutton; last; }

    ... and you can ignore case (eg. match "Server IS down" and "SERVER is Down" both) by adding i at the end:

    if ($line =~ /server is DOWN/i) { $got_button = 1; print $redbutton; last; }

    But definitely look into regular expressions, which will open up a whole world for you in terms of pattern-matching power.


    s''(q.S:$/9=(T1';s;(..)(..);$..=substr+crypt($1,$2),2,3;eg;print$..$/
      Thanks, I am about halfway through ch.8 'matching with regular expressions' in my learning perl book. Ok, I tried the following:
      $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 chomp @logfile; my $got_button = 0; foreach my $line (@logarray) { if ($line eq $error) { $got_button = 1; print $redbutton; last; } elsif ($line eq $warn) { $got_button = 1; print $yellowbutton; last; } } if (not $got_button) { print $greenbutton; }
      But it still only displays the green button!
        Okay, let's see what we can do here.

        First off, your assignments to $error and $warn probably aren't doing what you think they're doing. If you "use warnings" and "use strict" at the top of your code, you'll see an error that is telling:

        Use of uninitialized value in pattern match (m//)

        Just set $error = 'down' and $warn = 'problem'. Worry about the regular expression stuff later in the code.

        As someone already mentioned, why not go ahead and set the $got_button variable to $greenbutton before you enter the loop? It's nice to have a default state already setup. Then there's no need to do any gymnastics at the end to set it.

        Inside the loop, you have the right idea. Try using a regular expression there. So the "if" clause would look something like:

        if( $line =~ m/$error/i ){

        I used the "i" modifier to make it case insensitive, just in case someone changes log messages on you some day.

        As someone already pointed out, reading the whole log into memory may not be a very good idea since you're bailing out as soon as you find a red/yellow state, so consider just processing the file a line at a time using <LOG>.

        You need to look at my example more closely.

        With regular expressions, you can't just set a string to $error=(/DOWN/), and then check whether your line matches it with if ($line eq $error) {.

        You need to use the regular expression match operator =~ to see whether your string matches the pattern:

        if ($line =~ /DOWN/) { # ... } elsif ($line =~ /PROBLEM/) { # ... }

        Now, it's true that you can abstract some of this.  For example, you could assign string variables, and then use pattern matching on those variables:

        my $error = "DOWN"; my $warn = "PROBLEM"; # ... and later ... if ($line =~ /$error/) { # ... } elsif ($line =~ /$warn/) { # ... }

        Which in the above case should work fine.

        But be aware that, if your variable contains any characters which have special meaning within regular expressions, those meanings will continue to apply.

        For example, if the string you're trying to match is literally "Server ... DOWN", and you attempt to assign and match against that string with:

        my $error = "Server ... DOWN"; if ($line =~ /$error/) { # conditional clause }

        then you'll be using the special meaning of "." within a regular expression (that of matching any character other than end-of-line), and your conditional will thus also match strings like the hypothetical "Server Not DOWN".


        s''(q.S:$/9=(T1';s;(..)(..);$..=substr+crypt($1,$2),2,3;eg;print$..$/
      EDIT: Success! I got it now, thanks for your help everyone!
      use warnings; $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 :$!"; my $button = $greenbutton; while (<LOG>) { if ($_ =~ /$error/i) { $button = $redbutton; print "<!--Content-type: text/html-->\n\n"; print "$redbutton"; } if ($_ =~ /$warn/i) { $button = $yellowbutton; print "<!--Content-type: text/html-->\n\n"; print "$yellowbutton"; } } close LOG;
        Your problem is here...
        $logfile="log.txt";
        and here...
        if ($logfile =~ /$error/i) {
        Can you see?
        You are trying to match the string "DOWN" against the string "log.txt". Of course, that will never match :)

        I think you want:

        while (my $line = <LOG>) { if ($line =~ /$error/i) { etc....

        Cheers,
        Darren :)

        OK, here's 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"; } elsif ($_ =~ /$warn/i) { $button = $yellowbutton; print "<!--Content-type: text/html-->\n\n"; print "$button"; } else { print "<!--Content-type: text/html-->\n\n"; print "$button"; } } close LOG;
        Now, when I test the output upon changing the log values locally, I get exactly the outputs I want; I am using Perl 5.8.8. However, when I test it on my server, which is using Perl 5.6.1 my server-side include to call this file from HTML does some really funky stuff (it actually shows two green, one yellow, two red, one yellow then one red,) but I think that may be a different issue with my server configuration. Should I upgrade the Perl version on my server to get this to execute correctly? Or, are you guys still seeing problems with my code? Thanks for any help.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others browsing the Monastery: (10)
As of 2014-08-20 15:51 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The best computer themed movie is:











    Results (118 votes), past polls