#!/bin/bash
#获取时间
day=`date -d '-1 day ' +%Y%m%d`
excepttime=`date -d '-1 day ' +%Y-%m-%d`
echo "" > /root/tmptxt/time.txt
echo "" > /root/tmptxt/accesslist.txt
#获取接口
for project in NAME
do
#排除0-7点
cat "/data/logs/$project/$day.log" | grep -v "$excepttime 0[0-7]" > /root/tmptxt/except.txt
cat /root/tmptxt/except.txt | grep response | awk -F \:\: '{ print $8}' | awk -F \? '{printf $1 "\n"}' | awk -F \& '{printf $1 "\n"}'| sort | uniq | grep -v ReadJpush >> /root/tmptxt/accesslist.txt
for access in `cat /root/tmptxt/accesslist.txt | grep -v test`
do
curtime=`cat /root/tmptxt/except.txt | grep response | grep $access | awk -F \:\: '{printf $7 " " $1 "\n"}' | awk '{printf $1 " " $2 "\n"}' | grep -v \- | awk 'BEGIN{sum=0;num=0}{sum+=$1;num+=1}END{printf ("%.3f %d",sum/num,num)}'`
avgtime=`echo $curtime | awk '{printf $1}'`
num=`echo $curtime | awk '{printf $2}'`
idtime=`cat /root/tmptxt/except.txt | grep response | grep $access | awk -F \:\: '{printf $7 " " $1 "\n"}' | awk '{printf $1 " " $2 "\n"}' | grep -v \- | sort -nr | head -1| awk '{printf $2" " $1}'`
id=`echo $idtime | awk '{printf $1}'`
longtime=`echo $idtime | awk '{printf $2}'`
#只取大于0.5秒的
if [[ `expr $avgtime \> 0.5` -eq 1 ]]
then
echo "$avgtime $longtime $num $access $id" >> /root/tmptxt/time.txt
fi
done
echo "平均耗时 最长耗时 次数 接口地址 最长耗时接口ID" > /root/tmptxt/result.txt
cat /root/tmptxt/time.txt | sort -nr >> /root/tmptxt/result.txt
done
shell脚本-通过分析日志统计php接口耗时
最新推荐文章于 2024-06-19 17:58:05 发布