In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-01 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >
Share
Shulou(Shulou.com)06/03 Report--
Idea of sending Wechat alarm 2.1with pictures by zabbix
2.2 the python script is used to prepare the environment script, and the running environment is python 2.7.5 dependent library pre-installed: requests2.3 × × tid,secret
For this part, you can see that the previous article without a diagram has a detailed description.
2.4 script implementation #! / usr/bin/python#coding=utf-8_author__ = 'zhangdongdong'import requests, jsonimport urllib3import smtplib,sys,os,time,re,requestsfrom email.mime.image import MIMEImageif sys.getdefaultencoding ()! =' utf-8': reload (sys) sys.setdefaultencoding ('utf-8') urllib3.disable_warnings () class WechatImage (object): # define a class according to WeCom's api interface document, using the mpnews type 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): # get 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): # upload temporary footage images Then return 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): # define the parameter dictionary in mpnews type data =''messages = {} body = {} content_html=text_to_html (content) token = self.get_token (corpid, secret) image = self.get_image_url (token, path) content_html + = "
"% (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): # defines sending data of type 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 # user account "agentid" in enterprise account "agentid": agentid, # application id "msgtype" in enterprise account: "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.textdef text_to_html (text): # convert the text field of the message content to HTML format d=text.splitlines () # store the message content in the list as a list element for each line: html_text='' for i in d: iTunes'+ I +'
'html_text+=i +'\ n'# add the newline tag return html_textdef get_itemid (): # of html to each element of the list to get the itemid itemid=re.search of the alarm (r' monitoring ID: (\ d +)' Sys.argv [3]) .group (1) return itemiddef get_graph (itemid): # get the chart of the alarm and save session=requests.Session () # create a session session try: loginheaders= {"Host": host, "Accept": "text/html,application/xhtml+xml,application/xml" Qcoach 0.9 imageCharpy webpCharge imageUniverse apngMagneTime * QSecret0.8 "} # define request header payload = {" name ": user," password ": password," autologin ":" 1 "," enter ":" Sign in ",} # define incoming data login=session.post (url=loginurl,headers=loginheaders Data=payload) # Log in to graph_params= {"from": "now-10m", "to": "now", "itemids": itemid, "width": "290", # the height and width parameter of the image "height": "40" } # define the parameter graph_req=session.get (url=graph_url,params=graph_params) # send a get request to get the image data time_tag=time.strftime ("% Y%m%d%H%M%S" Time.localtime () graph_name='baojing_'+time_tag+'.png' # use alarm time as picture name to save graph_name= os.path.join (graph_path, graph_name) # use absolute path to save picture with open (graph_name) 'wb') as f: f.write (graph_req.content) # writes the acquired image data to a file return graph_name except Exception as e: print (e) return Falseif _ _ name__ = =' _ _ main__': user='Admin' # defines zabbix user name password='zabbix' # defines zabbix user I password Graph_path='/usr/lib/zabbix/alertscripts/graph/' # defines the image storage path Pictures need to be cleaned regularly graph_url=' http://192.168.73.133/chart.php' # define url loginurl= "http://192.168.73.133/index.php" # define login 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) 2.5Definitions of alarm media types
Open the zabbix monitoring web, select the alarm media type from the management menu, create the media type, select the script, fill in the name of the Wechat script you just wrote, zabbix_weixin_pic.py, script parameters, and finally add
Open the user in the management, click the user who needs to set the email alarm, then add the alarm medium to the alarm medium, select the type just defined in the pop-up box, then fill in the department id created by WeCom, and finally add
Define alarm Action Click the Action in the configuration menu, create an action, and then fill in the action according to the picture default title Zabbix alarm: deputy server: {HOSTNAME} occurrence: {TRIGGER.NAME} failure! Monitoring ID: {ITEM.ID} alarm host: {HOST.NAME} alarm host: {HOST.IP} alarm time: {EVENT.DATE} {EVENT.TIME} alarm level: {TRIGGER.SEVERITY} alarm information: {TRIGGER.NAME} alarm project: {TRIGGER.KEY} problem details: {ITEM.NAME}: {ITEM.VALUE} current status: {TRIGGER.STATUS}: {ITEM.VALUE} event ID: {EVENT.ID} resume exercise Make Zabbix alarm: deputy server: {HOST.NAME} occurs: {TRIGGER.NAME} has been restored! Monitoring ID: {ITEM.ID} alarm host: {HOST.NAME} alarm host: {HOST.IP} alarm time: {EVENT.DATE} {EVENT.TIME} alarm level: {TRIGGER.SEVERITY} alarm information: {TRIGGER.NAME} alarm project: {TRIGGER.KEY} problem details: {ITEM.NAME}: {ITEM.VALUE} current status: {TRIGGER.STATUS}: {ITEM.VALUE} event ID: {EVENT.ID}
2.7 Test effect
You can manually trigger an alarm test effect, and you can receive an alarm with a picture on the phone, and you can also see a historical picture on the page after clicking the message.
Master Huan × × Chen "
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.