I wanted a Zabbix API wrapper so that I could copy and paste code from Zabbix's API docs instead of learning whatever quirks library maintainers decided to build into their implementation.
The result is zenvelope. The method
and params
portions of the JSON
requests are used in a transparent way that is meant to be readable and most
importantly, usable without having to read any new docs.
gem install zenvelope
or add to Gemfile:
gem 'zenvelope'
To demonstrate, I'll follow the example workflow found here: https://www.zabbix.com/documentation/3.0/manual/api
require 'zenvelope'
# Use your server's URL
z = Zenvelope.new('http://company.com/zabbix')
z.login(user: 'Admin', password: 'zabbix')
Your auth token is stored in the class instance z
for subsequent API requests.
You only need to pass in the params
section of the JSON. The id
and auth
are handled for you.
z.host.get(output: %w(hostid host), selectInterfaces: %w(interfaceid ip))
# => [{:hostid=>"10084", :host=>"Zabbix server", :interfaces=>[{:interfaceid=>"1", :ip=>"127.0.0.1"}]}]
z.item.create(
name: 'Free disk space on $1',
key_: 'vfs.fs.size[/home/joe/,free]',
hostid: '10084',
type: 0,
value_type: 3,
interfaceid: '1',
delay: 30
)
# => {:itemids=>["23660"]}
z.host.create(
host: 'Linux server',
interfaces: [
{
type: 1,
main: 1,
useip: 1,
ip: '192.168.3.1',
dns: '',
port: '10050'
}
]
)
# => {:jsonrpc=>"2.0", :error=>{:code=>-32602, :message=>"Invalid params.", :data=>"No groups for host \"Linux server\"."}, :id=>70719}
- gracefully handling errors