python对接zabbix API

from pyzabbix.api import ZabbixAPI

with ZabbixAPI(url='http://192.168.1.10:8080', user='admin', password='admin') as zapi:
    hosts = zapi.host.get(
        # 获取所有字段
        output=["hostid", "host"],
        selectGroups="extend",
        # 只获取特定字段
        # output=[
        #     "hostid",
        #     "ip",
        #     "type"
        # ],
        # 过滤需要获取
        filter={
            "groups": {
                "name": ["Meeting-Device", "Network_Device"]
            }
        }
    )
    hostdic = {}
    for i in hosts:
        hostsip = zapi.hostinterface.get(output=["hostid","ip"],
                                         filter={
                                             "hostid": i.get('hostid'),
                                         }
                                         )
        hostdic[i.get('host')] = hostsip[0].get('ip')
    print(hostdic)

 

posted @ 2024-06-03 10:39  Me-lihu  阅读(33)  评论(0编辑  收藏  举报