码迷,mamicode.com
首页 > 数据库 > 详细

nginx access日志按时间进行分割

时间:2018-08-06 16:26:54      阅读:324      评论:0      收藏:0      [点我收藏+]

标签:ace   pre   logfile   tac   put   host   begin   eth0   日志   

按小时过滤日志:

#!/bin/bash
#file log path
log_file=‘/var/log/nginx/access.log‘

last_hour=1
# start time
start_time=`date -d "$last_hour hour ago" +"%H:%M:%S"`
# end time
end_time=`date +"%H:%M:%S"`
#Get the host ip address
host_ip=`ip addr |grep eth0|awk ‘BEGIN{FS="([[:space:]]|/)+"}NR==2{print $3}‘`
# output log file
filter_ip=‘/opt/scripts/log/hour_ip.txt‘
# app name
app_name="#"

echo "$app_name: $host_ip $end_time" > $filter_ip

tac $log_file | awk -v st="$start_time" -v et="$end_time" ‘{t=substr($4,RSTART+14,15);
if(t>=st && t<=et) {print $0}}‘ |awk ‘{if($9~/404/)a[$1" "$7" "$9]++}END{for(i in a) print i,a[i]}‘ |awk ‘{print $4,$1,$2}‘|sort -nr|head -n 10 >> $filter_ip

num=`cat $filter_ip|wc -l`

if [ $num -ge 2 ]; then
  cat $result | mail -s "$app_name: suspect_attack" test@163.com
fi

按分钟过滤日志:

#/bin/bash
#日志文件
logfile=‘/var/log/nginx/access.log‘
log_file=‘/opt/scripts/log/half_ip.txt‘

# app name
app_name="#"
#host ip addr
host_ip=`ip addr |grep eth0|awk ‘NR==2{print $2}‘|awk -F/ ‘{print $1}‘`

#time interval
last_minutes=30

#开始时间
start_time=`date -d "$last_minutes minutes ago" +"%H:%M:%S"`

#结束时间
stop_time=`date +"%H:%M:%S"`

echo "$app_name: $host_ip $stop_time" > $log_file

#过滤出单位之间内的日志并统计最高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 |egrep -v ‘106.14.240.239‘|awk ‘{if($1 > 1000){print $0}}‘ >> $log_file

num=`cat $log_file|wc -l`

if [ $num -ge 2 ]; then
  cat $log_file | mail -s "$app_name: suspect_attack" test@163.com
fi

nginx access日志按时间进行分割

标签:ace   pre   logfile   tac   put   host   begin   eth0   日志   

原文地址:http://blog.51cto.com/1054054/2155294

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!