Thanks very much Bill, however I'm getting the following compilation error:
Bareword "SEEK_SET" not allowed while "strict subs" in use at logwatch.pl line 32. I'm not really sure what that means as I can't find any documentation on it... Thanks in advance for any insight, Chris -----Original Message----- From: $Bill Luebkert [mailto:[EMAIL PROTECTED]] Sent: Tuesday, April 09, 2002 5:34 PM To: Lewis, Chris Cc: '[EMAIL PROTECTED]' Subject: Re: log file monitoring script Lewis, Chris wrote: > Below is my code which attempts to monitor a log file, and upon encountering > any keywords (i.e. error, warning, etc.), sends an e-mail notification with > the error message. I'm just not sure how to parse the log file for the > keywords after the script's detected a change... I don't want to send all > the error messages in the file, just the new ones... > > Thanks in advance for any help, > Chris > > P.S. A couple of lines of the log file: > > Thu Jan 24 16:44:09 PST 2002 () (objectOperations:warning:1134): Asking for > Variant of Partition.Any > Thu Jan 24 16:44:09 PST 2002 () (objectOperations:error:1134): Asking for > Variant of Partition.None > > > ============================================================================ > ========== > use Win32::ChangeNotify; > use Mail::SendMail; > use English; > use strict; > my (@stat, $beginsize, $notify, $endsize, $err, %mail, $to, $from, > $directory, $file); > > $log = 'C:/Ariba/Buyer7_1/Coreserver/logs/AribaORMSNode1Log.txt'; > $to = '[EMAIL PROTECTED]'; > $from = '[EMAIL PROTECTED]'; > $directory = 'C:/Ariba/Buyer7_1/Coreserver/logs'; > > while (1) { > @stat = stat("$log"); > $beginsize=$stat[7]; > $notify = Win32::ChangeNotify->new($directory,0,'SIZE') or die > "$^E"; > $notify->wait or warn "Error occurred: $!\n"; > > # There has been a change > @stat = stat($log); > $endsize=$stat[7]; > > #Did the log change? > if ($beginsize != $endsize) { > print "Error...\n"; > > open (ERR, $log); > undef $/; > $err=<ERR>; > close ERR; > > $err = ??? > > # Send e-mail notification of error > %mail = (To => $to, > From => $from, > Subject => "Error in production", > Message => "$err" > ); > sendmail(%mail) or die $Mail::Sendmail::error; > print "Notification sent\n", $Mail::Sendmail::log; > } Try something like (untested) : use Win32::ChangeNotify; use Mail::SendMail; use Fcntl; use English; use strict; my $log = 'C:/Ariba/Buyer7_1/Coreserver/logs/AribaORMSNode1Log.txt'; my $to = '[EMAIL PROTECTED]'; my $from = '[EMAIL PROTECTED]'; my $directory = 'C:/Ariba/Buyer7_1/Coreserver/logs'; my @stat = stat $log; my $start = 0; # or init from a file that contains last position while (1) { @stat = stat $log; my $end = $stat[7]; if ($start != $end) { print "Checking ...\n"; open ERR, $log or warn "Error opening log $log: $!"; seek ERR, $start, SEEK_SET; # seek to last end my @lines = <ERR>; close ERR; my @errs; foreach (@lines) { push @errs, $_ if /:(?:warning|error):/i; } # Send e-mail notification of error my %mail = (To => $to, From => $from, Subject => "Error in production", Message => "@errs", # or join into string ); sendmail (%mail) or die $Mail::Sendmail::error; print "Notification sent\n", $Mail::Sendmail::log; $start = $end; # save new start pointer } else { warn "Notification with no size change\n"; } my $notify = Win32::ChangeNotify->new($directory, 0, 'SIZE') or die "$^E"; $notify->wait or warn "Error occurred: $!\n"; } __END__ -- ,-/- __ _ _ $Bill Luebkert ICQ=14439852 (_/ / ) // // DBE Collectibles Mailto:[EMAIL PROTECTED] / ) /--< o // // http://dbecoll.tripod.com/ (Free site for Perl) -/-' /___/_<_</_</_ Castle of Medieval Myth & Magic http://www.todbe.com/ _______________________________________________ Perl-Win32-Users mailing list [EMAIL PROTECTED] To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs
