CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Need a Custom or Prewritten Perl Program?: I need a program that...: Log monitoring script help: Edit Log



liton79
Novice

Jan 18, 2014, 8:17 AM


Views: 17327
Log monitoring script help

 
Hi Gurus,

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:

P:1

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:
$log="abcEarlyCheckout.out.2014.01.17.$todaydate.*.log";


P:2
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?




Code
#!/usr/bin/perl 

$todaydate=`date +"%Y.%m.%d"`;
open ( FILE, "> error.txt" ) || die "Cannot open " ;

$log="abcEarlyCheckout.out.2014.01.17.$todaydate.*.log";

if (-e $log)
{
open (LOGFILE, $log) or die ("Could not open log file.\n");
while(<LOGFILE>)
{

chomp;
if (/FATAL/)
{
print FILE "Script has failed with below errors, please investigate.\n\n ";
print FILE $_;
}
}
close(LOGFILE);
}
else
{
print FILE "Job did not run today, please investigate. \n";
}
close (FILE);

$to = "abc\@gmail.com";
$from = "abc\@gmail.com";
$subject = 'abcEarlyCheckout error';


my $message;
{
local $/ = undef;
open (ERROR, "error.txt") or die "file does not exists";
$message .= <ERROR>;
close FRROR;
}

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;

close(MAIL);
#print "Email Sent Successfully\n";

Frown


(This post was edited by FishMonger on Jan 18, 2014, 8:26 AM)


Edit Log:
Post edited by FishMonger (Veteran) on Jan 18, 2014, 8:26 AM


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives