我使用Python查询Zabbix服务器,试图获取hostid和主机名列表。我正在测试以下内容:
zapi = ZabbixAPI(server=server, log_level=debuglevel)
zapi.login(username, password)
hosts = zapi.host.get({"params":{"output":"hostid", "name"}})
print hosts
以上测试仅打印出hostid。未检索主机名。
输出示例:
[{u'hostid': u'10084'}, {u'hostid': u'30000'}, {u'hostid': u'30001'}, {u'hostid': u'30002'}]
我做错了什么? :(
答案 0 :(得分:4)
你的参数错了。它必须是数组:
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'}]
答案 1 :(得分:0)
对于那些在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
}
}
)
删除filter
对象以返回Zabbix数据库中的所有主机。
答案 2 :(得分:0)
尝试此代码:
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