×

基于 Curl 的 Zabbix API 调用

hqy hqy 发表于2022-08-04 22:54:42 浏览525 评论0

抢沙发发表评论

http://www.cnblogs.com/ruiy/p/curlCallZabbixAPI.html

https://www.zabbix.com/documentation/5.0/zh/manual/api/reference/configuration/export


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下所有监控主机hostids

curl -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下指定主机的所有itemids

curl -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 的url

Examples,都是Ruiy根据官网及相关文档改写亲测成功的,你仅需的就是修改下你的zabbix服务器API的URL即可;

  1. 获取监控主机信息

    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

  2. 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

  3. 获取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";

  4. 获取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;


  5. 获取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.get

2, alter.get

3, apiinfo.version

4, application

5, configuration.import/export

6, discovered host dhost/service/check/rule.get

7, event.get

8, graph.get

9, graphitem.get

10, prototype.get

11, history.get

12, host/group/interface/prototype/.get

13, icon.get

14, image.get

15, item

16, .....


打赏

本文链接:https://www.kinber.cn/post/2594.html 转载需授权!

分享到:


推荐本站淘宝优惠价购买喜欢的宝贝:

image.png

 您阅读本篇文章共花了: 

群贤毕至

访客