zabbix api

https://blog.csdn.net/JReno/article/details/96349837

https://www.zabbix.com/documentation/current/manual/api

https://blog.csdn.net/weixin_43790276/article/details/90664236

 

 

from pyzabbix import ZabbixAPI

ZABBIX_SERVER = 'http://zabbix.com'

zapi = ZabbixAPI(ZABBIX_SERVER)
zapi.login('Admin', 'zabbix')

host_list = zapi.host.get(
    output=["host"],  # kv形式。extend表示所有,主键是默认带的。可以先选择extend,然后看具体要哪个字段
    selectInterfaces=["ip"]
)

for host in host_list:
    hostid = host['hostid']
    hostname = host['host']
    hostip = host['interfaces'][0]['ip']

    cpu_num = zapi.item.get(
        hostids=hostid,
        output=["lastvalue"],
        search={"key_": "system.cpu.num"}
    )[0]['lastvalue']

    cpu_idle = zapi.item.get(
        hostids=hostid,
        output=["lastvalue"],
        search={"key_": "system.cpu.util[,idle]"}
    )[0]['lastvalue']

    mem_total = zapi.item.get(
        hostids=hostid,
        output=["lastvalue"],
        search={"key_": "vm.memory.size[total]"}
    )[0]['lastvalue']

    mem_free = zapi.item.get(
        hostids=hostid,
        output=["lastvalue"],
        search={"key_": "vm.memory.size[available]"}
    )[0]['lastvalue']

    disk_total = zapi.item.get(
        hostids=hostid,
        output=["lastvalue"],
        search={"key_": "vfs.fs.size[/,total]"}
    )[0]['lastvalue']

    disk_used = zapi.item.get(
        hostids=hostid,
        output=["lastvalue"],
        search={"key_": "vfs.fs.size[/,used]"}
    )[0]['lastvalue']



    print('hostname:{0:45} ip:{1:16} cpu:{2:4}  cpu_idle:{3:7}%    mem_total:{4:5} MB   mem_free:{5:5} MB   disk_total:{6:3} GB   disk_free:{7:3} GB'.
                format(hostname, hostip,
                cpu_num, cpu_idle,
                int(int(mem_total) / 1000 ** 2), int(int(mem_free) / 1000 ** 2),
                int(int(disk_total) / 1000 ** 3), int(int(disk_total) / 1000 ** 3) - int(int(disk_used) / 1000 ** 3)
            )
          )

  

 

posted @ 2020-04-27 14:59  jabbok  阅读(332)  评论(0编辑  收藏  举报