perl 监控日志告警

<pre name="code" class="sql">my $dir  = '/usr/local/apache-tomcat-7.0.55_8082/logs';
my $SDATE = strftime("%Y-%m-%d",localtime());
my $file = "localhost.$SDATE.log";
$mon_file = "$dir/$file";
##########监控关键字,以空格隔开################
###$count 上一次Exception的次数
@warn_arr = qw/Exception/;
foreach $a (@warn_arr) {
    undef @err_info;
    open( C, "<", "$a.tmp" );
    while (<C>) {
        $count = $_;
    }
print "=========================================================================================================================
";
print "######监控异常关键字:      "$a"---数量=$count
";

##$b是匹配到Exception关键字的最后行的行号

##$i是当前匹配Exception的行数
    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
";send_mail('zhaoyangjian@zjcap.cn',"zj-api02-@err_info is   @err_info
");
close B;
    }
    open( C, ">", "$a.tmp" );
    print C ("$i
");
}

jrhapt11:/home/tomcat/sbin> perl mon_log.pl 
=========================================================================================================================
######监控异常关键字:      "Exception"---数量=180

######$b is 4553######
######@err_info is   Nov 26, 2015 5:23:30 PM org.apache.catalina.core.StandardWrapperValve invoke
 SEVERE: Servlet.service() for servlet [springMVC] in context with path [/api] threw exception [java.lang.IllegalStateException: Cannot call sendRedirect() after the response has been committed] with root cause
 java.lang.IllegalStateException: Cannot call sendRedirect() after the response has been committed
 	at org.apache.catalina.connector.ResponseFacade.sendRedirect(ResponseFacade.java:482)
 	at javax.servlet.http.HttpServletResponseWrapper.sendRedirect(HttpServletResponseWrapper.java:137)

jrhapt11:/home/tomcat/sbin> 




                                    
原文地址:https://www.cnblogs.com/hzcya1995/p/13351174.html