zabbix items 历史数据导出python脚本

zabbix items采集到的数据不支持页面导出,这对于需要源数据进行二次加工或生成报表来说不是很友好

做个小脚本来导出历史数据

  把itemid替换成需要的id

  username passwd ssl证书等自行替换

  时间跨度自行替换

  数据导出后会在当前目录生成xlsx文件

import requests,os,sys
import time
import openpyxl
class ZabbixOperates:
    def __init__(self,zabbix_url,zabbix_user,zabbix_pass,verify_file=None,ssl_pem=None,ssl_key=None):
        self.zabbix_url = zabbix_url
        self.user=zabbix_user
        self.pwd=zabbix_pass
        self.verify_file,self.ssl_pem,self.ssl_key=verify_file,ssl_pem,ssl_key
        self.head={"Content-Type":"application/json"}
        self.token = self.get_token()
    def get_token(self):
        data = {
        "jsonrpc":"2.0",
        "method":"user.login",
        "params":{
            "user":self.user,
            "password":self.pwd
        },
        "id":1 
        }
        res = requests.post(self.zabbix_url,headers=self.head,json=data,verify=self.verify_file,cert=(self.ssl_pem,self.ssl_key))
        return res.json()['result']
    def reuqest(self,data):
        res = requests.post(self.zabbix_url,json=data,verify=self.verify_file,cert=(self.ssl_pem,self.ssl_key))
        return(res.json())
    def get_history(self,itemid,starttime,endtime):
        time_from = int(time.mktime(time.strptime(starttime,'%Y-%m-%d %H:%M:%S')))
        time_till = int(time.mktime(time.strptime(endtime,'%Y-%m-%d %H:%M:%S')))
        data = {
                "jsonrpc": "2.0",
                "method": "history.get",
                "params": {
                    "output": "extend",
                    "history": 0,
                    "itemids": itemid,
                    "time_from": time_from,
                    "time_till": time_till,
                    "sortfield": "clock",
                },
                "auth": self.token,
                "id": 1
                }
        return self.reuqest(data)
def history(zabbix_url,zabbix_user,zabbix_pass,itemid,starttime,endtime,verify_file=None,ssl_pem=None,ssl_key=None):
    operate_office = ZabbixOperates(zabbix_url,zabbix_user,zabbix_pass,verify_file,ssl_pem,ssl_key)
    basdir = os.path.dirname(os.path.realpath(sys.argv[0]))
    historydata = openpyxl.Workbook()
    wb = historydata.active
    data=operate_office.get_history(itemid,starttime,endtime)['result']
    for index,history_dic in enumerate(data):
        history_time,history_data = time.strftime('%y-%m-%d %H:%M:%S',time.localtime(int(history_dic['clock']))) ,history_dic['value']
        wb['A'+str(index+1)] = history_time
        wb['B'+str(index+1)] = history_data
    historydata.save(basdir+'/'+starttime+'-'+endtime+'.xlsx')
    historydata.close()
if __name__ == '__main__':
    zabbix_url='http://www.zabbix-server.com/api_jsonrpc.php'
    zabbix_user   = "Admin"
    zabbix_pass   = "passwd"
    itemid='30531'
    starttime = '2022-07-06 00:00:00'
    endtime = '2022-07-06 01:10:00'
    history(zabbix_url,zabbix_user,zabbix_pass,itemsid,starttime,endtime)

itemid的获取

  zabbix的页面跳转使用GET请求,我们选中需要的item,在浏览器url里可以看到itemid

Leave a Reply

Your email address will not be published. Required fields are marked *

X