您好,登錄后才能下訂單哦!
這部分內(nèi)容,可以查看前面不帶圖的文章有詳細(xì)描述
#!/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): # 根據(jù)企業(yè)微信api接口文檔,定義一個(gè)類,使用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): # 上傳臨時(shí)素材圖片,然后返回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類型中的參數(shù)字典
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): #定義發(fā)送mpnews類型的數(shù)據(jù)
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, # 企業(yè)號(hào)中的用戶帳號(hào)
"agentid": agentid, # 企業(yè)號(hào)中的應(yīng)用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): #將郵件內(nèi)容text字段轉(zhuǎn)換成HTML格式
d=text.splitlines()
#將郵件內(nèi)容以每行作為一個(gè)列表元素存儲(chǔ)在列表中
html_text=''
for i in d:
i='' + i + '<br>'
html_text+=i + '\n'
#為列表的每個(gè)元素后加上html的換行標(biāo)簽
return html_text
def get_itemid():
#獲取報(bào)警的itemid
itemid=re.search(r'監(jiān)控ID:(\d+)',sys.argv[3]).group(1)
return itemid
def get_graph(itemid):
#獲取報(bào)警的圖表并保存
session=requests.Session() #創(chuàng)建一個(gè)session會(huì)話
try:
loginheaders={
"Host":host,
"Accept":"text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8"
}
#定義請(qǐng)求消息頭
payload = {
"name":user,
"password":password,
"autologin":"1",
"enter":"Sign in",
}
#定義傳入的data
login=session.post(url=loginurl,headers=loginheaders,data=payload)
#進(jìn)行登錄
graph_params={
"from" :"now-10m",
"to" : "now",
"itemids" : itemid,
"width" : "290", #圖片的高寬參數(shù)
"height" : "40",
}
#定義獲取圖片的參數(shù)
graph_req=session.get(url=graph_url,params=graph_params)
#發(fā)送get請(qǐng)求獲取圖片數(shù)據(jù)
time_tag=time.strftime("%Y%m%d%H%M%S", time.localtime())
graph_name='baojing_'+time_tag+'.png'
#用報(bào)警時(shí)間來(lái)作為圖片名進(jìn)行保存
graph_name = os.path.join(graph_path, graph_name)
#使用絕對(duì)路徑保存圖片
with open(graph_name,'wb') as f:
f.write(graph_req.content)
#將獲取到的圖片數(shù)據(jù)寫入到文件中去
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/' #定義圖片存儲(chǔ)路徑,圖片需要定時(shí)清理
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)
打開(kāi)zabbix監(jiān)控web,在管理菜單中選擇報(bào)警媒介類型,創(chuàng)建媒體類型,選擇腳本,填寫剛才編寫的微信帶圖腳本名稱zabbix_weixin_pic.py,腳本參數(shù),最后添加
打開(kāi)管理中的用戶,點(diǎn)擊需要設(shè)置郵件告警的用戶,然后在報(bào)警媒介中添加報(bào)警媒介,在彈框中選擇剛才定義的類型,然后填寫企業(yè)微信中創(chuàng)建的部門id,最后添加
操作
默認(rèn)標(biāo)題
Zabbix告警:
副務(wù)器:{HOSTNAME}發(fā)生: {TRIGGER.NAME}故障!
監(jiān)控ID:{ITEM.ID}
告警主機(jī):{HOST.NAME}
告警主機(jī):{HOST.IP}
告警時(shí)間:{EVENT.DATE} {EVENT.TIME}
告警等級(jí):{TRIGGER.SEVERITY}
告警信息: {TRIGGER.NAME}
告警項(xiàng)目:{TRIGGER.KEY}
問(wèn)題詳情:{ITEM.NAME}:{ITEM.VALUE}
當(dāng)前狀態(tài):{TRIGGER.STATUS}:{ITEM.VALUE}
事件ID:{EVENT.ID}
恢復(fù)操作
Zabbix告警:
副務(wù)器:{HOST.NAME}發(fā)生: {TRIGGER.NAME}已恢復(fù)!
監(jiān)控ID:{ITEM.ID}
告警主機(jī):{HOST.NAME}
告警主機(jī):{HOST.IP}
告警時(shí)間:{EVENT.DATE} {EVENT.TIME}
告警等級(jí):{TRIGGER.SEVERITY}
告警信息: {TRIGGER.NAME}
告警項(xiàng)目:{TRIGGER.KEY}
問(wèn)題詳情:{ITEM.NAME}:{ITEM.VALUE}
當(dāng)前狀態(tài):{TRIGGER.STATUS}:{ITEM.VALUE}
事件ID:{EVENT.ID}
可以手動(dòng)觸發(fā)一個(gè)報(bào)警測(cè)試效果,手機(jī)上就可以收到帶圖的報(bào)警了,點(diǎn)擊消息之后的頁(yè)面也可以看到歷史的圖片
歡×××陳師傅”
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。