Skip to content

Commit

Permalink
Merge pull request #86 from lehn-etracker/feature/add-drule-api
Browse files Browse the repository at this point in the history
Feature: add support for Discovery Rules (`drule` API)
  • Loading branch information
gdubicki authored Dec 25, 2018
2 parents e528b07 + 437dc53 commit e1f6c62
Show file tree
Hide file tree
Showing 4 changed files with 133 additions and 0 deletions.
22 changes: 22 additions & 0 deletions examples/DiscoveryRule.md
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',
}],
)
```
6 changes: 6 additions & 0 deletions lib/zabbixapi.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
require 'zabbixapi/classes/usermacros'
require 'zabbixapi/classes/users'
require 'zabbixapi/classes/valuemaps'
require 'zabbixapi/classes/drules'

class ZabbixApi
# @return [ZabbixApi::Client]
Expand Down Expand Up @@ -167,4 +168,9 @@ def users
def valuemaps
@valuemaps ||= ValueMaps.new(@client)
end

# @return [ZabbixApi::Drules]
def drules
@drules ||= Drules.new(@client)
end
end
55 changes: 55 additions & 0 deletions lib/zabbixapi/classes/drules.rb
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
50 changes: 50 additions & 0 deletions spec/drule.rb
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

0 comments on commit e1f6c62

Please sign in to comment.