菜鸟版:
times=`tail -1 /Data/logs/nginx/access.log | awk '{print $4}'`
m_time=`echo ${times} | awk -F: '{print $2}'`
for i in 01 02 03 04 05 06 07 08 09;do
if [ $i -eq ${m_time} ];then
m_time=`echo ${m_time#0}`
fi
done
if [ ${m_time} = "00" ];then
s_time="23"
else
let "s_time=${m_time}-1"
num=`expr length ${s_time}`
if [ ${num} -eq 1 ];then
s_time="0${s_time}"
fi
fi
for i in 0 1 2 3 4 5 6 7 8 9;do
if [ $i -eq ${m_time} ];then
m_time="0${m_time}"
fi
done
time=`echo ${times#[} | sed "s/:${m_time}:/:${s_time}:/"`
time=`echo ${time} | awk -F/ '{print $1"\\\/"$2"\\\/"$3}'`
times=`echo ${times} | awk -F/ '{print $1"\\\/"$2"\\\/"$3}'`
echo "Start time is ${time}"
echo "Stop time is ${times#[}"
st_time=`date "+%H:%M:%S"`
echo "Start time is ${st_time}"
sed -n "/${time}/,/${times#[}/p" /Data/logs/nginx/access.log | awk '{print $1}' | sort | uniq -c | sort -nr | head -15
st_time=`date "+%H:%M:%S"`
echo "Stop time is ${st_time}"
小鸟版:
#!/bin/bash
S_dir="/tmp/"
localfind(){
time=`date +%d\\\/%b\\\/%Y:%H:%M:%S -d -30minutes`
times=`date +%d\\\/%b\\\/%Y:%H:%M:%S`
sed -n "/${time}/,/${times}/p" ${S_dir}${name} |awk '{print $1}' | sort | uniq -c | sort -nr |head -50 >./temp.txt
while read num ip;do
from=`curl -s http://ip.cn/index.php?ip=${ip}|awk '{print $2,$3,$4,$5}'`
mess=`grep ${ip} ${S_dir}${name} |awk '{print $1,$7}'|sort |uniq -c | sort -nr |head -1`
echo " " ${num} ${ip} ${from} ${mess}
done<./temp.txt
}
select sel in "access" "error" "item" "sale" "search" "list"; do
case ${sel} in
access)
name="access.log"
echo ${name}
localfind
;;
itemapi)
name="error.log"
echo ${name}
localfind
;;
item)
name="item.log"
echo ${name}
localfind
;;
sale)
name="sale.log"
echo ${name}
localfind
;;
search)
name="search.log"
echo ${name}
localfind
;;
list)
name="list.log"
echo ${name}
localfind
esac
done
转载于:https://blog.51cto.com/syydrwyl/1561748