成功最有效的方法就是向有经验的人学习!

使用Python调用Zabbix API

Zabbix API官方文档: https://www.zabbix.com/documentation/4.0/zh/manual/api

之前我发了一版bash脚本调用zabbix api,现在这版是用了python可以对比看一下
https://www.linuxbaodian.com/shellsp/83.html

1、向 api_jsonrpc.php 发送HTTP_POST 登录请求,获取身份验证令牌

# -*- coding:utf-8 -*-
import json
import requests

url = 'http://10.0.0.5/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://10.0.0.5/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://10.0.0.5/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://10.0.0.5/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://10.0.0.5/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": "10.0.0.10",
                "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
}
赞(2) 打赏
未经允许不得转载:陈桂林博客 » 使用Python调用Zabbix API
分享到

大佬们的评论 抢沙发

全新“一站式”建站,高质量、高售后的一条龙服务

微信 抖音 支付宝 百度 头条 快手全平台打通信息流

橙子建站.极速智能建站8折购买虚拟主机

觉得文章有用就打赏一下文章作者

非常感谢你的打赏,我们将继续给力更多优质内容,让我们一起创建更加美好的网络世界!

支付宝扫一扫打赏

微信扫一扫打赏

登录

找回密码

注册