scott_apc has asked for the wisdom of the Perl Monks concerning the following question:

This node falls below the community's threshold of quality. You may see it by logging in.

Replies are listed 'Best First'.
Re: creating a perl script
by Anonymous Monk on Feb 12, 2009 at 11:06 UTC
    First please put your code in CODE tags so it is laid out properly.
    if ( $_ == "has gone down" ) { run start server command }
    Should be
    if ( $_ eq "has gone down" ) { run start server command } as it's a string comparison, but actually should be if ( /has gone down/ ) { run start server command } as it's a match you're looking for.
    You'll have to extract the timestamp from the matching line and save that to a file somewhere for comparison when you next run.
Re: creating a perl script
by rovf (Priest) on Feb 12, 2009 at 11:06 UTC

    I don't fully understand your question, but I first notice that your program as it is written, should not find any matching line in your log. The reason is that you are using

    if($_ == "has gone down" ) { ... }
    to check for the interesting lines. $_ certainly will never be exactly the string has gone down, because at least it will have a \n at the end, plus, according your description, it has some more information in this line (such as the timestamp).

    If you are only interested in lines containing that magin string, the easiest way would be to use index. Note that index returns -1 on failure.

    If you need, however, some of the other fields in the logline as well, in order to run your start server command, you are better of using regular expressions. For example if you need the server name and port, you could write something like

    if(/Server\s(.+):(\d+) has gone down/) { my $server=$1; my $port=$2; ... }

    -- 
    Ronald Fischer <ynnor@mm.st>

      ...and, of course, one should use eq vs == for a string comparison. The string on the right side of the test ("has gone down") will numify as 0, which will match any string that numifies as 0, which is probably most of them.


      Information about American English usage here and here. Floating point issues? Please read this before posting. — emc

      Adding to the code above
      my %downserver = (); if (...) { my $server = $1; if (!exists $downserver{$server}) { $downserver{$server} = "Restarting $server"; run start server command } }
      or you could maintain the list and process at the end.
      my %downserver = (); if (...) { my $server = $1; my $downtime = $n; # some other extracted bit from regex $downserver{$server} = $downtime; } foreach my $server (keys %downserver) { if ($downserver{$server) down after last run) { run start server write to log server upped } }
Re: creating a perl script
by lakshmananindia (Chaplain) on Feb 12, 2009 at 14:03 UTC
    But I may have issue as the words occur multiple times.

    Use Regular expression to match multiple lines with m modifier.

    To run the script for every fifteen minutes, use sleep function.

Re: creating a perl script
by irah (Pilgrim) on Feb 12, 2009 at 12:45 UTC
    To execute the script in every 15 minutes, make a program as daemon and use the function sleep for 15 seconds at end of while loop.