• 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:                              "33[32m [  OK  ] 33[0m"
    while [ 2 > 1 ]
    do
    check
    
    done &
    pid=`ps ax | grep -i 'nginxstatus' | head -n 1|awk '{print $1}'`
    echo -e  pid is  "33[33m  $! 33[0m" 
    echo $! >> /var/run/nginxstatus/nginxstatus.pid
    }
    #stop nginx 
    stop() {
    echo -e stop nginxstatus collect  "33[32m  [  OK  ] 33[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" "33[32m  [  OK  ] 33[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日志的内容重新统计

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

  • 相关阅读:
    Sublime Text 3 支持的热门插件推荐
    Sublime text 2/3 中 Package Control 的安装与使用方法
    UML类图中箭头和线条的含义和用法
    this guy gonna be a daddy
    PHP设计模式之:单例模式
    PHP FTP操作类( 上传、拷贝、移动、删除文件/创建目录 )
    php memcache 基础操作
    获取IP地址方法
    短信发送
    使用Shell脚本对Linux系统和进程资源进行监控
  • 原文地址:https://www.cnblogs.com/zphj1987/p/13575331.html
Copyright © 2020-2023  润新知