python對接zabbix API

Me-lihu發表於2024-06-03
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)

相關文章