perl 监控单个ip调用接口的次数
发布时间:2020-12-15 23:42:26 所属栏目:大数据 来源:网络整理
导读:#!/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个参数n";exit 1}; ($m,$n) = @_; #将参数赋值给变量 my $to
#!/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个参数n";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_usern"; my $subject = "Subject: zjcap infon"; 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("nn"); $smtp->datasend("$messagen"); # 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_filen"; ##如果文件存在,清空文件 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 "上次记录的记录数为$countn"; ###文件的最新记录数 open(my $fh,'<',"$mon_file"); $. = 0; while (<$fh>) { ($num=$.) if eof; }; print "文件最新的记录数为$num"."n"; ###最新的文件记录数 写入count.txt open( B,">","count.txt" ); print B ("$numn"); ##记录最新的日志 if ( defined($count) && $num != $count && $num !=0 ) {print "开始处理n"; open( C,"$mon_file" ) || die "$!n"; 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})$/){ $ip=$2; $hash{$ip}++; } }; while(my($ip,$times) = each %hash) { print "$ip access count(*) == $timesn"; open( E,"sum_acc.log" ); print E ("$ip access count(*) == $timesn"); if ( "$times" > "30" ){send_mail('zhaoyangjian@zjcap.cn',"flow01-$ip access count(*) == $timesn"); close E; }}; (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |