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);
}

沒有留言:

20170721的回憶錄

20170721是個對我來說有特殊意義的時間點,所以一定要挑這天回憶一下... 時間回朔到20年前的19970721,那天正是我兵單上面報到的日子,這天早上9點鐘我就揹著簡單行李到舊板橋火車站去報到了。舊板橋火車站在這天的剛好兩年後被拆除(就是我退伍回來的時候,科科),網路上...