阿里云-云小站(无限量代金券发放中)
【腾讯云】云服务器、云数据库、COS、CDN、短信等热卖云产品特惠抢购

Zabbix调用api检索方法

220次阅读
没有评论

共计 3168 个字符,预计需要花费 8 分钟才能阅读完成。

环境

zabbix:172.16.128.16;zabbix_web:172.16.16.16/zabbix

用户名:Admin 密码:zabbix

获取的数据仅做参考,以 Linux 发送 HTTP 的 POST 请求为例

a. 登录并获取身份验证令牌

{
    “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

如果你正确提供了凭据,API 返回的响应将包含用户身份验证令牌

{
    “jsonrpc”: “2.0”, #jsonrpc – JSON-RPC 协议的版本
    “result”: “7ef823a58b59c1a17f519fe4d0e3cc44”, #result – 方法返回的数据
    “id”: 1 #id – 相应请求的标识符
}

b. 检索所有已配置主机 ID,主机名和接口

{
    “jsonrpc”: “2.0”,
    “method”: “host.get”,
    “params”: {
        “output”: [
            “hostid”,
            “host”
        ],
        “selectInterfaces”: [
            “interfaceid”,
            “ip”
        ]
    },
    “id”: 1,
    “auth”: “7ef823a58b59c1a17f519fe4d0e3cc44″ #auth – 属性现在设置为我们通过调用 user.login 方法获得的身份验证令牌
}

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 #通过 host 名称,检索 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 #通过 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 #通过 hostid 和 itemid,检索 lastvalue 值

d. 获取监控项历史数据

{
    “jsonrpc”: “2.0”,
    “method”: “history.get”,
    “params”: {
        “output”: “extend”,
        “history”: 3, #对象类型
        “itemids”: “1095468”,
        “sortfield”: “clock”,
        “sortorder”: “DESC”,
        “limit”: 10 #数据数量
    },
    “auth”: “7ef823a58b59c1a17f519fe4d0e3cc44”,
    “id”: 1
}

Zabbix 调用 api 检索方法

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 #从无符号数字监控项中获取最近 10 条数据

Zabbix 调用 api 检索方法

e. 检索多个 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

正文完
星哥说事-微信公众号
post-qrcode
 
星锅
版权声明:本站原创文章,由 星锅 2022-01-21发表,共计3168字。
转载说明:除特殊说明外本站文章皆由CC-4.0协议发布,转载请注明出处。
【腾讯云】推广者专属福利,新客户无门槛领取总价值高达2860元代金券,每种代金券限量500张,先到先得。
阿里云-最新活动爆款每日限量供应
评论(没有评论)
验证码
【腾讯云】云服务器、云数据库、COS、CDN、短信等云产品特惠热卖中