Automating Offline Device Alerts via WeCom Robot in Zabbix

Agent Preparation

A dedicated WeCom group must be created first, containing only WeCom enterprise accounts. Inviting personal WeChat accounts will invalidate the bot later.

1. Register a Group Bot

Inside the group chat, select the menu, navigate to Message PushAdd, and define a custom bot name. Once saved, a Webhook URL is generated. Copy this address.

2. Backend Script Setup

Log into the Zabbix server and place the notification script in the default alert scripts directory:

cd /usr/lib/zabbix/alertscripts/

Create a Python file:

vim /usr/lib/zabbix/alertscripts/wecom_push.py

Insert the following implementation, replacing the webhook URL and optional mobile numbers:

#!/usr/bin/env python3
# -*- coding: utf-8 -*-

import sys
import json
import requests

def deliver(webhook, content, mobiles=None):
    """Send a text message to a WeCom group bot."""
    headers = {"Content-Type": "application/json; charset=utf-8"}
    payload = {
        "msgtype": "text",
        "text": {
            "content": content
        }
    }
    if mobiles:
        payload["text"]["mentioned_mobile_list"] = mobiles

    response = requests.post(webhook, data=json.dumps(payload), headers=headers)
    print(response.text)

if __name__ == "__main__":
    if len(sys.argv) < 2:
        sys.exit(1)

    WEBHOOK_URL = "https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=YOUR-ACTUAL-KEY"
    MENTION_PHONES = ["13800138000", "13900139000"]   # List group member phones
    message_text = sys.argv[1]
    deliver(WEBHOOK_URL, message_text, MENTION_PHONES)

Test the script locally:

python3 wecom_push.py "Device offline test alert"

3. Zabbix Frontend Configuration

  • User & Media: Under AdministrationUsers, create (or reuse) a user assigned to a group that has read permissions for the monitored hosts. Under AdministrationMedia types, define a new media type. Set Type to Script, Script name to the file created above (e.g., wecom_push.py), and pass parameters as needed. In the user’s profile, add this media type with appropriate contact information.

  • Trigger Actions: Navigate to ConfigurationAcsions. Create a new action that fires on the desired trigger (e.g., a host unreachable condition). In the Operations tab, add a operation and configure:

    • Send to user groups: Select the group containing the user.

    • Send only to: The WeCom media type.

    • Custom message: Enable and fill in:

      Subject:

      {HOSTNAME1} connectivity lost
      

      Message:

      Alert: {HOSTNAME1} has gone offline
      Host: {HOSTNAME1}
      Time: {EVENT.DATE} {EVENT.TIME}
      Severity: Disaster
      Trigger: {TRIGGER.NAME}
      Status: {TRIGGER.STATUS} (value: {ITEM.VALUE1})
      Event ID: {EVENT.ID}
      Please verify power, cables, and network path.
      

    Add a Recovery operation with similar settings:

    Subject:

    Recovery: {HOSTNAME1} is reachable again
    

    Message:

    Recovery: {HOSTNAME1} connectivity restored
    Host: {HOSTNAME1}
    Time recovered: {EVENT.RECOVERY.TIME}
    Outage duration: {EVENT.AGE}
    Trigger: {TRIGGER.NAME}
    Status: {TRIGGER.STATUS} (value: {ITEM.VALUE1})
    Event ID: {EVENT.ID}
    

4. Verification

Trigger a test event for a monitored device (e.g., by simulating a interface down). Confirm that the WeCom group receives both the alert and the recovery message, including the calculated downtime.

Tags: Zabbix WeChat Work Alerting python monitoring

Posted on Wed, 08 Jul 2026 17:42:41 +0000 by Vince