ZABBIX docking flight book for alarm notification

Flybook provides a rich api for message notification, including text messages, picture messages, rich text messages. This introduction uses Flybook api to send text messages, the following is the implementation ideas
Flying Book API address: https://open.feishu.cn/document/ukTMukTMukTM/uITNz4iM1MjLyUzM

Ideas for implementation


1. Three authorization credentials are required

  • app_access_token: Access the App resource related interface.
  • tenant_access_token: Accesses the enterprise resource-related interface.
  • user_access_token: Accesses the user resource related interface.

2. Obtain the user_id from the phone number of the recipient of the zabbix alarm, which can be used for @ the responsible person in the group behind, or sent directly to a responsible person
3.chat_id is used to send to a specified group. Here I provide two ways to get chat_id, which will be described later
4. Incoming zabbix alarm message, and the person in charge of Aite sent to the flying books group or individuals

Obtain Authorization Certificate

1. Get App ID and App Secret

Log in to the developer background and create your own enterprise application on the My Apps page.Go to the Enterprise Build-in Application Details page to get the App ID and App Secret.

2. Get tenant_access_token

One way is through enterprise-built apps, the other is through app store apps, where I use the first method to create apps directly

3. Once the application is created, it can be obtained from the APP ID and App Secret construction requests

def gettenant_access_token():
    tokenurl="https://open.feishu.cn/open-apis/auth/v3/tenant_access_token/internal/"
    headers={"Content-Type":"application/json"}
    data={
        "app_id":"cli_9ec625abcdefg",
        "app_secret":"f716Gi27Yi25n5K0Wbafgwghhstv"

    }
    request=requests.post(url=tokenurl,headers=headers,json=data)
    response=json.loads(request.content)['tenant_access_token']
    return response

Get user_id

User_id can be obtained from a registered phone number or mailbox, defined in zabbix, and passed in a parameter to get user_id

def getuserid(tenant_access_token):
    #mobiles="15101234584"
    userurl="https://open.feishu.cn/open-apis/user/v1/batch_get_id?mobiles=%s"%mobiles
    headers={"Authorization":"Bearer %s"%tenant_access_token}
    request=requests.get(url=userurl,headers=headers)
    response=json.loads(request.content)['data']['mobile_users'][mobiles][0]['user_id']
    return response

Get chat_id

Here I provide two ways to get chat_id, one is to join a robot in a group to get chat_id from the group information, the other is to create a group chat by a robot to get the group information, and of course there are other ways, which I will only cover here, I will use the first method to get chat_id

First join the robot to group chat

Construct request to get chat_id

def getchatid(tenant_access_token):
    #Get chatid
    chaturl="https://open.feishu.cn/open-apis/chat/v4/list?page_size=20"
    headers={"Authorization":"Bearer %s"%tenant_access_token,"Content-Type":"application/json"}
    request=requests.get(url=chaturl,headers=headers)
    response=json.loads(request.content)['data']['groups'][0]['chat_id']
    return response

Send messages to Flying Books or Flying Books users

There are three parameters required, one is user_id, one is chat_id, and the other is tenant_access_token, which can be sent by passing in an alarm message

def sendmes(user_id,chat_id,tenant_access_token):
    #Send a message to the group
    sendurl="https://open.feishu.cn/open-apis/message/v4/send/"
    headers={"Authorization":"Bearer %s"%tenant_access_token,"Content-Type":"application/json"}
    data={"chat_id":chat_id,
        "msg_type":"text",
        "content":{
            "text":"%s<at user_id=\"%s\">test</at>"%(messages,user_id)
        }
    }
    #Send a message to an individual
    # data={"user_id":user_id,
    #     "msg_type":"text",
    #     "content":{
    #         "text":"%s<at user_id=\"%s\">test</at>"%(messages,user_id)
    #     }
    # }
    request=requests.post(url=sendurl,headers=headers,json=data)
    print(request.content)

Configure alarm action and receiver on ZABBIX

Configure alarm media types

Note that the order of parameters should not be confused

Configure user's receiving information

That is, the mobile number of the user registering for Flybook

Configure Actions

Alarm test

Here I disable one of the windows agent s for testing


Alarm with picture information will be added later, complete code please visit github organization shade notes
https://github.com/sunsharing-note/zabbix/blob/master/feishu.py

Welcome to the personal bulletin "Master Chen without Stories"

Tags: Linux JSON Zabbix github Mobile

Posted on Wed, 19 Feb 2020 12:25:15 -0500 by moe