環境 zabbix:172.16.128.16;zabbix_web:172.16.16.16/zabbix 用戶名:Admin 密碼:zabbix 獲取的數據僅做參考,以Linux發送HTTP的POST請求為例 a.登錄並獲取身份驗證令牌 如果你正確提供了憑據,API返回的響應將包含用戶身份驗證令 ...
環境
zabbix:172.16.128.16;zabbix_web:172.16.16.16/zabbix
用戶名:Admin 密碼:zabbix
獲取的數據僅做參考,以Linux發送HTTP的POST請求為例
a.登錄並獲取身份驗證令牌
{ "jsonrpc": "2.0", "method": "user.login", "params": { "user": "Admin", "password": "zabbix" }, "id": 1, "auth": null }
curl -H "Content-Type: application/json-rpc" -d '{"jsonrpc":"2.0","method":"user.login","params":{"user":"Admin","password":"zabbix"},"id":1,"auth":null}' http://172.16.128.16/zabbix/api_jsonrpc.php
如果你正確提供了憑據,API返回的響應將包含用戶身份驗證令牌
{ "jsonrpc": "2.0", #jsonrpc - JSON-RPC協議的版本 "result": "7ef823a58b59c1a17f519fe4d0e3cc44", #result - 方法返回的數據 "id": 1 #id - 相應請求的標識符 }
b.檢索所有已配置主機ID,主機名和介面
{ "jsonrpc": "2.0", "method": "host.get", "params": { "output": [ "hostid", "host" ], "selectInterfaces": [ "interfaceid", "ip" ] }, "id": 1, "auth": "7ef823a58b59c1a17f519fe4d0e3cc44" #auth - 屬性現在設置為我們通過調用user.login方法獲得的身份驗證令牌 }
curl -H "Content-Type: application/json-rpc" -d '{"jsonrpc":"2.0","method":"host.get","params":{"output":["hostid","host"],"selectInterfaces":["interfaceid","ip"]},"id":1,"auth":"7ef823a58b59c1a17f519fe4d0e3cc44"}' http://172.16.128.16/zabbix/api_jsonrpc.php
c.由獲取到的 hostid 利用 item.get 得到 itemid 以及其 lastvalue
curl -H 'Content-Type: application/json-rpc' -d '{"jsonrpc": "2.0","method":"host.get","params":{"output":["hostid"],"filter": {"host":"50278791-59ab-2966-e86a-e04cd01eff6a"}},"auth": "7ef823a58b59c1a17f519fe4d0e3cc44","id":1}' http://172.16.128.16/zabbix/api_jsonrpc.php #通過host名稱,檢索hostid
curl -H "Content-Type: application/json-rpc" -d '{"jsonrpc": "2.0","method": "item.get","params": {"output": "extend","hostids": "27789","search": {"key_": "vmware.vm.cpu.usage"},"sortfield": "name"},"id":1,"auth":"7ef823a58b59c1a17f519fe4d0e3cc44"}' http://172.16.128.16/zabbix/api_jsonrpc.php #通過hostid,獲取itemid 及其lastvalue值
curl -H "Content-Type: application/json-rpc" -d '{"jsonrpc": "2.0","method": "item.get","params": {"output": "extend","hostids": "27789","itemids": "1095468","sortfield": "name"},"id":1,"auth":"7ef823a58b59c1a17f519fe4d0e3cc44"}' http://172.16.128.16/zabbix/api_jsonrpc.php #通過hostid和itemid,檢索lastvalue值
d.獲取監控項歷史數據
{ "jsonrpc": "2.0", "method": "history.get", "params": { "output": "extend", "history": 3, #對象類型 "itemids": "1095468", "sortfield": "clock", "sortorder": "DESC", "limit": 10 #數據數量 }, "auth": "7ef823a58b59c1a17f519fe4d0e3cc44", "id": 1 }
curl -H "Content-Type: application/json-rpc" -d '{"jsonrpc": "2.0","method": "history.get","params": {"output": "extend","history": 3,"itemids": "1095468","sortfield": "clock","sortorder": "DESC","limit":10},"id":1,"auth":"7ef823a58b59c1a17f519fe4d0e3cc44"}' http://172.16.128.16/zabbix/api_jsonrpc.php #從無符號數字監控項中獲取最近10條數據
e.檢索多個itemid
curl -H "Content-Type: application/json-rpc" -d '{"jsonrpc":"2.0","method":"history.get","params":{"output":"extend","hostids":"1095468","itemids":["26353","26352","26357","26356","26355","26354","26359","26358","25754","25750","25751","25748","25768","25755","25752","25759","25760","25753","25761","26348","26350","26349","26351","25749","25767","25756","25757","25758","25769","25770","25771"],"sortfield":"clock","sortorder":"DESC","limit": 31},"id":1,"auth":"7ef823a58b59c1a17f519fe4d0e3cc44"}' http://172.16.128.16/zabbix/api_jsonrpc.php