環(huán)境
zabbix:172.16.128.16;zabbix_web:172.16.16.16/zabbix
用戶名:Admin 密碼:zabbix
獲取的數(shù)據(jù)僅做參考,以Linux發(fā)送HTTP的POST請(qǐng)求為例
a.登錄并獲取身份驗(yàn)證令牌
{
“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
如果你正確提供了憑據(jù),API返回的響應(yīng)將包含用戶身份驗(yàn)證令牌
{
“jsonrpc”: “2.0”, #jsonrpc – JSON-RPC協(xié)議的版本
“result”: “7ef823a58b59c1a17f519fe4d0e3cc44”, #result – 方法返回的數(shù)據(jù)
“id”: 1 #id – 相應(yīng)請(qǐng)求的標(biāo)識(shí)符
}
b.檢索所有已配置主機(jī)ID,主機(jī)名和接口
{
“jsonrpc”: “2.0”,
“method”: “host.get”,
“params”: {
“output”: [
“hostid”,
“host”
],
“selectInterfaces”: [
“interfaceid”,
“ip”
]
},
“id”: 1,
“auth”: “7ef823a58b59c1a17f519fe4d0e3cc44″ #auth – 屬性現(xiàn)在設(shè)置為我們通過(guò)調(diào)用user.login方法獲得的身份驗(yàn)證令牌
}
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 #通過(guò)host名稱(chēng),檢索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 #通過(guò)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 #通過(guò)hostid和itemid,檢索lastvalue值
d.獲取監(jiān)控項(xiàng)歷史數(shù)據(jù)
{
“jsonrpc”: “2.0”,
“method”: “history.get”,
“params”: {
“output”: “extend”,
“history”: 3, #對(duì)象類(lèi)型
“itemids”: “1095468”,
“sortfield”: “clock”,
“sortorder”: “DESC”,
“limit”: 10 #數(shù)據(jù)數(shù)量
},
“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 #從無(wú)符號(hào)數(shù)字監(jiān)控項(xiàng)中獲取最近10條數(shù)據(jù)
e.檢索多個(gè)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