Zabbix::Api
This zabbix api implementation for Ruby strives for three things:
- To the greatest extent possible, be syntactically & structurally identical to the API as it is described in the Zabbix documentation
- Be API version agnostic
- Be internally simple, unobtrusive, and low-maintenance
It accomplishes these goals primarily by deferring (via Faraday) to the Zabbix API itself for just about everything - it does not attempt to re-create or otherwise "fancy-up" the API. The reasoning behind this is that the zabbix api itself is complex enough, and the Zabbix project can alter the specifics of the api at any time. This library just provides Ruby semantics that closely follow the API specification. The advantage to you is that you do not need to wrap your head around two APIs (the Zabbix API proper, and the API library's API)
Detailed documentation for this library is >>HERE<<
Source repository is >>HERE<<
If you need to send data into Zabbix via the "Zabbix trapper" protocol, you might also be interested in zabbix_sender_api
Installation
Add this line to your application's Gemfile:
gem 'zabbix-api-simple'
And then execute:
$ bundle install
Or install it yourself as:
$ gem install zabbix-api-simple
Usage
Once you have an authenticated instance of the client, syntax is basically always this:
clientinstance.object.verb(<param kv pairs>)
If "object" is unknown, "Unknown zabbix object given" will be raised.
If this call results in an error from the API, a RuntimeError will be raised indicating the specifics.
Complete detail about the last transaction you attempted is available via clientinstance.last
e.g.:
#!/usr/bin/env ruby
require 'zabbix/api'
require 'optimist'
require 'amazing_print'
opts = Optimist:: do
opt :url, "URL up to but no including api_jsonrpc.php",type: :string,default:'http://localhost'
opt :user, "User name to authenticate", type: :string, required: true
opt :pass, "Pass to auth user with", type: :string, required: true
opt :hostname, "Host name to search for", type: :string, required: true
end
api = Zabbix::Api::Client.new(url: opts[:url])
api.login(user: opts[:user],pass:opts[:pass])
hosts = api.host.get(search: {host: opts[:hostname]}) # return value is an OpenStruct
hosts.each {|host|
puts host.name
puts host.hostid
ap host.to_h
}
api.host.create(
host: "mynewhost",
interfaces: {
type: 1,
main: 1,
useip: 1,
ip: "1.2.3.4",
dns: "",
port: "10050"
},
groups: {groupid: "42"}
)
ap api.last # shows detail of the last transaction
ap api.logout
There's a little cli program that gets installed with the gem called zapishell.rb:
# zapishell.rb --help
Options:
-u, --url=<s> URL up to but no including api_jsonrpc.php (default: http://localhost)
-s, --user=<s> User name to authenticate
-p, --pass=<s> Pass to auth user with
-h, --help Show this message
It attempts to authenticate you to the given server, and then drops you to a pry prompt. You can play around with the api in that by doing api.object.verb(key: 'value pairs')
Refer to the API documentation for your version of Zabbix for the particulars.
Contributing
Bug reports and pull requests are welcome on GitLab at https://gitlab.com/svdasein/zabbix-api