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