Zabbix API可以通过JSON RPC协议来获取历史数据。
可以采用脚本或者任何支持JSON RPC的工具来使用API。
基本请求格式
Zabbix API 简化的JSON请求如下:
{
"jsonrpc": "2.0",
"method": "method.name",
"params": {
"param_1_name": "param_1_value",
"param_2_name": "param_2_value"
},
"id": 1,
"auth": "a826fca79a0795ccc1224dc76329972f",
}
下面一行一行来看:
● "jsonrpc": "2.0"-这是标准的JSON RPC参数以标示协议版本。所有的请求都会保持不变。
● "method": "method.name"-这个参数定义了真实执行的操作。例如:host.create、item.update,history.get等等
● "params"-这里通过传递JSON对象来作为特定方法的参数。如果你希望创建监控项,"name"和"key_"参数是需要的,每个方法需要的参数在Zabbix API文档中都有描述。
● "id": 1-这个字段用于绑定JSON请求和响应。响应会跟请求有相同的"id"。在一次性发送多个请求时很有用,这些也不需要唯一或者连续
● "auth": "a826fca79a0795ccc1224dc76329972f"-这是一个认证令牌【authentication token】用以鉴别用户、访问API。这也是使用API进行相关操作的前提-获取认证ID。(以上为转载http://paperplane.ruhoh.com/zabbix/intro-to-zabbix-api/)
实例:获取192.168.211.60从2014.2.19 14:00:00到2014.2.19 14:10:00的cpu idle值
基于curl命令:
(1):认证并且取得加密字段
curl -i -X POST -H 'Content-Type: application/json' -d '{"jsonrpc":
"2.0","method":"user.login","params":{"user":"xxxx","password":"xxxx"},"auth":
null,"id":0}' http://x.x.x.x/api_jsonrpc.php
#之后会得到一串输出:{"jsonrpc":"2.0","result":"a826fca79a0795ccc1224dc76329972f","id":0}。记住这
段输出。
(2)获取监控主机的hostids
curl -i -X POST -H 'Content-Type: application/json' -d '{"jsonrpc": "2.0","method":"host.get","params":{"output":["hostid"],"filter": {"host":"192.168.211.60"}},"auth": "a826fca79a0795ccc1224dc76329972f","id": 0}' http://x.x.x.x/api_jsonrpc.php
#"hostid":"10243"
(3)获得监控项itemids
curl -i -X POST -H 'Content-Type: application/json' -d '{"jsonrpc": "2.0","method":"item.get","params":{"output":"itemids","hostids":"10243","search":{"key_":"system.cpu.util
[,idle,avg1]"}},"auth": "a826fca79a0795ccc1224dc76329972f","id": 0}' http://x.x.x.x/api_jsonrpc.php
#“item”:"24526"
(4)获取监控项"system.cpu.util[,idle,avg1]"在2014.2.19 14:00~14:20的值
curl -i -X POST -H 'Content-Type: application/json' -d '{"jsonrpc": "2.0","method":"history.get","params":{"history":0,"itemids":["24526"],"time_from":"1392789600","time_till":"1392790200","output":"extend"},"auth": "a826fca79a0795ccc1224dc76329972f","id": 0}' http://x.x.x.x/api_jsonrpc.php
#输出:
[{"itemid":"24526","clock":"1392789646","value":"95.7539","ns":"138978589"},
{"itemid":"24526","clock":"1392789706","value":"84.9595","ns":"578198422"},
{"itemid":"24526","clock":"1392789766","value":"94.5259","ns":"186216653"},
{"itemid":"24526","clock":"1392789826","value":"96.2286","ns":"789434167"},
{"itemid":"24526","clock":"1392789886","value":"94.7712","ns":"191954191"},
{"itemid":"24526","clock":"1392789946","value":"90.8338","ns":"781241168"},
{"itemid":"24526","clock":"1392790006","value":"89.1735","ns":"294674458"},
{"itemid":"24526","clock":"1392790066","value":"92.3015","ns":"877714419"},
{"itemid":"24526","clock":"1392790126","value":"96.3051","ns":"426421789"},
{"itemid":"24526","clock":"1392790186","value":"97.7931","ns":"174500891"}]
#“value”就是cpu idle值,采样间隔时间与监控项的数据更新时间一致。
补充:获取192.168.211.60监控项"system.cpu.util[,idle,avg1]"在2014年2月19日 下午2:02:46 的值
curl -i -X POST -H 'Content-Type: application/json' -d '{"jsonrpc": "2.0","method":"history.get","params":{"history":0,"itemids":["24526"],"time_from":"1392789600","time_till":"1392790200","output":"extend","filter":{"clock":"1392789766"}},"auth": "a826fca79a0795ccc1224dc76329972f","id": 0}' http://x.x.x.x/api_jsonrpc.php
#"value":"94.5259"要获取的值
参考:
Zabbix API 官方文档:https://www.zabbix.com/documentation/2.2/manual/api