Loading

Zabbix API获取监控项的值

使用history.get API进行监控项的获取,进行排序和limit就可以取到最新值;

1.1 如何进行API认证?

获取认证中的result认证值,在后面的请求中使用;

curl -X POST \
  http://IP:Port/zabbix/api_jsonrpc.php \
  -H 'Content-Type: application/json' \-d '{
    "jsonrpc": "2.0",
    "method": "user.login",
    "params": {
        "user": "Admin",
        "password": "zabbix"
    },
    "id": 1
}'

返回结果是:

{
    "jsonrpc": "2.0",
    "result": "f3f5xxx69e958",
    "id": 1
}

 

1.2 如何获取某一个监控项的值?

使用的方法是history.get;其中的history表示数据类型;limit=1表示只取一条记录,再按照时间倒排就可以取到最新值;
history取值:0-浮点,1-字符,2-日志,3-整数(默认),4-文本;

itemids可以在zabbix界面查看到,点击查看该监控项的图形可以在地址栏上面看到;

auth的值就是上一步获取的认证结果;

curl -X POST \
  http://IP:Port/zabbix/api_jsonrpc.php \
  -H 'Content-Type: application/json' \-d '{
    "jsonrpc": "2.0",
    "method": "history.get",
    "params": {
        "output": "extend",
        "history": 3,
        "itemids": "30632",
        "sortfield": "clock",
        "sortorder": "DESC",
        "limit": 1
    },
    "auth": " f3f5xxx69e958",
    "id": 1
}'

返回结果:

{
    "jsonrpc": "2.0",
    "result": [
        {
            "itemid": "30632",
            "clock": "1603271688",
            "value": "812304",
            "ns": "882748120"
        }
    ],
    "id": 1
}

 

posted @ 2020-10-22 11:17  stono  阅读(1678)  评论(0编辑  收藏  举报