美文网首页
python调用zabbix api获取主机信息

python调用zabbix api获取主机信息

作者: Sigers | 来源:发表于2018-12-20 17:24 被阅读0次
    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

    相关文章

      网友评论

          本文标题:python调用zabbix api获取主机信息

          本文链接:https://www.haomeiwen.com/subject/dqqukqtx.html