Wechat official account: Operation and maintenance development Story: author: Wanger

Implementation idea:

  1. First of all, there should be an ItemID in the alarm message. This is the premise. According to the parameters passed in the message, a re is used to match the ItemID

  2. Construct a session session, or log in using cookies, request pictures according to ItemID, and save the obtained pictures locally. Since markdown’s picture link needs to be accessed, my Zabbix is in the Intranet. So you need to upload the image to the map bed or to a Web server with a public IP address, which I did on my personal server

  3. Convert the alarm information to markdown syntax format

  4. Construct requests to send messages using a pinned Webhook

Set up the nailing robot

  • I have defined the keyword “notification”, which means that the message sent must contain this word

Script implementation

  • Since images need to be copied from local to remote, security exemption needs to be configured in advance. The script is executed by zabbix user, and the security policy of the Web server directory should be set up
#! /usr/bin/python # -*- coding: Utf-8 - * - import requests, time import json, sys, re and OS zabbixserver_url = 'http://192.168.99.200/index.php' web server address on the far side of # define. Copy images to the far side of the web directory pname_path = 'http://47.103.15.51/dingding_pic/' pictures of # define access address testUrl = "http://192.168.99.200/chart.php" The host = '192.168.99.200 def get_itemid () : itemid=re.search(r'ITEM ID:(\d+)',sys.argv[2]).group(1) return itemid def get_picture(itemid,pname): MyRequests = requests.Session() try: loginHeaders = { "Host":host, "Accept":"text/html,application/xhtml+xml,application/xml; Q = 0.9, image/webp image/apng, * / *; PlayLoad = {"name": "Admin", "password": 'zabbix', "autologin": "1", "enter": "Sign in", } myRequests.post(url=zabbixserver_url, headers=loginHeaders, data=playLoad) testUrlplayLoad = { "from": "now-10m", "to": "now", "itemids": itemid, "width": "700", } testGraph = myRequests.get(url=testUrl,params=testUrlplayLoad) IMAGEPATH = OS. Path. Join ('/usr/lib/zabbix alertscripts/dingding_pic/', pname) # will get image data written to the file with the open (IMAGEPATH, 'wb) as f: OS f.w rite (testGraph. The content). The system (" sudo SCP % s [email protected]: / usr/share/nginx/HTML/dingding_pic "% IMAGEPATH) pname_url = pname_path+pname return pname_url except Exception as e: Def send_msg(pname_url,info3): headers = {' content-type ': 'application/json '; Charset =utf-8'} print(info3) data = {"msgtype": "markdown", "markdown": {"title": info1, "text": "## inform :\n"+info3+"! [screenshot](%s)\n"%(pname_url) }, "at":{ "atMobiles": reminders, "isAtAll": False,},} r = requests. Post (url= webhook_URL,json=data,headers=headers) print(headers) def info_text(): new_text = "" x = info2.split('\n') for i in x: if re.search('ITEM ID',str(i)): pass else: new_text+="- "+str(i)+('\n') print(type(new_text)) return new_text if __name__ == '__main__': Os.system ("echo hello >> / TMP /syslog.md") pname = STR (int(time.time()))+'.png' info1 = STR (sys.argv[1]) infO2  = str(sys.argv[2]) info3 = info_text() with open('/tmp/syslog.md','a') as f: f.write(info1) f.write(info2) f.close() reminders = [] webhook_url = 'https://oapi.dingtalk.com/robot/send?access_token=771ded387e6be652c51a2b6c83cade4e048e3da4fdfe128f1db6b124b87df18a' itemid = get_itemid() pname_url=get_picture(itemid,pname) print(pname_url) send_msg(pname_url,info3)Copy the code

Define alarm media types

  • Open the Zabbix monitoring Web, select the alarm media type in the management menu, create the media type, select the script, fill in the script name zabbix_email_pic.py, script parameters, and finally add

  • Open the User in management, click the user for which you want to set email alarms, and add alarm media to alarm media. In the dialog box that is displayed, select the type defined just now, and add the user to be customized by recipient

Defining alarm Actions

Click on actions in the Configuration menu, create actions, and then fill them in according to the picture

Operation Default Title Zabbix Alarm: Server :{HOSTNAME} Occurred :{TRIGGER.NAME} Fault! ITEMID:{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 ITEM :{trigger. KEY} Problem details :{item. NAME}:{item. VALUE} Current STATUS :{trigger. STATUS}:{item. VALUE} EVENT ID:{EVENT Zabbix alarm: Server :{host. NAME} Occurred :{trigger. NAME} recovered! ITEMID:{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 ITEM :{trigger. KEY} Problem details :{item. NAME}:{item. VALUE} Current STATUS :{trigger. STATUS}:{item. VALUE} EVENT ID:{event.id}Copy the code

The final result

You can manually trigger an alarm to test the effect

  • So far, Zabbix’s wechat, mailbox and staked alarm with picture have been configured

Welcome to communicate with us