• perl 监控单个ip调用接口的次数


    #!/usr/bin/perl
    #取文件行数
    ##循环开始清空文件
    use POSIX;
    use CGI;
    use DBI;
    use HTTP::Date qw(time2iso str2time time2iso time2isoz);  
    use Net::SMTP;
    sub send_mail{
    if (@_ != 2){print "请输入2个参数
    ";exit 1};
        ($m,$n) = @_; #将参数赋值给变量  
        my $to_address  = $m;
    my $CurrTime = time2iso(time());
        my $to_address  = $m;
    
     my $mail_user   = 'zhaoyangjian@zjcap.cn';
        my $mail_pwd    = 'xxx5';
        my $mail_server = 'smtp.exmail.qq.com';
    
        my $from    = "From: $mail_user
    ";
        my $subject = "Subject: zjcap info
    ";
        my $info = "$CurrTime--$n";
         my $message = <<CONTENT; 
         $info
    CONTENT
        my $smtp = Net::SMTP->new($mail_server);
    
        $smtp->auth($mail_user, $mail_pwd) || die "Auth Error! $!";
        $smtp->mail($mail_user);
        $smtp->to($to_address);
    
        $smtp->data();             # begin the data
        $smtp->datasend($from);    # set user
        $smtp->datasend($subject); # set subject
        $smtp->datasend("
    
    ");
        $smtp->datasend("$message
    "); # set content
        $smtp->dataend();
        $smtp->quit();
    };
    
    my $SDATE = strftime("%Y-%m-%d",localtime());
    my $XDATE = strftime("%Y%m%d%H%M%S",localtime());
    my $dir  = '/usr/local/apache-tomcat-7.0.55_8081/logs';
    my $file = "localhost_access_log.$SDATE.txt";
    $mon_file = "$dir/$file";
    print "$mon_file is $mon_file
    ";
    ##如果文件存在,清空文件
    if (-f "tmp.out"){
    open FILE,">tmp.out";
    close FILE;}
    
    if (-f "sum_acc.log"){
    open FILE,">sum_acc.log";
    close FILE;}
    ###上次文件的记录数
    open( A, "<", "count.txt" );
                while (<A>) {
                $count = $_;
                            };
    print "上次记录的记录数为$count
    ";
    
    ###文件的最新记录数
    open(my $fh, '<', "$mon_file");
    $. = 0;
    while (<$fh>) {
        ($num=$.) if eof;
    };
    print "文件最新的记录数为$num"."
    ";
    ###最新的文件记录数 写入count.txt
    open( B, ">", "count.txt" );
    print B ("$num
    ");
    
    ##记录最新的日志
    if ( defined($count)  && $num != $count && $num !=0  )
    {print "开始处理
    ";
         open( C, "<", "$mon_file" ) || die "$!
    ";
           while (<C>) {
                 if  ($. > "$count" ){ 
                  open( D, ">>", "tmp.out" );
                  print D ("$_");
                 }
                      }
    };
                   close D;
    ####判断同一IP 调用接口30次以上的,发送邮件
    open (LOG ,"<","/home/tomcat/sum_acc/tmp.out");  
                        while (<LOG>) {  
                        chomp; 
     if ($_ =~ /.* "GETs*(.*?)=.*s+(d{1,3}.d{1,3}.d{1,3}.d{1,3})$/){
    $ip=$2;
    $hash{$ip}++;
    }
                                   };
    while(my($ip, $times) = each %hash) {
                       print "$ip access count(*) ==   $times
    ";
    open( E, ">>", "sum_acc.log" );
    print E ("$ip access count(*) ==   $times
    ");                     
    if ( "$times" > "30" ){send_mail('zhaoyangjian@zjcap.cn',"flow01-$ip access count(*) ==   $times
    ");
    close E;
    }};
    

  • 相关阅读:
    战火魔兽CJQ圣印问题
    sublime插件总汇
    js引用类型
    一、vue的数据双向绑定的实现
    渲染机制
    帆布指纹识别
    call、apply与bind在理解
    webpack的世界
    line-height与vertical-align
    'abc' 转换成[a, b, c]一道面试题的思考
  • 原文地址:https://www.cnblogs.com/hzcya1995/p/13351518.html
Copyright © 2020-2023  润新知