nginx状态监控统计

nginx是一款很优秀的web服务器软件,很多地方都有接触和使用到他,大部分的场景压力还没达到需要调优的地步,而调优的难点其实不在于调,而在于各项状态的监控,能够很快的找到资源在什么时候出现问题,调整前后出现的变化,如果都不知道变化在哪里所做的调优只能是凭感觉的

之前看到有技术人员用nginx作为rgw的前端的时候,通过优化去实现将nginx的并发提高到很大,而不出现4xx等问题,nginx的access.log里面是有记录访问的状态码的,而这个日志的分析如果是一次次的去看,这样的分析是无法用精确的数据去展示的

最开始的想法是想根据时间点去统计时间点的状态码,后来发现这样做既复杂,又无法输出到一些数据展示软件当中,实际上我只需要统计一定时间的总的状态值,然后定期去取这个值,然后在数据展示的时候,就可以看到一个数值的曲线图,增量即为这个时间区间所产生的状态值

下面就是我的实现,一个脚本就可以统计了,这个是最初的版本,纯统计状态码,还没有区分读写分离的情况,这个在后面会加入分离的情况

#!/bin/sh
#
### BEGIN INIT INFO
# Provides:          nginxstatus
# Required-Start:    $nginx
# Short-Description: nginxstatus
# Description: collectstatus of nginx
### END INIT INFO
#
#
# pidfile: /var/run/nginx/nginxstatus.pid
#
# Source function library.
##########################################
#状态码一般分为1xx,2xx,3xx,4xx,5xx,total
statucode="2 3 4 5"
##check intervel setting 
interval=2
########################################
#check the nginxstatus pid dir if exist
if [ ! -d /var/run/nginxstatus/ ];then
mkdir  /var/run/nginxstatus/
fi
##check the status of nginx access 
check(){
for code in $statucode
do
echo "$code"xx:`cat /var/log/nginx/access.log |awk '{if( substr($9,0,1) == '''$code''' )  print $9}'  |wc -l` > /var/log/nginx/"$code"xx.log
done
sleep $interval
}
#start nginx status
start() {
echo -e Starting nginxstatus:                              "\033[32m [  OK  ] \033[0m"
while [ 2 > 1 ]
do
check

done &
pid=`ps ax | grep -i 'nginxstatus' | head -n 1|awk '{print $1}'`
echo -e  pid is  "\033[33m  $! \033[0m" 
echo $! >> /var/run/nginxstatus/nginxstatus.pid
}
#stop nginx 
stop() {
echo -e stop nginxstatus collect  "\033[32m  [  OK  ] \033[0m" 

pid=`cat /var/run/nginxstatus/nginxstatus.pid  2>/dev/null`
kill -10 $pid  2>/dev/null
#killall nginxstatus
rm -rf /var/run/nginxstatus/nginxstatus.pid
}


status() {
for code in $statucode
do
cat /var/log/nginx/"$code"xx.log
done
}

clean () {
for code in $statucode
do
echo "" >  /var/log/nginx/"$code"xx.log
done
echo "" > /var/log/nginx/access.log
echo "clean /var/log/nginx/access.log"
echo -e "clean /var/log/nginx/access.log" "\033[32m  [  OK  ] \033[0m"
}

case "$1" in
        start)
                start  && exit 0
                ;;
        stop)
                stop || exit 0
                ;;
        status)
                status
                ;;
        clean)
                clean
                ;;
        *)
                echo $"Usage: $0 {start|stop|status|clean}"
                exit 2
esac
exit $?

使用方法:

1、启动进程

[root@zhongbo ~]# /etc/init.d/nginxstatus start
Starting nginxstatus:  [  OK  ] 
pid is   166534

会生成下面的状态文件,周期为2s一更新
[root@zhongbo ~]# ll /var/log/nginx/*xx.log
-rw-r--r-- 1 root root 7 Feb 23 00:25 /var/log/nginx/2xx.log
-rw-r--r-- 1 root root 6 Feb 23 00:25 /var/log/nginx/3xx.log
-rw-r--r-- 1 root root 7 Feb 23 00:25 /var/log/nginx/4xx.log
-rw-r--r-- 1 root root 6 Feb 23 00:25 /var/log/nginx/5xx.log

2、当前nginx的状态查询

[root@zhongbo ~]# /etc/init.d/nginxstatus status
2xx:21
3xx:1
4xx:10
5xx:0

3、停止nginxstatus进程

[root@zhongbo ~]# /etc/init.d/nginxstatus stop
stop nginxstatus collect   [  OK  ]

4、清理历史数据

[root@zhongbo ~]# /etc/init.d/nginxstatus clean
clean /var/log/nginx/access.log   [  OK  ]

这个操作会清空/var/log/nginx/access.log日志的内容重新统计

这个会在后期根据需求进行优化

posted @ 2016-02-22 16:15  武汉-磨渣  阅读(179)  评论(0编辑  收藏  举报