python调用zabbix api获取主机信息

2018-12-20  本文已影响0人  Sigers
import requests
import json

headers = {
    'Content-Type': 'application/json'
}

class GetZabbix:
    def __init__(self):
        #用户信息
        self.username = "****"
        self.password = "****"
        self.url = "http://192.168.0.*/zabbix/api_jsonrpc.php"
        self.token = self.getToken()

    def getToken(self):
        data = {
            "jsonrpc": "2.0",
            "method": "user.login",
            "params": {
                "user": self.username,
                "password": self.password
            },
            "id": 1,
            "auth": None
        }
        r = requests.post(url=self.url, headers=headers, data=json.dumps(data))
        token = json.loads(r.content).get("result")
        return token

    def getHosts(self):
        data = {
            "jsonrpc": "2.0",
            "method": "host.get",
            "params": {
                "output": [
                    "hostid",
                    "host"
                ],
                "selectInterfaces": [
                    "interfaceid",
                    "ip"
                ]
            },
            "id": 2,
            "auth": self.token
        }
        r = requests.post(url=self.url, headers=headers, data=json.dumps(data))
        print(r.content)


if __name__ == "__main__":
    start = GetZabbix()
    start.getHosts()

参考:https://www.zabbix.com/documentation/3.4/zh/manual/api

上一篇下一篇

猜你喜欢

热点阅读