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

沒有留言:

正所謂人言可畏

近來女作家自殺事件,引起一圈又一圈的迴響。 很多舊事件又被翻了出來討論,其中有一篇吸引了我的注意。 那是引用2014年管仁健寫的文章 功在黨國,玩個台灣女人算什麼? 裡面提到加害者的方姓商人是蔣家姻親前高公局長方恩緒的兒子,所以得出這個標題。 不過,這事件網路上還有當...