zabbix是个非常强大的监控工具,可以监控linux和windows的服务器数据,也可以通过自定义key来扩展默认的监控项,但是自带的邮件报警提供的信息却不太友善。本文想通过自定脚本的方式,实现在报警邮件的同时发送对应的图像和url连接。
步骤如下:
- 创建一个脚本媒体报警:修改zabbix脚本报警配置路径:AlertScritpPath为存放脚本的路径
-
### Option: AlertScriptsPath # AlertScriptsPath=${datadir}/zabbix/alertscripts #AlertScriptsPath=/usr/lib/zabbix/alertscripts AlertScriptsPath=/etc/zabbix/alert
- 点击创建媒体类型,并关联默认动作报警
- 为用户启用报警脚本
- 修改第一步关联的报警动作,让其使用脚步发送报警,注意修改仅发送到的报警方式,还要修改报警的触发模版中带上Itemid这个参数(为脚本报警提供参数)
- 最后,准备工作都已完成,将python脚本上传到第1步的路径中即可,此脚本的思路是通过动作报警中的itmeid去获取监控项的item值,并通过history.php获取图形,并提供url连接
将下面脚本中#注意修改下面的zabbix地址 修改到你实际的zabbix server地址哦!
#注意修改日志路径 修改到你实际日志的路径
#! /usr/bin/python # -*- coding: utf-8 -*- import sys,os,re,smtplib,mimetypes,pycurl,json,urllib,StringIO,time,traceback from email.mime.text import MIMEText from email.mime.multipart import MIMEMultipart from email.mime.image import MIMEImage leng=len(sys.argv) def log4py(logpath,logtxt): f=open(logpath,‘a‘) logtime=time.strftime(‘%Y%m%d%H%M%S‘,time.localtime()) f.writelines(logtime+":") f.writelines(logtxt) f.writelines(‘\n‘) f.close() def printmlinestr(mlinestr): d=mlinestr.splitlines() res="" for i in d: i="<br>"+i+"</br>" res+=i+‘\n‘ return res #getvalue 通过正则表达式获取对应itemID def getvalue(string,rexp): r=re.compile(rexp,re.M) #r=re.compile("^ITEMID:[0-9]*",re.M) log4py(logpath,‘method getvalue‘) log4py(logpath,‘string‘) log4py(logpath,string) #for i in string: d=r.findall(string) log4py (logpath,‘rexp:‘) log4py (logpath,rexp) str1=d[0] log4py(logpath, ‘rexresult:‘) log4py(logpath,str1) print str1 # arrstr=str.split("\xa3\xba") arrstr=str1.split(":") result=arrstr[1] log4py(logpath,‘result:‘) log4py(logpath,result) log4py(logpath,‘end method getvalue‘) return result #getpic 获取报警图片,写入文件 def getpic(itemid): log4py(logpath,‘method getpic‘) # try: c = pycurl.Curl() pathroot=‘/etc/zabbix/alert/‘ #path=‘c:/alert.png‘ #head=‘Content-Type:application/json‘ global stime endtime=time.strftime(‘%Y%m%d%H%M%S‘,time.localtime()) tmptime=int(endtime)-10000 stime = repr(tmptime) post_data_dic={"itemid":itemid,"period":"3600","stime":stime} picpath=(pathroot)+(stime)+(itemid)+‘.png‘ print ‘picpath:‘+picpath str=file(picpath,‘wb‘) #注意修改下面的zabbix地址 c.setopt(pycurl.URL, "http://10.1.1.67/zabbix/chart.php") c.setopt(pycurl.WRITEFUNCTION, str.write) c.setopt(pycurl.FOLLOWLOCATION, 1) #c.setopt(pycurl.HEADER, True) c.setopt(c.POSTFIELDS,urllib.urlencode(post_data_dic)) c.perform() log4py(logpath,‘end method getpic‘) return picpath # except Exception,e: # f.writelines(‘expforgetpic:\n‘) # f.writelines(e) def SendMail(receivers,subject,data,picpath,itemid): msg = MIMEMultipart() msg[‘From‘] = "[email protected]" #msg[‘To‘] = "[email protected]" receives=receivers msg[‘To‘] = receivers msg[‘Subject‘] = subject body=printmlinestr(data) file1 = picpath image = MIMEImage(open(file1,‘rb‘).read()) image.add_header(‘Content-ID‘,‘<image1>‘) msg.attach(image) #print txt #print ‘txt end‘ #print type (txt) #注意修改下面的zabbix地址 url=" #msg.attach(txt) html = """ <html> <body> """ html+=body html+="<br><img src=cid:image1></br>" html+="<p><a href="+url+">URLLINK</a></p>" html+=""" </body> </html> """ htm = MIMEText(html,‘html‘,‘gb2312‘) msg.attach(htm) server = smtplib.SMTP() server.connect(‘mail.huazhu.com‘) #server.login(‘htinns\qinlaw‘,‘~liuqing0530‘) #server.sendmail(msg[‘From‘],msg[‘To‘],msg.as_string()) server.sendmail(msg[‘From‘],receives,msg.as_string()) server.quit() #if __name__ == "__main__": #def main #获取zbx的传入参数 ,参数1-收件人地址,参数2-邮件主题,参数3-报警内容 #注意修改日志路径 global logpath logpath=‘/etc/zabbix/alert/ts.log‘ log4py(logpath,"--------------------------startx") rece=sys.argv[1] subject=sys.argv[2] data=sys.argv[3] #picpath=‘c:/alert.png‘ #picpath=‘/etc/zabbix/alert/alert.png‘ #\xa3\xba代表中文的冒号 #以后要加上异常处理,如果没有找到ID或者图片 #itemid=getvalue(data,"^ITEMID\xa3\xba[0-9]*") #itemid=getvalue(data,"^ITEMID:[0-9]*) #f.writelines(data) try: itemid=getvalue(data,"ITEMID:[0-9]*") picpath= getpic(itemid) SendMail(rece,subject,data,picpath,itemid) os.remove(picpath) except Exception,e: log4py(logpath,e) #os.system(‘echo $3|mail -s $2 $1 ‘) shellcmd=‘echo ‘+data+‘|mail -s ‘+subject+‘ ‘+rece print shellcmd os.system(shellcmd) log4py(logpath,"--------------------------end")
补上效果图:
时间: 2024-10-01 04:20:35