Perl script issues - Need help

Discussion in 'Perl Misc' started by George Monappallil, Jan 21, 2005.

  1. Hi guys, Below i have a created a simple perl script that checks an
    application log file for words like "ERROR" or "Warning" . I want the script
    to send me an alert on my cell whenever it finds those words. I have checked
    the net but wasn't successful in finding a way to do it. How would I do
    that.
    I am a complete newbie but I have already started liking Perl

    -------------------------
    #!/bin/perl
    $LOG="/var/tretsov/tretsov.log";
    if (-e $LOG && -s $LOG) {
    open (FILE, $LOG) || die "Cannot open";
    while (<FILE>) {
    if (/ERROR/|/WARNING/i) {
    print $_;
    }
    }
    }
    else {
    print "$LOG file doesn't exist\n";
    }
    close (FILE);
    ------------------------

    As you can see right now it just prints the lines out to stdout. I would
    also like to get your insights on your experiences on running such programs.
    I would like to have this program run as a daemon...is it possible ? I also
    want this script to just send one message per alert even if it checks the
    logs multiple times. Any ideas how I can do it ?

    Thanks in advance.

    George
    George Monappallil, Jan 21, 2005
    #1
    1. Advertising

  2. George Monappallil

    Guest

    "George Monappallil" <> wrote:
    > Hi guys, Below i have a created a simple perl script that checks an
    > application log file for words like "ERROR" or "Warning" . I want the
    > script to send me an alert on my cell whenever it finds those words. I
    > have checked the net but wasn't successful in finding a way to do it. How
    > would I do that.


    For finding the words only in "new" parts of the file, see
    perldoc -q tail

    As for sending the alert to your pager, I can't help you there.

    Xho

    --
    -------------------- http://NewsReader.Com/ --------------------
    Usenet Newsgroup Service $9.95/Month 30GB
    , Jan 21, 2005
    #2
    1. Advertising

  3. George Monappallil

    Dave Weaver Guest

    George Monappallil <> wrote:
    > if (/ERROR/|/WARNING/i) {
    > print $_;
    > }


    I don't think that's doing quite what you expected it to do.

    The '|' operator is a binary 'or'.

    Maybe you're thinking of the '||' (logical 'or') operator, in which
    case you want:

    if ( /ERROR/ || /WARNING/i ) {

    which is IMHO better written as:

    if ( /ERROR/ or /WARNING/i ) {

    (Note that you only have the "i" modifier on the one pattern
    match, so it won't match 'Error' but it will match 'Warning').

    Or maybe you're thinking of the regex alternation operator:

    if ( /ERROR|WARNING/i ) {

    (which *will* match 'Error' as well as 'Warning').

    Also, note that $_ is the default argument for print, so you
    could have written:

    if ( /ERROR|WARNING/i ) {
    print;
    }

    or, more succinctly:

    print if /ERROR|WARNING/i;
    Dave Weaver, Jan 21, 2005
    #3
  4. Great tips. Thanks much. Would you know how I can have this script monitor a
    log file continiously..like a daemon. Is it possible ?

    -George


    "Dave Weaver" <> wrote in message
    news:41f0cca2$0$13376$...
    > George Monappallil <> wrote:
    > > if (/ERROR/|/WARNING/i) {
    > > print $_;
    > > }

    >
    > I don't think that's doing quite what you expected it to do.
    >
    > The '|' operator is a binary 'or'.
    >
    > Maybe you're thinking of the '||' (logical 'or') operator, in which
    > case you want:
    >
    > if ( /ERROR/ || /WARNING/i ) {
    >
    > which is IMHO better written as:
    >
    > if ( /ERROR/ or /WARNING/i ) {
    >
    > (Note that you only have the "i" modifier on the one pattern
    > match, so it won't match 'Error' but it will match 'Warning').
    >
    > Or maybe you're thinking of the regex alternation operator:
    >
    > if ( /ERROR|WARNING/i ) {
    >
    > (which *will* match 'Error' as well as 'Warning').
    >
    > Also, note that $_ is the default argument for print, so you
    > could have written:
    >
    > if ( /ERROR|WARNING/i ) {
    > print;
    > }
    >
    > or, more succinctly:
    >
    > print if /ERROR|WARNING/i;
    >
    George Monappallil, Jan 21, 2005
    #4
  5. George Monappallil <> wrote:

    > Great tips.



    Here is another one:

    check the Perl FAQ *before* posting to the Perl newsgroup.


    > Would you know how I can have this script monitor a
    > log file continiously..like a daemon.


    perldoc -q daemon

    How do I fork a daemon process?



    [ snip upside-down quoted text, please don't do that ]

    --
    Tad McClellan SGML consulting
    Perl programming
    Fort Worth, Texas
    Tad McClellan, Jan 21, 2005
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Wet Basement
    Replies:
    1
    Views:
    2,504
    BobMonk
    Jul 15, 2003
  2. dpackwood
    Replies:
    3
    Views:
    1,767
  3. pj
    Replies:
    3
    Views:
    274
  4. Petterson Mikael

    Beginner need help on perl issues

    Petterson Mikael, May 27, 2004, in forum: Perl Misc
    Replies:
    3
    Views:
    107
    Eric Schwartz
    May 27, 2004
  5. Petterson Mikael

    Execute another perl script from my perl script

    Petterson Mikael, Jan 5, 2005, in forum: Perl Misc
    Replies:
    3
    Views:
    127
    Paul Lalli
    Jan 5, 2005
Loading...

Share This Page