1. 程式人生 > >Zabbix監控nginx併發

Zabbix監控nginx併發

NGINX配置

配置nginx的status模組(這個需要在編譯nginx時候就加上--with-http_stub_status_module)

//根據實際需求配置
    server {
        listen       8000;
        server_name  localhost;

        location /NginxStatus {
        stub_status on;
        access_log off;
        }

測試

# /usr/bin/curl  "http://localhost:8000/NginxStatus"
Active connections: 2 
server accepts handled requests
 2434 2434 3243 
Reading: 0 Writing: 1 Waiting: 1

Zabbix基本配置

只在agent端配置
# vim /usr/local/zabbix/etc/zabbix_agentd.conf
//agent使用指令碼需要將此項開啟
UnsafeUserParameters=1
//指定agent配置檔案位置
Include=/usr/local/zabbix/etc/zabbix_agentd.conf.d/*.conf

# vim /usr/local/zabbix/etc/zabbix_agentd.conf.d/UserParameter_script.conf
//指定ngxin監控配置
#Nginx
UserParameter=nginx.status[*],/usr/local/zabbix/scripts/nginx_monitor.sh $1

Nginx監控指令碼

# cat /usr/local/zabbix/scripts/nginx_monitor.sh

#!/bin/bash

HOST="localhost"
PORT="8000"
NGINX_COMMAND=$1

# 檢測nginx程序是否存在
function nginx_process() {
    /usr/bin/pgrep nginx | wc -l
}
# 檢測nginx效能
function nginx_active() {
    /usr/bin/curl "http://$HOST:$PORT/NginxStatus/" 2>/dev/null| awk '/Active/ {print $NF}'
}
function nginx_reading() {
    /usr/bin/curl "http://$HOST:$PORT/NginxStatus/" 2>/dev/null| awk '/Active/ {print $NF}'
}
function nginx_writing() {
    /usr/bin/curl "http://$HOST:$PORT/NginxStatus/" 2>/dev/null| awk '/Active/ {print $NF}'
}
function nginx_waiting() {
    /usr/bin/curl "http://$HOST:$PORT/NginxStatus/" 2>/dev/null| awk '/Active/ {print $NF}'
}
function nginx_accepts() {
    /usr/bin/curl "http://$HOST:$PORT/NginxStatus/" 2>/dev/null| awk NR==3 | awk '{print $1}'
}
function nginx_handled() {
    /usr/bin/curl "http://$HOST:$PORT/NginxStatus/" 2>/dev/null| awk NR==3 | awk '{print $2}'
}
function nginx_requests() {
    /usr/bin/curl "http://$HOST:$PORT/NginxStatus/" 2>/dev/null| awk NR==3 | awk '{print $3}'
}

# 執行function
#${NGINX_COMMAND}
case $NGINX_COMMAND in
	process)
	nginx_process;
	;;
	active)
	nginx_active;
	;;
	reading)
	nginx_reading;
	;;
	writing)
	nginx_writing;
	;;
	waiting)
	nginx_waiting;
	;;
	accepts)
	nginx_accepts;
	;;
	handled)
	nginx_handled;
	;;
	requests)
	nginx_requests;
	;;
      	*)
	echo $"USAGE:$0 {process|active|reading|writing|waiting|accepts|handled|requests}"
    	esac

重啟agent並測試

Agent端重啟zabbix_agent
# /etc/init.d/zabbix_agentd restart

Sserver端測試(ip填寫agent的IP)
# ./zabbix_get -s 172.16.28.118 -k  nginx.status[process]
4

# ./zabbix_get -s 172.16.28.118 -k  nginx.status[active]
3

web頁面配置

流程:

建立監控模板(Template App Nginx)-->建立應用集(NginxStatus)-->建立監控項(指令碼中的8個)-->建立圖形

 

建立模板

建立應用集

建立監控項(舉例一項,其餘相同)

建立圖形

全部建立好後,被監控主機就可以呼叫nginx監控模板,來監控nginx各項資料了