1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131
| import sys import requests import json import os import time import re url = 'http://10.1.1.59/zabbix/api_jsonrpc.php' headers = {'Content-Type': 'application/json-rpc'} graph_path = '/data/zabbix/images/' graph_url = 'http://10.1.1.59/zabbix/chart.php' loginurl = "http://10.1.1.59/zabbix/index.php" def uploadImg(path,accessToken): img_url = "https://qyapi.weixin.qq.com/cgi-bin/media/uploadimg?access_token="+accessToken files = {'media': open(path, 'rb')} r = requests.post(img_url, files=files) re = json.loads(r.text) print(re) return re['url'] def get_itemid(message): itemid = re.search(r'ITEMID:(\d+)', message).group(1) return itemid def get_imgUrl(itemid): session = requests.Session() try: loginheaders = { "Host": "10.1.1.59", "Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9" } payload = { "name": 'yantao', "password": 'xxxxxxx', "autologin": "1", "enter": "Sign in", } login = session.post(url=loginurl, headers=loginheaders, data=payload) print(login) graph_params = { "from": "now-10m", "to": "now", "itemids": itemid, "width": "400", } graph_req = session.get(url=graph_url, params=graph_params) 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 def getAccessToken(): api_url = "https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid=corpid&corpsecret=corpsecret" content = requests.get(api_url) return content.json().get("access_token") def getImgUrl(mediaId,accessToken): api_url = "https://qyapi.weixin.qq.com/cgi-bin/media/get?access_token="+accessToken+"&media_id="+mediaId content = requests.get(api_url) print(mediaId) print(content.json()) return content.json().get("url") def send_message(imgUrl,title,desc,openUrl,key): url = "https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=" + key params = { "msgtype": "template_card", "template_card": { "card_type": "news_notice", "source": { "desc": "Zabbix网络警报", "desc_color": 0 }, "main_title":{ "title":"Zabbix网络警报", }, "quote_area":{ "type":0, "quote_text":desc }, "card_image": { "url": imgUrl }, "card_action": { "type": 1, "url": openUrl, "appid": "APPID", "pagepath": "PAGEPATH" } } } req = requests.post(url, data=json.dumps(params)) print(req.json()) if __name__ == '__main__': message = sys.argv[1] print(message) itemid = get_itemid(message) imgpath = get_imgUrl(itemid) accessToken = getAccessToken(); imgUrl = uploadImg(imgpath,accessToken) print(imgUrl) send_message(imgUrl,sys.argv[2],sys.argv[3],imgUrl,sys.argv[4])
|