Objective:
The following is a summary of the official documents according to Zabbix 3.2: Https://www.zabbix.com/documentation/3.2/manual/api
This document simply describes the basic use of the API, and some of the full features or advanced features will be shown in the following blog post
Invoking the Zabbix API is using the HTTP POST request method
Where the requested header is:
1 |
{ "Content-Type" : "application/json-rpc" } |
The requested URL is:
1 |
http: / / zabbix.aek.com / api_jsonrpc.php |
If the URL contains a directory, you need to write the directory, such as HTTP./Zabbix.aek.com/zabbix/api_ Jsonrpc.php, finally, is the requested data, such as obtaining tokens, creating a host, getting the host ID, getting the template ID, getting the group ID, deleting the host, and so on.
Take the example of using Python to invoke the API, example of a usage case:
Test whether the connection was successful
1 #!/usr/bin/python2 #Coding:utf:83 4 ImportRequests5 ImportJSON6 7URL ="http://zabbix.aek.com/zabbix/api_jsonrpc.php"8Header = {"Content-type":"Application/json-rpc"}9data = {"Jsonrpc":"2.0","Method":"apiinfo.version","ID": 1,"Auth": None,"params":{}}TenRequest = Requests.post (Url=url, Headers=header, data=json.dumps (data)) One Print(request.content)
View Code
The data above is used to test whether the connection succeeds, the value of auth in the official document is NULL, but null in Python is represented by none, so it needs to be changed to none.
The return value after success is probably as follows: Zabbix version is displayed
1 {"jsonrpc":"2.0","result ":"3.2.3","ID": 1}
Get token
1 #!/usr/bin/python2 #Coding:utf:83 4 ImportRequests5 ImportJSON6 7URL ="http://zabbix.aek.com/zabbix/api_jsonrpc.php"8Header = {"Content-type":"Application/json-rpc"}9data = {"Jsonrpc":"2.0",Ten "Method":"User.login", One "params": { A "User":"Admin", - "Password":"Zabbix" - }, the "ID": 1, - "Auth": None - } -Request = Requests.post (Url=url, Headers=header, data=json.dumps (data)) + Print(request.content)
View Code
The return value after success is as follows: Returns a token
1 {"jsonrpc":"2.0","result ":"391277f070a3ebc5b2afe9cf466cb74c","ID" : 1}
Finally, take the information for all hosts with a full Python script through the Zabbix API as an example:
1 #!/usr/bin/python2 #Coding:utf:83 4 ImportRequests5 ImportJSON6 7 classZabbix:8 9 def __init__(self, URL, header, username, password):Ten OneSelf.url =URL ASelf.header =Header -Self.username =username -Self.password =Password the - defGetToken (self): - #get token and return character token string - +data = {"Jsonrpc":"2.0", - "Method":"User.login", + "params": { A "User": Self.username, at "Password": Self.password - }, - "ID": 1, - "Auth": None - } -token = Requests.post (Url=self.url, Headers=self.header, data=json.dumps (data)) in returnJson.loads (token.content) ["result"] - to defGetallhost (self): + #Get all host information - thedata = {"Jsonrpc":"2.0", * "Method":"Host.get", $ "params": {Panax Notoginseng "Output": [ - "HostID", the "Host", + ], A "selectgroups":"Extend", the "selectinterfaces": [ + "InterfaceID", - "IP" $ ] $ }, - "ID": 2, - "Auth": Self.gettoken () the } -hosts = Requests.post (Url=self.url, Headers=self.header, data=json.dumps (data))Wuyi returnJson.loads (hosts.content) ["result"] the - Wu if __name__=="__main__": -Header = {"Content-type":"Application/json-rpc"} AboutUrl="http://zabbix.aek.com/zabbix/api_jsonrpc.php" $Test = Zabbix (Url=url, Header=header, username="Admin", password="Zabbix") - Print(Test.getallhost ())
View Code
Zabbix API Basic Usage Method Introduction