-
Notifications
You must be signed in to change notification settings - Fork 130
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #86 from lehn-etracker/feature/add-drule-api
Feature: add support for Discovery Rules (`drule` API)
- Loading branch information
Showing
4 changed files
with
133 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
# Discovery Rule | ||
|
||
This example assumes you have already initialized and connected the ZabbixApi. | ||
|
||
For more information and available properties please refer to the Zabbix API documentation for Actions: | ||
[https://www.zabbix.com/documentation/3.2/manual/api/reference/drule](https://www.zabbix.com/documentation/3.2/manual/api/reference/drule) | ||
|
||
## Create Discovery Rule | ||
```ruby | ||
zbx.drules.create( | ||
:name => 'zabbix agent discovery', | ||
:delay => '1800', # discover new machines every 30min | ||
:status => '0', # action is enabled | ||
:iprange => '192.168.0.0/24', # iprange to discover zabbix agents in | ||
:dchecks => [{ | ||
:type => '9', # zabbix agent | ||
:uniq => '0', # (default) do not use this check as a uniqueness criteria | ||
:key_ => 'system.hostname', | ||
:ports => '10050', | ||
}], | ||
) | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
class ZabbixApi | ||
class Drules < Basic | ||
# The method name used for interacting with Drules via Zabbix API | ||
# | ||
# @return [String] | ||
def method_name | ||
'drule' | ||
end | ||
|
||
# The id field name used for identifying specific Drule objects via Zabbix API | ||
# | ||
# @return [String] | ||
def indentify | ||
'name' | ||
end | ||
|
||
# The default options used when creating Drule objects via Zabbix API | ||
# | ||
# @return [Hash] | ||
def default_options | ||
{ | ||
name: nil, | ||
iprange: nil, | ||
delay: 3600, | ||
status: 0, | ||
} | ||
end | ||
|
||
# Get or Create Drule object using Zabbix API | ||
# | ||
# @param data [Hash] Needs to include name to properly identify Drule via Zabbix API | ||
# @raise [ApiError] Error returned when there is a problem with the Zabbix API call. | ||
# @raise [HttpError] Error raised when HTTP status from Zabbix Server response is not a 200 OK. | ||
# @return [Integer] Zabbix object id | ||
def get_or_create(data) | ||
log "[DEBUG] Call get_or_create with parameters: #{data.inspect}" | ||
|
||
unless (id = get_id(name: data[:name])) | ||
id = create(data) | ||
end | ||
id | ||
end | ||
|
||
# Create or update Drule object using Zabbix API | ||
# | ||
# @param data [Hash] Needs to include name to properly identify Drules via Zabbix API | ||
# @raise [ApiError] Error returned when there is a problem with the Zabbix API call. | ||
# @raise [HttpError] Error raised when HTTP status from Zabbix Server response is not a 200 OK. | ||
# @return [Integer] Zabbix object id | ||
def create_or_update(data) | ||
druleid = get_id(name: data[:name]) | ||
druleid ? update(data.merge(druleid: druleid)) : create(data) | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
# encoding: utf-8 | ||
|
||
require 'spec_helper' | ||
|
||
describe 'drule' do | ||
before do | ||
@drulename = gen_name 'drule' | ||
@usergroupid = zbx.usergroups.create(:name => gen_name('usergroup')) | ||
@dcheckdata = [{ | ||
:type => '9', # zabbix agent | ||
:uniq => '0', # (default) do not use this check as a uniqueness criteria | ||
:key_ => 'system.hostname', | ||
:ports => '10050', | ||
}] | ||
@druledata = { | ||
:name => @drulename, | ||
:delay => '60', | ||
:status => '0', # action is enabled | ||
:iprange => '192.168.0.0/24', # iprange to discover zabbix agents in | ||
:dchecks => @dcheckdata, | ||
} | ||
end | ||
|
||
context 'when not exists' do | ||
describe 'create' do | ||
it 'should return integer id' do | ||
druleid = zbx.drules.create(@druledata) | ||
expect(druleid).to be_kind_of(Integer) | ||
end | ||
end | ||
end | ||
|
||
context 'when exists' do | ||
before do | ||
@druleid = zbx.drules.create(@druledata) | ||
end | ||
|
||
describe 'create_or_update' do | ||
it 'should return id' do | ||
expect(zbx.drules.create_or_update(@druledata)).to eq @druleid | ||
end | ||
end | ||
|
||
describe 'delete' do | ||
it 'should return id' do | ||
expect(zbx.drules.delete(@druleid)).to eq @druleid | ||
end | ||
end | ||
end | ||
end |