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
}'
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章