python3调用zabbix-api
Zabbix API官方文档: https://www.zabbix.com/documentation/4.0/zh/manual/api
转自:https://www.cnblogs.com/zhenwei66/p/11059457.html
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": "10084", "host": "Zabbix server", "interfaces": [ { "interfaceid": "1", "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": "2", "name": "Linux servers", "internal": "0", "flags": "0" } ], "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": "0", "host": "Template OS Linux", "status": "3", "disable_until": "0", "error": "", "available": "0", "errors_from": "0", "lastaccess": "0", "ipmi_authtype": "-1", "ipmi_privilege": "2", "ipmi_username": "", "ipmi_password": "", "ipmi_disable_until": "0", "ipmi_available": "0", "snmp_disable_until": "0", "snmp_available": "0", "maintenanceid": "0", "maintenance_status": "0", "maintenance_type": "0", "maintenance_from": "0", "ipmi_errors_from": "0", "snmp_errors_from": "0", "ipmi_error": "", "snmp_error": "", "jmx_disable_until": "0", "jmx_available": "0", "jmx_errors_from": "0", "jmx_error": "", "name": "Template OS Linux", "flags": "0", "templateid": "10001", "description": "", "tls_connect": "1", "tls_accept": "1", "tls_issuer": "", "tls_subject": "", "tls_psk_identity": "", "tls_psk": "", "proxy_address": "", "auto_compress": "1" } ], "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": "10050" } ], "groups": [ { "groupid": "2" #填写第3步获取的组ID } ], "templates": [ { "templateid": "10001" #填写第4步获取的模板ID } ], "macros": [ { "macro": "{$USER_ID}", "value": "123321" } ], "inventory_mode": 0, "inventory": { "macaddress_a": "01234", "macaddress_b": "56768" } }, "auth": "dfba5d41dc9b46d6525f70af13631cb6", "id": 1 } ret = requests.post(url, data = json.dumps(post_data), headers = post_headers) print(ret.text)
输出结果:
{ "jsonrpc": "2.0", "result": { "hostids": [ "10264" ] }, "id": 1 }
http://www.yueminghai.top