天天看點

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,如需轉載請自行聯系原作者