Jan 18, 2014, 8:17 AM
Post #1 of 4
Log monitoring script help
I am trying to build a script that will check for multiple patterns in a log file and will send the matching lines plus more details of the error by email (i.e, . the email should contain, 10 lines before the pattern matching lines + pattern matching line + 10 lines after the pattern matching line).
I have started building my script as below. I am new in perl and getting few obstracles and will appreciate if any of you kindly help me to finish this.
Here are my problems:
Original log file name = abcEarlyCheckout.out.2014.01.17T04.35.02.log
This log file will be produced once daily but I am unable to assign log with with $todaydate variable plus the '*' as perl does not recognize '*'.
Trying to assign the log file in the script as:
Now that I am able to search the pattern and get the error line, how can I include pre and post matching pattern lines (10 lines before the pattern matching lines + pattern matching line + 10 lines after the pattern matching line) in my logic so that the email will have the detail of the error lines?
open ( FILE, "> error.txt" ) || die "Cannot open " ;
if (-e $log)
open (LOGFILE, $log) or die ("Could not open log file.\n");
print FILE "Script has failed with below errors, please investigate.\n\n ";
print FILE $_;
print FILE "Job did not run today, please investigate. \n";
$to = "abc\@gmail.com";
$from = "abc\@gmail.com";
$subject = 'abcEarlyCheckout error';
local $/ = undef;
open (ERROR, "error.txt") or die "file does not exists";
$message .= <ERROR>;
open(MAIL, "|/usr/sbin/sendmail -t");
# Email Header
print MAIL "To: $to\n";
print MAIL "From: $from\n";
print MAIL "Subject: $subject\n\n";
# Email Body
print MAIL $message;
#print "Email Sent Successfully\n";
(This post was edited by FishMonger on Jan 18, 2014, 8:26 AM)