wiki:zbx_api

Zabbix API

Dependencies

The Zabbix Ruby API interface requires the following packages

The following is an example file which shows the basic use of the Gem. In addition this example shows how to use the proxy capabilities as well. This example assumes the zbxapi Gem has already been installed.

#!/usr/bin/ruby

require "rubygems"
require "zbxapi"
require "pp"

@server_url="http://zabbix.example.com/zabbix"

@login_user="admin"
@login_pass="zabbix"

@proxy_host="localhost"
@proxy_port=3128

zabbix = ZabbixAPI.new(@server)
zabbix.set_proxy(@proxy_host,@proxy_pass)
zabbix.login(@login_user,@login_pass)

hosts = zabbix.host.get

pp hosts
puts
pp hosts[1]

This script will result in output similar to the follwoing:

[{"hostid"=>"10017"}, 
 {"hostid"=>"10047"}, 
 {"hostid"=>"10049"}]

{"hostid"=>"10047"}

Continuing on with the above, we can also send arguments along with the host.get statement, such as the following:

hosts = zabbix.host.get({"output"=>"extend"})
pp hosts[1..2]

Will result in the following:

[{"snmp_errors_from"=>"0",
  "ipmi_port"=>"623",
  "outbytes"=>"0",
  "proxy_hostid"=>"0",
  "maintenance_status"=>"0",
  "ipmi_password"=>"",
  "ipmi_privilege"=>"2",
  "ipmi_disable_until"=>"0",
  "available"=>"1",
  "maintenances"=>[{"maintenanceid"=>"0"}],
  "ipmi_error"=>"",
  "maintenanceid"=>"0",
  "useipmi"=>"0",
  "port"=>"10050",
  "useip"=>"1",
  "ipmi_errors_from"=>"0",
  "maintenance_from"=>"0",
  "ipmi_ip"=>"",
  "hostid"=>"10047",
  "ipmi_available"=>"0",
  "disable_until"=>"0",
  "ip"=>"1.2.3.4",
  "maintenance_type"=>"0",
  "ipmi_authtype"=>"-1",
  "inbytes"=>"0",
  "lastaccess"=>"0",
  "host"=>"Admin",
  "snmp_error"=>"",
  "ipmi_username"=>"",
  "status"=>"0",
  "dns"=>"",
  "snmp_available"=>"0",
  "snmp_disable_until"=>"0",
  "errors_from"=>"0",
  "error"=>""},
 {"snmp_errors_from"=>"0",
  "ipmi_port"=>"623",
  "outbytes"=>"0",
  "proxy_hostid"=>"0",
  "maintenance_status"=>"0",
  "ipmi_password"=>"",
  "ipmi_privilege"=>"2",
  "ipmi_disable_until"=>"0",
  "available"=>"0",
  "maintenances"=>[{"maintenanceid"=>"0"}],
  "ipmi_error"=>"",
  "maintenanceid"=>"0",
  "useipmi"=>"0",
  "port"=>"10050",
  "useip"=>"1",
  "ipmi_errors_from"=>"0",
  "maintenance_from"=>"0",
  "ipmi_ip"=>"",
  "hostid"=>"10049",
  "ipmi_available"=>"0",
  "disable_until"=>"0",
  "ip"=>"1.2.3.5",
  "maintenance_type"=>"0",
  "ipmi_authtype"=>"-1",
  "inbytes"=>"0",
  "lastaccess"=>"0",
  "host"=>"SSG-20",
  "snmp_error"=>"",
  "ipmi_username"=>"",
  "status"=>"0",
  "dns"=>"",
  "snmp_available"=>"1",
  "snmp_disable_until"=>"0",
  "errors_from"=>"0",
  "error"=>""}]