请选择 进入手机版 | 继续访问电脑版

IT运维管理,ITIL,ITSS,ITSM,ISO20000-ITIL先锋论坛

 找回密码
 微信、QQ、手机号一键注册

扫描二维码登录本站

QQ登录

只需一步,快速开始

艾拓先锋
搜索
查看: 186|回复: 0

[Zabbix专区] Curl 的 Zabbix API 调用

[复制链接]
来自- 湖南娄底

参加活动:0

组织活动:12

发表于 2017-8-19 10:18:51 | 显示全部楼层 |阅读模式 来自- 湖南娄底
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;

1.png

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;

2.png

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

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;

4.png

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

5.png

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

6.png

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

7.png

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

8.png

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即可;

  • 获取监控主机信息
    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;
    9.png

  • 获取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

11.png

关于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,  .....
原创:秦瑞

本版积分规则

选择云运维时代的王牌讲师-长河老师,助你轻松入门ITIL Foundation培训课程

QQ|小黑屋|手机版|Archiver|艾拓先锋网 ( 粤ICP备11099876号-1|网站地图

Baidu

GMT+8, 2018-12-13 01:32 , Processed in 0.211257 second(s), 33 queries .

Powered by Discuz! X3.2

© 2001-2013 Comsenz Inc.

快速回复 返回顶部 返回列表