1. 程式人生 > >zabbix 監控nginx狀態

zabbix 監控nginx狀態

1. 安裝nginx,參考其它

[root@iZ2zeapnvuohe8p14289u6Z /]# vim /etc/nginx/nginx.conf
# 加上下面
 location /nginx_status {
           stub_status;
}  
[root@iZ2zeapnvuohe8p14289u6Z /]# systemctl start nginx.service
# 效果如下

    

  2. 匯入模板

3. 匯入結果

4. 準備zabbix的配置檔案

 

[root@iZ2zeapnvuohe8p14289u6Z /]# cd /etc/zabbix/zabbix_agentd.d

# nginx_status.conf檔案上傳至此

[root@iZ2zeapnvuohe8p14289u6Z /]# cat nginx_status.conf

5. 把配置檔案移到到上面指定的目錄

[root@iZ2zeapnvuohe8p14289u6Z /]# mkdir /etc/zabbix/scripts
# nginx_status.sh檔案上傳至此
[root@iZ2zeapnvuohe8p14289u6Z /]# cat nginx_status.sh

 

NGINX_PORT=9088  #如果埠不同僅需要修改指令碼即可,否則修改xml很麻煩
NGINX_COMMAND=$1
nginx_active(){
    /usr/bin/curl -s "http://127.0.0.1:"$NGINX_PORT"/nginx_status/" |awk '/Active/ {print $NF}'
}
nginx_reading(){
    /usr/bin/curl -s "http://127.0.0.1:"$NGINX_PORT"/nginx_status/" |awk '/Reading/ {print $2}'
}
nginx_writing(){
    /usr/bin/curl -s "http://127.0.0.1:"$NGINX_PORT"/nginx_status/" |awk '/Writing/ {print $4}'
       }
nginx_waiting(){
    /usr/bin/curl -s "http://127.0.0.1:"$NGINX_PORT"/nginx_status/" |awk '/Waiting/ {print $6}'
       }
nginx_accepts(){
    /usr/bin/curl -s "http://127.0.0.1:"$NGINX_PORT"/nginx_status/" |awk 'NR==3 {print $1}'
       }
nginx_handled(){
    /usr/bin/curl -s "http://127.0.0.1:"$NGINX_PORT"/nginx_status/" |awk 'NR==3 {print $2}'
       }
nginx_requests(){
    /usr/bin/curl -s "http://127.0.0.1:"$NGINX_PORT"/nginx_status/" |awk 'NR==3 {print $3}'
       }
  case $NGINX_COMMAND in
	active)
		nginx_active;
		;;
	reading)
		nginx_reading;
		;;
	writing)
		nginx_writing;
		;;
	waiting)
		nginx_waiting;
		;;
	accepts)
		nginx_accepts;
		;;
	handled)
		nginx_handled;
		;;
	requests)
		nginx_requests;
		;;
	      *)
		echo $"USAGE:$0 {active|reading|writing|waiting|accepts|handled|requests}"
    esac  

6. 給指令碼新增執行許可權

[root@iZ2zeapnvuohe8p14289u6Z /]# chmod +x nginx_status.sh

 

7. 重啟zabbix-agent

[root@iZ2zeapnvuohe8p14289u6Z /]# systemctl restart zabbix-agent 

 

8. 測試zabbix-get 獲取值

[root@iZ2zeapnvuohe8p14289u6Z /]# zabbix-get -s ip -k nginx-status[Active]

9. 顯示效果如下 

 

 

 

 

&n