先过滤NGINX日志
#!/bin/bash
months=("Jan" "Feb" "Mar" "Apr" "May" "Jun" "Jul" "Aug" "Sep" "Oct" "Nov" "Dec")
time1=$(date +"%d")/${months[$(date +"%-m")-1]}/$(date +"%Y")':'$(date -d "1 hour ago" +"%H")
time2=$(date +"%d")/${months[$(date +"%-m")-1]}/$(date +"%Y")':'$(date +"%H")
time3=$(date +"%d")/${months[$(date +"%-m")-1]}/$(date +"%Y")':'$(date -d +1hour +"%H")
grep $time1'\|'$time2 /data/nginx/logs/access.log | grep "POST ">/root/statics/statics.log
python /root/statics/AnalyzeLogSecondsAuto.py /root/statics/statics.log $time1 $time3
再使用python做统计,同时发送到钉钉群
import sys
import numpy as np
import requests
inputdata=open(sys.argv[1], 'r')
time1=sys.argv[2]
time2=sys.argv[3]
datax=[]
datay=[]
contents = ''
webhook = 'https://oapi.dingtalk.com/robot/send?access_token=a984a10d58dd890a602abe781186fa18231399f0a852d3bf6c4a8924a4e2d9fb'
for line in inputdata:
line = line.split(" ")
datax.append(line[3][1:21])
datax_sorted = sorted(datax)
unique_datax = np.unique(datax)
for i in unique_datax:
datay.append(datax.count(i))
datay_sorted=sorted(datay,reverse = True)
total_requests=str(len(datax)*4)
qps=str(datay_sorted[0]*4)
contents += '<font color=#00b38a>%s</font>到<font color=#00b38a>%s</font>, 最大并发数: <font color=#00b38a>%s</font>(tps), 访问量: <font color=#00b38a>%s</font>(请求数), 未启动限流, 系统服务器状态运行正常, 系统支持最大并发数2000(tps).'%(time1,time2,qps,total_requests)+'\n\n'
def send_dd(webhook,content):
headers = {"Content-Type": "application/json; charset=utf-8"}
msg = {
"msgtype": 'markdown',
'markdown': {'title': '两小时最大TPS与总访问量统计',
'text': '--------统计结果--------\n\n%s'%content
},
'at': {
"atMobiles": [],
"isAtAll": False
}
}
resp = requests.post(webhook, headers=headers, json=msg)
send_dd(webhook,contents)