zoukankan      html  css  js  c++  java
  • 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;
    }};
    

  • 相关阅读:
    Android studio导入开源项目
    使用Kindeditor上传图片
    IOS实现自动循环滚动广告--ScrollView的优化和封装
    Android开发之Drag&Drop框架实现拖放手势
    IOS中的手势详解
    Android实现图片轮显效果——自定义ViewPager控件
    IOS欢迎界面Launch Screen动态加载广告
    tomcat 启动参数 Xms, Xmx, XX:MaxNewSize, XX:PermSize, -XX:MaxPermSize, Djava.awt.headless
    PHP提升echo, printf, print, file_put_contents等输出方法的效率
    WIN7下强制分第四个主分区的方法
  • 原文地址:https://www.cnblogs.com/hzcya1995/p/13351518.html
Copyright © 2011-2022 走看看