思路:找到zabbix图片的url,分析url,读取内容生成图片,邮件发送
1、找到zabbix中该图形,然后 右击图形 复制图片地址链接,下面为 事例
简单分析一下这个url的各个参数:
chart2.php 输出页面
graphid=26962 图形的id
from=2019-06-19+00%3A00%3A00 图形范围选择的开始时间
to=2019-06-19+15%3A00%3A00 图形范围选择的结束时间
profileIdx2=26962 和图形的id一样
width=954 图形宽度
提取以上信息,需要参数为 1、输出页面;2、图形id;3、范围时间;4、图形宽度
2、python脚本事例
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import MySQLdb
import datetime
import cookielib, urllib2,urllib
import smtplib
from email.header import Header
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from email.mime.image import MIMEImage
#mail收件人
receiver = ["测试1 <test@mail>","测试组 <test_group@mail>"]
#mail标题
Subject =Header('昨日带宽图', 'utf-8')
#mail服务器
smtpserver = 'mail.com.cn'
mail_username = 'mail@mail'
mail_password = '12345678'
#zabbix hostname ip
HostName = "10.0.13.104"
#图形名称
GraphsName = "Network traffice"
#图形url
gr_url="http://10.0.13.104/zabbix/chart2.php"
#登陆url
indexURL="http://10.0.13.104/zabbix/index.php"
username="Admin"
password="zabbix"
#生成图片所在路径
image_dir="/etc/zabbix/image"
class ReportForm:
def getGraphID(self,HostName,GraphsName):
graphid = 26962
return graphid
class ZabbixGraph(object):
def __init__(self,url,name,password):
self.url=url
self.name=name
self.password=password
#cookies
cookiejar = cookielib.CookieJar()
urlOpener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cookiejar))
values = {"name":self.name,'password':self.password,'autologin':1,"enter":'Sign in'}
data = urllib.urlencode(values)
request = urllib2.Request(url, data)
try:
urlOpener.open(request,timeout=10)
self.urlOpener=urlOpener
except urllib2.HTTPError, e:
print e
def GetGraph(self,url,values,image_dir):
data=urllib.urlencode(values)
request = urllib2.Request(url,data)
url = self.urlOpener.open(request)
image = url.read()
imagename="%s/%s_%s.png" % (image_dir, values["graphid"], values["from"])
f=open(imagename,'wb')
f.write(image)
def SendMail(self,receiver,Subject,smtpserver,mail_username,mail_password,values,image_dir,HostName,GraphsName):
msgRoot = MIMEMultipart('related')
msgRoot['Subject'] = Subject
msgRoot['From'] = ("%s <mail@mail>")%(Header('流量发送','utf-8'))
msgRoot['to'] = ','.join(receiver)
sendText='<b><br /><fontsize="20">昨日带宽<br /><br><img src="cid:image1"><br /><br /><fontsize="20">最近7天带宽<br /><br><img src="cid:image2">'
msgText = MIMEText(sendText,'html','utf-8')
msgRoot.attach(msgText)
sendpng="%s/%s_%s.png" % (image_dir, values["graphid"], values["from1"])
fp = open(sendpng, 'rb')
msgImage = MIMEImage(fp.read())
fp.close()
sendpng2="%s/%s_%s.png" % (image_dir, values["graphid"], values["from7"])
fp2 = open(sendpng2, 'rb')
msgImage2 = MIMEImage(fp2.read())
fp2.close()
msgImage.add_header('Content-ID', '<image1>')
msgImage2.add_header('Content-ID', '<image2>')
msgRoot.attach(msgImage)
msgRoot.attach(msgImage2)
smtp = smtplib.SMTP()
smtp.connect(smtpserver)
smtp.login(mail_username, mail_password)
smtp.sendmail(mail_username, receiver, msgRoot.as_string())
smtp.quit()
if __name__ == "__main__":
Report = ReportForm()
get_graphid=Report.getGraphID(HostName,GraphsName)
#zabbix 图形 graphid
#print(get_graphid)
stime=datetime.datetime.now().strftime('%Y%m%d%H%M%S')
from1='now-24h'
to='now'
profileIdx='web.graphs.filter'
profileIdx2='26962'
from7='now-7d'
values={"profileIdx":profileIdx,"profileIdx2":profileIdx2,"graphid":get_graphid,"from":from1,"to":to,"period":86400,"width":700,"height":150,"stime":stime}
values2={"profileIdx":profileIdx,"profileIdx2":profileIdx2,"graphid":get_graphid,"from":from7,"to":to,"period":86400,"width":700,"height":150,"stime":stime}
ZabbixG=ZabbixGraph(indexURL,username,password)
ZabbixG.GetGraph(gr_url,values,image_dir)
ZabbixG.GetGraph(gr_url,values2,image_dir)
values={"graphid":get_graphid,"from1":from1,"to":to,"from7":from7}
ZabbixG.SendMail(receiver,Subject,smtpserver,mail_username,mail_password,values,image_dir,HostName,GraphsName)
3、本来脚本为 只发送昨日带宽图, 后又添加了最近7天的图片,所以脚本中出现时间有2个
from1='now-24h'
to='now'
profileIdx='web.graphs.filter'
profileIdx2='26962'
from7='now-7d'
4、注意图片生成位置 及权限, 图片生成名称每天一样 节省删除时间,有需要保存的可 更改生成图片名称。