Zabbix API官方文档: https://www.zabbix.com/documentation/4.0/zh/manual/api
1、向 api_jsonrpc.php
发送HTTP_POST 登录请求,获取身份验证令牌
# -*- coding:utf-8 -*-
import json
import requests url = 'http://172.10.10.2/zabbix/api_jsonrpc.php'
post_headers = {'Content-Type': 'application/json'}
post_data = {
"jsonrpc" : "2.0",
"method" : "user.login",
"params" : {
"user" : "Admin",
"password" : "zabbix"
},
"id" : 1
} ret = requests.post(url, data = json.dumps(post_data), headers = post_headers)
print(ret.text)
输出结果:
{
"jsonrpc":"2.0",
"result":"da336b04d376d914bf06bd2192c4ce3f", #身份验证令牌
"id":1
}
2、查询所有主机的信息
url = 'http://172.10.10.2/zabbix/api_jsonrpc.php'
post_headers = {'Content-Type': 'application/json'}
post_data = {
"jsonrpc": "2.0",
"method": "host.get",
"params": {
"output": [
"hostid",
"host"
],
"selectInterfaces": [
"interfaceid",
"ip"
]
},
"id": 2,
"auth": "da336b04d376d914bf06bd2192c4ce3f" #这是第一步获取的身份验证令牌
} ret = requests.post(url, data = json.dumps(post_data), headers = post_headers)
print(ret.text)
输出结果:
{
"jsonrpc": "2.0",
"result": [
{
"hostid": "",
"host": "Zabbix server",
"interfaces": [
{
"interfaceid": "",
"ip": "127.0.0.1"
}
]
}
],
"id": 2
}
3、获取主机组信息
url = 'http://172.10.10.2/zabbix/api_jsonrpc.php'
post_headers = {'Content-Type': 'application/json'}
post_data = {
"jsonrpc": "2.0",
"method": "hostgroup.get",
"params": {
"output": "extend",
"filter": {
"name": [
"Linux servers"
]
}
},
"auth": "da336b04d376d914bf06bd2192c4ce3f",
"id": 1
} ret = requests.post(url, data = json.dumps(post_data), headers = post_headers)
print(ret.text)
输出结果:
{
"jsonrpc": "2.0",
"result": [
{
"groupid": "",
"name": "Linux servers",
"internal": "",
"flags": ""
}
],
"id": 1
}
4、获取模版信息
url = 'http://172.10.10.2/zabbix/api_jsonrpc.php'
post_headers = {'Content-Type': 'application/json'}
post_data = {
"jsonrpc": "2.0",
"method": "template.get",
"params": {
"output": "extend",
"filter": {
"host": [
"Template OS Linux"
]
}
},
"auth": "dfba5d41dc9b46d6525f70af13631cb6",
"id": 1
} ret = requests.post(url, data = json.dumps(post_data), headers = post_headers)
print(ret.text)
输出结果:
{
"jsonrpc": "2.0",
"result": [
{
"proxy_hostid": "",
"host": "Template OS Linux",
"status": "",
"disable_until": "",
"error": "",
"available": "",
"errors_from": "",
"lastaccess": "",
"ipmi_authtype": "-1",
"ipmi_privilege": "",
"ipmi_username": "",
"ipmi_password": "",
"ipmi_disable_until": "",
"ipmi_available": "",
"snmp_disable_until": "",
"snmp_available": "",
"maintenanceid": "",
"maintenance_status": "",
"maintenance_type": "",
"maintenance_from": "",
"ipmi_errors_from": "",
"snmp_errors_from": "",
"ipmi_error": "",
"snmp_error": "",
"jmx_disable_until": "",
"jmx_available": "",
"jmx_errors_from": "",
"jmx_error": "",
"name": "Template OS Linux",
"flags": "",
"templateid": "",
"description": "",
"tls_connect": "",
"tls_accept": "",
"tls_issuer": "",
"tls_subject": "",
"tls_psk_identity": "",
"tls_psk": "",
"proxy_address": "",
"auto_compress": ""
}
],
"id": 1
}
5、创建主机
url = 'http://172.10.10.2/zabbix/api_jsonrpc.php'
post_headers = {'Content-Type': 'application/json'}
post_data = {
"jsonrpc": "2.0",
"method": "host.create",
"params": {
"host": "Linux server",
"interfaces": [
{
"type": 1,
"main": 1,
"useip": 1,
"ip": "192.168.3.1",
"dns": "",
"port": ""
}
],
"groups": [
{
"groupid": "" #填写第3步获取的组ID
}
],
"templates": [
{
"templateid": "" #填写第4步获取的模板ID
}
],
"macros": [
{
"macro": "{$USER_ID}",
"value": ""
}
],
"inventory_mode": 0,
"inventory": {
"macaddress_a": "",
"macaddress_b": ""
}
},
"auth": "dfba5d41dc9b46d6525f70af13631cb6",
"id": 1
} ret = requests.post(url, data = json.dumps(post_data), headers = post_headers)
print(ret.text)
输出结果:
{
"jsonrpc": "2.0",
"result": {
"hostids": [
""
]
},
"id": 1
}