Perl Script: Oracle Database Alert Log Parser, Send Email Notification if Error is Found

No GoodNeed ImprovementOKGoodExcellent (1 votes, average: 5 out of 5)
Loading ... Loading ...

#print $timestamp ."\n";
my $cur_error_html=”";
if ($timestamp eq ”)
{
$timestamp =$last_log_timestamp_str;
#print $last_log_timestamp_str .”\n”;
}
open (OUT, “>$output”) or warn “can not write file”;
foreach my $line (@error_msg)
{
#print OUT $line ;
print OUT $line .”\n”;
$cur_error_html .=$line .”<br>”;
$cur_error_text .=$line .”\n”;
}
if ($counter==0)
{
print OUT $timestamp .”\n”;
$cur_error_text = $timestamp;
}
close (IN);
close (OUT);
my $subject=”";
my $command=”";
# print “old:” .$old_error_text . “\n”;
# print “new:” .$cur_error_text. “\n”;
# print length ($cur_error_text) .”\n”;
#print “shutdown status: “. $db_shutdown_status .”\n”;
#print “start status: “. $db_start_status .”\n”;

if ( ($cur_error_text ne $old_error_text) && length ($cur_error_text)>25)
{

if ($error_found==1)
{
$subject = “$counter Oracle Error: ” . $oracle_DB_name;
#sendGmail ($cur_error_text);
#my $command =”\”C:\\Program Files\\Java\\jdk1.6.0\\bin\\java\” SendGmail \”$subject\” \”$cur_error_text\”";
$command =$working_dir.”\\SI_Emailer_command_line.exe /TO \”$recipients2\” /SUB \”$subject\” /EX \”$exchange_server\” /BODY \”$cur_error_html\”";
#print $command .”\n”;
system ($command);
$command =”\”". $java.”\”". ” SendGmail \”$subject\” \”$cur_error_text\”";
# print $command .”\n”;
# system ($command);
}
}
if ($elapsed >=0 && ($db_shutdown_status !=0 or $db_start_status !=0))
{
{
$db_status_subject = $db_status_subject .”:”. $oracle_DB_name;
$command =$working_dir.”\\SI_Emailer_command_line.exe /TO \”$recipients2\” /SUB \”$db_status_subject\” /EX \”$exchange_server\” /BODY \”$message\”";
# print $command .”\n”;
system ($command);
$command =”\”". $java.”\”". ” SendGmail \”$db_status_subject\” \”$text_message\”";
# print $command .”\n”;
# system ($command);
}
}
foreach $trc_file (@trc_files)
{
if ($trc_file ne “”)
{
print “sending trace file $trc_file .\n”;
sendmail_mime ($mailFrom, $mailServer1, $mailServer2, $mailTo,$subject_for_trace, $text_message, $trc_file);
}
}
exit 0;
END
{
# $dbh->disconnect if (defined $dbh);
# undef $alert_fh if (defined $alert_fh);
}
sub check_error
{
my $line=shift;
my $error1 = “ORA\-”;
my $error4 = “OSD\-”;
my $error2 = “Error”;
#my $error2 = “Errors”;
my $error3 = “error”;
my $error5 = “terminated”;
my $trc = ‘.trc’;
my @temp_arr=();
my $trc_file=”";

my $found = 0;
if ( $line =~ /$error1/ || $line =~ /$error2/ || $line =~ /$error3/ || $line =~ /$error4/ || $line =~ /$error5/)
{
$error_found = 1;
$found=1;

if ($line =~ /$trc/gi)
{
# print $line .”\n”;
@temp_arr= split(/\s/, $line);
foreach my $item (@temp_arr)
{
if ($item =~ /$trc/)
{
$trc_file=$item;
$trc_file =~ s/\’//g;
$trc_file =~ s/\.$//;
$trc_file =~ s/\:$//;
}
}
}
}

return ($found, $trc_file);
}
sub check_db_status
{
# my $line = $_[0];
# $mesg1 = “Starting ORACLE instance”; 1
# $mesg2 =”OPEN”; 2
# $mesg3 = “Shutting down instance:”; 3
# $mesg4 =”DISMOUNT”; 4
# $mesg5 =”Completed”;
# $mesg6 =”Shutting down instance (immediate)”; 5
# $mesg7 =”immediate”;
my $status=0;
if (/$mesg1/)
{
$status=1;
}
if (/$mesg2/ && /$mesg5/ )
{
$status=2;
}
if (/$mesg3/)
{
$status=3;
}
if (/$mesg4/ && /$mesg5/ )
{
$status=4;
}
if (/$mesg6/ && /$mesg7/ )
{
$status=5;
}
return $status;
}
sub sendGmail
{
my $message =shift;
my ( $gmail ) = Mail::Webmail::Gmail->new(
username => $account, password =>$password , encrypt_session => 1 );
my $msgid = $gmail->send_message( to => $recipients, subject => $subject, msgbody => $message );
# print “Msgid: $msgid\n”;
if ( $msgid )
{
if ( $gmail->error() )
{
print $gmail->error_msg();
}
else
{
print “message was successful sent to all recipients. \n”;
}
}
else
{
print “message coult not be sent. \n”;
}
}
sub sendMailbySMTP
{
my @group = shift;
my $smtp = Net::SMTP->new(’$smt_phost’, Timeout => 60)
or warn “Couldn’t connect to mail server: $!\n”;
$smtp->auth($from, $password) || warn “Authentication failed!\n”;
$smtp->recipient(@recipient_array, { Notify => ['NEVER'], SkipBad => 1 });
$smtp->data();
$smtp->datasend(”Subject: $subject\n”);
$smtp->data(@group);
$smtp->dataend();
$smtp->quit;
}
sub get_alert
{
# my $alert_log=’C:\oracle\admin92\siit\bdump\alert_siit.log’;
my $alert_log=’C:\Temp\alert_siit.log’;
return $alert_log;
}

