Perl script issues - Need help

G

George Monappallil

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
 
X

xhoster

George Monappallil said:
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
 
D

Dave Weaver

George Monappallil said:
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;
 
G

George Monappallil

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
 
T

Tad McClellan

George Monappallil said:
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 ]
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,767
Messages
2,569,572
Members
45,045
Latest member
DRCM

Latest Threads

Top