1、编辑nginx的配置文件,在server下添加如下内容:
location /status {
stub_status on;
access_log off;
allow 127.0.0.1;
deny all;
}
保存退出并重启nginx
2、创建监控nginx的脚本目录
mkdir /usr/local/zabbix/libexec
3、vim /usr/local/zabbix/libexec/nginx.sh && chmod +x /usr/local/zabbix/libexec/nginx.sh
#!/bin/bash
##################################################
# Description:zabbix 通过 status 模块监控 nginx
# Note:Zabbix 3.2
# DateTime: 2016-11-22
##################################################
HOST="localhost"
PORT="80"
function ping {
/sbin/pidof nginx | wc -l
}
function active() {
/usr/bin/curl -s "http://$HOST:$PORT/status/" 2>/dev/null| grep ‘Active‘ | awk ‘{print $NF}‘
}
function accepts() {
/usr/bin/curl -s "http://$HOST:$PORT/status/" 2>/dev/null| awk NR==3 | awk ‘{print $1}‘
}
function handled() {
/usr/bin/curl -s "http://$HOST:$PORT/status/" 2>/dev/null| awk NR==3 | awk ‘{print $2}‘
}
function requests() {
/usr/bin/curl -s "http://$HOST:$PORT/status/" 2>/dev/null| awk NR==3 | awk ‘{print $3}‘
}
function reading() {
/usr/bin/curl -s "http://$HOST:$PORT/status/" 2>/dev/null| grep ‘Reading‘ | awk ‘{print $2}‘
}
function writing() {
/usr/bin/curl -s "http://$HOST:$PORT/status/" 2>/dev/null| grep ‘Writing‘ | awk ‘{print $4}‘
}
function waiting() {
/usr/bin/curl -s "http://$HOST:$PORT/status/" 2>/dev/null| grep ‘Waiting‘ | awk ‘{print $6}‘
}
case "$1" in
active)
active
;;
accepts)
accepts
;;
handled)
handled
;;
requests)
requests
;;
reading)
reading
;;
writing)
writing
;;
waiting)
waiting
;;
*)
echo $"Usage $0 {active|accepts|handled|requests|reading|writing|waiting}"
exit
esac
4、创建zabbix_agentd 监控的key
vim /usr/local/zabbix/etc/zabbix_agentd.conf.d/userparameter_nginx.conf
UserParameter=nginx.status[*],/usr/local/zabbix/libexec/nginx.sh $1
重启zabbix_agentd
5、测试脚本及key
[[email protected] ~]# vim /usr/local/zabbix/etc/zabbix_agentd.conf.d/userparameter_nginx.conf
[[email protected] ~]# /usr/local/zabbix/libexec/nginx.sh -h
Usage /usr/local/zabbix/libexec/nginx.sh {active|accepts|handled|requests|reading|writing|waiting}
[[email protected] ~]# /usr/local/zabbix/libexec/nginx.sh accepts
2016
[[email protected] ~]# /usr/local/zabbix/bin/zabbix_get -s 127.0.0.1 -k ‘nginx.status[accepts]‘
965
6、导入模板以及管理主机
模板:nginx.xml
7、验证