Share a custom Zabbix alarm script that is used on the line, and the script idea is roughly as follows:
1. Use the crawler to get the alarm picture (if you want to get the alarm item)
2. Integrate images with email content
3. Send mail
4. Logging
The script reads as follows:
#!/usr/bin/python#coding:utf-8import sys,time,re,os,globimport smtplibfrom email.mime.text import mimetextfrom email.mime.image import mimeimagefrom email.mime.multipart import MIMEMultipartimport urllib,urllib2,cookielibimport zlibmail_host = ' * * * * ' # e-mail Sender-related information mail_user = ' ************ ' mail_pass = ' ************ ' mail_postfix = ' ************ ' zabbix_ user = "********" # Zabbix-web related information zabbix_pass = "********" url = "http://***********/" alartscript_path= "/****** /alertscripts/" # mail script and log location line = " ItemID "Me = "Zabbix" + ' < ' +mail_user+ ' > ' ######### use crawlers to obtain images to ensure the availability of cookies def get_graph (ItemID): login_url = url + "index.php" post_value = {"Request": ", "Name":zabbix_user, "Password":zabbix_pass, "Autologin": "1", #注意这个是否记住密码的选项 , will cause the generated cookie length is different, short can look at the graph but no data "Enter": "Sign in" } post_data = urllib.urlencode (Post_value) headers = { "User-agent": "mozilla/5.0 (windows nt 10.0; wow64; rv:43.0) Gecko/20100101 firefox/43.0 ", "Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8", " Accept-encoding ":" Gzip, deflate ", "Accept-language": "zh-cn,zh;q=0.8,en-us;q=0.5,en;q=0.3", " Connection ":" Keep-alive ", &NBSP;&Nbsp; " Host ":" ************** ", "Referer":login_url, ########## #这个cookie很重要, requires that the current cookie is currently in use on the browser, in order to successfully take pictures " Cookie ":" ******** zbx_sessionid=********* " } request = Urllib2. Request (login_url,post_data,headers) cj = cookielib. Cookiejar () opener = urllib2.build_opener (urllib2. Httpcookieprocessor (cj)) urllib2.install_opener (opener) result = opener.open (Request ) print result.info () #查看下内容信息, if it is gzip to extract write file result_html = zlib.decompress (Result.read (), 16+zlib. Max_wbits) global graph_url # : Build url graph_url = url+ "chart.php" + "for accessing pictures? Itemids= "+str (ItemID) graph_data = urllib2.urlopen ( Graph_url) print graph_data.info () #.get (' Content-Encoding ' ) #graph_html = zlib.decompress (Graph_data.read (), 16+zlib. max_wbits) print graph_data.info (). Get (' Content-Type ') image_data = graph_data.read () if graph_data.info (). Get (' Content-type ') == "Image/png": image_file = open (' Last_%s.pnj '%itemid, ' WB ') &nbSp;image_file.write (Image_data) image_file.close () else: trigged = open (' Zabbix.png ', ' RB ') # if get image data failed, Open a prepared map, here is the use of Zabbix logo, in the mail is very direct reaction to get the picture failure image_data = trigged.read () trigged.close () print "Graph_url:", Graph_url return image_data def mail_con (TxtData,imageData): # Rewrite message content (add picture) msg = mimemultipart (' related ') msg[' Subject '] = subject msg[' from ' ] = me msg[' to '] = receiver con_txt = mimetext (txtdata,_subtype= ' HTML ', _charset= ' utf-8 ') msg.attach (con_txt) con_img = mimeimage (ImageData) con_img.add_ Header (' Content-id ', ' digglife ') # embed the image you want to send into the header of the message and rewrite the message content msg.attach (con_img) return msg.as_string () &Nbsp; def send_mail (receiver,subject,contents): # email and record the results logfile = open (' Alarm_mail.log ', ' a ') try: s = smtplib. SMTP () S.connect (mail_host) s.login (Mail_user,mail_pass) s.sendmail (me,receiver,contents) s.close () &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBsp; log = time.ctime () + "\tok\t" + subject + "\ t" + receiver + "\ n" except exception,e: Log = time.ctime () + "\tfail\t" + subject + "\ T" + receiver + "\ n" logfile.write ("\ n" + "\ T" +graph_url+ "\ n") logfile.write (log) logfile.close ( ) if __name__ == "__main__": &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;CUR_PWD&NBSP;=&NBSP;OS.GETCWD () Print cur_pwd os.chdir (Alartscript_path) The path of the # zabbix-server work is/, where the mail script path is switched, Can write log and picture processing to reduce errors old_pnjs = glob.glob (' Last_*.pnj ') for old _file in old_pnjs: os.remove (Old_file) receiver = sys.argv[1] subject = sys.argv[2] src_mail_con = sys.argv[3] try: Itemid = re.match (R '. *itemid: (\d+) [^\d]+.* ', src_mail_con.replace (' \ n ', '), re. S). Group (1) &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;#&NBSP; get itemid except: from mail content using regular ItemID = 0000 # item 0000 is an empty diagram that reduces anomalies print "ItemID:",itemid imagedata = get_graph (ItemID) mailcon = mail_con (Src_mail_con,ImageData) send_mail (Receiver,subject,mailcon)
The ' ******** ' section of the script is hidden from the security of personal information and can be modified by the reader (Outbox information, Zabbix-web login information and cookies).
The script first obtains the alarm itemid, as well as the message content to place the picture information, therefore also must customize the alarm message content, as follows:
Message Content :
<body> Failure Notification <br/> trigger: { trigger.name}<br/> Host name: {Host.name} ({IPADDRESS}) <br/> Status: {trigger. status}<br/> Alarm level: {trigger. Severity}<br/> url: {trigger. url}<br/> <br/> Alarm Project:<br/> <br/> {item. name1} ({HOST. Name1}:{item. KEY1}): {item. Value1}<br/> <br/> <div style= "Line-height : 1.7;color: #000000; font-size:14px;font-family:arial "> </div> itemid:{item.id}<br/> eventid: {event.id}<br/> Alarm time: {EVENT. Date} {event. time} <br/> <br/> More information: <a href= "/http /history.php?action=showgraph&itemids[]={item.id} " target=" _blank "> Login view </a><br/ > </body>
This article from the "11414123" blog, reproduced please contact the author!
Online use of Zabbix alarm script (with picture)