指定时间内网站访问次数的监控

需求说明:
在日常运维工作中,为了防止一些恶意访问的行为,例如不断的请求刷流量,通过实时过滤Nginx访问日志,将单位时间内访问次数达到指定阀值的来源ip查找出来,并通过邮件报警方式及时通知运维人员!

比如针对url为http://192.168.10.202:8888的访问进行监控,当在1分钟内访问次数超过300次数,就邮件报警给运维人员。
1)nginx日志监控脚本

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

[root@Fastdfs_storage_s1 ~]# cat /opt/nginx_log_monit.sh

#!/bin/bash

#日志文件

logfile=/usr/local/nginx/logs/access.log

   

#开始时间

start_time=`date -d"$last_minutes minutes ago" +"%H:%M:%S"`

   

#结束时间

stop_time=`date +"%H:%M:%S"`

   

#过滤出单位之间内的日志并统计最高ip数

tac $logfile | awk -v st="$start_time" -v et="$stop_time" '{t=substr($4,RSTART+14,21);if(t>=st && t<=et) {print $0}}' \

awk '{print $1}' sort uniq -c | sort -nr > /root/log_ip_top10

ip_top=`cat /root/log_ip_top10 head -1 | awk '{print $1}'`

# 单位时间[1分钟]内单ip访问次数超过300次,则触发邮件报警

if [[ $ip_top -gt 300 ]];then

 /usr/bin/python /opt/send_mail.py &

fi

2)python报警脚本

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

[root@Fastdfs_storage_s1 ~]# cat /opt/send_mail.py

# -*- coding: utf-8 -*-

from email import encoders

from email.header import Header

from email.mime.text import MIMEText

from email.utils import parseaddr, formataddr

from email.mime.multipart import MIMEMultipart

from email.mime.base import MIMEBase

from datetime import datetime

import os

import smtplib

def _format_addr(s):

 name, addr = parseaddr(s)

 return formataddr((Header(name, 'utf-8').encode(), addr))

# 邮箱定义

smtp_server = 'smtp.kevin.com'

smtp_port = 465

from_addr = 'monit@kevin.com'

password = os.environ.get('monit@123')

to_addr = ['wangshibo@kevin.com']

# 邮件对象

msg = MIMEMultipart()

msg['From'] = _format_addr('发件人 <%s>' % from_addr)

msg['To'] = _format_addr('收件人 <%s>' % to_addr)

msg['Subject'] = Header('Warning:单ip请求次数异常''utf-8').encode()

# 获取系统中要发送的文本内容

with open('/root/log_ip_top10''r') as f:

 line = f.readline().strip()

 line = line.split(" ")

print(line)

# 邮件正文是MIMEText:

html = '<html><body><h2>一分钟内单ip请求次数超过阀值</h2>' + \

 '<p>ip:%s  请求次数/min:%s</p>' % (line[1],line[0]) + \

 '</body></html>'

msg.attach(MIMEText(html, 'html''utf-8'))

server = smtplib.SMTP_SSL(smtp_server, smtp_port)

server.login(from_addr, password)

server.sendmail(from_addr, to_addr, msg.as_string())

server.quit()

3)写个测试脚本不停curl请求资源触发报警

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

[root@Fastdfs_storage_s1 ~]# cat /opt/curl.sh

#!/bin/bash

#example:curl.sh http://www.kevin.com 100

usage()

{

 echo "usage: `basename $0` url count"

}

if [ $# -ne 2 ]; then

 usage

 exit 1

fi

for in `seq 1 $2`;do

 http_code=`curl -o /dev/null -s -w %{http_code} $1`

 echo $1 $http_code

done

手动执行测试脚本

[root@Fastdfs_storage_s1 ~]# /bin/bash /opt/curl.sh http://192.168.10.202:8888 300

http://192.168.10.202:8888 200

http://192.168.10.202:8888 200

http://192.168.10.202:8888 200

http://192.168.10.202:8888 200

http://192.168.10.202:8888 200

http://192.168.10.202:8888 200

http://192.168.10.202:8888 200

http://192.168.10.202:8888 200

http://192.168.10.202:8888 200

http://192.168.10.202:8888 200

http://192.168.10.202:8888 200

...........

4)定时任务,由于上面脚本是监控一分钟内的日志,因此每分钟执行一次

1

2

[root@Fastdfs_storage_s1 ~]# crontab -e

* * * * * /bin/bash -x /opt/nginx_log_monit.sh >/dev/null 2>&1

这里仅仅是实现了邮件告警功能,实际上还可以实现自动屏蔽恶意访问的ip。
可以通过Nginx deny来实现,也可以通过iptables屏蔽("iptables -I INPUT -s x.x.x.x -j DROP"方式)。

*************** 当你发现自己的才华撑不起野心时,就请安静下来学习吧!***************

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值