akagera
akagera

Reputation: 25

zabbix api get all host names

I'm using Python to query a Zabbix server in an attempt to get a list of hostids and hostnames. I'm testing with the following:

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

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

The above test only prints out the hostids. The host names are not retrieved.

example of output:

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

What I am doing wrong? :(

Upvotes: 2

Views: 14777

Answers (3)

Thiago Eltz
Thiago Eltz

Reputation: 21

try this code:

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

Upvotes: 2

FilBot3
FilBot3

Reputation: 3708

For those looking for the Express42/zabbixapi version of this in Ruby:

#!/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
    }
  }
)

Remove the filter object to return ALL hosts in the Zabbix database.

Upvotes: 1

Jan Garaj
Jan Garaj

Reputation: 28636

Your parameters are wrong. It must be array:

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'}]

Upvotes: 7

Related Questions