• 欢迎访问显哥博客,本网站纯属学习技术,绝无商业用途,欢迎小伙伴们共同学习!研究技术!QQ:52249909 加我QQ
  • 世界75亿人,这么小的概率,能认识你,是我一生的幸运,不妨加个QQ接触一下:52249909 加我QQ

企业级zabbix监控实战——zabbix API场景实战【显哥出品,必为精品】

Zabbix监控 lixian 5年前 (2020-05-26) 2828次浏览 0个评论 扫描二维码
文章目录[隐藏]

一、zabbix API 概述

Zabbix API允许你以编程方式检索和修改Zabbix的配置,并提供对历史数据的访问。它广泛用于:
创建新的应用程序以使用Zabbix;
将Zabbix与第三方软件集成;
自动执行常规任务。

Zabbix API是基于Web的API,作为Web前端的一部分提供。它使用JSON-RPC 2.0协议,这意味着两件事:
该API包含一组独立的方法;
客户端和API之间的请求和响应使用JSON格式进行编码。

二、获取令牌

[root@tomcat ~]# curl -s -X POST -H 'Content-Type: application/json' -d  '{
    "jsonrpc": "2.0",
    "method": "user.login",
    "params": {
        "user": "Admin",
        "password": "zabbix"
    },
    "id": 1,
    "auth": null
}'  http://10.0.0.71/zabbix/api_jsonrpc.php
{"jsonrpc":"2.0","result":"c6364595b488525afb17b9f3c9ee9376","id":1}

三、检索主机

 curl -s -X POST -H 'Content-Type: application/json' -d  '{
    "jsonrpc": "2.0",
    "method": "host.get",
    "params": {
        "output": [
            "hostid",
            "host"
        ],
        "selectInterfaces": [
            "interfaceid",
            "ip"
        ]
    },
    "id": 2,
    "auth": "7f59a9f697235782ff2ee0578c6cbb70"
}'  http://10.0.0.110/zabbix/api_jsonrpc.php

[root@db01 ~]#  curl -s -X POST -H 'Content-Type: application/json' -d  '{
>     "jsonrpc": "2.0",
>     "method": "host.get",
>     "params": {
>         "output": [
>             "hostid",
>             "host"
>         ],
>         "selectInterfaces": [
>             "interfaceid",
>             "ip"
>         ]
>     },
>     "id": 2,
>     "auth": "7f59a9f697235782ff2ee0578c6cbb70"
> }'  http://10.0.0.110/zabbix/api_jsonrpc.php

{"jsonrpc":"2.0","result":[{"hostid":"10084","host":"Zabbix server","interfaces":[{"interfaceid":"1","ip":"127.0.0.1"}]},{"hostid":"10287","host":"web01","interfaces":[{"interfaceid":"11","ip":"172.16.1.7"}]},{"hostid":"10288","host":"tomcat","interfaces":[{"interfaceid":"12","ip":"172.16.1.8"}]},{"hostid":"10289","host":"db01","interfaces":[{"interfaceid":"13","ip":"172.16.1.51"}]}],"id":2}

四、创建主机

curl -s -X POST -H 'Content-Type: application/json' -d  '{
    "jsonrpc": "2.0",
    "method": "host.create",
    "params": {
        "host": "web01",
        "interfaces": [
            {
                "type": 1,
                "main": 1,
                "useip": 1,
                "ip": "172.16.1.7",
                "dns": "",
                "port": "10050"
            }
        ],
        "groups": [
            {
                "groupid": "15"
            }
        ],
        "templates": [
            {
                "templateid": "10285"
            }
        ],
        "inventory_mode": 0,
        "inventory": {
            "macaddress_a": "01234",
            "macaddress_b": "56768"
        }
    },
    "auth": "7f59a9f697235782ff2ee0578c6cbb70",
    "id": 1
}'  http://10.0.0.110/zabbix/api_jsonrpc.php


{"jsonrpc":"2.0","result":{"hostids":["10290"]},"id":1}

五、创建监控项

curl -s -X POST -H 'Content-Type: application/json' -d  '{
    "jsonrpc": "2.0",
    "method": "item.create",
    "params": {
        "name": "用户登录的数量",
        "key_": "login_number",
        "hostid": "10290",
        "type": 0,
        "value_type": 3,
        "interfaceid": "14",
        "delay": 30
    },
    "auth": "7f59a9f697235782ff2ee0578c6cbb70",
    "id": 3
}'  http://10.0.0.110/zabbix/api_jsonrpc.php



{"jsonrpc":"2.0","result":{"itemids":["30228"]},"id":3}

六、删除主机

curl -s -X POST -H 'Content-Type: application/json' -d  '{
    "jsonrpc": "2.0",
    "method": "host.delete",
    "params": [
        "10290"
    ],
    "auth": "7f59a9f697235782ff2ee0578c6cbb70",
    "id": 1
}'  http://10.0.0.110/zabbix/api_jsonrpc.php


{"jsonrpc":"2.0","result":{"hostids":["10290"]},"id":1}

七、zabbix优化


本站博主 , 版权所有丨如未注明 , 均为原创
转载请注明原文链接:企业级zabbix监控实战——zabbix API场景实战【显哥出品,必为精品】
喜欢 (0)

您必须 登录 才能发表评论!