環(huán)境
[root@centos181001 ~]# cat /etc/centos-release
CentOS Linux release 7.6.1810 (Core)
[root@centos181001 ~]# zabbix_server -V
zabbix_server (Zabbix) 4.0.5
Revision 90164 25 February 2019, compilation time: Feb 25 2019 09:41:55
Copyright (C) 2019 Zabbix SIA
License GPLv2+: GNU GPL version 2 or later <http://gnu.org/licenses/gpl.html>.
This is free software: you are free to change and redistribute it according to
the license. There is NO WARRANTY, to the extent permitted by law.
This product includes software developed by the OpenSSL Project
for use in the OpenSSL Toolkit (http://www.openssl.org/).
Compiled with OpenSSL 1.0.1e-fips 11 Feb 2013
Running with OpenSSL 1.0.1e-fips 11 Feb 2013
[root@centos181001 ~]# zabbix_agentd -V
zabbix_agentd (daemon) (Zabbix) 4.0.5
Revision 90164 25 February 2019, compilation time: Feb 25 2019 09:42:17
Copyright (C) 2019 Zabbix SIA
License GPLv2+: GNU GPL version 2 or later <http://gnu.org/licenses/gpl.html>.
This is free software: you are free to change and redistribute it according to
the license. There is NO WARRANTY, to the extent permitted by law.
This product includes software developed by the OpenSSL Project
for use in the OpenSSL Toolkit (http://www.openssl.org/).
Compiled with OpenSSL 1.0.1e-fips 11 Feb 2013
Running with OpenSSL 1.0.1e-fips 11 Feb 2013
第一步:Agent端設(shè)置
1.開啟Nginx狀態(tài)監(jiān)控
## 1.修改配置文件
location /nginx_status {
stub_status on;
access_log off;
allow 127.0.0.1;
deny all;
}
## 2.檢查配置文件
nginx -t
## 3.重載配置文件
nginx -s reload
## 4.測(cè)試狀態(tài)
curl http://127.0.0.1/stub_status
2.編輯監(jiān)控腳本
mkdir /etc/zabbix/scripts
cat >/etc/zabbix/scripts/nginx.sh <<\EOF
#!/bin/bash
##################################################
# AUTHOR: Neo <netkiller@msn.com>
# WEBSITE: http://www.netkiller.cn
# Description:zabbix 通過 status 模塊監(jiān)控 nginx
# Note:Zabbix 3.2
# DateTime: 2016-11-22
##################################################
HOST="127.0.0.1"
PORT="80"
nginx_status=nginx_status
function check() {
if [ -f /sbin/pidof ]; then
/sbin/pidof nginx | wc -w
else
ps ax | grep "nginx:" | grep -v grep | wc -l
fi
}
function active() {
/usr/bin/curl -s "http://$HOST:$PORT/${nginx_status}" 2>/dev/null| grep 'Active' | awk '{print $NF}'
}
function accepts() {
/usr/bin/curl -s "http://$HOST:$PORT/${nginx_status}" 2>/dev/null| awk NR==3 | awk '{print $1}'
}
function handled() {
/usr/bin/curl -s "http://$HOST:$PORT/${nginx_status}" 2>/dev/null| awk NR==3 | awk '{print $2}'
}
function requests() {
/usr/bin/curl -s "http://$HOST:$PORT/${nginx_status}" 2>/dev/null| awk NR==3 | awk '{print $3}'
}
function reading() {
/usr/bin/curl -s "http://$HOST:$PORT/${nginx_status}" 2>/dev/null| grep 'Reading' | awk '{print $2}'
}
function writing() {
/usr/bin/curl -s "http://$HOST:$PORT/${nginx_status}" 2>/dev/null| grep 'Writing' | awk '{print $4}'
}
function waiting() {
/usr/bin/curl -s "http://$HOST:$PORT/${nginx_status}" 2>/dev/null| grep 'Waiting' | awk '{print $6}'
}
function lastminuteaccepts() {
accepts=`/usr/bin/curl -s "http://$HOST:$PORT/${nginx_status}" 2>/dev/null| awk NR==3 | awk '{print $1}'`
lastaccepts=`cat /tmp/nginxaccepts.txt`
lastminuteaccepts=`expr $accepts - $lastaccepts`
echo $lastminuteaccepts
echo $accepts > /tmp/nginxaccepts.txt
}
case "$1" in
check)
check
;;
active)
active
;;
accepts)
accepts
;;
handled)
handled
;;
requests)
requests
;;
reading)
reading
;;
writing)
writing
;;
waiting)
waiting
;;
lastminuteaccepts)
lastminuteaccepts
;;
*)
echo $"Usage $0 {check|active|accepts|handled|requests|reading|writing|waiting|lastminuteaccepts}"
exit
esac
EOF
chmod +x /etc/zabbix/scripts/nginx.sh
cat /etc/zabbix/scripts/nginx.sh
3.編輯agent配置文件
cat >/etc/zabbix/zabbix_agentd.d/userparameter_nginx.conf <<\EOF
UserParameter=nginx.status[*],/etc/zabbix/scripts/nginx.sh $1
EOF
cat /etc/zabbix/zabbix_agentd.d/userparameter_nginx.conf
4.測(cè)試腳本
/etc/zabbix/scripts/nginx.sh
[root@centos181002 zabbix_agentd.d]# /etc/zabbix/scripts/nginx.sh
Usage /etc/zabbix/scripts/nginx.sh {check|active|accepts|handled|requests|reading|writing|waiting}
/etc/zabbix/scripts/nginx.sh check
5.重啟Agent
systemctl restart zabbix-agent.service
systemctl status zabbix-agent.service
6.Zabbix Server端測(cè)試
zabbix_get -s 11.11.11.62 -k 'nginx.status[check]'
第二步:導(dǎo)入模板
附錄:
參考:https://github.com/oscm/zabbix/tree/master/nginx
模板下載: