Realize Zabbix + enterprise wechat alarm

1. Register an enterprise wechat and get the enterprise ID

 

 

 

2. Create a robot

 

 

When creating, you need to select the visible range of the applet - which users can see the applet and receive the information sent by the applet.

After creation, record the following two values.

So far, three values (enterprise id, agentid, and secret key) have been obtained. The script on the subsequent server sends alarm information to the applet according to these three values.

 

 

 

  3. Write script for pushing information

Create a script file under the script path on the ZABBIX server. The default path is usually / etc/zabbix/alertscripts /. Please check the ZABBIX configuration file in advance to confirm the path (AlertScriptsPath=/etc/zabbix/alertscripts).

Script name: weixin.py

Storage path: / etc/zabbix/alertscripts/

Modify strategy: fill the three values obtained before into corpid, appsecret and agentid respectively;

After modifying the script, be sure to add executable permissions for it!

#!/usr/bin/env python
#-*- coding: utf-8 -*-
#author: yanggd
#date: 2018-04-20
#comment: zabbix Access wechat alarm script
import requests
import sys
import os
import json
import logging
logging.basicConfig(level = logging.DEBUG, format = '%(asctime)s, %
(filename)s, %(levelname)s, %(message)s',
datefmt = '%a, %d %b %Y %H:%M:%S',
filename = os.path.join('/tmp','weixin.log'),
filemode = 'a')
corpid='wwbbaxxxxxx'
appsecret='m2HUZXUxxxxxxxxxx'
agentid=1000004
#obtain accesstoken token_url='https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid=' + corpid + '&corpsecret=' + appsecret req=requests.get(token_url) accesstoken=req.json()['access_token']
#send message msgsend_url='https://qyapi.weixin.qq.com/cgi-bin/message/send? access_token=' + accesstoken touser=sys.argv[1] subject=sys.argv[2] #toparty='3|4|5|6' message=sys.argv[3] params={ "touser": touser, # "toparty": toparty, "msgtype": "text", "agentid": agentid, "text": { "content": message }, "safe":0 } req=requests.post(msgsend_url, data=json.dumps(params)) logging.info('sendto:' + touser + ';;subject:' + subject + ';;message:' + message)

So far, you can test whether the script can send information;

[root@test2 alertscripts]# ./weixin.py Cheng-Loto 'Title' 'test content'

 

  The following errors may be encountered, which can be solved by the way shown in the figure:

 

 

4. Configure Zabbix

Add alarm media: manage > alarm media type > create media type

 

 

 

  Add receiving object: the id of the visible object of the applet. Log in to the enterprise wechat address book to view it.

 

 

5. After configuration, verify

 

 

6. Others

① In zabbix, the user ID in the enterprise wechat group is manually added in the alarm media. If it needs to be applied to Prometheus, Prometheus can automatically obtain it through the following methods. With the help of the API of enterprise wechat: https://work.weixin.qq.com/api/doc/90000/90135/90200

All member IDS in the applet can be obtained through the following py Code:

import requests

req = requests.get('https://qyapi.weixin.qq.com/cgi-bin/user/simplelist?access_token=xxoxxxxxtAw&department_id=1&fetch_child=1', timeout=30);
req_jason = req.json();

list2 = req_jason.get('userlist');

for i in list2:
    print(i.get('userid'))

 

② If the message can only be accepted in the enterprise wechat, the personal wechat cannot receive the information.

On the enterprise wechat client, click Settings - > new message notification - > receive messages only in enterprise wechat on the personal home page, and click to turn off all options.

 

③ Other configurations are correct, but the message is still not received. Check whether there is an enabled action in zabbix. Check the correctness of zabbix configuration.

 

Tags: Zabbix monitor and control

Posted on Thu, 11 Nov 2021 04:36:28 -0500 by Krik