The use of Zabbix Api in Linux shell environment

  • 2021-01-03 21:12:36
  • OfStack

Just call directly in linux shell, according to the website: before you can access any data in Zabbix, you need to log in and get the authentication token. This can be done using the user.login method.


[root@localhost ~]# curl -i -X POST -H 'Content-Type: application/json' -d '{"jsonrpc": "2.0","method":"user.login","params":{"user":"Admin","password":"zabbix"},"auth": null,"id":0}' http://192.168.149.129/zabbix/api_jsonrpc.php
HTTP/1.1 200 OK
Date: Fri, 26 Oct 2018 07:46:05 GMT
Server: Apache/2.4.6 (CentOS) PHP/5.6.36
X-Powered-By: PHP/5.6.36
Access-Control-Allow-Origin: *
Access-Control-Allow-Headers: Content-Type
Access-Control-Allow-Methods: POST
Access-Control-Max-Age: 1000
Content-Length: 68
Content-Type: application/json
{"jsonrpc":"2.0","result":"77bfe44db3f5c016477110c5748ac3e1","id":0}

According to the official website, returned to the user authentication token "77 bfe44db3f5c016477110c5748ac3e1", all api request need take it back.

Follow suit and query 1 more, this time with the above authentication token:


[root@localhost ~]# curl -i -X POST -H 'Content-Type: application/json' -d '{"jsonrpc": "2.0","method":"host.get","params":{"output":["host"],"selectInterfaces":["ip"]},"auth":
 "77bfe44db3f5c016477110c5748ac3e1","id":4}' http://192.168.149.129/zabbix/api_jsonrpc.p
hp
HTTP/1.1 200 OK
Date: Fri, 26 Oct 2018 08:17:23 GMT
Server: Apache/2.4.6 (CentOS) PHP/5.6.36
X-Powered-By: PHP/5.6.36
Access-Control-Allow-Origin: *
Access-Control-Allow-Headers: Content-Type
Access-Control-Allow-Methods: POST
Access-Control-Max-Age: 1000
Content-Length: 111
Content-Type: application/json
{"jsonrpc":"2.0","result":[{"hostid":"10084","host":"Zabbix server","interfaces":[{"ip":"127.0.0.1"}]}],"id":4}

The host name Zabbix server is returned, and host Ip 127.0.0.1 is returned

In fact, API should be used according to the official website documents, such as ES28en.version, the official website makes it clear: this method can be used for unauthenticated users, and must be used when sending es30EN-ES31en request without adding the "auth" parameter.

Examples are as follows:


[root@localhost ~]# curl -i -X POST -H 'Content-Type: application/json' -d '{"jsonrpc": "2.0","method":"apiinfo.version","params":[],"id":5}' http://192.168.149.129/zabbix/api_
jsonrpc.php
HTTP/1.1 200 OK
Date: Fri, 26 Oct 2018 08:40:17 GMT
Server: Apache/2.4.6 (CentOS) PHP/5.6.36
X-Powered-By: PHP/5.6.36
Access-Control-Allow-Origin: *
Access-Control-Allow-Headers: Content-Type
Access-Control-Allow-Methods: POST
Access-Control-Max-Age: 1000
Content-Length: 42
Content-Type: application/json
{"jsonrpc":"2.0","result":"3.4.14","id":5}

The version of Zabbix API that shows the target host is 3.4.14

Attached is the official website link:

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

conclusion


Related articles: