Nginx access log split by time

Source: Internet
Author: User

Filter logs by hour:

#!/bin/bash#file log pathlog_file=‘/var/log/nginx/access.log‘last_hour=1# start timestart_time=`date -d "$last_hour hour ago" +"%H:%M:%S"`# end timeend_time=`date +"%H:%M:%S"`#Get the host ip addresshost_ip=`ip addr |grep eth0|awk ‘BEGIN{FS="([[:space:]]|/)+"}NR==2{print $3}‘`# output log filefilter_ip=‘/opt/scripts/log/hour_ip.txt‘# app nameapp_name="#"echo "$app_name: $host_ip $end_time" > $filter_iptac $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_ipnum=`cat $filter_ip|wc -l`if [ $num -ge 2 ]; then  cat $result | mail -s "$app_name: suspect_attack" [email protected]fi

Filter logs by minute:

#/bin/bash#日志文件logfile=‘/var/log/nginx/access.log‘log_file=‘/opt/scripts/log/half_ip.txt‘# app nameapp_name="#"#host ip addrhost_ip=`ip addr |grep eth0|awk ‘NR==2{print $2}‘|awk -F/ ‘{print $1}‘`#time intervallast_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_filenum=`cat $log_file|wc -l`if [ $num -ge 2 ]; then  cat $log_file | mail -s "$app_name: suspect_attack" [email protected]fi

Nginx access log split by time

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.