本文参考链接 http://liqingbiao.blog.51cto.com/3044896/1894902,非常感谢作者!
1. Nginx编译安装,指定--with-http_stub_status_module
2. 设置nginx主机配置文件,开启模块设置,并测试生效
vim cat /usr/local/nginx/conf/vhosts/huangzhenping.conf
3. 在zabbix_agentd客户端,创建获取状态信息的脚本文件,并授予执行权限
vim /usr/local/zabbix/scripts/nginx-check_performance.sh
脚本内容:
#!/bin/bash
##################################
# Zabbix monitoring script
#
# nginx:
# - anything available via nginx stub-status module
#
##################################
# Zabbix requested parameter
ZBX_REQ_DATA="$1"
ZBX_REQ_DATA_URL="$2"
# Nginx defaults
NGINX_STATUS_DEFAULT_URL="172.16.115.87/nginx-status"
WGET_BIN="/usr/bin/wget"
#
# Error handling:
# - need to be displayable in Zabbix (avoid NOT_SUPPORTED)
# - items need to be of type "float" (allow negative + float)
#
ERROR_NO_ACCESS_FILE="-0.9900"
ERROR_NO_ACCESS="-0.9901"
ERROR_WRONG_PARAM="-0.9902"
ERROR_DATA="-0.9903" # either can not connect / bad host / bad port
# Handle host and port if non-default
if [ ! -z "$ZBX_REQ_DATA_URL" ]; then
URL="$ZBX_REQ_DATA_URL"
else
URL="$NGINX_STATUS_DEFAULT_URL"
fi
# save the nginx stats in a variable for future parsing
NGINX_STATS=$($WGET_BIN -q $URL -O - 2> /dev/null)
# error during retrieve
if [ $? -ne 0 -o -z "$NGINX_STATS" ]; then
echo $ERROR_DATA
exit 1
fi
#
# Extract data from nginx stats
#
case $ZBX_REQ_DATA in
active_connections) echo "$NGINX_STATS" | head -1 | cut -f3 -d' ';;
accepted_connections) echo "$NGINX_STATS" | grep -Ev '[a-zA-Z]' | cut -f2 -d' ';;
handled_connections) echo "$NGINX_STATS" | grep -Ev '[a-zA-Z]' | cut -f3 -d' ';;
handled_requests) echo "$NGINX_STATS" | grep -Ev '[a-zA-Z]' | cut -f4 -d' ';;
reading) echo "$NGINX_STATS" | tail -1 | cut -f2 -d' ';;
writing) echo "$NGINX_STATS" | tail -1 | cut -f4 -d' ';;
waiting) echo "$NGINX_STATS" | tail -1 | cut -f6 -d' ';;
*) echo $ERROR_WRONG_PARAM; exit 1;;
esac
exit 0
执行权限命令: chmod +x /usr/local/zabbix/scripts/nginx-check_performance.sh
4. 修改zabbix_agentd.conf配置文件,启用并设置UserParameter参数
vim /etc/zabbix/zabbix_agentd.conf
设置如下:
UnsafeUserParameters=1
UserParameter=nginx[*],/usr/local/zabbix/scripts/nginx-check_performance.sh "$1"
5. 重启客户客户端,并在服务端测试
zabbix_get -s 10.253.17.20 -p 10050 -k "nginx[reading]"
6. 导入nginx模板信息,并给主机添加模板