Zapix is a tool which makes the communication with the zabbix's api simple.
If you need a more detailed information of how to use zapix see the specs.
Add this line to your application's Gemfile:
gem 'zapix', '0.1.7'
And then execute:
$ bundle install
Or install it with gem:
$ gem install zapix
First create a remote client. Feel free to disable the debug mode if you find it annoying.
require 'zapix'
zrc = ZabbixAPI.connect(
:service_url => 'https://zabbix-server.foo/api_jsonrpc.php',
:username => 'guybrush',
:password => 'threepwood',
:debug => true
)
zrc.hostgroups.create('test_hostgroup')
zrc.hostgroups.exists?('test_hostgroup')
zrc.hostgroups.any_hosts?('test_hostgroup')
zrc.hostgroups.get_host_ids_of('test_hostgroup')
Note that deleting a hostgroups with attached hosts also deletes the hosts.
zrc.hostgroups.delete('test_hostgroup')
zrc.hostgroups.get_id('test_hostgroup')
zrc.hostgroups.get_all
zrc.hosts.get_id('test_host')
zrc.hosts.exists?('test_host')
zrc.hosts.get_all
Note that in zabbix host cannot exists on its own, it always needs a hostgroup.
hostgroup_id = zrc.hostgroups.get_id('test_hostgroup')
zabbix_interface = Interface.new(
'ip' => '127.0.0.1',
'dns' => 'abrakadabra.com'
)
jmx_interface = Interface.new(
'ip' => '127.0.0.1',
'dns' => 'abrakadabra.com',
'type' => 4, # JMX
'main' => 1, # default jmx interface
'port' => 9003
)
template_1 = zrc.templates.get_id('example_template_1')
template_2 = zrc.templates.get_id('example_template_2')
example_host = Host.new
example_host.add_name('test_host')
example_host.add_interfaces(zabbix_interface.to_hash)
example_host.add_interfaces(jmx_interface.to_hash)
example_host.add_macros({'macro' => '{$TESTMACRO}', 'value' => 'test123'})
example_host.add_group_ids(hostgroup_id)
example_host.add_template_ids(template_1, template_2)
zrc.hosts.create(example_host.to_hash)
zrc.hosts.delete('test_host')
zrc.templates.exists?('test_template')
zrc.templates.get_id('test_template')
zrc.templates.get_templates_for_host(zrc.hosts.get_id('test_host'))
Note that an application always belogs to a host.
zrc.applications.get_id({
'name' => 'test_app',
'hostid' => zrc.hosts.get_id('test_name')
})
zrc.applications.create({
'name' => 'test_application'
'hostid' => zrc.hosts.get_id('test_host')
})
Note that a web scenario needs a host and an application in zabbix 2.0.6. This is going to change in the next versions of zabbix. When creating scenarios it also makes sense to create triggers for them.
zrc.scenarios.exists?({
'name' => 'test_scenario',
'hostid' => zrc.hosts.get_id('test_host')
})
zrc.scenarios.get_id({
'name' => 'test_scenario'
'hostid' => zrc.hosts.get_id('test_host')
})
zrc.applications.create({
'name' => 'test_app',
'hostid' => zrc.hosts.get_id('test_name')
})
webcheck_options = Hash.new
webcheck_options['hostid'] = zrc.hosts.get_id('host')
webcheck_options['name'] = 'my first scenario'
webcheck_options['applicationid'] = zrc.applications.get_id('test_app')
webcheck_options['steps'] = [{'name' => 'Homepage', 'url' => 'm.test.de', 'status_codes' => 200, 'no' => 1}]
zrc.scenarios.create(webcheck_options)
zrc.scenarios.delete({
'name' => 'test_scenario',
'hostid' => zrc.hosts.get_id('test_host')
})
zrc.triggers.exists?({
'expression' => "{test_host:web.test.fail[test_scenario].max(#3)}#0"
})
zrc.triggers.get_id({
'expression' => "{test_host:web.test.fail[test_scenario].max(#3)}#0"
})
options = Hash.new
options['description'] = 'Webpage failed on {HOST.NAME}'
options['expression'] = "{test_host:web.test.fail[test_scenario].max(#3)}#0"
options['priority'] = 2 # 2 means Warning
zrc.triggers.create(options)
trigger_id = zrc.triggers.get_id({
'expression' => "{test_host:web.test.fail[test_scenario].max(#3)}#0"
})
zrc.triggers.delete(trigger_id)
zrc.usergroups.exists?({
'name' => 'test_usergroup'
})
zrc.usergroups.get_id({
'name' = 'test_usergroup'
})
options = Hash.new
options['name'] = 'test_usergroup'
options['rights'] = {
'permission' => 3,
'id' => zrc.hostgroups.get_id('test_hostgroup')
}
zrc.usergroups.create(options)
usergroup_id = zrc.usergroups.get_id({'name' => 'test_usergroup'})
zrc.usergroups.delete(usergroup_id)
zrc.users.exists?({'alias' => 'max'})
zrc.users.get_id({'alias' => 'max'})
group_id = zrc.usergroups.get_id({'name' => 'test_usergroup'})
user_options = Hash.new
user_options['alias'] = 'igor'
user_options['passwd'] = 'geheim'
user_options['usrgrps'] = [{
'usrgrpid' => group_id
}]
user_options['user_medias'] = [{
'mediatypeid' => 1,
'sendto' => 'support@company.com',
'active' => 0,
'severity' => 63,
'period' => '1-7,00:00-24:00'
}]
zrc.users.create(user_options)
zrc.actions.exists?({'name' => 'Report problems to Zabbix administrators'})
zrc.actions.get_id({'name' => 'Report problems to Zabbix administrators'})
usergroup_options = Hash.new({'name' = 'test_usergroup'})
action_options = Hash.new
action_options['name'] = 'Report problems to Zabbix administrators'
action_options['eventsource'] = 0
action_options['evaltype'] = 1 # AND
action_options['status'] = 1 # Disabled
action_options['esc_period'] = 3600
action_options['def_shortdata'] = '{TRIGGER.NAME}: {TRIGGER.STATUS}'
action_options['def_longdata'] = "{TRIGGER.NAME}: {TRIGGER.STATUS}\r\nLast value: {ITEM.LASTVALUE}\r\n\r\n{TRIGGER.URL}"
action_options['conditions'] = [{
'conditiontype' => 0, # Hostgroup
'operator' => 0, # =
'value' => zrc.hostgroups.get_id('Templates')
},
# not in maintenance
{
'conditiontype' => 16, # Maintenance
'operator' => 7, # not in
'value' => 'maintenance'
}]
action_options['operations'] = [{
'operationtype' => 0,
'esc_period' => 0,
'esc_step_from' => 1,
'esc_step_to' => 1,
'evaltype' => 0,
'opmessage_grp' => [{
'usrgrpid' => zrc.usergroups.get_id(usergroup_options)
}],
'opmessage' => {
'default_msg' => 1,
'mediatypeid' => 1
}
}]
zrc.actions.create(action_options)
In order to run the rspec tests you need a running zabbix test server.
These environment variables also need to be set:
ZABBIX_API_URL
ZABBIX_API_LOGIN
ZABBIX_API_PASSWORD
Open source the docker-compose setup of the whole zabbix installation.
- Fork it
- Create your feature branch (
git checkout -b my-new-feature
) - Don't forget to write tests
- Commit your changes (
git commit -am 'Add some feature'
) - Push to the branch (
git push origin my-new-feature
) - Create new Pull Request