Zabbix对api接口的调用

脚本一、进入监控主机

[root@server6 zabbix]# vim zabbix-api.sh
curl -s -X POST -H 'Content-Type:application/json' -d '
{
    "jsonrpc": "2.0",
    "method": "user.login",
    "params": {
        "user": "Admin",
        "password": "westos"
    },
    "id": 1,
    "auth": null
}' http://172.25.23.1/zabbix/api_jsonrpc.php | python -m json.tool

[root@server6 zabbix]# sh zabbix-api.sh 
{
    "id": 1, 
    "jsonrpc": "2.0", 
    "result": "fc716ee078209e8e6c445ba67e9c93f8"
}

Zabbix对api接口的调用_第1张图片

脚本二、查询主机的信息

[root@server6 zabbix]# vim zabbix-api.sh

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": "fc716ee078209e8e6c445ba67e9c93f8" 
}' http://172.25.23.1/zabbix/api_jsonrpc.php | python -m json.tool
[root@server6 zabbix]# sh zabbix-api.sh 
{
    "id": 2, 
    "jsonrpc": "2.0", 
    "result": [
        {
            "host": "Zabbix server", 
            "hostid": "10084", 
            "interfaces": [
                {
                    "interfaceid": "1", 
                    "ip": "127.0.0.1"
                }
            ]
        }, 
        {
            "host": "server6", 
            "hostid": "10258", 
            "interfaces": [
                {
                    "interfaceid": "6", 
                    "ip": "172.25.23.6"
                }
            ]
        }
    ]
}

Zabbix对api接口的调用_第2张图片
Zabbix对api接口的调用_第3张图片

脚本三:删除监控主机

curl -s -X POST -H 'Content-Type:application/json' -d '
{
    "jsonrpc": "2.0",
    "method": "host.delete",
    "params": [
        "10259"
    ],
    "auth": "fc716ee078209e8e6c445ba67e9c93f8",
    "id": 2
}' http://172.25.23.1/zabbix/api_jsonrpc.php | python -m json.tool

[root@server6 zabbix]# sh zabbix-del.sh 
{
    "id": 2, 
    "jsonrpc": "2.0", 
    "result": {
        "hostids": [
            "10259"
        ]
    }
}

Zabbix对api接口的调用_第4张图片
Zabbix对api接口的调用_第5张图片

脚本四、实现创建主机脚本

[root@server6 zabbix]# vim zabbix-create.sh


curl -s -X POST -H 'Content-Type:application/json' -d '
{
    "jsonrpc": "2.0",
    "method": "host.create",
    "params": {
        "host": "Linux server",
        "interfaces": [
            {
                "type": 1,
                "main": 1,
                "useip": 1,
                "ip": "172.25.23.6",
                "dns": "",
                "port": "10050"
            }
        ],
        "groups": [
            {
                "groupid": "2"
            }
        ],
        "templates": [
            {
                "templateid": "10001"
            }
        ]
    },
    "auth": "fc716ee078209e8e6c445ba67e9c93f8",
    "id": 1
}' http://172.25.23.1/zabbix/api_jsonrpc.php | python -m json.tool

[root@server6 zabbix]# sh zabbix-create.sh 
{
    "id": 1, 
    "jsonrpc": "2.0", 
    "result": {
        "hostids": [
            "10260"
        ]
    }
}

Zabbix对api接口的调用_第6张图片
Zabbix对api接口的调用_第7张图片

Zabbix对api接口的调用_第8张图片
Zabbix对api接口的调用_第9张图片

你可能感兴趣的:(服务)