sub sendmail
{
my $sender=shift;
my $email_smtp_server1=shift;
my $email_smtp_server2=shift;
my $recipient=shift;
my $subject=shift;
my $message=shift;
print $recipient . $subject . $message.”\n”;
my $mail = new NTsendmail;

$mail->setvar(”NTSENDMAIL”, $email_smtp_server1); # or “localhost” or “smtp.test.kom” etc.
$mail->setvar(”nodeath”, 1);
# Or the alternate syntax:
# $ENV{NTsendmail} = “192.168.0.250″;
if (! $mail->send($sender, $recipient, $subject, $message))
{
warn(”*** ERROR: ” . $mail->getvar(”error_report”));

$mail->setvar(”NTSENDMAIL”, $email_smtp_server1);
$mail->setvar(”nodeath”, 1);
$mail->send($sender, $recipient, $subject, $message);
}
}
sub sendmail_mime
{
my $sender=shift;
my $email_smtp_server1=shift;
my $email_smtp_server2=shift;
my $recipient=shift;
my $subject=shift;
my $message=shift;
my $file=shift;

my $msg = MIME::Lite->new(
From=>”$sender”,
To => “$recipient”,
Subject=>”$subject”,
Type=>’multipart/mixed’);
$msg->attach( Type =>’TEXT’,
Data =>”$message”);

$msg->attach( Type =>’TEXT’,
Path =>”$file”,
Disposition => ‘attachment’);

MIME::Lite->send(’smtp’, “$email_smtp_server1″,Timeout=>60);
$msg->send ;
}

Popularity: 14%

Pages: 1 2 3

1 Comment »

  1. ron reidy said,

    June 28, 2007 @ 5:06 pm

    Please remove this script from this site, or remove the author name listed and “All Rights Reserved” clause.

    The person who put hist name on this script along with the “All Rights Reserved” clause did not right this Perl program - I did. I posted it to orafaq.com/scripts/perl/trlogmon.pl in 1999. The Windows changes are a minor addition to a program that was originally written to run on Solaris and Linux

    Very unoriginal of a Ph.D., indeed.

RSS feed for comments on this post · TrackBack URI

Leave a Comment

*
To prove you're a person (not a spam script), type the security word shown in the picture. Click on the picture to hear an audio file of the word.
Click to hear an audio file of the anti-spam word