monicazhang 发表于 2017-8-19 10:18:51

Curl 的 Zabbix API 调用

1:认证并取得加密字段
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.1.222/zabbix/api_jsonrpc.php;

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.1.223"}},"auth": "b61b7168931be1065cd722ab44e32d2a","id": 0}' http://192.168.1.222/zabbix/api_jsonrpc.php;

zabbix下所有监控主机hostidscurl -i -X POST -H 'Content-Type: application/json' -d '{"jsonrpc": "2.0","method":"host.get","params":{"output":["hostid"]},"auth": "b61b7168931be1065cd722ab44e32d2a","id": 0}'http://192.168.1.222/zabbix/api_jsonrpc.php;


3:获得指定主机下指定监控项或是所有监控项itemids
curl -i -X POST -H 'Content-Type: application/json' -d '{"jsonrpc": "2.0","method":"item.get","params":{"output":"itemids","hostids":"10119","search":{"key_":"system.cpu.util[,idle]"}},"auth": "91aca8c1fe89c89c2e457ab18a28e79b","id": 0}' http://192.168.1.222/zabbix/api_jsonrpc.php;

zabbix下指定主机的所有itemidscurl -i -X POST -H 'Content-Type: application/json' -d '{"jsonrpc": "2.0","method":"item.get","params":{"output":["itemids"],"hostids":"10119"},"auth": "91aca8c1fe89c89c2e457ab18a28e79b","id": 0}' http://192.168.1.222/zabbix/api_jsonrpc.php

4:获取指定监控项在指定时间段的值
获取指定itemids的所有历史数据;curl -i -X POST -H 'Content-Type: application/json' -d '{"jsonrpc": "2.0","method":"history.get","params":{"history":0,"itemids":["23902"],"output":"extend"},"auth": "91aca8c1fe89c89c2e457ab18a28e79b","id": 0}' http://192.168.1.222/zabbix/api_jsonrpc.php获取指定监控项itemids的指定时间段历史数据;我们获取下2014-05-01到2014-05-03期间的数据语句如下curl -i -X POST -H 'Content-Type: application/json' -d '{"jsonrpc": "2.0","method":"history.get","params":{"history":0,"itemids":["23902"],"time_from":"1398873600","time_till":"1399046400","output":"extend"},"auth": "91aca8c1fe89c89c2e457ab18a28e79b","id": 0}' http://192.168.1.222/zabbix/api_jsonrpc.php

数据比较多,其凌乱,value就是我们要的值了;

此种使用的是秒数需将字符串时间值(2014/05/01 --转换为自1970/1/1以来的秒数值)换算一下我在这里用shell简单搞下

5:抓指定时间点的值
curl -i -X POST -H 'Content-Type: application/json' -d '{"jsonrpc": "2.0","method":"history.get","params":{"history":0,"itemids":["23902"],"time_from":"1398873600","time_till":"1399046400","output":"extend","filter":{"clock":"1399026180"}},"auth": "91aca8c1fe89c89c2e457ab18a28e79b","id": 0}' http://192.168.1.222/zabbix/api_jsonrpc.php
看抓取的数据结构完全可以给他导到xml中;zabbix API Object specifications without the 'draft' mark are stable and can be used for production purposes
curl模拟调用zabbix JSON-RPC格式curl -i -X POST -H 'Content-Type: application/json' -d ‘api json语句’ zabbix-server--即我的测试机http://192.168.1.222/zabbix/api_jsonrpc.php;

来个模板吧,你copy此处到你的终端吧数据语句放到''中要是没问题的话即可返回你所要的数据curl -i -X POST -H 'Content-Type: application/json' -d'你的json语句拷贝与此处;'  你的zabbix服务器api的urlExamples,都是Ruiy根据官网及相关文档改写亲测成功的,你仅需的就是修改下你的zabbix服务器API的URL即可;


[*]获取监控主机信息curl -i -X POST -H 'Content-Type:application/json' -d '{"jsonrpc":"2.0","method":"host.get","params":{"output":"extend"},"auth":"91aca8c1fe89c89c2e457ab18a28e79b","id":4 }' http://192.168.1.222/zabbix/api_jsonrpc.php

[*]zabbix版本curl -i -X POST -H 'Content-Type:application/json' -d '{"jsonrpc":"2.0","method":"apiinfo.version","params":{"output":"extend"},"auth":"91aca8c1fe89c89c2e457ab18a28e79b","id":1 }' http://192.168.1.222/zabbix/api_jsonrpc.php

[*]获取auth加密字段curl -i -X POST -H 'Content-Type:application/json' -d '{"jsonrpc":"2.0","method":"user.authenticate","params":{"user":"admin","password":"zabbix"},"auth":null,"id":0}' http://192.168.1.222/zabbix/api_jsonrpc.php将"method":"user.authenticate"改成"method":"user.login";

[*]获取web scenario信息curl -i -X POST -H 'Content-Type:application/json' -d '{"jsonrpc":"2.0","method":"httptest.get","params":{"output":"extend"},"auth":"91aca8c1fe89c89c2e457ab18a28e79b","id":1 }' http://192.168.1.222/zabbix/api_jsonrpc.php;
[*]

[*]获取zabbix系统中以定义了的macro

    curl -i -X POST -H 'Content-Type:application/json' -d   '{"jsonrpc":"2.0","method":"usermacro.get","params":    {"output":"extend"},"auth":"91aca8c1fe89c89c2e457ab18a28e79b","id":1 }'   http://192.168.1.222/zabbix/api_jsonrpc.php

6. 告警介质curl -i -X POST -H 'Content-Type:application/json' -d '{"jsonrpc":"2.0","method":"usermedia.get","params":{"output":"extend"},"auth":"91aca8c1fe89c89c2e457ab18a28e79b","id":1 }' http://192.168.1.222/zabbix/api_jsonrpc.php

关于json的相关格式语法本人没接触也不懂,需要的同仁自己查阅;相关的测试我也就不一一列举了,下面我把zabbix-API所以的method reference给各位同仁罗列下,参考自zabbix Official manual我仅仅罗列method,关于每个method的动作操作(.get,.create,.delete,.update,.exists,...不足或不限制于此处罗列)省1,  action.get2,  alter.get3,  apiinfo.version4,  application5,  configuration.import/export6,  discovered host dhost/service/check/rule.get7,  event.get8,  graph.get9,  graphitem.get10,  prototype.get11,  history.get12,  host/group/interface/prototype/.get13,  icon.get14,  image.get15,  item16,  .....原创:秦瑞
页: [1]
查看完整版本: Curl 的 Zabbix API 调用