要求:期待日常邮件发出的那一天,某网络服务器的监管。如果某一天登录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.p_w_picpath 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)" #用以图片储放的文件目录 p_w_picpath_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,p_w_picpath_dir): data=urllib.urlencode(values) request = urllib2.Request(url,data) url = self.urlOpener.open(request) p_w_picpath = url.read() p_w_picpathname="%s/%s_%s.png" % (p_w_picpath_dir, values["graphid"], values["stime"]) f=open(p_w_picpathname,'wb') f.write(p_w_picpath) def SendMail(self,receiver,Subject,smtpserver,mail_username,mail_password,values,p_w_picpath_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="cid:p_w_picpath1"><br>谢谢!' % (HostName,GraphsName) msgText = MIMEText(sendText,'html','utf-8') msgRoot.attach(msgText) sendpng="%s/%s_%s.png" % (p_w_picpath_dir, values["graphid"], values["stime"]) fp = open(sendpng, 'rb') msgImage = MIMEImage(fp.read()) fp.close() msgImage.add_header('Content-ID', '<p_w_picpath1>') 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() if __name__ == "__main__": Report = ReportForm() get_graphid=Report.getGraphID(HostName,GraphsName) #图片的主要参数,该词典最少传到graphid。 stime=datetime.datetime.now().strftime('%Y%m%d%H%M%S') values={"graphid":get_graphid,"stime":stime,"period":86400,"width":800,"height":200} ZabbixG=ZabbixGraph(indexURL,username,password) ZabbixG.GetGraph(gr_url,values,p_w_picpath_dir) ZabbixG.SendMail(receiver,Subject,smtpserver,mail_username,mail_password,values,p_w_picpath_dir,HostName,GraphsName) 根据之上获得的图片,在拼装html,随后应用系统软件任务计划可完成自动化技术日报推送。欢迎分享,转载请注明来源:内存溢出
评论列表(0条)