這部份內容,能夠查看前面不帶圖的文章有詳細描述php
#!/usr/bin/python #coding=utf-8 _author__ = 'zhangdongdong' import requests, json import urllib3 import smtplib,sys,os,time,re,requests from email.mime.image import MIMEImage if sys.getdefaultencoding() != 'utf-8': reload(sys) sys.setdefaultencoding('utf-8') urllib3.disable_warnings() class WechatImage(object): # 根據企業微信api接口文檔,定義一個類,使用mpnews類型,https://qydev.weixin.qq.com/wiki/index.php?title=%E6%B6%88%E6%81%AF%E7%B1%BB%E5%9E%8B%E5%8F%8A%E6%95%B0%E6%8D%AE%E6%A0%BC%E5%BC%8F def get_token(self, corpid, secret): # 獲取token url = "https://qyapi.weixin.qq.com/cgi-bin/gettoken" data = {"corpid": corpid, "corpsecret": secret} r = requests.get(url=url, params=data, verify=False) token = r.json()['access_token'] return token def get_image_url(self, token, path): # 上傳臨時素材圖片,而後返回media_id url = "https://qyapi.weixin.qq.com/cgi-bin/media/upload?access_token=%s&type=image" % token data = {"media": open(path, 'rb')} r = requests.post(url=url, files=data) dict_data = r.json() return dict_data['media_id'] def get_messages( self,subject,content,path): #定義mpnews類型中的參數字典 data = '' messages = {} body = {} content_html=text_to_html(content) token = self.get_token(corpid, secret) image = self.get_image_url(token, path) content_html += "<br/> <img src='https://qyapi.weixin.qq.com/cgi-bin/media/get?access_token=%s&media_id=%s'>" % (token, image) body["title"] = subject body['digest'] = content body['content'] = content_html body['thumb_media_id'] = image data = [] data.append(body) messages['articles'] = data return messages def send_news_message(self, corpid, secret,to_user, agentid,path): #定義發送mpnews類型的數據 token = self.get_token(corpid, secret) messages = self.get_messages( subject, content,path) url = "https://qyapi.weixin.qq.com/cgi-bin/message/send?access_token=%s" % token data = {"toparty": to_user, # 企業號中的用戶賬號 "agentid": agentid, # 企業號中的應用id "msgtype": "mpnews", "mpnews": messages, "safe": "0"} headers = {'content-type': 'application/json'} data_dict = json.dumps(data, ensure_ascii=False).encode('utf-8') r = requests.post(url=url, headers=headers, data=data_dict) return r.text def text_to_html(text): #將郵件內容text字段轉換成HTML格式 d=text.splitlines() #將郵件內容以每行做爲一個列表元素存儲在列表中 html_text='' for i in d: i='' + i + '<br>' html_text+=i + '\n' #爲列表的每一個元素後加上html的換行標籤 return html_text def get_itemid(): #獲取報警的itemid itemid=re.search(r'監控ID:(\d+)',sys.argv[3]).group(1) return itemid 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" : "290", #圖片的高寬參數 "height" : "40", } #定義獲取圖片的參數 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 if __name__ == '__main__': user='Admin' #定義zabbix用戶名 password='zabbix' #定義zabbix用戶i密 graph_path='/usr/lib/zabbix/alertscripts/graph/' #定義圖片存儲路徑,圖片須要定時清理 graph_url='http://192.168.73.133/chart.php' #定義圖表的url loginurl="http://192.168.73.133/index.php" #定義登陸的url host='192.168.73.133' itemid=get_itemid() path =get_graph(itemid) to_user = str(sys.argv[1]) subject = str(sys.argv[2]) content = str(sys.argv[3]) corpid= "xxxxx" secret = "xxxxxxx" agentid = "1000002" wechat_img = WechatImage() wechat_img.send_news_message(corpid, secret,to_user, agentid, path)
打開zabbix監控web,在管理菜單中選擇報警媒介類型,建立媒體類型,選擇腳本,填寫剛纔編寫的微信帶圖腳本名稱zabbix_weixin_pic.py,腳本參數,最後添加html
打開管理中的用戶,點擊須要設置郵件告警的用戶,而後在報警媒介中添加報警媒介,在彈框中選擇剛纔定義的類型,而後填寫企業微信中建立的部門id,最後添加
python
操做 默認標題 Zabbix告警: 副務器:{HOSTNAME}發生: {TRIGGER.NAME}故障! 監控ID:{ITEM.ID} 告警主機:{HOST.NAME} 告警主機:{HOST.IP} 告警時間:{EVENT.DATE} {EVENT.TIME} 告警等級:{TRIGGER.SEVERITY} 告警信息: {TRIGGER.NAME} 告警項目:{TRIGGER.KEY} 問題詳情:{ITEM.NAME}:{ITEM.VALUE} 當前狀態:{TRIGGER.STATUS}:{ITEM.VALUE} 事件ID:{EVENT.ID} 恢復操做 Zabbix告警: 副務器:{HOST.NAME}發生: {TRIGGER.NAME}已恢復! 監控ID:{ITEM.ID} 告警主機:{HOST.NAME} 告警主機:{HOST.IP} 告警時間:{EVENT.DATE} {EVENT.TIME} 告警等級:{TRIGGER.SEVERITY} 告警信息: {TRIGGER.NAME} 告警項目:{TRIGGER.KEY} 問題詳情:{ITEM.NAME}:{ITEM.VALUE} 當前狀態:{TRIGGER.STATUS}:{ITEM.VALUE} 事件ID:{EVENT.ID}
能夠手動觸發一個報警測試效果,手機上就能夠收到帶圖的報警了,點擊消息以後的頁面也能夠看到歷史的圖片
web
歡×××陳師傅」
json