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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
| #!/usr/bin/python
#coding:utf-8
import sys,time,re,os,glob
import smtplib
from email.mime.text import MIMEText
from email.mime.image import MIMEImage
from email.mime.multipart import MIMEMultipart
import urllib,urllib2,cookielib
import zlib
mail_host = '************' # 邮件发送方相关信息
mail_user = '************'
mail_pass = '************'
mail_postfix = '************'
zabbix_user = "********" # zabbix-web相关信息
zabbix_pass = "********"
url = "http://***********/"
alartscript_path="/**********/alertscripts/" # 邮件脚本及日志位置
line = "ItemID"
me = "zabbix"+'<'+mail_user+'>'
######### 利用爬虫获取图片,保证cookie的可用性
def get_graph(itemID):
login_url = url + "index.php"
post_value = {"request":"",
"name":zabbix_user,
"password":zabbix_pass,
"autologin":"1", #注意这个是否记住密码的选项,将造成生成的cookie长度不一样,短的能查看到图但没有数据
"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",
"Host":"**************",
"Referer":login_url,
###########这个cookie很重要,要求当前cookie正在浏览器上正常使用,才能顺利取图
"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() #查看下内容信息,如果是gzip要解压写入文件
result_html = zlib.decompress(result.read(), 16+zlib.MAX_WBITS)
global graph_url # 构建用于访问图片的URL
graph_url = url+"chart.php"+"?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')
image_file.write(image_data)
image_file.close()
else:
trigged = open('zabbix.png','rb') # 如果获取图片data失败,打开一张事先准备好的图,这里使用的是zabbix的logo,在邮件中很直接的反应出获取图片失败
image_data = trigged.read()
trigged.close()
print "Graph_URL:",graph_url
return image_data
def mail_con(txtData,imageData): # 重写邮件内容(添加图片)
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') # 将要发送的图片嵌入到邮件头部,重写邮件内容
msg.attach(con_img)
return msg.as_string()
def send_mail(receiver,subject,contents): # 邮件发送,并将结果记录
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()
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__":
cur_pwd = os.getcwd()
print cur_pwd
os.chdir(alartscript_path) # zabbix-server工作的路径为/,在这里切换邮件脚本路径,可以写入日志和图片处理,减少出错
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) # 利用正则从邮件内容中获取itemID
except:
ItemID = 0000 # item为0000的是一张空图,减少异常情况
print "ItemID:",ItemID
ImageData = get_graph(ItemID)
MailCon = mail_con(src_mail_con,ImageData)
send_mail(receiver,subject,MailCon)
|