2011年4月12日 星期二

scan log file in perl - another version

modify again, use egrep command to parse log directory.
修改另一各版本,用egrep指令+正規表示式來搜尋關鍵字
##########################################################
use strict;
use diagnostics;

### Variables defined here ###
my $domain = "abc.com"; # used in mail sender
chomp(my $host = "" || "localhost"); # hostname
my $DIR = "/WebSphere/AppServer/profiles/appsrv/logs"; # log directory
my $l_log = "SystemOut"; # log name
my $SENDMAIL = '/usr/sbin/sendmail';
### regular express here ###
my $keyword = 'SystemErr|dump';
### use , to seperate and put \ before @  ###
my $Admins = "abc\@abc.com";

### main logistic here ###
my $filename = $l_log . "*.log";
my @l_log = process_log($filename, $keyword);
if ( scalar @l_log > 0 ) {
&write_mail();
}
exit; # jump out...

### functions here ###
sub process_log {
my $target_log  = shift;
my $search_data = shift;

my $CMD = "/usr/bin/egrep -e '" . $search_data . "' " . $DIR . "/" . $filename;
my @RES = `$CMD`;
return @RES;
}

sub write_mail {
open (MAIL, "|$SENDMAIL -t") || die ("$0: Can't open $SENDMAIL: $!\n");
print MAIL "Reply-to: root\@$host.$domain\n";
print MAIL "From: \"$host Log\" \\n";
print MAIL "To: $Admins\n";
print MAIL "Subject: [Notice] $host Error Log Report at ", scalar localtime, "\n";
print MAIL " report from " . $host . ":\n";
print MAIL "\n$filename -\n";
foreach (@l_log) {print MAIL "$_\n"};
close (MAIL);
}

沒有留言:

最常被訪