nginx并发连接数和响应时间抓取shell笨拙版

并发连接数和响应时间抓取想法
1.数据图使用cacti for nginx插件进行记录
2.nginx编译stub_status模块,log记录添加[$time_local]和$request_time字段
3.并发连接数定期curl status页面获得
4.响应时间使用本地时间戳做条件在日志中匹配得到(未改造nginx记录日期格式)

本地抓取shell笨拙基础版
1.根据时间戳抓取响应时间和并发连接数做join
2.nginxstatus current显示当前的并发连接数,响应时间,一秒钟内的响应时间均值和最大值nginxstatus 3.detail显示过去24小时内的每一分钟内的并发连接数和响应时间均值
4.nginxstatus crond每秒钟记录一次并发连接数
5.nginxstatus help
#!/bin/bash
####nginxstaus.sh####
QPS=`curl -s status.test.com |awk '{ if( $1~/Active/ ) print $NF}'`
DATE=`date +"%d/%b/%Y:%T"`
LAST_DATE=`date -d '-1 day' +"%d/%b/%Y:%T"`
LOG="/var/nginxlog/access.log"
LOG1=/tmp/logs
AVG_RT=`grep $DATE $LOG |awk -F "-" '{ total+=$5 }END{ if( $total!=0 ) print total/NR }'`
MAX_RT=`grep $DATE $LOG |awk -F "-" '{ print $5 }' |sort -nr |head -1`
DAYS_AVG=`sed -n "\#'$LAST_DATE'#,\#'$DATE'#p"  $LOG |awk -F "-" '{ total+=$5 } END{ if( $total!=0 ) print "avg_rt = ", total/NR }'`
QPS_AVG=`sed -n "\#'$LAST_DATE'#,\#'$DATE'#p"  $LOG1 |awk '{ total+=$2 } END{ if( $total!=0 ) print "qps_rt = ", total/NR }'`
DAYS_MAX=`sed -n "\#'$LAST_DATE'#,\#'$DATE'#p"  $LOG |awk -F "-" '{ print $5 }' | sort -nr | head -1`
QPS_MAX=`sed -n "\#'$LAST_DATE'#,\#'$DATE'#p"  $LOG1 |awk '{ print $2 }' | sort -nr | head -1`
case $1 in
current )
echo "$DATE qps:$QPS avg_rt:$AVG_RT max_rt:$MAX_RT"
;;
detail )
echo -e "time   rt  qps\n----------------"
join <( sed -n "\#'$LAST_DATE'#,\#'$DATE'#p"  $LOG |awk -F '[[ :]+' '{k=$4":"$5":"$6;a[k]+=$NF;b[k]++}END{for(i in a)if( $a[i]!=0 ) print i,a[i]/b[i]}' |sort -t : -k1n ) <( sed -n "\#'$LAST_DATE'#,\#'$DATE'#p"  $LOG1 |awk -F "/|:| " '{k=$3":"$4":"$5;a[k]+=$NF;b[k]++}END{for(i in a)if( $a[i]!=0 ) print i,a[i]/b[i]}' |sort -t : -k1n )
echo -e "\n----------------\n$DAYS_AVG $DAYS_MAX $QPS_AVG $QPS_MAX"
;;
crond )
echo "$DATE $QPS" >>$LOG1
;;
help )
echo "useage current | detail | blank | help |crond"
;;
esac

 

你可能感兴趣的:(nginx,shell,awk,休闲,美女)