zoukankan      html  css  js  c++  java
  • Python编写从ZabbixAPI获取信息

    此脚本用Python3.6执行是OK的。

    # -*- coding: utf-8 -*-
    import json
    import urllib.request, urllib.error, urllib.parse

    class ZabbixAPI:
    def __init__(self):
    self.__url = 'http://192.168.56.102/zabbix/api_jsonrpc.php'
    self.__user = 'admin'
    self.__password = 'zabbix'
    self.__header = {"Content-Type": "application/json-rpc"}
    self.__token_id = self.UserLogin()
    #登陆获取token
    def UserLogin(self):
    data = {
    "jsonrpc": "2.0",
    "method": "user.login",
    "params": {
    "user": self.__user,
    "password": self.__password
    },
    "id": 0,
    }
    return self.PostRequest(data)
    #推送请求
    def PostRequest(self, data):
    request = urllib.request.Request(self.__url,json.dumps(data).encode('utf-8'),self.__header)
    result = urllib.request.urlopen(request)
    response = json.loads(result.read().decode('utf-8'))
    try:
    # print response['result']
    return response['result']
    except KeyError:
    raise KeyError
    #主机列表
    def HostGet(self,hostid=None,hostip=None):
    data = {
    "jsonrpc":"2.0",
    "method":"host.get",
    "params":{
    "output":"extend",
    "selectGroups": "extend",
    "selectParentTemplates": ["templateid","name"],
    "selectInterfaces": ["interfaceid","ip"],
    "selectInventory": ["os"],
    "selectItems":["itemid","name"],
    "selectGraphs":["graphid","name"],
    "selectApplications":["applicationid","name"],
    "selectTriggers":["triggerid","name"],
    "selectScreens":["screenid","name"]
    },
    "auth": self.__token_id,
    "id":1,
    }
    if hostid:
    data["params"]={
    "output": "extend",
    "hostids": hostid,
    "sortfield": "name"
    }
    return self.PostRequest(data)
    #主机列表
    def HostCreate(self,hostname,hostip,groupid=None,templateid=None):
    data = {
    "jsonrpc":"2.0",
    "method":"host.create",
    "params": {
    "host": hostname,
    "interfaces": [
    {
    "type": 1,
    "main": 1,
    "useip": 1,
    "ip": hostip,
    "dns": "",
    "port": "10050"
    }
    ],
    "groups": [
    {
    "groupid": groupid
    }
    ],
    "templates": [
    {
    "templateid": templateid
    }
    ]
    },
    "auth": self.__token_id,
    "id":1,
    }
    return self.PostRequest(data)

    #主机组列表
    def HostGroupGet(self,hostid=None,itemid=None):
    data = {
    "jsonrpc":"2.0",
    "method":"hostgroup.get",
    "params":{
    "output": "extend",
    "hostids": hostid,
    "itemids": itemid,
    "sortfield": "name"
    },
    "auth": self.__token_id,
    "id":1,
    }
    return self.PostRequest(data)
    #监控项列表
    def ItemGet(self,hostid=None,itemid=None):
    data = {
    "jsonrpc":"2.0",
    "method": "item.get",
    "params": {
    "output": "extend",
    "hostids": hostid,
    "itemids": itemid,
    "sortfield": "name"
    },
    "auth": self.__token_id,
    "id":1,
    }
    return self.PostRequest(data)
    #模板列表
    def TemplateGet(self, hostid=None,templateid=None):
    data = {
    "jsonrpc":"2.0",
    "method": "template.get",
    "params": {
    "output": "extend",
    "hostids": hostid,
    "templateids": templateid,
    "sortfield": "name"
    },
    "auth": self.__token_id,
    "id":1,
    }
    return self.PostRequest(data)
    #图像列表
    def GraphGet(self,hostid=None,graphid=None):
    data = {
    "jsonrpc":"2.0",
    "method": "graph.get",
    "params": {
    "output": "extend",
    "hostids": hostid,
    "graphids": graphid,
    "sortfield": "name"
    },
    "auth": self.__token_id,
    "id":1,
    }
    return self.PostRequest(data)
    #历史数据
    def History(self,itemid,data_type):
    data = {
    "jsonrpc": "2.0",
    "method": "history.get",
    "params": {
    "output": "extend",
    "history": data_type,
    "itemids": itemid,
    "sortfield": "clock",
    "sortorder": "DESC",
    "limit": 30
    },
    "auth": self.__token_id,
    "id": 2
    }
    return self.PostRequest(data)
    #测试:python manager.py shell ; from ZABBIX.ZabbixAPI import * ; main(),代码修改了要ctrl+Z退出重进
    def main():
    zapi=ZabbixAPI()
    token=zapi.UserLogin()
    print(token)
    #39378ec03aa101c2b17d1d2bd6f4ef16
    hosts=zapi.HostGet()
    print(hosts)
    #[{u'host': u'Zabbix server', u'hostid': u'10084', u'interfaces': [{u'interfaceid': u'1', u'ip': u'127.0.0.1'}]}]

    if __name__ == '__main__':
    main()
  • 相关阅读:
    收集一些关于前端的网站(持续更新)
    关于表单设计(登录)的几点感悟
    css样式表中的样式覆盖顺序(转)
    UI设计常用网站(随时更新)
    关于ps安装遇到的问题解决(转载整理)
    【工作记录】解决溢出div自适应的高度问题
    Cross origin requests are only supported for protocol schemes: http, data, chrome-extension, https, chrome-extension-resource. 报错处理
    [css]img垂直居中的方法
    iOS学习第三天杂记
    iOS学习第二天杂记--UI
  • 原文地址:https://www.cnblogs.com/TaleG/p/8672316.html
Copyright © 2011-2022 走看看