nginx的stub_status模块详解


nginx的stub_status模块


该模块用于实时监控nginx的网络连接,这个模块是nginx官方提供的一个模块。这个模块提供的很多数据对于我们定位性能问题很有帮助。(reload的时候master进程没有改变,程序文件程序代码也没有改变,所以是不会导致数据清零的。热升级master进程也改掉了,数据清零)

 

nginx sub_status模块监控nginx状态


accept只要我的worker进程被唤醒 去建立一个新的连接,我的accept就会加1,默认情况下,没有超出如果worker_connections的配置,该值和accepts的值相同。但是如果handle的值<accept的值,那么肯定是因为你的worker_connection值配置小了。

因为很多请求时keepalived的,即长连接请求,长连接只accept一次,只handle一次,但是可能传了很多请求。默认配置的时keepalive_requests 是100,那么最多可以传入100个请求。所以requests的值一定会大于等于handled。

  1.  
    location =/status {
  2.  
    stub_status;
  3.  
    }
  4.  
     
  5.  
    [ root@localhost ~]# curl 192.168.179.104/status 2>/dev/null
  6.  
    Active connections: 1
  7.  
    server accepts handled requests
  8.  
    21 21 21
  9.  
    Reading: 0 Writing: 1 Waiting: 0
  10.  
     
  11.  
    Active connections: 活跃的连接数量
  12.  
     
  13.  
    Server accepts handled requests: Nginx总共处理了21个连接,成功创建21次握手(证明中间没有失败的),总共处理了21个请求.
  14.  
     
  15.  
    Reading: Nginx 读取到客户端的Header信息数.
  16.  
    Writing: Nginx 返回给客户端的Header信息数.
  17.  
     
  18.  
    Waiting: 开启keep-alive的情况下,这个值等于 active (reading + writing),意思就是Nginx已经处理完成,正在等候下一次请求指令的驻留连接.
  19.  
    所以,在访问效率高,请求很快被处理完毕的情况下,Waiting数比较多是正常的.如果reading +writing数较多,则说明并发访问量非常大,正在处理过程中.

 

使用zabbix来监控nginx的stub_status模块给我们提供的信息(不要监控太频繁,定时或者几个小时监控一次,监控太频繁会给nginx带来压力)


  1.  
    [root @localhost sh]# chmod o+x nginx_status.sh
  2.  
    [root @localhost sh]# chmod u+s nginx_status.sh
  3.  
     
  4.  
    #这个脚本是让zabbix来调用获取到nginx状态
  5.  
    [root @localhost sh]# cat nginx_status.sh
  6.  
    #! /bin/bash
  7.  
    function active(){
  8.  
    curl 192.168.179.104/status 2>/dev/null | awk 'NR==1 {print $NF}'
  9.  
    }
  10.  
     
  11.  
    function accepts(){
  12.  
    curl 192.168.179.104/status 2>/dev/null | awk 'NR==3 {print $1}'
  13.  
    }
  14.  
     
  15.  
    function handled(){
  16.  
    curl 192.168.179.104/status 2>/dev/null | awk 'NR==3 {print $2}'
  17.  
    }
  18.  
     
  19.  
    function requests(){
  20.  
    curl 192.168.179.104/status 2>/dev/null | awk 'NR==3 {print $3}'
  21.  
    }
  22.  
     
  23.  
    function reading(){
  24.  
    curl 192.168.179.104/status 2>/dev/null | awk 'NR==4{print $2}'
  25.  
    }
  26.  
     
  27.  
    function writing(){
  28.  
    curl 192.168.179.104/status 2>/dev/null | awk 'NR==4 {print $4}'
  29.  
    }
  30.  
     
  31.  
    function waiting(){
  32.  
    curl 192.168.179.104/status 2>/dev/null | awk 'NR==4 {print $NF}'
  33.  
    }
  34.  
     
  35.  
    $ 1
  36.  
     
  37.  
     
  38.  
    #zabbix agent自定义key值,通过上面的脚本来获取到nginx的状态
  39.  
    [root @localhost ~]# vim /etc/zabbix_agentd.conf
  40.  
    UserParameter=nginx.status[*], /usr/bin/bash /data/sh/nginx_status.sh $1
  41.  
    [root @localhost ~]# systemctl restart zabbix-agent
  42.  
     
  43.  
     
  44.  
    #zabbix-agent端执行脚本
  45.  
    [root @localhost sh]# bash nginx_status.sh accepts
  46.  
    19
  47.  
    [root @localhost sh]# bash nginx_status.sh handled
  48.  
    20
  49.  
     
  50.  
     
  51.  
    #zabbix-server 端获取key值
  52.  
    [root @localhost ~]# zabbix_get -s 192.168.179.104 -k nginx.status[requests]
  53.  
    26
  54.  
    [root @localhost ~]# zabbix_get -s 192.168.179.104 -k nginx.status[active]
  55.  
    1

 

在zabbix里面配置以上自定义的item监控项 


添加key为nginx.status[waiting] ,这里的update interval可以设置常一点,不要频繁的访问nginx拿key值

 添加key为nginx.status[requests] 

 添加key为nginx.status[active] 

剩下的照葫芦画瓢,不一一展示 

 

 

创建graph将上面item图像合并在一起


 

 

  1.  
    #如果你希望你的脚本美观一些不是那么多函数可以如下,一次性获取所有的值:
  2.  
    #!/bin/sh
  3.  
    values=`curl 192.168.179.104/status 2>/dev/null | awk 'NR==1 {print $NF};NR==3 {print $1,$2,$3};NR==4 {print $2,$4,$NF}'`
  4.  
     
  5.  
    case $1 in
  6.  
    active)
  7.  
    echo `echo $values | awk '{print $1}'`
  8.  
    ;;
  9.  
    accept)
  10.  
    echo `echo $values | awk '{print $2}'`
  11.  
    ;;
  12.  
    handled)
  13.  
    echo `echo $values | awk '{print $3}'`
  14.  
    ;;
  15.  
    esac
  16.  
     
  17.  
     
  18.  
     
  19.  
    [root@localhost sh] # ./nginx_status.sh active
  20.  
    1
  21.  
    [root@localhost sh] # ./nginx_status.sh accept
  22.  
    1155


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM