插件窝 干货文章 Zabbix自定义脚本监控nginx及微信告警的方法

Zabbix自定义脚本监控nginx及微信告警的方法

Nginx 告警 Zabbix status 189    来源:    2025-04-22

Zabbix自定义脚本监控Nginx及微信告警方案

一、Nginx监控脚本配置

1. 创建Nginx状态监控脚本

在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

2. 配置Nginx状态页

确保Nginx已启用状态页,在Nginx配置中添加:

server {
    listen 80;
    server_name localhost;

    location /nginx_status {
        stub_status on;
        access_log off;
        allow 127.0.0.1;
        deny all;
    }
}

3. 配置Zabbix Agent

/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 Server配置

1. 创建监控项

在Zabbix Web界面中: 1. 进入"Configuration" → "Hosts" 2. 选择需要监控的主机 3. 点击"Items" → "Create item"

创建以下监控项(示例): - 键值:nginx.status[active] 名称:Nginx Active Connections - 键值:nginx.status[requests] 名称:Nginx Requests

2. 创建触发器

为关键指标创建触发器,例如当活跃连接数过高时告警:

{Template_App_Nginx:nginx.status[active].last()}>1000

三、微信告警配置

1. 申请企业微信应用

  1. 注册企业微信(https://work.weixin.qq.com/)
  2. 创建自建应用,获取以下信息:
    • CorpID:企业ID
    • AgentID:应用ID
    • Secret:应用Secret
    • 部门ID(可选)

2. 创建微信告警脚本

在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

3. 安装jq工具(用于JSON解析)

yum install jq -y  # CentOS
# 或
apt-get install jq -y  # Ubuntu/Debian

4. 配置Zabbix告警媒介

  1. 进入"Administration" → "Media types"
  2. 点击"Create media type":

    • Name: WeChat Alert
    • Type: Script
    • Script name: wechat_alert.sh
    • Script parameters:
      • {ALERT.SENDTO}
      • {ALERT.SUBJECT}
      • {ALERT.MESSAGE}
  3. 为用户配置接收微信告警:

    • 进入"Administration" → "Users"
    • 选择用户 → "Media" → "Add"
    • Type: WeChat Alert
    • Send to: 企业微信用户ID(如@all或特定用户ID)

5. 配置告警动作

  1. 进入"Configuration" → "Actions"
  2. 创建新的Action:
    • Name: Nginx Problem Alert
    • Conditions: Trigger value = Problem
    • Operations:
      • Send to Users: 选择相应用户
      • Send only to: WeChat Alert
    • Recovery operations: 配置恢复通知

四、测试验证

  1. 手动停止Nginx服务,检查是否收到微信告警
  2. 恢复Nginx服务,检查是否收到恢复通知
  3. 在Zabbix的"Monitoring" → "Problems"中查看告警状态

五、优化建议

  1. 监控项扩展:可以添加更多Nginx监控指标,如:

    • 各server块的状态
    • 响应时间监控
    • 错误日志监控
  2. 告警分级:根据严重程度设置不同级别的告警(信息、警告、严重)

  3. 告警聚合:对于频繁触发的告警,可以设置告警聚合避免消息轰炸

  4. 模板化:将Nginx监控配置导出为模板,方便其他主机复用

  5. 安全加固

    • 限制Nginx状态页的访问IP
    • 使用HTTPS访问企业微信API
    • 保护脚本中的敏感信息(如Secret)