What Zabbix API call API can do
Zabbix API allows programmatically retrieve and modify the configuration of Zabbix and provides access to Historica L data.
It is widely used to:
- Create new applications to work with Zabbix;
- Integrate Zabbix with the third party software;
- Automate routine tasks.
My own understanding is that can be programmed to operate the Zabbix, such as the creation of the host Ah, create graphics, of course, not only to create, delete and change the basic support.
Protocol for API
Created in PHP based on the JSON-RPC 2.0
means that the request and reply formats for the API are encoded in JSON
A module that the Python authoring program calls the API to call
- Urllib2
- Json
Steps are broadly divided into two steps, authentication, access to AC (authentication token)
{ "jsonrpc"2.0", "method"user.login", "params{ "user": "Admin", "password": "zabbix" }, "id1, "authnull}
Return is
{ "jsonrpc"2.0", "result"0424bd59b807674191e7d77572075f33", "id1}
Sends the API request body.
{ "Jsonrpc":"2.0", "Method":"Host.get", "params":{"output": [ "HostID", "host" ], " Selectinterfaces": [ " InterfaceID ", " IP " ] }, "ID":2, "Auth":"0424bd59b807674191e7d77572075f33"}
Return:
{ "Jsonrpc":"2.0", "result":[{"HostID": "10084", "host": "Zabbix Server", "Interfaces": [{"InterfaceID": "1", "IP": "127.0.0.1" } ]}], "ID":2}
Written in the program is
Python Example
ImportUrllib2ImportJsonzabbix_url="http://zabbix.xxx.com/api_jsonrpc.php"api_pass=' xxxx 'auth_data={' Jsonrpc ':' 2.0 ',' method ':' User.login ',' params ':{' user ':' API ',' Password ': Api_pass},' id ':1}#auth function def Get_auth():Request=urllib2. Request (Zabbix_url,json.dumps (Auth_data)) Request.add_header (' Content-type ',' Application/json ') Response=urllib2.urlopen (Request) Var1=json.loads (Response.read ())returnvar1[' Result ']#get Auth SessionSession=get_auth ()
JSON requests to follow the API document HTTPS://WWW.ZABBIX.COM/DOCUMENTATION/2.2/MANUAL/API inside the copy is OK. Note that the Zabbix version, the API request content of 2.0,2.2 and 2.4 are different.
The ID order is incremented, and the next API request ID is 2. You can do it with Urllib2.urlopen. For the use of URLLIB2, refer to my previous article. Python urllib2 module
Zabbix API calls