环境版本:
Python 3.6
requests 2.21
Zabbix 3.4
#!/usr/bin/env python #_*_ coding:utf-8 _*_ import requests import json class Zabbix_api(): def __init__(self): self.url = 'http://192.168.110.110/zabbix/api_jsonrpc.php' self.username = 'Admin' self.password = '123456789' self.header = {"Content-Type": "application/json-rpc"} def Login(self): data = { "jsonrpc": "2.0", "method": "user.login", "params": { "user": self.username, "password": self.password }, "id": 1 } request = requests.post(url=self.url, headers=self.header, data=json.dumps(data)) dict = json.loads(request.text) request.close() return dict['result'] def HostList(self, token): data = { "jsonrpc": "2.0", "method": "host.get", "params": { "output": [ "hostid" ], "selectInterfaces": [ "ip" ] }, "id": 2, "auth": token } request = requests.post(url=self.url, headers=self.header, data=json.dumps(data)) dict = json.loads(request.text) request.close() return dict['result'] if __name__ == '__main__': zabapi = Zabbix_api() token = zabapi.Login() zab_host_list = zabapi.HostList(token) for host in zab_host_list: print(host)
文献:https://www.zabbix.com/documentation/3.4/zh/manual/api