<pre name="code" class="sql">#!/usr/bin/perl
use POSIX;
use CGI;
use DBI;
my $dbName = 'oadb';
my $dbUser = 'system';
my $dbUserPass = 'a2xxxxxmvh';
my $dbh = DBI->connect("dbi:Oracle:$dbName", $dbUser, $dbUserPass) or die "can't connect to database " ;
my $dir = '/usr/local/apache-tomcat-7.0.55_8081/logs';
my $file = 'catalina.out';
my $tom_home='/usr/local/apache-tomcat-7.0.55_8081/webapps/mon';
$mon_file = "$dir/$file";
$IPMACHINE = "MBANKtest1";
$RTXNO="04657";
my $SDATE = strftime("%Y%m%d%H%M%S",localtime());
$UPLOADPATH = "/data/swap/mbanklog/$IPMACHINE-$SDATE$file";
print "########################################################################################################################
";
print "######监控日志文件: $mon_file
";
print "######监控服务器: $IPMACHINE
";
print "######报警通知腾讯通号码: $RTXNO
";
print "######当前报警时间戳: $SDATE
";
##########监控关键字,以空格隔开################
@warn_arr = qw/Exception 登录失败 登陆失败 用户名或密码错误/;
if (-f "$tom_home/file.html"){
open FILE,">$tom_home/file.html";
close FILE;};
foreach $a (@warn_arr) {
undef @err_info;
open( C, "<", "$a.tmp" );
while (<C>) {
$count = $_;
}
print "=========================================================================================================================
";
print "######监控异常关键字: "$a"---数量=$count
";
open( A, "<", "$mon_file" ) || die "$!
";
$i = 0;
$num = 0;
$b = 0;
#$b是取得最后一行匹配关键字的行号
while (<A>) {
$num++;
if ( $_ =~ /$a/i ) { $i++; $b = $num }
};
#defined($count) 是第一次读取文件时为空 $i!=0 是累计次数不为0 防止文件清空的情况
if ( $i != 0 && $i != $count && defined($count) )
{
open( B, "<", "$mon_file" ) || die "$!
";
while (<B>) {
if (($. >= "$b" -1) and ($. <= "$b" + 1) )
{push( @err_info, $_ );
if ($_ =~ /.*username=(.*?)&password.*/){$USER=$1};
};
};
close B;
print "######错误信息:@err_info
";
$dbh->do("INSERT INTO SYSTEM.ERR_INFO values('$USER','11.4.19.62',to_date('$SDATE','yyyy-mm-dd hh24:mi:ss'),'@err_info')") or die($DBI::errstr);
};
open( D, ">", "$a.tmp" );
print D ("$i
");
};