Skip to content

Commit

Permalink
Added details to host network interface for SNMP
Browse files Browse the repository at this point in the history
Added details to host network interface for SNMP

Added details to host network interface for SNMP

Added spec acceptance test for interfacetype and interfacedetails

Added context test3.example.com to acceptance test

Fix include in test3 context

Fix hash syntax in test3 context

Change template for snmp in acceptance test

Fix case for var template_snmp

Removed conflicts on templates used for snmp host test acceptance

Trying to fix interface details data types

Sub double quote with single quote in include

Trying to understand if interface details is nil

Fix trailing whitespace :-O!

Zabbix version 4.0 doesn't have the interface details hash, excluded from test

Wip (#1)

* Added details to result_hosts

* Add TODO file

* Run test specific to zabbix >= 4.0

* Add param documentation

* Fix spaces

* Remove TODO file

Fix suggested
  • Loading branch information
oraziobattaglia committed Aug 28, 2021
1 parent 330da65 commit 12eaefa
Show file tree
Hide file tree
Showing 5 changed files with 133 additions and 38 deletions.
18 changes: 15 additions & 3 deletions lib/puppet/provider/zabbix_host/ruby.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ def self.instances
method: 'host.get',
params: {
selectParentTemplates: ['host'],
selectInterfaces: %w[interfaceid type main ip port useip],
selectInterfaces: %w[interfaceid type main ip port useip details],
selectGroups: ['name'],
selectMacros: %w[macro value],
output: %w[host proxy_hostid]
Expand All @@ -35,7 +35,8 @@ def self.instances
templates: h['parentTemplates'].map { |x| x['host'] },
macros: h['macros'].map { |macro| { macro['macro'] => macro['value'] } },
proxy: proxy_select,
interfacetype: interface['type'].to_i
interfacetype: interface['type'].to_i,
interfacedetails: interface['details']
)
end
end
Expand Down Expand Up @@ -68,7 +69,8 @@ def create
ip: @resource[:ipaddress],
dns: @resource[:hostname],
port: @resource[:port],
useip: @resource[:use_ip] ? 1 : 0
useip: @resource[:use_ip] ? 1 : 0,
details: @resource[:interfacedetails].nil? ? {} : @resource[:interfacedetails]
}
],
templates: templates,
Expand Down Expand Up @@ -157,6 +159,16 @@ def interfacetype=(int)
)
end

def interfacedetails=(hash)
zbx.query(
method: 'hostinterface.update',
params: {
interfaceid: @property_hash[:interfaceid],
details: hash
}
)
end

def groups=(hostgroups)
gids = get_groupids(hostgroups, @resource[:group_create])
groups = transform_to_array_hash('groupid', gids)
Expand Down
7 changes: 7 additions & 0 deletions lib/puppet/type/zabbix_host.rb
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,13 @@ def munge_boolean(value)
desc 'Interface type. 1 for zabbix agent.'
end

newproperty(:interfacedetails) do
desc 'Additional interface details.'
def insync?(is)
is.to_s == should.to_s
end
end

newproperty(:use_ip, boolean: true) do
desc 'Using ipadress instead of dns to connect.'

Expand Down
29 changes: 16 additions & 13 deletions manifests/agent.pp
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,14 @@
# @param zbx_group_create Whether to create hostgroup if missing.
# @param zbx_templates List of templates which will be added when host is configured.
# @param zbx_macros List of macros which will be added when host is configured.
# @param zbx_interface_type Integer specifying type of interface to be created
# @param agent_configfile_path Agent config file path defaults to /etc/zabbix/zabbix_agentd.conf
# @param zbx_interface_type Integer specifying type of interface to be created.
# @param zbx_interface_details Hash with interface details for SNMP when interface type is 2.
# @param agent_configfile_path Agent config file path defaults to /etc/zabbix/zabbix_agentd.conf.
# @param pidfile Name of pid file.
# @param servicename Zabbix's agent service name.
# @param logfile Name of log file.
# @param logfilesize Maximum size of log file in MB.
# @param logtype Specifies where log messages are written to. Can be one of: console, file, system
# @param logtype Specifies where log messages are written to. Can be one of: console, file, system.
# @param debuglevel Specifies debug level.
# @param sourceip Source ip address for outgoing connections.
# @param allowkey Allow execution of item keys matching pattern.
Expand Down Expand Up @@ -148,6 +149,7 @@
$zbx_templates = $zabbix::params::agent_zbx_templates,
Array[Hash] $zbx_macros = [],
Integer[1,4] $zbx_interface_type = 1,
Hash[String, Any] $zbx_interface_details = {},
$agent_configfile_path = $zabbix::params::agent_configfile_path,
$pidfile = $zabbix::params::agent_pidfile,
$servicename = $zabbix::params::agent_servicename,
Expand Down Expand Up @@ -251,16 +253,17 @@
$_hostname = pick($hostname, $facts['networking']['fqdn'])

class { 'zabbix::resources::agent':
hostname => $_hostname,
ipaddress => $listen_ip,
use_ip => $agent_use_ip,
port => $listenport,
groups => [$groups].flatten(),
group_create => $zbx_group_create,
templates => $zbx_templates,
macros => $zbx_macros,
interfacetype => $zbx_interface_type,
proxy => $use_proxy,
hostname => $_hostname,
ipaddress => $listen_ip,
use_ip => $agent_use_ip,
port => $listenport,
groups => [$groups].flatten(),
group_create => $zbx_group_create,
templates => $zbx_templates,
macros => $zbx_macros,
interfacetype => $zbx_interface_type,
interfacedetails => $zbx_interface_details,
proxy => $use_proxy,
}
}

