天天看点

zabbix实现邮件报警--发送对应graph图片

由于最近在研究zabbix,学习python,于是就拿python练练手,写了下面这个脚本

脚本作用:发送tigger内容并附加发送tirgger所对应的graph

脚本思路:通过zabbix传递的"Default subject"取出触发的tirgger对应的主机host,以及对应的graphname,通过host和graphname在数据库中查询出所对应的graphid,之后通过graphid使用curl通过web页面下载对应graph的图片,最后将图片作为邮件内容发送出去。

脚本依赖:由于获取host和graphname依赖于zabbix传递的参数形式及内容,所以需要做如下设置

    设置1:修改tirgger名字和graph名字对应,grap名字中不包含tirgger中所使用的宏,如:

          tirgger名:Running processes on {HOST.NAME}

          graph名:Running processes on

    设置2:创建Actions时,"Default subject"和"Recovery subject"要设置为:"{HOST.NAME1} {TRIGGER.STATUS}: {TRIGGER.NAME}"

    设置3:在zabbix上创建一个具有只读权限的用户,在zabbix数据库上创建一个具有查询权限的用户

脚本进度:由于个人技能还未达到一定的层次,该脚本在手动执行是正常的,但是在zabbix的Action触发调用时,却不能成功执行,希望有兴趣的朋友给我一些建议和指导,或者为我指出哪里考虑不周全导致的脚本不能正常执行。希望能够得到广大IT爱好者的帮助与建议。

#!/usr/bin/python 

# When: 2012/12/17 

# Who: [email protected] 

import os,sys,smtplib,MySQLdb 

from email.MIMEMultipart import MIMEMultipart 

from email.MIMEText import MIMEText 

from email.MIMEImage import MIMEImage 

######  Variable declaration 

hostname=os.popen('echo $HOSTNAME').read() 

mysql_server="127.0.0.1" 

mysql_user="scripts" 

mysql_pass="135246" 

db_name="zabbix" 

zabbix_url="XXXXXXXXXX" 

zabbix_user="XXXXX" 

zabbix_pass="XXXXXX" 

cookie="/tmp/cookie" 

image_path="/tmp/zabbix-graph/" 

Stime=os.popen('date +%Y%m%d%H%M%S').read().rstrip() 

Period=3600 

Width=1222 

mail_server="127.0.0.1" 

mail_port=25 

mail_from="root"+"@"+"%s" % hostname.rstrip() 

tag = 0 

###### get Host 

def _getHost(Subject): 

        Host = Subject.split(" ")[0] 

        return Host 

###### get Graphname 

def _getgraphname(Subject): 

        tmp1 = Subject.split(" ") 

        tmp2 = tmp1[2:-1] 

        Graphname = " ".join(tmp2) 

        return Graphname 

###### get graphid by Graphname and Host 

def _getgraphid(Graphname,Host): 

        db = MySQLdb.connect(mysql_server,mysql_user,mysql_pass,db_name) 

        cursor = db.cursor() 

        sql = "select graphs.graphid from hosts,items,graphs,graphs_items where hosts.host= '%s' and graphs.name = '%s' and hosts.hostid=items.hostid and items.itemid=graphs_items.itemid and graphs.graphid=graphs_items.graphid;" % (Host,Graphname) 

        cursor.execute(sql) 

        results = cursor.fetchall() 

        db.close() 

        for id in results: 

                graphid = int(id[0]) 

        return graphid 

###### get graph using by curl 

def _getgraph(graphid): 

        os.popen("""curl -c '%s' -b '%s' -d "request=&name='%s'&password='%s'&autologin=1&enter=Sign+in" '%s'/index.php""" % (cookie,cookie,zabbix_user,zabbix_pass,zabbix_url)) 

        os.popen("""curl -b '%s' -F "graphid=%d" -F "period=%d" -F "stime='%s'" -F "width=%d" '%s'/chart2.php > '%s''%s''.png'""" % (cookie,graphid,Period,Stime,Width,zabbix_url,image_path,Stime)) 

        image_name = '%s.png' % Stime 

return image_name 

###### Creating email content 

def _content(Subject,body,mail_to,*img): 

        msgRoot = MIMEMultipart() 

        msgRoot['From'] = mail_from 

        msgRoot['To'] = mail_to 

        msgRoot['Subject'] = Subject 

        msgText = MIMEText(body) 

        msgRoot.attach(msgText) 

        if tag == 0: 

                image_path = img[0] 

                image_name = img[1] 

                f = open('%s%s' % (image_path,image_name),'rb') 

                msgimg = MIMEImage(f.read()) 

                f.close() 

                msgimg.add_header('Content-Disposition', 'attachment', filename = image_name) 

                msgRoot.attach(msgimg) 

        return msgRoot 

######  definition sendmail function 

def send_mail(mail_server,mail_port,mail_from,mail_to,content): 

        mail=smtplib.SMTP(mail_server,mail_port) 

        mail.sendmail(mail_from,mail_to,content) 

        mail.quit() 

###### definition main function 

def main(mail_to,Subject,body): 

        Host = _getHost(Subject) 

                Graphname = _getgraphname(Subject) 

                graphid = _getgraphid(Graphname,Host) 

                image_name = _getgraph(graphid) 

                msgRoot = _content(Subject,body,mail_to,image_path,image_name) 

        else: 

                msgRoot = _content(Subject,body,mail_to) 

        send_mail(mail_server,mail_port,mail_from,mail_to,msgRoot.as_string()) 

if __name__ == "__main__": 

        if "Zabbix agent" in Subject: 

                tag = 1 

        main(sys.argv[1],sys.argv[2],sys.argv[3]) 

邮件内容:

<a href="http://blog.51cto.com/attachment/201212/162613939.jpg" target="_blank"></a>

<a href="http://blog.51cto.com/attachment/201212/173559180.jpg" target="_blank"></a>

<a href="http://blog.51cto.com/attachment/201212/173443626.jpg" target="_blank"></a>

希望得到广大IT爱好者的建议与指导。

本文转自 向阳草米奇 51CTO博客,原文链接:http://blog.51cto.com/grass51/1094098,如需转载请自行联系原作者