Zabbix监控Nginx状态

Zabbix监控Nginx状态,首先,在nginx的配置文件中添加如下一段代码

server

 listen localhost:80; 
 server_name localhost;
location /nginxstatus { 
 stub_status on; 
 access_log off; 
 allow 127.0.0.1;
 allow 192.168.1.165; 
 deny all; 
 } 
}
保存之后重启nginx


# Set Variables 


# Functions to return nginx stats
nginx=/usr/local/zabbix-2.4.4/scripts/nginx.txt
function active {
                curl "http://localhost/nginxstatus"&>$nginx && awk '/Active/{print $3}' $nginx
}


function reading {
                curl "http://localhost/nginxstatus"&>$nginx && awk '/Reading/{print $2}' $nginx
}

 


function writing {
                curl "http://localhost/nginxstatus"&>$nginx && awk '/Writing/{print $4}' $nginx
}


function waiting {
                curl "http://localhost/nginxstatus"&>$nginx && awk '/Waiting/{print $6}' $nginx
}


function accepts {
                curl "http://localhost/nginxstatus"&>$nginx && awk NR==6 $nginx| awk '{print $1}'
}


function handled {
                curl "http://localhost/nginxstatus"&>$nginx && awk NR==6 $nginx| awk '{print $2}'
}


function requests {
                curl "http://localhost/nginxstatus"&>$nginx && awk NR==6 $nginx| awk '{print $3}'
}


# Run the requested function 
case "$1" in
active)
        active
        ;;
reading)
        reading
        ;;
writing)
        writing
        ;;
waiting)
        waiting
        ;;
accepts)
        accepts
        ;;
handled)
        handled
        ;;
requests)
        requests
        ;;
*)
        echo "Usage: $0 {nginx_site_dicovery}"
                echo "Usage: $0 {active [host] | reading [host] | writing [host] | waiting [host] | accepts [host] | handled [host] | requests [host]}"
esac

然后修改该文件的属主和属组为zabbix,并且具有执行权限
chown zabbix.zabbix /usr/local/zabbix-2.4.4/scripts/nginx_status
chmod 755 /usr/local/zabbix-2.4.4/scripts/nginx_status
再创建一个nginx.txt的文件,将属组和属主定为zabbix,具有读写权限
然后在zabbix_agentd.conf配置文件中添加如下代码
#monitor nginx 
UserParameter=nginx.accepts,/usr/local/zabbix-2.4.4/scripts/nginx_status accepts 
UserParameter=nginx.handled,/usr/local/zabbix-2.4.4/scripts/nginx_status handled 
UserParameter=nginx.requests,/usr/local/zabbix-2.4.4/scripts/nginx_status requests 
UserParameter=nginx.connections.active,/usr/local/zabbix-2.4.4/scripts/nginx_status active 
UserParameter=nginx.connections.reading,/usr/local/zabbix-2.4.4/scripts/nginx_status reading 
UserParameter=nginx.connections.writing,/usr/local/zabbix-2.4.4/scripts/nginx_status writing 
UserParameter=nginx.connections.waiting,/usr/local/zabbix-2.4.4/scripts/nginx_status waiting
这段代码可以视自己目录情况而改变


创建好了之后就可以在web页面配置item监控项了

一些Zabbix相关教程集合

ZABBIX 的详细介绍:请点这里
ZABBIX 的下载地址:请点这里

相关推荐