|
|
Question : Reading Log Files with Perl Then E-mailing Errors
|
|
In Perl 5.005 what is the best way to read a log file and search for the word ERROR then return those ERROR messages (possibly in a string) to another file as well as then e-mail them to myself - likely this log will have errors everyday (hopefully not but I'm just assuming)
I'm very new to Perl and don't even know where to start on this one - any snippets anyone has on this would be great
|
Answer : Reading Log Files with Perl Then E-mailing Errors
|
|
Oke here it is. You can uncomment the # print lines for testen. I have tested the code on a Unix machine, so you need to set it for Windows.
The e-mailing I left out. You can just e-mail the $errors using any email code you want.... since you work with windows the 'sendmail' will not work for you.
----------------------------------- #!/usr/bin/perl print "Content-type: text/html\n\n";
open(LOG,"basew.log") or die "Unable to open logfile:$!\n"; open(ERRORS,"errors.log") or die "Can not read iwddderrors.log $!\n"; @ERRORRECS=; close(ERRORS);
$dateprinted = false; $reported = false; while () { if (/^Started/i) { $startedline = $_; # print " startedline: $startedline"; if (grep{$_ eq $startedline} @ERRORRECS) { # print " Grep: $_ Exist allready"; $reported = true; } else { # print " Not exist"; $reported = false; $dateprinted = false; } #end if grep } #end if started
if ((/^ERROR/) && ($reported eq false)) { if ($dateprinted eq true) { $mailerrors.="$_\n"; } else { $mailerrors.="$startedline\n$_\n"; $dateprinted = true; } #end if dateprinted
} #end if error } #end while Log1
close(LOG);
open(ERRORS,">>errors.log") or die "Can not write to iwddderrors.log $!\n"; print ERRORS "$mailerrors\n"; close(ERRORS);
print "
Here Mailerror: $mailerrors";
|
|
|
|