nginx的stub_status模塊
location =/status {
stub_status;
}
[root@localhost ~]# curl 192.168.179.104/status 2>/dev/null
Active connections: 1
server accepts handled requests
21 21 21
Reading: 0 Writing: 1 Waiting: 0
Active connections: 活躍的連接數量
Server accepts handled requests: Nginx總共處理了21個連接,成功創建21次握手(證明中間沒有失敗的),總共處理了21個請求.
Reading: Nginx 讀取到客戶端的Header信息數.
Writing: Nginx 返回給客戶端的Header信息數.
Waiting: 開啓keep-alive的情況下,這個值等於 active – (reading + writing),意思就是Nginx已經處理完成,正在等候下一次請求指令的駐留連接.
所以,在訪問效率高,請求很快被處理完畢的情況下,Waiting數比較多是正常的.如果reading +writing數較多,則說明併發訪問量非常大,正在處理過程中.
使用zabbix來監控nginx的stub_status模塊給我們提供的信息(不要監控太頻繁,定時或者幾個小時監控一次,監控太頻繁會給nginx帶來壓力)
[root@localhost sh]# chmod o+x nginx_status.sh
[root@localhost sh]# chmod u+s nginx_status.sh
#這個腳本是讓zabbix來調用獲取到nginx狀態
[root@localhost sh]# cat nginx_status.sh
#!/bin/bash
function active(){
curl 192.168.179.104/status 2>/dev/null | awk 'NR==1 {print $NF}'
}
function accepts(){
curl 192.168.179.104/status 2>/dev/null | awk 'NR==3 {print $1}'
}
function handled(){
curl 192.168.179.104/status 2>/dev/null | awk 'NR==3 {print $2}'
}
function requests(){
curl 192.168.179.104/status 2>/dev/null | awk 'NR==3 {print $3}'
}
function reading(){
curl 192.168.179.104/status 2>/dev/null | awk 'NR==4{print $2}'
}
function writing(){
curl 192.168.179.104/status 2>/dev/null | awk 'NR==4 {print $4}'
}
function waiting(){
curl 192.168.179.104/status 2>/dev/null | awk 'NR==4 {print $NF}'
}
$1
#zabbix agent自定義key值,通過上面的腳本來獲取到nginx的狀態
[root@localhost ~]# vim /etc/zabbix_agentd.conf
UserParameter=nginx.status[*],/usr/bin/bash /data/sh/nginx_status.sh $1
[root@localhost ~]# systemctl restart zabbix-agent
#zabbix-agent端執行腳本
[root@localhost sh]# bash nginx_status.sh accepts
19
[root@localhost sh]# bash nginx_status.sh handled
20
#zabbix-server 端獲取key值
[root@localhost ~]# zabbix_get -s 192.168.179.104 -k nginx.status[requests]
26
[root@localhost ~]# zabbix_get -s 192.168.179.104 -k nginx.status[active]
1
在zabbix裏面配置以上自定義的item監控項
添加key爲nginx.status[waiting] ,這裏的update interval可以設置常一點,不要頻繁的訪問nginx拿key值
添加key爲nginx.status[requests]
添加key爲nginx.status[active]
剩下的照葫蘆畫瓢,不一一展示
創建graph將上面item圖像合併在一起
#如果你希望你的腳本美觀一些不是那麼多函數可以如下,一次性獲取所有的值:
#!/bin/sh
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}'`
case $1 in
active)
echo `echo $values | awk '{print $1}'`
;;
accept)
echo `echo $values | awk '{print $2}'`
;;
handled)
echo `echo $values | awk '{print $3}'`
;;
esac
[root@localhost sh]# ./nginx_status.sh active
1
[root@localhost sh]# ./nginx_status.sh accept
1155