首先打開平常的zabbix監控圖頁面,點擊 F12,而後點擊,這個時候選中頁面中的折線圖,就能夠看到 對應的HTML代碼,最後點擊對應的html代碼右鍵 copy下圖片的連接地址,便可知道 zabbix的監控圖 的url。php
1 # -*- coding: utf-8 -*- 2 __author__ = 'xinysu' 3 __date__ = '2017/10/12 14:38' 4 import sys 5 import datetime 6 import http.cookiejar, urllib.request, urllib 7 from lxml import etree 8 import requests 9 class ZabbixChart(object): 10 def __init__(self, name, password): 11 url="http://company.monitor.com/index.php"; 12 self.url = url 13 self.name = name 14 self.password = password 15 cookiejar = http.cookiejar.CookieJar() 16 urlOpener = urllib.request.build_opener(urllib.request.HTTPCookieProcessor(cookiejar)) 17 values = {"name": self.name, 'password': self.password, 'autologin': 1, "enter": 'Sign in'} 18 data = urllib.parse.urlencode(values).encode(encoding='UTF8') 19 request = urllib.request.Request(url, data) 20 try: 21 urlOpener.open(request, timeout=10) 22 self.urlOpener = urlOpener 23 except urllib.request.HTTPError as e: 24 print(e) 25 def download_chart(self, image_dir,itemids,stime,etime): 26 # 此url是獲取圖片是的,請注意餅圖的URL 和此URL不同,請仔細觀察! 27 url="http://company.monitor.com/chart.php"; 28 # 折線圖的大小 30 url_par={} 31 url_par={"width":1778, "height":300,"itemids":itemids} 32 # 開始日期、結束日期從str轉換爲datetime 33 stime = datetime.datetime.strptime(stime, "%Y-%m-%d") 34 etime=datetime.datetime.strptime(etime, "%Y-%m-%d") 35 # 計算period 36 diff_sec = etime - stime 37 period = diff_sec.days*24*3600 + diff_sec.seconds 38 url_par["period"] = period 39 # stime轉換str 40 stime = stime.strftime('%Y%m%d%H%M%S') 41 url_par["stime"] = stime 42 key = url_par.keys() 43 data = urllib.parse.urlencode(url_par).encode(encoding='UTF8') 44 request = urllib.request.Request(url, data) 45 url = self.urlOpener.open(request) 46 image = url.read() 47 html = requests.get('http://company.monitor.com/history.php?action=showgraph&itemids[]={}'.format(itemids)).text 48 page = etree.HTML(html) 49 hostname_itemname = page.xpath('//div[@class="header-title"]/h1/text()')[0].split(':') 50 hostname = hostname_itemname[0] 51 hostname_itemname.pop(0) 52 itemname = '_'.join(hostname_itemname).replace('/','_') 53 imagename = "{}\{}_{}_{}_({}).png".format(image_dir,hostname,stime,etime.strftime('%Y%m%d%H%M%S'),itemname) 54 f = open(imagename, 'wb') 55 f.write(image) 56
根據寫好的類,輸入zabbix的登陸賬號、監控圖的起始跟結束時間、本地存放圖片目錄、itemid的list,運行後以下:html
1 # 登錄URL 2 username = "xinysu" 3 password = "passwd" 4 5 # 圖片的參數,該字典至少傳入graphid 6 stime = "2017-09-01" 7 etime = "2017-10-01" 8 9 # 用於圖片存放的目錄 10 image_dir = "E:\\03 WORK\\03 work_sql\\201709" 11 12 #運行 13 b = ZabbixChart(username, password) 14 item_list =(35295,35328,38080,37992,38102,38014,35059,35022,42765,35024,35028,35035,35036,35044,35045,35046,35047,38248,36369,36370,36371,36372) 15 for i in item_list: 17 itemids = i 18 b.download_chart(image_dir,itemids,stime,etime)
隨便輸入的itemid 測試下載,實際須要根據監控須要過濾itemid,下載後在文件夾中顯示以下:web