在Zabbix Agent端创建监控脚本,通常放在/etc/zabbix/zabbix_agentd.d/
目录下:
#!/bin/bash
# nginx_status.sh
# 定义Nginx状态页地址(根据实际配置调整)
NGINX_STATUS_URL="http://localhost/nginx_status"
case $1 in
active)
curl -s $NGINX_STATUS_URL | grep 'Active' | awk '{print $3}'
;;
accepts)
curl -s $NGINX_STATUS_URL | awk NR==3 | awk '{print $1}'
;;
handled)
curl -s $NGINX_STATUS_URL | awk NR==3 | awk '{print $2}'
;;
requests)
curl -s $NGINX_STATUS_URL | awk NR==3 | awk '{print $3}'
;;
reading)
curl -s $NGINX_STATUS_URL | grep 'Reading' | awk '{print $2}'
;;
writing)
curl -s $NGINX_STATUS_URL | grep 'Writing' | awk '{print $4}'
;;
waiting)
curl -s $NGINX_STATUS_URL | grep 'Waiting' | awk '{print $6}'
;;
*)
echo "Usage: $0 [active|accepts|handled|requests|reading|writing|waiting]"
exit 1
;;
esac
确保Nginx已启用状态页,在Nginx配置中添加:
server {
listen 80;
server_name localhost;
location /nginx_status {
stub_status on;
access_log off;
allow 127.0.0.1;
deny all;
}
}
在/etc/zabbix/zabbix_agentd.d/userparameter_nginx.conf
中添加:
UserParameter=nginx.status[*],/etc/zabbix/zabbix_agentd.d/nginx_status.sh $1
重启Zabbix Agent:
systemctl restart zabbix-agent
在Zabbix Web界面中: 1. 进入"Configuration" → "Hosts" 2. 选择需要监控的主机 3. 点击"Items" → "Create item"
创建以下监控项(示例):
- 键值:nginx.status[active]
名称:Nginx Active Connections
- 键值:nginx.status[requests]
名称:Nginx Requests
为关键指标创建触发器,例如当活跃连接数过高时告警:
{Template_App_Nginx:nginx.status[active].last()}>1000
在Zabbix Server上创建脚本/usr/lib/zabbix/alertscripts/wechat_alert.sh
:
#!/bin/bash
# wechat_alert.sh
# 企业微信相关参数
CORPID="your_corpid"
CORPSECRET="your_corpsecret"
AGENTID="your_agentid"
# 获取部门或用户ID(根据实际配置)
TOUSER="$1"
TOPARTY="$2"
TOTAG="$3"
# 获取传入参数
SUBJECT="$4"
MESSAGE="$5"
# 获取access_token
ACCESS_TOKEN=$(curl -s "https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid=${CORPID}&corpsecret=${CORPSECRET}" | jq -r .access_token)
# 发送消息
curl -s -X POST "https://qyapi.weixin.qq.com/cgi-bin/message/send?access_token=${ACCESS_TOKEN}" \
-H "Content-Type: application/json" \
-d @<(cat <<EOF
{
"touser": "${TOUSER}",
"toparty": "${TOPARTY}",
"totag": "${TOTAG}",
"msgtype": "text",
"agentid": "${AGENTID}",
"text": {
"content": "${SUBJECT}\n${MESSAGE}"
},
"safe": 0
}
EOF
)
给脚本执行权限:
chmod +x /usr/lib/zabbix/alertscripts/wechat_alert.sh
chown zabbix:zabbix /usr/lib/zabbix/alertscripts/wechat_alert.sh
yum install jq -y # CentOS
# 或
apt-get install jq -y # Ubuntu/Debian
点击"Create media type":
为用户配置接收微信告警:
监控项扩展:可以添加更多Nginx监控指标,如:
告警分级:根据严重程度设置不同级别的告警(信息、警告、严重)
告警聚合:对于频繁触发的告警,可以设置告警聚合避免消息轰炸
模板化:将Nginx监控配置导出为模板,方便其他主机复用
安全加固: