342144 发表于 2016-7-25 09:49:34

Python脚本通过邮件发送zabbix报警图片

Python脚本通过邮件发送zabbix报警图片流程如下:


[*]通过zabbix传递给脚本的message参数,筛选出报警信息的itemid;

[*]通过获取的itemid,在数据库中查找对应的grpahid;

[*]拉取对应graphid的图片并保存;

[*]将报警信息和图片组装成html;

[*]发送邮件。


Python脚本如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
#!/usr/bin/python
#coding=utf-8
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
from email.mime.image import MIMEImage
import MySQLdb,smtplib,sys,os,time,re
user=''
#zabbix用户名
password=''
#zabbix密码
url=''
#zabbix首页
period='3600'
chart2_url=''
#zabbix获取图片url http://192.168.0.1/chart2.php
mysql_host=''
mysql_user=''
mysql_pass=''
mysql_db=''
#zabbix数据库相关信息
graph_path='/usr/local/zabbix/alertscripts/'
#图片保存路径

def get_itemid():
    #获取itemid
    a=re.findall(r"ITEM ID: \d+",sys.argv)
    i=str(a)
    itemid=re.findall(r"\d+",i)
    return str(itemid).lstrip('[\'').rstrip('\']')

def get_graphid(itemid):
    #获取graphid
    conn =MySQLdb.connect(host=mysql_host,user=mysql_user,passwd=mysql_pass,db=mysql_db,connect_timeout=20)
    cur=conn.cursor()
    cur.execute("SELECT graphidFROM `graphs_items` WHERE `itemid`=%s;" %itemid)
    result=cur.fetchone()
    cur.close()
    conn.close()
    graphid=re.findall(r'\d+',str(result))
    return str(graphid).lstrip('[\'').rstrip('\']')

def get_graph():
    #拉取图片
    time_tag=time.strftime("%Y%m%d%H%M%S", time.localtime())
    os.system('curl -L -c /usr/local/zabbix/alertscripts/cookie.txt --user-agent Mozilla/4.0-d "reauest=&name=%s&password=%s&autologin=1&enter=Sign+in"%s' %(user,password,url))
    os.system('curl -c /usr/local/zabbix/alertscripts/cookie.txt -b /usr/local/zabbix/alertscripts/cookie.txt --user-agent Mozilla/4.0 -F "graphid=%s" -F "period=%s" -F "width=900" %s > /usr/local/zabbix/alertscripts/zabbix_%s_%s.png' %(graphid,period,chart2_url,graphid,time_tag))
    graph_name= '/usr/local/zabbix/alertscripts/' + 'zabbix_' + graphid + '_' + time_tag+'.png'
    return graph_name

def text_transfe_html(text):
    #将message转换为html
    d=text.splitlines()
    html_text=''
    for i in d:
      i='' + i + '</br>'
      html_text+=i + '\n'
    return html_text

def send_mail(to_email,subject):      
    #发送邮件
    graph_name=get_graph()
    html_text=text_transfe_html(sys.argv)
    smtp_host = 'smtp.exmail.qq.com'
    from_email = ''
    #邮箱账户
    passwd = ''
    #邮箱密码
    msg=MIMEMultipart('related')
    fp=open(graph_name,'rb')
    image=MIMEImage(fp.read())
    fp.close()
    image.add_header('Content-ID','<image1>')
    msg.attach(image)
    html="""
   <html>
      <body>
    """
    html+=html_text
    html+='<img src="cid:image1"></br>'
    html+="""
      </body>
    </html>
    """
    html=MIMEText(html,'html','gb2312')
    msg.attach(html)
    msg['Subject'] = subject
    msg['From'] = from_email
    smtp_server=smtplib.SMTP_SSL()
    smtp_server.connect(smtp_host,'465')
    smtp_server.login(from_email,passwd)
    smtp_server.sendmail(from_email,to_email,msg.as_string())
    smtp_server.quit()

if __name__ == '__main__':
    to=sys.argv
    subject=sys.argv
    itemid=get_itemid()
    graphid=get_graphid(itemid)
    send_mail(to,subject)




脚本实现效果如下:



页: [1]
查看完整版本: Python脚本通过邮件发送zabbix报警图片