1.cd /home/
vim zabbix_send_mail.py
#!/usr/bin/python
# -*- coding:utf-8 -*-
# http://www.cnblogs.com/hoods/p/6413330.html
# http://blog.csdn.net/xiegh2014/article/details/76572556
# http://www.runoob.com/python/python-email.html
# 感谢上面三位仁兄的源码。
# 各取一半一半,揉合之。
# 能达到获取图片与发送邮件的效果。
# 略粗糙,仅参考。
# 2017-10-24
#
import HTMLParser
import urlparse
import urllib
import urllib2
import cookielib
import string
import os
import smtplib
import datetime
import cookielib, urllib2, urllib
from email.header import Header
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from email.mime.image import MIMEImage
host = "192.168.31.131"
smtpserver = 'smtp.aliyun.com'
mail_username = 'user1@aliyun.com'
mail_password = 'redhat'
receiver = 'user2@aliyun.com'
#receiver = ['user3@aliyun.com', 'user4@aliyun.com']
# 如需多个收到邮件
Subject = Header('测试','utf-8')
# 图片页面
hosturl = "http://192.168.31.131/chart2.php?graphid=796&period=3600"
# post数据接收和处理的页面(我们要向这个页面发送我们构造的Post数据)
posturl = "http://192.168.31.131/index.php"
# 设置一个cookie处理器,它负责从服务器下载cookie到本地,并且在发送请求时带上本地的cookie
cj = cookielib.LWPCookieJar()
cookie_support = urllib2.HTTPCookieProcessor(cj)
opener = urllib2.build_opener(cookie_support, urllib2.HTTPHandler)
urllib2.install_opener(opener)
# 打开图片页面(他的目的是从页面下载cookie,这样我们在再送post数据时就有cookie了,否则发送不成功)
h = urllib2.urlopen(hosturl)
# 构造header,一般header至少要包含一下两项。
# 抓包得到。
headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:56.0) Gecko/20100101 Firefox/56.0',
'Referer': 'http://192.168.31.131/zabbix.php?action=dashboard.view'}
# 构造Post数据。
# 抓包得到。
postData = {
'name': 'Admin', # 用户名
'password': 'zabbix', # 密码
'autologin': 1,
'enter': 'Sign in'
}
# 需要给Post数据编码
postData = urllib.urlencode(postData)
# 通过urllib2提供的request方法来向指定Url发送我们构造的数据,并完成登录过程
request = urllib2.Request(posturl, postData, headers)
response = urllib2.urlopen(request)
text = response.read()
def get_graph(host, graphid, period, image_name):
path = "/tmp/test/"
#zabbix的图片的地址的构造
url = "http://%s/chart2.php?graphid=%s&period=%s" % (host, graphid, period)
img_req = urllib2.Request(url)
png = urllib2.urlopen(img_req).read()
file = path + image_name + '.png'
with open(file,'wb') as f:
f.write(png)
def SendMail(receiver, Subject, smtpserver, mail_username, mail_password):
msgRoot = MIMEMultipart('related')
msgRoot['Subject'] = Subject
msgRoot['From'] = ("%s<youyou12@aliyun.com>") % (Header('testt!!!', 'utf-8'),)
sendText = '<b>Some <i>HTML</i> text</b> and an image.<br><img src="cid:image1"><br>'
msgText = MIMEText(sendText, 'html', 'utf-8')
msgRoot.attach(msgText)
fp = open('/tmp/test/cpu_load.png', 'rb')
msgImage = MIMEImage(fp.read())
fp.close()
msgImage.add_header('Content-ID', '<image1>')
msgRoot.attach(msgImage)
smtp = smtplib.SMTP()
smtp.connect(smtpserver)
smtp.login(mail_username, mail_password)
smtp.sendmail(mail_username, receiver, msgRoot.as_string())
smtp.quit()
get_graph(host, 796, 3600, 'cpu_load')
SendMail(receiver, Subject, smtpserver, mail_username, mail_password)
2.mkdir /tmp/test/
3.python zabbix_send_mail.py
4.ls /tmp/test/ # 图片获取成功
cpu_load.png
5.查看邮件 # 成功收到邮件