在linux shell環(huán)境下直接調(diào)用就可以,根據(jù)官網(wǎng)所述:在訪問Zabbix中的任何數(shù)據(jù)之前,你需要登錄并獲取身份驗證令牌。這可以使用該 user.login 方法完成。
[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}
根據(jù)官網(wǎng)所述,返回了用戶身份驗證令牌"77bfe44db3f5c016477110c5748ac3e1",后面所有的api請求都需要帶著它。
依葫蘆畫瓢又查詢了一個,這次帶著上面的身份驗證令牌:
[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}
返回了主機名Zabbix server,主機Ip 127.0.0.1
實際上,API怎么用還是要以官網(wǎng)文檔為準,比如apiinfo.version,官網(wǎng)寫的很清楚:此方法可用于未經(jīng)身份驗證的用戶,必須在發(fā)送 JSON-RPC 請求中不加“auth”參數(shù)的情況下調(diào)用。
示例如下:
[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}
顯示目標主機的Zabbix API 版本是3.4.14
附官網(wǎng)鏈接:
https://www.zabbix.com/documentation/3.4/zh/manual/api
總結(jié)
以上就是這篇文章的全部內(nèi)容了,希望本文的內(nèi)容對大家的學習或者工作具有一定的參考學習價值,謝謝大家對腳本之家的支持。如果你想了解更多相關(guān)內(nèi)容請查看下面相關(guān)鏈接