调用zabbix的api接口导出主机配置信息

Posted 我的紫霞辣辣

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了调用zabbix的api接口导出主机配置信息相关的知识,希望对你有一定的参考价值。

调用zabbix的api接口导出主机配置信息

zabbix官方文档

import requests
import json
import xlwt
from xlwt.Worksheet import Worksheet

# ZABBIX 配置
ZBX_API = "https://1.1.1.1/zabbix/api_jsonrpc.php"
ZBX_USER = "lala"
ZBX_PASS = "lala520"
ZBX_Headers = {'Content-Type': "application/json"}
ZBX_GROUP = ["shanghai_mysql2008","beijing_mysql2009"]

# Token获取
def token(ZBX_API, ZBX_USER, ZBX_PASS, ZBX_Headers):
    Post_Body = {
        "jsonrpc": "2.0",
        "method": "user.login",
        "params": {
            "user": ZBX_USER,
            "password": ZBX_PASS
        },
        "id": 1
    }
    result = requests.post(ZBX_API,verify=False,headers=ZBX_Headers,data=json.dumps(Post_Body)).json()
    return result['result']

Token = token(ZBX_API,ZBX_USER,ZBX_PASS,ZBX_Headers)

# 组ID获取
def host_group(ZBX_GROUP,Token):
    Post_Body = {
        "jsonrpc": "2.0",
        "method": "hostgroup.get",
        "params": {
            "output": "groupid",
            "filter": {
                "name": ZBX_GROUP
            }
        },
    "auth": Token,
    "id": 1
    }
    result = requests.post(ZBX_API,verify=False,headers=ZBX_Headers, data=json.dumps(Post_Body)).json()
    return result['result']

# 获Item信息
def item(groupid,Token):
    Post_Body = {
        "jsonrpc": "2.0",
        "method": "item.get",
        "params": {
            "output": ["name"],
            "selectHosts": ["name"],
            "selectTriggers": ["triggerid"],
            "selectInterfaces": "extend",
            "groupids": groupid
        },
        "auth": Token,
        "id": 1
    }
    result = requests.post(ZBX_API,verify=False,headers=ZBX_Headers,data=json.dumps(Post_Body)).json()
    return result['result']

# trigger 信息获取
def trigger_inf(item_list,Token):
    Post_Body = {
        "jsonrpc": "2.0",
        "method": "trigger.get",
        "params": {
            "itemids": item_list,
            "output": ["expression", "description", "status", "priority"],
            "expandDescription": "true",
            "expandExpression": "true",
            "selectItems": "true"
        },
        "auth": Token,
        "id": 1
    }
    result = requests.post(ZBX_API,verify=False,headers=ZBX_Headers,data=json.dumps(Post_Body)).json()
    return result['result']


workbook = xlwt.Workbook(encoding="utf-8")
Worksheet = workbook.add_sheet("ZABBIX清单导出")
h = 0

Worksheet.write(h, 0, label="主机")
Worksheet.write(h, 1, label="主机组")
Worksheet.write(h, 2, label=str("IP地址或DNS"))
Worksheet.write(h, 3, label=str("指标"))
Worksheet.write(h, 4, label=str("触发器表达式"))
Worksheet.write(h, 5, label=str("触发器描述"))
Worksheet.write(h, 6, label=str("触发器等级"))
Worksheet.write(h, 7, label=str("触发器状态"))

status = {"0": "启用的","1": "已禁用"}
priority = {"0":"not classified", "1":"information","2":"warning","3":"average","4":"high","5": "disaster"}

for i in ZBX_GROUP:
    groupid = host_group(i, Token)[0]['groupid']
    item_list = []
    item_inf = item(groupid, Token)
    tirgger = {}
    for x in item_inf:
        item_list.append(x['itemid'])
    for f in trigger_inf(item_list,Token):
        for j in f['items']:
            tirgger[j['itemid']]= {"expression": f["expression"], "description": f["description"], "status": status[f["status"]], "priority": priority[f["priority"]]}
    for u in item_inf:
        h += 1
        host_name = u["hosts"][0]["name"]
        item_name = u['name']    
        try:
            host_ip = u["interfaces"][0]['ip']
        except:
            host_ip = " "
        try:
            trigger_expression = tirgger[u['itemid']]['expression']
            trigger_description = tirgger[u['itemid']]['description']
            trigger_statsu = tirgger[u['itemid']]['status']
            trigger_priority = tirgger[u['itemid']]['priority']
        except:
            trigger_expression = " --- "
            trigger_description = " --- "
            trigger_statsu = " --- "
            trigger_priority = " --- "
        Worksheet.write(h, 0, label=str(host_name))
        Worksheet.write(h, 1, label=str(i))
        Worksheet.write(h, 2, label=str(host_ip))
        Worksheet.write(h, 3, label=str(item_name))
        Worksheet.write(h, 4, label=str(trigger_expression))
        Worksheet.write(h, 5, label=str(trigger_description))
        Worksheet.write(h, 6, label=str(trigger_priority))
        Worksheet.write(h, 7, label=str(trigger_statsu))
        print("完成主机\\t\\t"+host_name+"\\t\\t"+item_name+"\\t\\t信息获取")
        
workbook.save("zabbix_configure.xlsx")

调用zabbix的api接口导出主机配置信息演示

zabbix官网的API接口

# 在命令行执行
curl -X POST -H 'Content-Type:application/json' -k -d '
{
    "jsonrpc": "2.0",
    "method": "user.login",
    "params": {
        "user": "nana",
        "password": "123"
    },
    "id": 1,
    "auth": null
}' https://1.1.1.1/zabbix/api_jsonrpc.php 

# 获取到Token值
{"jsonrpc":"2.0","result":"deb180984af5d5f185ec0c92e20dfb09","id":1}


curl -X POST -H 'Content-Type:application/json' -k -d '
{
    "jsonrpc": "2.0",
    "method": "host.get",
    "params": {
        "output": [
            "hostid",
            "host"
        ],
        "selectInterfaces": [
            "interfaceid",
            "ip"
        ]
    },
    "id": 1,
    "auth": "deb180984af5d5f185ec0c92e20dfb09"
}'  https://1.1.1.1/zabbix/api_jsonrpc.php 

# 获取到数据
{"hostid":"11988","host":"CL-CNSNL06","interfaces":[{"interfaceid":"1851","ip":"1.1.1.1"}]}],"id":1}

以上是关于调用zabbix的api接口导出主机配置信息的主要内容,如果未能解决你的问题,请参考以下文章

Zabbix 调用API 批量添加主机等

python3调用zabbix api接口对自动发现添加的主机修改主机名

zabbix调用api接口批量添加主机

利用ZABBIX进行服务器自动巡检并导出报表

python 调用zabbix api实现查询主机信息,输出所有主机ip

python 调用zabbix api实现查询主机信息,输出所有主机ip