Expand Down
45 changes: 24 additions & 21 deletions manifests/resources/agent.pp
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,21 @@
# @param templates List of templates which should be attached to this host.
# @param macros Array of hashes (macros) which should be attached to this host.
# @param proxy Whether it is monitored by an proxy or not.
# @param interfacetype Internally used identifier for the host interface
# @param interfacetype Internally used identifier for the host interface.
# @param interfacedetails Hash with interface details for SNMP when interface type is 2.
class zabbix::resources::agent (
$hostname = undef,
$ipaddress = undef,
$use_ip = undef,
$port = undef,
$group = undef,
Array[String[1]] $groups = undef,
$group_create = undef,
$templates = undef,
$macros = undef,
$proxy = undef,
$interfacetype = 1,
$hostname = undef,
$ipaddress = undef,
$use_ip = undef,
$port = undef,
$group = undef,
Array[String[1]] $groups = undef,
$group_create = undef,
$templates = undef,
$macros = undef,
$proxy = undef,
$interfacetype = 1,
Hash[String, Any] $interfacedetails = {},
) {
if $group and $groups {
fail("Got group and groups. This isn't support! Please use groups only.")
Expand All @@ -35,14 +37,15 @@
}

@@zabbix_host { $hostname:
ipaddress => $ipaddress,
use_ip => $use_ip,
port => $port,
groups => $_groups,
group_create => $group_create,
templates => $templates,
macros => $macros,
proxy => $proxy,
interfacetype => $interfacetype,
ipaddress => $ipaddress,
use_ip => $use_ip,
port => $port,
groups => $_groups,
group_create => $group_create,
templates => $templates,
macros => $macros,
proxy => $proxy,
interfacetype => $interfacetype,
interfacedetails => $interfacedetails,
}
}
72 changes: 71 additions & 1 deletion spec/acceptance/zabbix_host_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,15 @@
['Linux by Zabbix agent', 'ICMP Ping']
end

template_snmp = case zabbix_version
when '4.0'
['Template OS Linux SNMPv2']
when '5.0'
['Template OS Linux SNMP']
else
['Linux SNMP']
end

pp1 = <<-EOS
class { 'apache':
mpm_module => 'prefork',
Expand Down Expand Up @@ -83,8 +92,31 @@ class { 'zabbix':
apply_manifest(pp2, catch_changes: true)
end

# Zabbix version 4.0 doesn't support interface details hash
if zabbix_version != '4.0'
pp3 = <<-EOS
zabbix_host { 'test3.example.com':
ipaddress => '127.0.0.3',
use_ip => false,
port => 161,
groups => ['Virtual machines'],
templates => #{template_snmp},
macros => [],
interfacetype => 2,
interfacedetails => {"version" => "2", "bulk" => "0", "community" => "public"},
}
EOS

it 'creates host with SNMP interface and details without errors' do
apply_manifest(pp3, catch_failures: true)
end
it 'creates host with SNMP interface and details without changes' do
apply_manifest(pp3, catch_changes: true)
end
end

let(:result_hosts) do
zabbixapi('localhost', 'Admin', 'zabbix', 'host.get', selectParentTemplates: ['host'], selectInterfaces: %w[dns ip main port type useip], selectGroups: ['name'], output: ['host', '']).result
zabbixapi('localhost', 'Admin', 'zabbix', 'host.get', selectParentTemplates: ['host'], selectInterfaces: %w[dns ip main port type useip details], selectGroups: ['name'], output: ['host', '']).result
end

context 'test1.example.com' do
Expand Down Expand Up @@ -150,6 +182,44 @@ class { 'zabbix':
expect(test2['parentTemplates'].map { |t| t['host'] }.sort).to eq(template.sort)
end
end

# Zabbix version 4.0 doesn't support interface details hash
if zabbix_version != '4.0'
context 'test3.example.com' do
let(:test3) { result_hosts.select { |h| h['host'] == 'test3.example.com' }.first }

it 'is created' do
expect(test3['host']).to eq('test3.example.com')
end
it 'is in group Virtual machines' do
expect(test3['groups'].map { |g| g['name'] }).to eq(['Virtual machines'])
end
it 'has a correct interface dns configured' do
expect(test3['interfaces'][0]['dns']).to eq('test3.example.com')
end
it 'has a correct interface ip configured' do
expect(test3['interfaces'][0]['ip']).to eq('127.0.0.3')
end
it 'has a correct interface main configured' do
expect(test3['interfaces'][0]['main']).to eq('1')
end
it 'has a correct interface port configured' do
expect(test3['interfaces'][0]['port']).to eq('161')
end
it 'has a correct interface type configured' do
expect(test3['interfaces'][0]['type']).to eq('2')
end
it 'has a correct interface details configured' do
expect(test3['interfaces'][0]['details']).to eq('version' => '2', 'bulk' => '0', 'community' => 'public')
end
it 'has a correct interface useip configured' do
expect(test3['interfaces'][0]['useip']).to eq('0')
end
it 'has templates attached' do
expect(test3['parentTemplates'].map { |t| t['host'] }.sort).to eq(template_snmp.sort)
end
end
end
end
end
end

0 comments on commit 12eaefa

Please sign in to comment.