需求場景
lnmp環境 3臺nginx 每臺nginx服務上有幾個虛擬主機,分別跑了不同的域名,準備對主站做nginx
status 狀態做監控,監控每一臺nginx的活動鏈接數,和總的活動連接數。
解決方案1
每臺nginx server的主配置文件 都配置status模塊 代碼端如下
location /ngst{ stub_status on; access_log off; allow 127.0.0.1; allow xxx.xxx.xxx.xxx; deny all; }
引用網上文檔,是一種解決方案。
缺點:每一個zabbix agent 都需要配置/etc/zabbix/zabbix_agentd.conf 並重新啓動zabbix客戶端 ,繁瑣。
2、nginx-status的取值腳本
#!/bin/bash #HOST=`ifconfig eth0 | sed -n '/inet /{s/.*addr://;s/ .*//;p}'` HOST="192.168.103.2" PORT="8888" function active { /usr/bin/curl "http://$HOST:$PORT/nginx-status/" 2>/dev/null| grep 'Active' | awk '{print $NF}' } function reading { /usr/bin/curl "http://$HOST:$PORT/nginx-status/" 2>/dev/null| grep 'Reading' | awk '{print $2}' } function writing { /usr/bin/curl "http://$HOST:$PORT/nginx-status/" 2>/dev/null| grep 'Writing' | awk '{print $4}' } function waiting { /usr/bin/curl "http://$HOST:$PORT/nginx-status/" 2>/dev/null| grep 'Waiting' | awk '{print $6}' } function accepts { /usr/bin/curl "http://$HOST:$PORT/nginx-status/" 2>/dev/null| awk NR==3 | awk '{print $1}' } function handled { /usr/bin/curl "http://$HOST:$PORT/nginx-status/" 2>/dev/null| awk NR==3 | awk '{print $2}' } function requests { /usr/bin/curl "http://$HOST:$PORT/nginx-status/" 2>/dev/null| awk NR==3 | awk '{print $3}' } # Run the requested function $1
HOST和PORT,是監控nginx對應的服務器IP和端口可以直接用命令測試一下
/usr/bin/curl "http://192.168.103.2:8888/nginx-status/"
Active connections: 2
server accepts handled requests
1968 1968 3907
Reading: 0 Writing: 1 Waiting: 1
3、修改zabbix_agentd.conf的配置
/etc/zabbix/zabbix_agentd.conf UserParameter=nginx.accepts,/home/zabbix/nginx-status.sh accepts UserParameter=nginx.handled,/home/zabbix/nginx-status.sh handled UserParameter=nginx.requests,/home/zabbix/nginx-status.sh requests UserParameter=nginx.connections.active,/home/zabbix/nginx-status.sh active UserParameter=nginx.connections.reading,/home/zabbix/nginx-status.sh reading UserParameter=nginx.connections.writing,/home/zabbix/nginx-status.sh writing UserParameter=nginx.connections.waiting,/home/zabbix/nginx-status.sh waiting
解決方案2
1.每臺nginx server的主配置文件 都配置status模塊
2.在一臺nginx server 獲取到nginx status的值,寫到本地一個臨時文件
3.按需求處理臨時文件,使用UserParameter=nginx_status[*] 自定義key值的方式,
4.crontab 每分鐘獲取一次nginx status值
優點,不比每臺zabbix 客戶端都配置 /etc/zabbix/zabbix_agentd.conf 大大節約時間,並減少錯誤
率
缺點,一分鐘獲取一次,有稍稍不妥,但是時間還是看監控需求,腳本開發需要時間,(其實腳本很
簡單)
具體實現方法,腳本思路及解決方案2 不可直接拿來使用,根據實際環境編寫,簡單的小腳本。此處
,此服務器可以直接使用hostname 訪問,所以我使用了web1 web2。
#!/bin/bash rm -rf /tmp/ngst/* function GETSTATUS { for ip in {1..3} do curl http://web$ip:8080/ngst >/tmp/ngst/web$ip 2>/dev/null done } function HANDLE { for i in {1..3} do cat /tmp/ngst/web$i |grep Active |awk '{print $1,$NF}' >>/tmp/ngst/web${i}_status cat /tmp/ngst/web$i |grep Reading |awk '{print $1,$2}' >>/tmp/ngst/web${i}_status cat /tmp/ngst/web$i |grep Writing |awk '{print $3,$4}' >>/tmp/ngst/web${i}_status cat /tmp/ngst/web$i |grep Waiting |awk '{print $5,$6}' >>/tmp/ngst/web${i}_status #mv /tmp/ngst/web$i.new /tmp/ngst/web$i done } function TOTAL{ cat /tmp/ngst/*status |grep Active |awk '{total+=$NF}END{print total}'>/tmp/ngst/total # 總的 Active 連接數 } GETSTATUS HANDLE TOTAL
執行後結果如下
#ls /tmp/ngst/
total web1 web1_status web2 web2_status web3 web3_status
#cat /tmp/ngst/web1
Active connections: 1
server accepts handled requests
7716005 7716005 7354221
Reading: 0 Writing: 1 Waiting: 0
# cat /tmp/ngst/web1_status 處理後的文件
Active 1
Reading: 0
Writing: 1
Waiting: 0
設置zabbix agent端 只需修改配置文件,添加如下幾行,默認zabbix自定義key以知曉。
UserParameter=nginx_status.total, cat /tmp/ngst/total UserParameter=nginx_status[*], cat /tmp/ngst/web1 |grep "$1"|awk '{print $NF}' UserParameter=nginx2_status[*], cat /tmp/ngst/web2 |grep "$1"|awk '{print $NF}' UserParameter=nginx3_status[*], cat /tmp/ngst/web3 |grep "$1"|awk '{print $NF}' #只需要寫這麼幾行就能實現對 Active Reading 等或者其他值得監控。
此時在zabbix server端添加下列監控。
1,創建模板,默認都會
2,創建監控項
解決方案2 爲自己搗鼓,有很多不足之處,大神有更好的解決方案,請分享。