飛書提供了豐富的api來實現消息的通知,包括文本消息、圖片消息、富文本消息,本次介紹使用飛書api發送富文本消息,如下是實現思路
飛書API地址:https://open.feishu.cn/document/ukTMukTMukTM/uITNz4iM1MjLyUzMhtml
1.根據正則獲取監控項id,須要在動做中定義報警信息
2.根據獲取的監控項id構造請求獲取圖片地址,並下載到本地
3.須要獲取三個受權憑證python
4.根據zabbix報警的收信人手機號獲取user_id,用於後面在羣裏@相關負責人,或者直接發給某個責任人
5.chat_id用於發送給指定的羣,這裏我提供兩種方法獲取chat_id,後面會介紹
6.上傳本地圖片到飛書,並獲取img_key,image_key用於發送圖片信息
7.傳入zabbix報警消息,並艾特相關負責人發送到飛書羣裏或者我的git
利用正則匹配報警信息中的itemID github
def get_itemid(): #獲取報警的itemid itemid=re.search(r'ITEM ID:(\d+)',sys.argv[3]).group(1) return itemid
根據傳入的itemID,構造請求下載報警圖片,並保存到本地 web
def get_graph(itemid): #獲取報警的圖表並保存 session=requests.Session() #建立一個session會話 try: loginheaders={ "Host":host, "Accept":"text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8" } #定義請求消息頭 payload = { "name":user, "password":password, "autologin":"1", "enter":"Sign in", } #定義傳入的data login=session.post(url=loginurl,headers=loginheaders,data=payload) #進行登陸 graph_params={ "from" :"now-10m", "to" : "now", "itemids" : itemid, "width" : "400", } #定義獲取圖片的參數 graph_req=session.get(url=graph_url,params=graph_params) #發送get請求獲取圖片數據 time_tag=time.strftime("%Y%m%d%H%M%S", time.localtime()) graph_name='baojing_'+time_tag+'.png' #用報警時間來做爲圖片名進行保存 graph_name = os.path.join(graph_path, graph_name) #使用絕對路徑保存圖片 with open(graph_name,'wb',) as f: f.write(graph_req.content) #將獲取到的圖片數據寫入到文件中去 return graph_name except Exception as e: print (e) return False
登陸開發者後臺,在「個人應用」頁面建立企業自建應用。進入企業自建應用詳情頁,獲取App ID和App Secret。 json
一種方法是經過企業自建應用方式獲取,另外一種是經過應用商店應用獲取,這裏我使用第一種方法,直接建立應用便可 windows
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
user_id能夠根據註冊的手機號或郵箱獲取,能夠在zabbix中定義用戶的手機號,而後傳入參數獲取user_id api
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
這裏我提供兩種方法獲取chat_id,一種是將機器人加入到羣裏,獲取羣信息中的chat_id;另外一種是經過機器人建立羣聊獲取羣信息,固然還有其餘的方法,這裏我就不過多介紹了,我將使用第一種方法來獲取chat_idsession
def getchatid(tenant_access_token): #獲取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
經過上傳報警圖片,會獲取到一個image_key,用於發送富文本消息的圖片信息app
def uploadimg(tenant_access_token,graph_name): with open(graph_name,'rb') as f: image = f.read() imgurl='https://open.feishu.cn/open-apis/image/v4/put/' headers={'Authorization': "Bearer %s"%tenant_access_token} files={ "image": image } data={ "image_type": "message" } resp = requests.post( url=imgurl, headers=headers, files=files, data=data) resp.raise_for_status() content = resp.json() return content['data']['image_key']
這裏須要四個參數,分別是user_id、chat_id、tenant_access_token和image_key,並傳入報警信息便可發送
def sendmes(user_id,chat_id,tenant_access_token,image_key): 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":"post", "content":{ "post":{ "zh_cn":{ "title":subject, "content":[ [ { "tag": "text", "un_escape": True, "text": messages }, { "tag": "at", "user_id": user_id } ], [ { "tag": "img", "image_key": image_key, "width": 700, "height": 400 } ] ] } } } } request=requests.post(url=sendurl,headers=headers,json=data) print(request.content)
注意參數順序不能亂
也就是用戶註冊飛書的手機號
後續會添加帶有圖片信息的報警,完整代碼請訪問github組織遮陽筆記
https://github.com/sunsharing-note/zabbix/blob/master/feishu_img.py
歡迎關注我的公號「沒有故事的陳師傅」