Zabbix--常用API

详细的API说明请查阅官网API文档

获取版本信息

curl http://192.168.100.101/zabbix/api_jsonrpc.php -H 'Content-Type:application/json-rpc' -d '{
    "jsonrpc": "2.0",
    "method": "apiinfo.version",
    "params": [],
    "id": 1
}' 

返回信息

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

获取Auth Token

curl http://192.168.100.101/zabbix/api_jsonrpc.php -H 'Content-Type:application/json-rpc' -d '{
    "jsonrpc": "2.0",
    "method": "user.login",
    "params": {
        "user": "Admin",
        "password": "zabbix"
    },
    "id": 1,
    "auth": null
}'

返回信息:

{
    "jsonrpc":"2.0",
    "result":"3fde27585d0c9a1e8f990d5413b42ab9",
    "id":1
}

结果中result的值即为Token,可以作为其他API auth参数的值

主机组

此步主要是获得主机组ID,创建主机时会用到此ID

创建主机组

curl http://192.168.100.101/zabbix/api_jsonrpc.php -H 'Content-Type:application/json-rpc' -d '{
    "jsonrpc": "2.0",
    "method": "hostgroup.create",
    "params": {
        "name": "Test Group"
    },
    "auth": "3fde27585d0c9a1e8f990d5413b42ab9",
    "id": 1
}'

返回信息:

{
    "id": 1,
    "jsonrpc": "2.0",
    "result": {
        "groupids": [
            "15"
        ]
    }
}

获取主机组

curl http://192.168.100.101/zabbix/api_jsonrpc.php -H 'Content-Type:application/json-rpc' -d '{
    "jsonrpc": "2.0",
    "method": "hostgroup.get",
    "params": {
        "output": "extend",
        "filter": {
            "name": [
                "Linux servers",
                "Test Group"
            ]
        }
    },
    "auth": "3fde27585d0c9a1e8f990d5413b42ab9",
    "id": 1
}'

返回信息:

{
    "id": 1,
    "jsonrpc": "2.0",
    "result": [
        {
            "flags": "0",
            "groupid": "2",
            "internal": "0",
            "name": "Linux servers"
        },
        {
            "flags": "0",
            "groupid": "15",
            "internal": "0",
            "name": "Test Group"
        }
    ]
}

模板

导出模板

curl http://192.168.100.101/zabbix/api_jsonrpc.php -H 'Content-Type:application/json-rpc' -d '{
    "jsonrpc": "2.0",
    "method": "configuration.export",
    "params": {
        "options": {
            "templates": [
                "10001"
            ]
        },
        "format": "xml"
    },
    "auth": "3fde27585d0c9a1e8f990d5413b42ab9",
    "id": 1
}'

导入模板

curl http://192.168.100.101/zabbix/api_jsonrpc.php -H 'Content-Type:application/json-rpc' -d '{
    "jsonrpc": "2.0",
    "method": "configuration.import",
    "params": {
        "format": "xml",
        "rules": {
            "templates": {
                "createMissing": true,
                "updateExisting": true
            },
            "items": {
                "createMissing": true,
                "updateExisting": true,
                "deleteMissing": true
            }
        },
        "source": "${template_xml_data}"
    },
    "auth": "3fde27585d0c9a1e8f990d5413b42ab9",
    "id": 1
}'

获取模板

curl http://192.168.100.101/zabbix/api_jsonrpc.php -H 'Content-Type:application/json-rpc' -d '{
    "jsonrpc": "2.0",
    "method": "template.get",
    "params": {
        "output": "extend",
        "filter": {
            "host": [
                "Template OS Linux"
            ]
        }
    },
    "auth": "3fde27585d0c9a1e8f990d5413b42ab9",
    "id": 1
}'

返回信息:

{
    "id": 1,
    "jsonrpc": "2.0",
    "result": [
        {
            "available": "0",
            "description": "",
            "disable_until": "0",
            "error": "",
            "errors_from": "0",
            "flags": "0",
            "host": "Template OS Linux",
            "ipmi_authtype": "-1",
            "ipmi_available": "0",
            "ipmi_disable_until": "0",
            "ipmi_error": "",
            "ipmi_errors_from": "0",
            "ipmi_password": "",
            "ipmi_privilege": "2",
            "ipmi_username": "",
            "jmx_available": "0",
            "jmx_disable_until": "0",
            "jmx_error": "",
            "jmx_errors_from": "0",
            "lastaccess": "0",
            "maintenance_from": "0",
            "maintenance_status": "0",
            "maintenance_type": "0",
            "maintenanceid": "0",
            "name": "Template OS Linux",
            "proxy_hostid": "0",
            "snmp_available": "0",
            "snmp_disable_until": "0",
            "snmp_error": "",
            "snmp_errors_from": "0",
            "status": "3",
            "templateid": "10001",
            "tls_accept": "1",
            "tls_connect": "1",
            "tls_issuer": "",
            "tls_psk": "",
            "tls_psk_identity": "",
            "tls_subject": ""
        }
    ]
}

可以从输出中获取模板ID,用于增加主机时关联模板

主机

创建主机

curl http://192.168.100.101/zabbix/api_jsonrpc.php -H 'Content-Type:application/json-rpc' -d '{
    "jsonrpc": "2.0",
    "method": "host.create",
    "params": {
        "host": "Centos_111",
        "interfaces": [
            {
                "type": 1,
                "main": 1,
                "useip": 1,
                "ip": "192.168.100.111",
                "dns": "",
                "port": "10050"
            }
        ],
        "groups": [
            {
                "groupid": "15"
            }
        ],
        "templates": [
            {
                "templateid": "10001"
            }
        ]
    },
    "auth": "3fde27585d0c9a1e8f990d5413b42ab9",
    "id": 1
}'

返回信息:

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

获取主机

curl http://192.168.100.101/zabbix/api_jsonrpc.php -H 'Content-Type:application/json-rpc' -d '{
    "jsonrpc": "2.0",
    "method": "host.get",
    "params": {
        "output": "extend",
        "filter": {
            "host": [
                "Centos_111"
            ]
        }
    },
    "auth": "3fde27585d0c9a1e8f990d5413b42ab9",
    "id": 1
}'

你可能感兴趣的:(Zabbix)