当前位置:网站首页>ZABBIX wechat alarm

ZABBIX wechat alarm

2022-06-13 05:12:00 I love Qianxi

One 、 Prerequisite steps
Register your own account in enterprise wechat , Then add in the application zabbix Alarm application , Here's the picture
 Insert picture description here
Two 、 Write alarm script

vim weixin.py
#!/usr/bin/env python
#-*- coding: utf-8 -*-
#author: bgx
#date: 2021
#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='ww9e4fc79450a62d64'
appsecret='BO3pAO6TShIuIuIYG_YVYUAGHDcM4CVmEE7FdlQYbBg'
agentid=1000002
# 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 a 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[2] + "\n\n" +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)  

The above will be used corpid,appsecret,agentid Both of them can be viewed at the bottom of my enterprise and the application , But I'm checking secret You must use your mobile phone to download the client to view , Download the mobile version of enterprise wechat .
2、 This script should be placed in the corresponding position

 Because this file /etc/zabbix/zabbix_server.conf Specifies the script storage location 
AlertScriptsPath=/usr/lib/zabbix/alertscripts
 So put the script in this position , Plus Execution Authority 
chmod +x weixin.py
 And create weixin.log file , modify weixin.py as well as weixin.log Belong to main group 
chown zabbix:zabbix weixin.py
chown zabbix:zabbix weixin.log

3、 ... and 、 To configure
1、 Configure the sender
stay zabbix Click Manage in the interface , Click alarm media , Click create media type and fill in parameters , as follows :
 Insert picture description here
2、 Configure recipients
 Insert picture description here
Because the triggering item has been defined for the mail alarm before , So now you can trigger it directly . You can receive wechat alarm .

原网站

版权声明
本文为[I love Qianxi]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/02/202202280515160142.html