zabbix api 获取所有主机名

zabbix api get all host names

我正在使用 Python 查询 Zabbix 服务器以尝试获取主机 ID 和主机名列表。我正在测试以下内容:

zapi = ZabbixAPI(server=server, log_level=debuglevel)
zapi.login(username, password)

hosts = zapi.host.get({"params":{"output":"hostid", "name"}})
print hosts

上面的测试只打印出hostids。不会检索主机名。

输出示例:

[{u'hostid': u'10084'}, {u'hostid': u'30000'}, {u'hostid': u'30001'}, {u'hostid': u'30002'}]

我做错了什么? :(

你的参数有误。必须是数组:

zapi = ZabbixAPI(server=server, log_level=debuglevel)
zapi.login(username, password)

hosts = zapi.host.get(output=["hostid", "name"])
print hosts
[{u'hostid': u'10084', u'name': u'Zabbix server'}]

对于那些在 Ruby 中寻找 Express42/zabbixapi 版本的人:

#!/usr/bin/env ruby
require 'pp'
require 'zabbixapi'

hostname = 'hostname.domain.com'

zbx = ZabbixApi.connect(
  url: 'http://localhost/zabbix/api_jsonrpc.php',
  user: 'Admin',
  password: 'zabbix'
)
pp zbx.query(
  method: 'host.get',
  params: {
    output: %w[extend hostid name],
    filter: {
      name: hostname
    }
  }
)

删除 Zabbix 数据库中 return 所有主机的 filter 对象。

试试这个代码:

from zabbix_api import ZabbixAPI


server = ""   #address   
username = ""              #user   
password = ""     # pass  


zapi = ZabbixAPI(server = server)
zapi.login(username, password)

hostgroups = zapi.hostgroup.get({"output": "extend", "sortfield": "name"}) # for groupid
hosts = zapi.host.get({"groupids": "36", "output": ["hostid","name", "host"]}) # for host and name