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