<pre name="code" class="sql">flow02:/home/tomcat> cat m1.pl
#!/usr/bin/perl
use POSIX;
my $dir = '/home/tomcat';
my $file = '1.log';
$mon_file = "$dir/$file";
my $SDATE = strftime("%Y%m%d%H%M%S",localtime());
$UPLOADPATH = "/data/swap/mbanklog/$IPMACHINE-$SDATE$file";
##########监控关键字,以空格隔开################
@warn_arr = qw/Exception 555/;
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;
while (<A>) {
$num++;
if ( $_ =~ /$a/i ) { $i++; $b = $num }
}
print "######$b is $b######
";
if ( $i != 0 && $i == $count && defined($count) ) {
open( B, "<", "$mon_file" ) || die "$!
";
while (<B>) {
if (($. >= "$b" -2) and ($. <= "$b" + 2) ){push( @err_info, $_ ) };
};
print "######@err_info is @err_info
";
#close B;
}
open( C, ">", "$a.tmp" );
print C ("$i
");
}
flow02:/home/tomcat> cat 1.log
4444
2223
5555
java.lang.NullPointerException]
4
java.lang.NullPointerException
5
8
flow02:/home/tomcat> perl m1.pl
=========================================================================================================================
######监控异常关键字: "Exception"---数量=2
$i is 2
$b is 6
######$b is 6######
######@err_info is java.lang.NullPointerException]
4
java.lang.NullPointerException
5
8
=========================================================================================================================
######监控异常关键字: "555"---数量=1
$i is 1
$b is 3
######$b is 3######
######@err_info is ###这个地方没有打印第3行的上下2行,为什么呢?
$i 表示匹配关键字的次数
产生原因为没有关键文件句柄B;
flow02:/home/tomcat> perl m1.pl
=========================================================================================================================
######监控异常关键字: "Exception"---数量=2
$i is 2
$b is 6
######$b is 6######
######@err_info is java.lang.NullPointerException]
4
java.lang.NullPointerException
5
8
=========================================================================================================================
######监控异常关键字: "555"---数量=1
$i is 1
$b is 3
######$b is 3######
######@err_info is 4444
2223
5555
java.lang.NullPointerException]
4
为什么呢?没有关闭文件句柄,行号累加了。
flow02:/home/tomcat> perl m1.pl
=========================================================================================================================
######监控异常关键字: "Exception"---数量=2
$i is 2
$count is 2
######$b is 6######
$. is 1
$. is 2
$. is 3
$. is 4
$. is 5
$. is 6
$. is 7
$. is 8
######@err_info is java.lang.NullPointerException]
4
java.lang.NullPointerException
5
8
=========================================================================================================================
######监控异常关键字: "555"---数量=1
$i is 1
$count is 1
######$b is 3######
$. is 9
$. is 10
$. is 11
$. is 12
$. is 13
$. is 14
$. is 15
$. is 16
######@err_info is