Operating System - HP-UX
1758020 Members
2239 Online
108866 Solutions
New Discussion юеВ

Perl script to monitor a running log for a particular message

 
SOLVED
Go to solution
Sammy_2
Super Advisor

Perl script to monitor a running log for a particular message

In perl or Shell script, how would I do this.

Everytime a page is sent out to a customer, the following last 2 lines (see below) are printed in the page log after the text message,if the page was successful.How do I monitor the log so that "Page Sent" line should always be followed by "Request Successful" line. If the line that contains "Page Sent" is followed by anything else then send mail to admin@abc.com with subject "PAGING ERROR". I guess I would run the script as:
tail -f page.log | monitor.pl &

page.log
------------cut-----------------
"Customer having problems with workstation"
Page Sent
Request Successful
------------cut-----------------
good judgement comes from experience and experience comes from bad judgement.
2 REPLIES 2
harry d brown jr
Honored Contributor
Solution

Re: Perl script to monitor a running log for a particular message


How do you prevent multiple simultaneous pages from wacking the log?

OF COURSE It makes more sense to change the program that is paging to "email" paging errors.

try this as a start

#!/opt/perl/bin/perl
#
$pagesent = 0;
while (<>) {
chop;
if ( ($pagesent) && (! m/^Request Successful/) ) {
printf("Error Error, panic\n");
}
$pagesent=0;
if (m/^Page Sent/) {
$pagesent=1;
}
}



live free or die
harry
Live Free or Die
Sammy_2
Super Advisor

Re: Perl script to monitor a running log for a particular message

Thanks a bunch Harry. That works great. A little twist now. How would you change the script so that only email me if the log does NOT have the following 2 lines in succession.

----cut----
Page Sent
Request Successful
-----cut----

What happens is, many times we see the line
"Page Sent" in the log but a blank line followed by it. (That tells us the page did not go).
If "Page Sent" line is followed by a line anything other than "^Request ", I need to get emailed. Otherwise, do not do anything.
Right now, I have modified your script to email me only if
----cut----
Page Sent
Request Successful
-----cut----
appear right after another. Right ? How would you reverse the logic.
---------------------cut-------------
#!/usr/bin/perl
#
$pagesent = 0;
while (<>) {
chop;
if ( ($pagesent=0) && (! m/^Request/) ) {
printf("Error Error, panic\n");
}
$pagesent=0;
if (m/^Page Sent/) {
$pagesent=1;
open MAILER, "|mailx -s 'PAGE ERROR' sam_p\@dom.com";
print MAILER "ERROR IN PAGE"
close MAILER;
}
}
-------------cut----------------------
good judgement comes from experience and experience comes from bad judgement.