Zabbix5.0 configure WeChat single user alarm

Hits: 0

*Original author: Xie Jin, operation and maintenance engineer*

Zabbix5.0 configure WeChat single user alarm

Enterprise WeChat configuration

  1. First of all, if there is no enterprise WeChat, you must first register a corporate WeChat, https://work.weixin.qq.com/

  2. After the registration is completed, log in to the enterprise WeChat through the webpage (some operations can only be operated by page management)

  3. Add an enterprise department

  4. Remember the newly added department ID, which is required for subsequent configuration scripts

  5. Add user to department

  6. Create a self-built application

  7. After the creation is completed, remember the AgentID and Secret code, and the subsequent scripts need to be modified
    . Click to view and there will be information in the enterprise WeChat, click to view the Secret

  8. Check the enterprise ID, and the subsequent scripts need to be modified

[Zabbix] -server configuration

zabbix-server file configuration

  1. Edit the zabbix-server.conf file to configure

# vi /etc/zabbix/zabbix_server.conf 
AlertScriptsPath =/usr/lib/zabbix/alertscripts #If    it exists, you do not need to create a folder according to this directory. If it does not exist, create this path and execute the following command to create a directory 
# mkdir -p /usr/lib/zabbix/alertscripts

  1. install component requests

# yum install python-requests -y

  1. Download the alarm script

# git clone https://github.com/X-Mars/Zabbix-Alert-WeChat.git   
# cp Zabbix-Alert-WeChat/wechat.py /usr/lib/zabbix/alertscripts/weixin.py #Because     of my side There is already a wechat script, so I copied it and modified the script name
 # chmod +x /usr/lib/zabbix/alertscripts/weixin.py     #Script empowerment

  1. Configuration script
    Modify the enterprise information in the script according to the enterprise information recorded by yourself

# cat /usr/lib/zabbix/alertscripts/weixin.py

#!/usr/bin/python2.7 
#_*_coding:utf-8 _*_ 
#auther: Martian Liu

import requests,sys,json
import urllib3
urllib3.disable_warnings()

reload(sys)
sys.setdefaultencoding('utf-8')

def GetTokenFromServer(Corpid,Secret):
    Url = "https://qyapi.weixin.qq.com/cgi-bin/gettoken"
    Data = {
        "corpid":Corpid,
        "corpsecret":Secret
    }
    r = requests.get(url=Url,params=Data,verify=False)
    print(r.json())
    if r.json()[ 'errcode' ] != 0 :
         return  False 
    else :
        Token = r.json()['access_token']
        file = open('/tmp/zabbix_wechat_config.json', 'w')
        file.write(r.text)
        file.close()
        return Token

def SendMessage(User,Agentid,Subject,Content):
    try:
        file = open('/tmp/zabbix_wechat_config.json', 'r')
        Token = json.load(file)['access_token']
        file.close()
    except:
        Token = GetTokenFromServer(Corpid, Secret)

    n = 0
    Url = "https://qyapi.weixin.qq.com/cgi-bin/message/send?access_token=%s" % Token
    Data = {
        "touser" : "XieJin" ,                                  # The user account in the enterprise account, configured in the zabbix user Media, if the configuration is not normal, the
Send by department.
        #"totag": Tagid, # The tag id in the enterprise account, used for group sending (recommended) 
        #"toparty": Partyid, # The department id in the enterprise account, used for group sending. 
        "msgtype" : "text" ,                               # message type. 
        "agentid" : 1000004 ,                              # The application id in the enterprise ID. 
        "text" : {
             "content" : Subject + '\n' + Content
        },
        "safe": "0"
    }
    r = requests.post(url=Url,data=json.dumps(Data),verify=False)
    while r.json()['errcode'] != 0 and n < 4:
        n+=1
        Token = GetTokenFromServer(Corpid, Secret)
        if Token:
            Url = "https://qyapi.weixin.qq.com/cgi-bin/message/send?access_token=%s" % Token
            r = requests.post(url=Url,data=json.dumps(Data),verify=False)
            print(r.json())

    return r.json()


if __name__ == '__main__':
    User = sys.argv[ 1 ]                                                                 # The first parameter passed by 
    zabbix Subject = str(sys.argv[ 2 ])                                                              # The second parameter passed by zabbix 
    Content = str(sys.argv[ 3 ])                                                              # zabbix The third parameter passed in

    Corpid = "wwc59"                                                                      # CorpID is the identification of the enterprise number 
    Secret = "3vzU5S"                                          # Secret is the management group credential key 
    #Tagid = "1" # Address book tag ID 
    Agentid = "1000004"                                                                  # Application ID 
    Partyid = "3"                                                                       # Department ID

    Status = SendMessage(User,Agentid,Subject,Content)
    print Status

  1. test script

# python /usr/lib/zabbix/alertscripts/weixin.py dsdsa fds ewqe 
{ u'msgid' : u'mrVtVXE39it1tWVvd57npP-C0Y8uy8F_-hstvi9e_Y0gSghmqd6IOO4SWKCTeFNyUYjV8TgdE3IXSGC3g2w_oQ' , u'errcode' : 0 , u'errmsg' : u'ok' }

zabbix-server web configuration

  1. Create a user group
    Configure the host master that the user needs to manage to add
  2. Create a user
    The recipient needs to see the account name of the user in the enterprise WeChat
  3. Add alarm medium
  4. test alarm
  5. Configure push content
    Alarm content
    Improve alarm push content:
    Problem content:

-=【Alarm】=-
Alarm host: {HOSTNAME1}
Hostname: {HOST.NAME}
Host address: {HOST.IP}
Alarm time: {EVENT.DATE} {EVENT.TIME}
Alert level: {TRIGGER.SEVERITY}
Warning information: {TRIGGER.NAME}
Issue Details: {ITEM.NAME}:{ITEM.VALUE} 
Current Status: {TRIGGER.STATUS}:{ITEM.VALUE1} 
Event ID: {EVENT.ID}

Problem recovery content:
-=[Alarm canceled]=-

Recovery host: {HOSTNAME1}
Hostname: {HOST.NAME}
Host address: {HOST.IP}
Recovery time: {EVENT.DATE} {EVENT.TIME}
Issue Details: {ITEM.NAME}:{ITEM.VALUE} 
Current Status: {TRIGGER.STATUS}:{ITEM.VALUE1} 
Event ID: {EVENT.ID}

  1. Configure Action
    Create Action Name, click Add Condition
  2. Test alarm
    Shut down a server to test whether Zabbix 5.0 will be pushed
    to configure WeChat single-user alarm configuration completed

Leave a Reply

Your email address will not be published.