Quantcast
Channel: CodeSection,代码区,Python开发技术文章_教程 - CodeSec
Viewing all articles
Browse latest Browse all 9596

如何通过python获取zabbix中的graph报表

$
0
0

正如我们都知道的,zabbix可以提供一些历史数据,例如:近7天的CPU使用情况,近14天的内存使用情况,近半年的磁盘使用情况,近7天的网络流量情况,还有一些自定义的数据

有时候,我们可能需要按照日期进行有序的整理报告,例如,我们需要每周给客户发送客户服务器的使用情况,包含以上的信息,我们当然不愿意每周手动去截图,这个太浪费时间了,所以我们可以通过python 来自动化的做这个事情

首先,zabbix是有提供api的,但是不支持直接给出pnp图片,所以,我们要做的就是模拟登陆zabbix之后,然后下载我们的图片,代码如下:

唯一需要注意的地方就是:如何获取我们的

https://localhost/zabbix/chart.php?sid=c6eaf3f5b92cf103&period=604800&stime=20180811034616&itemids%5B0%5D=2187887&itemids%5B1%5D=2188075&itemids%5B2%5D=2188263&type=1&batch=1&updateProfile=0&profileIdx=&profileIdx2=&width=1136&screenid=&curtime=1472111226924

获取方法如下:

首先,我们先手动获取一个graph,手动获取是的,因为你第一次要获取上边我们的连接,graph的id等,获取到graph之后(获取方法我就不赘述了吧,查看历史数据大家应该都用过),并不是当前连接,通过火狐或者chrome,定位到图片部分,你就可以看到如下的代码:

chart.php?sid=c6eaf3f5b92cf103&period=604800&stime=20180811034616&itemids%5B0%5D=2187887&itemids%5B1%5D=2188075&itemids%5B2%5D=2188263&type=1&batch=1&updateProfile=0&profileIdx=&profileIdx2=&width=1136&screenid=&curtime=1472111226924

这个其实就是我们需要的代码,也就是我们的图片路径(再说一遍,这个地址不是graph的url地址),需要查看源代码才可以找到

另外其实这个链接中许多参数,例如周期啊,当前日期的,所以,如果你的图片不正常显示,可以排查下这些参数

# -*- coding: utf8 -*- ''' @author: zhiming www.503error.com ''' import urllib2 import urllib import cookielib import hashlib import re import time from rhsm.certificateimport Content import smtplib from os.path import basename from email.mime.applicationimport MIMEApplication from email.mime.multipartimport MIMEMultipart from email.mime.textimport MIMEText from email.utils import COMMASPACE, formatdate import os class ZABBIX: def __init__(self): self.usercode = "" self.userid = "" self.cj = cookielib.LWPCookieJar(); self.opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(self.cj)); urllib2.install_opener(self.opener); def login(self, name, pwd,urls): loginPostData = { 'name' : name, 'password' : pwd , 'autologin':1 , 'enter': 'Sign in', 'request':'' }; loginRequest = urllib2.Request('https://localhost/zabbix/index.php',urllib.urlencode(loginPostData)); loginRequest.add_header('Accept','*/*'); loginRequest.add_header('Accept-Language','zh-CN,zh;q=0.8'); loginRequest.add_header('User-Agent','Mozilla/5.0 (windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/28.0.1500.72 Safari/537.36'); loginRequest.add_header('Content-Type','application/x-www-form-urlencoded'); sendPost = urllib2.urlopen(loginRequest).read(); print 'start donwload' for key,valuein downloadurl.items(): print 'key',key print 'value',value currenttime = time.strftime("%s") currentday = time.strftime("%y%m%d") if os.path.exists(key+currentday+".png"): print 'already donwload ,ignore' else: print 'start download :',key try: f = urllib2.urlopen(value,timeout=60) data = f.read() #print 'the data is %s'% data with open(key+currentday+'.png', "wb") as code: code.write(data) print 'done the download' except Exception,e: print e def sendtocus(self,send_from, send_to, subject, text, files=None, server="127.0.0.1"): msg = MIMEMultipart() msg['From'] = send_from msg['To'] = send_to msg['Date'] = formatdate(localtime=True) msg['Subject'] = subject msg.attach(MIMEText(text)) for f in filesor []: with open(f, "rb") as fil: part = MIMEApplication( fil.read(), Name=basename(f) ) part['Content-Disposition'] = 'attachment; filename="%s"' % basename(f) msg.attach(part) smtp = smtplib.SMTP(server) smtp.sendmail(send_from, send_to, msg.as_string()) smtp.close() if __name__ == "__main__": test = ZABBIX() downloadurl={"master-cpu":"https://localhost/zabbix/chart.php?sid=c6eaf2cf103&period=604800&stime=20180811034616&itemids%5B0%5D=2187887&itemids%5B1%5D=2188075&itemids%5B2%5D=2188263&type=1&batch=1&updateProfile=0&profileIdx=&profileIdx2=&width=1136&screenid=&curtime=1472111226924", "master-mem":"https://localhost/zabbix/chart.php?sid=c6eaff103&period=604800&stime=20180811034616&itemids%5B0%5D=2187887&itemids%5B1%5D=2188075&itemids%5B2%5D=2188263&type=1&batch=1&updateProfile=0&profileIdx=&profileIdx2=&width=1136&screenid=&curtime=1472111226924" } test.login('maple_m', '****your password***',downloadurl) files = [] todaydate = time.strftime("%y%m%d") for key,valuein downloadurl.items(): files.append(key+todaydate+".png") print 'start send email' content_of_email = """ Hi this is the report of this week thanks OpenShift Online Operation """ test.sendtocus("maple_m@hotmail.com", "maple_m@hotmail.com", "Weekly report of"+todaydate, content_of_email, files)

Viewing all articles
Browse latest Browse all 9596

Trending Articles