Zabbix Host Monitoring with DingTalk Alerts

Setting Up Zabbix Monitoring with DingTalk Notifications

1. Installing the Zabbix Agent

# yum install zabbix-agent-5.0.32-1.el8.x86_64.rpm

2. Configuring the Agent

# egrep -v '^$|^#' /etc/zabbix/zabbix_agentd.conf
PidFile=/var/run/zabbix/zabbix_agentd.pid
LogFile=/var/log/zabbix/zabbix_agentd.log
LogFileSize=0
Server=116.205.129.152
ServerActive=116.205.129.152
Hostname=cocoai-1
Include=/etc/zabbix/zabbix_agentd.d/*.conf

3. Starting the Agent Service

# systemctl start zabbix-agent

4. Creating Host Templates

5. Setting Up Web Monitoring

1) Create a monitoring template

2) Create a web monitoring scenario

6. Creating Triggers

7. Binding Hosts to Templates

8. Creating DingTalk Group and Robot

9. Writing the DingTalk Alert Script

Script location:

# grep ^Aler /etc/zabbix/zabbix_server.conf
# cd /usr/lib/zabbix/alertscripts
# vim dingtalk_alert.py

1) Python 3 Script

#!/usr/bin/python3
# -*- coding: utf-8 -*-
import requests
import json
import sys
import os
import time
headers = {'Content-Type': 'application/json;charset=utf-8'}
current_time=time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())
webhook_url = "Your Webhook URL"
def send_alert(message, recipient):
  payload = {
     "msgtype": "markdown",
        "markdown": {
            "title": "System Alert",
            "text": message + "\n \n @" + recipient
        },
        "at": {
            "atMobiles": [
                recipient
 ],
            "isAtAll": False
        }
    }
  response = requests.post(webhook_url, data=json.dumps(payload), headers=headers).json()
  status_code = response["errcode"]
if __name__ == '__main__':
    recipient = sys.argv[1]
    message = sys.argv[2]
    send_alert(message, recipient)

2) Python 2 Script

#!/usr/bin/python2
# -*- coding: utf-8 -*-
import requests
import json
import sys
import os
headers = {'Content-Type': 'application/json;charset=utf-8'}
webhook_url = "https://oapi.dingtalk.com/robot/send?access_token=c8a8336f237f35bc7efe68bcb4f8a71344950397b338f9fef1ed6019bbb4ef04"
def send_alert(message, recipient):
    payload = {
     "msgtype": "markdown",
        "markdown": {
            "title":"IT Alert",
            "text": message + "\n \n @" + recipient
        },
        "at": {
            "atMobiles": [
                recipient
 ],
            "isAtAll": False
        }
    }
    response=requests.post(webhook_url, data=json.dumps(payload), headers=headers).json()
    status_code = response["errcode"]
if __name__ == '__main__':
    recipient = sys.argv[1]
    message = sys.argv[2]
    send_alert(message, recipient)

3) Testing the Script

# pip3 install requests
# chown -R zabbix.zabbix /usr/lib/zabbix/alertscripts/
# chmod +x dingtalk_alert.py
# ./dingtalk_alert.py alert test

Note: "alert" is the keyword for DingTalk, "test" is the message content, requires requests module

10. Creating Alert Media

11. Creating Actions

1) Create an action

2) Create triggers

3) Create alert operations and recovery operations

Alert Host: {HOST.NAME}

Alert Address: {HOST.IP}

Monitoring Item: {ITEM.NAME}

Current Value: {ITEM.LASTVALUE}

Status: {TRIGGER.STATUS}

Alert Time: {EVENT.DATE} {EVENT.TIME}

12. Adding Alert Media to Users

13. DingTalk Webhook Alert Method

Script

var dingtalk = { 
  key: null, 
  message: null, 
  msgtype: "markdown", 
  proxy: null, 
  sendNotification: function () { 
    var parameters = { 
       msgtype: dingtalk.msgtype, 
       markdown: { 
          title: "Alert", 
          text: dingtalk.message 
        }, 
      }, 
      data, 
      response, 
      request = new CurlHttpRequest(), 
      url = 
        "https://oapi.dingtalk.com/robot/send?access_token=" + 
        dingtalk.key; 
    if (dingtalk.proxy) { 
      request.setProxy(dingtalk.proxy); 
    } 
    request.AddHeader("Content-Type: application/json"); 
    data = JSON.stringify(parameters); 
    Zabbix.Log( 
      4, 
      "[dingtalk Webhook] URL: " + url.replace(dingtalk.key, "<bot key="">") 
    ); 
    Zabbix.Log(4, "[dingtalk Webhook] parameters: " + data); 
    response = request.Post(url, data); 
    Zabbix.Log(4, "[dingtalk Webhook] HTTP code: " + request.Status()); 
    try { 
      response = JSON.parse(response); 
    } catch (error) { 
      response = null; 
    } 
    if (request.Status() !== 200 || response.errcode !== 0) { 
      if (typeof response.errmsg === "string") { 
        throw response.errmsg; 
      } else { 
        throw "Unknown error. Check debug log for more information."; 
      } 
    } 
  }, 
}; 
try { 
  var parameters = JSON.parse(value); 
  if (typeof parameters.Key === "undefined") { 
    throw 'Incorrect value is given for parameter "Key": parameter is missing'; 
  } 
  dingtalk.key = parameters.Key; 
  if (parameters.HTTPProxy) { 
    dingtalk.proxy = parameters.HTTPProxy; 
  } 
  dingtalk.recipient = parameters.To; 
  dingtalk.message = parameters.Subject + "\n" + parameters.Message; 
  dingtalk.sendNotification(); 
  return "OK"; 
} catch (error) { 
  Zabbix.Log(4, "[dingtalk Webhook] notification failed: " + error); 
  throw "Sending failed: " + error + "."; 
}</bot>

Tags: Zabbix DingTalk monitoring Alerting python

Posted on Tue, 06 Oct 2026 16:27:32 +0000 by jacobelias