• Perl 循环内没关闭文件句柄引发的问题


    v-lhb-db01:/root/perl# cat 1.log 
    1111
    2222
    3333
    4444
    5555
    异常111
    a1
    a2
    a3
    a4
    a5
    java.lang.NullPointerException]
    1
    2
    3
    4
    java.lang.NullPointerException
    5
    6
    7
    8
    
    --------------------------------------------
    #!/usr/bin/perl 
    use POSIX;
    my $dir  = '/root/perl';
    my $file = '1.log';
    $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 异常/;
    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
    ";
    #        system("wget -q --spider -t 1 http://10.130.130.248:8012/sendnotify.cgi?title="$IPMACHINE"\&msg="$SDATE$file\n@err_info"\&receiver="$RTXNO"");
    ##        sleep(5);
    ##        system("./putlog "$mon_file" "$UPLOADPATH"");
    #        print "######日志文件已上传至       $UPLOADPATH
    ";
    #close B;
        }
    
        open( C, ">", "$a.tmp" );
    
        print C ("$i
    ");
    
    }
    --------------------------------------------
    
    
    v-lhb-db01:/root/perl# perl m1.pl 
    ########################################################################################################################
    ######监控日志文件:          /root/perl/1.log
    ######监控服务器:            MBANKtest1
    ######报警通知腾讯通号码:    04657
    ######当前报警时间戳:        20150505101212
    =========================================================================================================================
    ######监控异常关键字:      "Exception"---数量=2
    
    ######$b is 17######
    ######错误信息:             3
     4
     java.lang.NullPointerException
     5
     6
    
    =========================================================================================================================
    ######监控异常关键字:      "异常"---数量=1
    
    ######$b is 6######
    ######错误信息:
    
    
    这里异常的上下行没有打印,原因为没关闭文件句柄:
    
    #!/usr/bin/perl 
    use POSIX;
    my $dir  = '/root/perl';
    my $file = '1.log';
    $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 异常/;
    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
    ";
    #        system("wget -q --spider -t 1 http://10.130.130.248:8012/sendnotify.cgi?title="$IPMACHINE"\&msg="$SDATE$file\n@err_info"\&receiver="$RTXNO"");
    ##        sleep(5);
    ##        system("./putlog "$mon_file" "$UPLOADPATH"");
    #        print "######日志文件已上传至       $UPLOADPATH
    ";
    close B;
        }
    
        open( C, ">", "$a.tmp" );
    
        print C ("$i
    ");
    
    }
    
    v-lhb-db01:/root/perl# vi m1.pl 
    v-lhb-db01:/root/perl# perl m1.pl 
    ########################################################################################################################
    ######监控日志文件:          /root/perl/1.log
    ######监控服务器:            MBANKtest1
    ######报警通知腾讯通号码:    04657
    ######当前报警时间戳:        20150505101314
    =========================================================================================================================
    ######监控异常关键字:      "Exception"---数量=2
    
    ######$b is 17######
    ######错误信息:             3
     4
     java.lang.NullPointerException
     5
     6
    
    =========================================================================================================================
    ######监控异常关键字:      "异常"---数量=1
    
    ######$b is 6######
    ######错误信息:             4444
     5555
     异常111
     a1
     a2
    

  • 相关阅读:
    自定义Bootstrap样式弹出框
    自定义Bootstrap样式弹出框
    Java高级
    Java高级
    Java高级
    Java高级
    与jQuery的感情碰撞——由浅入深学jQuery
    xgqfrms™, xgqfrms® : xgqfrms's offical website of GitHub!
    xgqfrms™, xgqfrms® : xgqfrms's offical website of GitHub!
    xgqfrms™, xgqfrms® : xgqfrms's offical website of GitHub!
  • 原文地址:https://www.cnblogs.com/hzcya1995/p/13351728.html
Copyright © 2020-2023  润新知