需求:希望每天邮件发出当天某台服务器的监控状态,如果某天都登陆zabbix截图很麻烦,而且并不能保证每天都准点操作,于是写了一段脚本实现自动抓取图片,并组装成html,通过定时邮件发送,实现日报自动化。
一、效果图:
二、代码:
#!/usr/bin/env Python
# -*- coding: utf-8 -*-
import MySQLdb
import datetime
import cookielib, urllib2,urllib
import smtplib
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from email.mime.image import MIMEImage
#数据库相关信息
dbhost = "服务器IP"
dbport = 3306
dbuser = "zabbix登录用户"
dbpasswd = "zabbix数据库密码"
dbname = "zabbix"
#发送邮件配置:
receiver = '收件人邮箱地址'
Subject = 'zabbix监控平台数据'
smtpserver = 'smtp.exmail.qq.com'
mail_username = '发送邮箱地址'
mail_password = '密码'
#查找zabbix的Hostname
HostName = "Zabbix server"
#查找图像名称
GraphsName = "CPU utilization"
#此url是获取图片是的,请注意饼图的URL 和此URL不一样,请仔细观察!
gr_url="http://zabbix.XXXX.com/chart2.php"
#登陆URL
indexURL="http://zabbix.XXXX.com/index.php"
username="sunday"
password="Aa(2016)"
#用于图片存放的目录
image_dir="/tmp/zabbix"
class ReportForm:
def __init__(self):
#打开数据库连接
self.conn = MySQLdb.connect(host=dbhost,user=dbuser,passwd=dbpasswd,db=dbname,port=dbport,charset='utf8')
self.cursor = self.conn.cursor(cursorclass=MySQLdb.cursors.DictCursor)
def getGraphID(self,HostName,GraphsName):
#获取graphid
sql = 'select distinct graphs_items.graphid from items join graphs_items on graphs_items.itemid=items.itemid join graphs on graphs_items.graphid=graphs.graphid where items.hostid=(select hostid from hosts where host="%s") and graphs.name="%s"' % (HostName,GraphsName)
if self.cursor.execute(sql):
graphid = self.cursor.fetchone()['graphid']
else:
graphid = None
return graphid
def __del__(self):
#关闭数据库连接
self.cursor.close()
self.conn.close()
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["stime"])
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'] = mail_username
sendText='<b>服务器: <i>"%s"</i></b> 提取的图像名称为<b>"%s"</b><br><img src="https://www.linuxidc.com/cid:image1"><br>多谢!' % (HostName,GraphsName)
msgText = MIMEText(sendText,'html','utf-8')
msgRoot.attach(msgText)
sendpng="%s/%s_%s.png" % (image_dir, values["graphid"], values["stime"])
fp = open(sendpng, '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()