Requirements: I hope that the Daily Mail issued on the day of a server monitoring status, if a day to log on Zabbix is very troublesome, and can not guarantee that every day on-time operation, so wrote a script to achieve automatic capture pictures, and assembled into HTML, Automate daily dailies with regular mail delivery.
One:
650) this.width=650; "src=" http://s1.51cto.com/wyfs02/M01/7A/74/wKiom1apuTvxL4RzAAOJ_PnzM98914.jpg "title=" 01.jpg "alt=" Wkiom1aputvxl4rzaaoj_pnzm98914.jpg "/>
Second, the code:
#!/usr/bin/env python# -*- coding: utf-8 -*-import mysqldbimport datetimeimport cookielib, urllib2,urllibimport smtplibfrom email.mime.multipart import mimemultipartfrom email.mime.text import mimetextfrom email.mime.image import mimeimage# Database related information dbhost = "Server IP" dbport = 3306dbuser = "Zabbix logon user" dbpasswd = "Zabbix Database Password" dbname = "Zabbix" #发送邮件配置:receiver = ' recipient email address ' Subject = ' Zabbix monitoring platform data ' smtpserver = ' smtp.exmail.qq.com ' mail_username = ' send email address ' mail_ password = ' password ' #查找zabbix的HostnameHostName = ' zabbix server ' #查找图像名称GraphsName = "Cpu utilization" #此url是获取图片是的, note that the url of the pie chart is not the same as this URL, please watch carefully! Gr_url= "Http://zabbix. xxxx.com/chart2.php "#登陆URLindexURL =" Http://zabbix. xxxx.com/index.php "Username=" Sunday "password=" Aa "#用于图片存放的目录image_dir ="/tmp/zabbix "Class reportform: &nBsp; 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) &nbSp; if self.cursor.execute (SQL): graphid = self.cursor.fetchone () [' Graphid '] else: graphid = none &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;RETURN&NBSP;GRAPHID&NBSP;&NBSP;&NBSP;&NBSP;DEF&NBSP;__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_usernamesendtext= ' <b> server: <i> '%s ' </i></b> extracted image name is <b> "%s" </b><br><br> Thank you! ' % (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 () if __name__ == "__main__": report = reportform () get_graphid=report.getgraphid (hostname,graphsname) #图片的参数, the dictionary is passed in at least Graphid. stime=datetime.datetime.now (). Strftime ('%y%m%d%h%m%s ') values={ "Graphid": Get_graphid, "stime": stime, "period": 86400, "width": +, "height": 200} zabbixg= Zabbixgraph (Indexurl,username,password) zabbixg.getgraph (gr_url,values,image_dir) zabbixg.sendmail (Receiver,subject,smtpserver,maiL_username,mail_password,values,image_dir,hostname,graphsname)
Through the above obtained picture, in the assembly HTML, then uses the system to plan the task can realize the Automation daily dispatch.
This article is from the "Urban Cloth" blog, please be sure to keep this source http://sunday208.blog.51cto.com/377871/1739522
Extracting Zabbix Monitoring Platform Single server graphics concurrent mail Python implementation