提取zabbix监控平台单台服务器图形并发邮件python实现
提取zabbix监控平台单台服务器图形并发邮件python实现
需求:希望每天邮件发出当天某台服务器的监控状态,如果某天都登陆zabbix截图很麻烦,而且并不能保证每天都准点操作,于是写了一段脚本实现自动抓取图片,并组装成html,通过定时邮件发送,实现日报自动化。
一、效果图:
二、代码:
#!/usr/bin/envpython #-*-coding:utf-8-*- importMySQLdb importdatetime importcookielib,urllib2,urllib importsmtplib fromemail.mime.multipartimportMIMEMultipart fromemail.mime.textimportMIMEText fromemail.mime.imageimportMIMEImage #数据库相关信息 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="Zabbixserver" #查找图像名称 GraphsName="CPUutilization" #此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" classReportForm: 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) defgetGraphID(self,HostName,GraphsName): #获取graphid sql='selectdistinctgraphs_items.graphidfromitemsjoingraphs_itemsongraphs_items.itemid=items.itemidjoingraphsongraphs_items.graphid=graphs.graphidwhereitems.hostid=(selecthostidfromhostswherehost="%s")andgraphs.name="%s"'%(HostName,GraphsName) ifself.cursor.execute(sql): graphid=self.cursor.fetchone()['graphid'] else: graphid=None returngraphid def__del__(self): #关闭数据库连接 self.cursor.close() self.conn.close() classZabbixGraph(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":'Signin'} data=urllib.urlencode(values) request=urllib2.Request(url,data) try: urlOpener.open(request,timeout=10) self.urlOpener=urlOpener excepturllib2.HTTPError,e: printe defGetGraph(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) defSendMail(self,receiver,Subject,smtpserver,mail_username,mail_password,values,image_dir,HostName,GraphsName): msgRoot=MIMEMultipart('related') msgRoot['Subject']=Subject msgRoot['From']=mail_username sendText='服务器:"%s"提取的图像名称为"%s" 多谢!'%(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','') 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,image_dir) ZabbixG.SendMail(receiver,Subject,smtpserver,mail_username,mail_password,values,image_dir,HostName,GraphsName)
通过以上获取的图片,在组装html,然后使用系统计划任务可实现自动化日报发送。
评论关闭