Skip to content

Commit 12eaefa

Browse files
Added details to host network interface for SNMP
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
1 parent 330da65 commit 12eaefa

File tree

5 files changed

+133
-38
lines changed

5 files changed

+133
-38
lines changed

lib/puppet/provider/zabbix_host/ruby.rb

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ def self.instances
88
method: 'host.get',
99
params: {
1010
selectParentTemplates: ['host'],
11-
selectInterfaces: %w[interfaceid type main ip port useip],
11+
selectInterfaces: %w[interfaceid type main ip port useip details],
1212
selectGroups: ['name'],
1313
selectMacros: %w[macro value],
1414
output: %w[host proxy_hostid]
@@ -35,7 +35,8 @@ def self.instances
3535
templates: h['parentTemplates'].map { |x| x['host'] },
3636
macros: h['macros'].map { |macro| { macro['macro'] => macro['value'] } },
3737
proxy: proxy_select,
38-
interfacetype: interface['type'].to_i
38+
interfacetype: interface['type'].to_i,
39+
interfacedetails: interface['details']
3940
)
4041
end
4142
end
@@ -68,7 +69,8 @@ def create
6869
ip: @resource[:ipaddress],
6970
dns: @resource[:hostname],
7071
port: @resource[:port],
71-
useip: @resource[:use_ip] ? 1 : 0
72+
useip: @resource[:use_ip] ? 1 : 0,
73+
details: @resource[:interfacedetails].nil? ? {} : @resource[:interfacedetails]
7274
}
7375
],
7476
templates: templates,
@@ -157,6 +159,16 @@ def interfacetype=(int)
157159
)
158160
end
159161

162+
def interfacedetails=(hash)
163+
zbx.query(
164+
method: 'hostinterface.update',
165+
params: {
166+
interfaceid: @property_hash[:interfaceid],
167+
details: hash
168+
}
169+
)
170+
end
171+
160172
def groups=(hostgroups)
161173
gids = get_groupids(hostgroups, @resource[:group_create])
162174
groups = transform_to_array_hash('groupid', gids)

lib/puppet/type/zabbix_host.rb

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,13 @@ def munge_boolean(value)
5252
desc 'Interface type. 1 for zabbix agent.'
5353
end
5454

55+
newproperty(:interfacedetails) do
56+
desc 'Additional interface details.'
57+
def insync?(is)
58+
is.to_s == should.to_s
59+
end
60+
end
61+
5562
newproperty(:use_ip, boolean: true) do
5663
desc 'Using ipadress instead of dns to connect.'
5764

manifests/agent.pp

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

253255
class { 'zabbix::resources::agent':
254-
hostname => $_hostname,
255-
ipaddress => $listen_ip,
256-
use_ip => $agent_use_ip,
257-
port => $listenport,
258-
groups => [$groups].flatten(),
259-
group_create => $zbx_group_create,
260-
templates => $zbx_templates,
261-
macros => $zbx_macros,
262-
interfacetype => $zbx_interface_type,
263-
proxy => $use_proxy,
256+
hostname => $_hostname,
257+
ipaddress => $listen_ip,
258+
use_ip => $agent_use_ip,
259+
port => $listenport,
260+
groups => [$groups].flatten(),
261+
group_create => $zbx_group_create,
262+
templates => $zbx_templates,
263+
macros => $zbx_macros,
264+
interfacetype => $zbx_interface_type,
265+
interfacedetails => $zbx_interface_details,
266+
proxy => $use_proxy,
264267
}
265268
}
266269

manifests/resources/agent.pp

Lines changed: 24 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -9,19 +9,21 @@
99
# @param templates List of templates which should be attached to this host.
1010
# @param macros Array of hashes (macros) which should be attached to this host.
1111
# @param proxy Whether it is monitored by an proxy or not.
12-
# @param interfacetype Internally used identifier for the host interface
12+
# @param interfacetype Internally used identifier for the host interface.
13+
# @param interfacedetails Hash with interface details for SNMP when interface type is 2.
1314
class zabbix::resources::agent (
14-
$hostname = undef,
15-
$ipaddress = undef,
16-
$use_ip = undef,
17-
$port = undef,
18-
$group = undef,
19-
Array[String[1]] $groups = undef,
20-
$group_create = undef,
21-
$templates = undef,
22-
$macros = undef,
23-
$proxy = undef,
24-
$interfacetype = 1,
15+
$hostname = undef,
16+
$ipaddress = undef,
17+
$use_ip = undef,
18+
$port = undef,
19+
$group = undef,
20+
Array[String[1]] $groups = undef,
21+
$group_create = undef,
22+
$templates = undef,
23+
$macros = undef,
24+
$proxy = undef,
25+
$interfacetype = 1,
26+
Hash[String, Any] $interfacedetails = {},
2527
) {
2628
if $group and $groups {
2729
fail("Got group and groups. This isn't support! Please use groups only.")
@@ -35,14 +37,15 @@
3537
}
3638

3739
@@zabbix_host { $hostname:
38-
ipaddress => $ipaddress,
39-
use_ip => $use_ip,
40-
port => $port,
41-
groups => $_groups,
42-
group_create => $group_create,
43-
templates => $templates,
44-
macros => $macros,
45-
proxy => $proxy,
46-
interfacetype => $interfacetype,
40+
ipaddress => $ipaddress,
41+
use_ip => $use_ip,
42+
port => $port,
43+
groups => $_groups,
44+
group_create => $group_create,
45+
templates => $templates,
46+
macros => $macros,
47+
proxy => $proxy,
48+
interfacetype => $interfacetype,
49+
interfacedetails => $interfacedetails,
4750
}
4851
}

spec/acceptance/zabbix_host_spec.rb

Lines changed: 71 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,15 @@
2020
['Linux by Zabbix agent', 'ICMP Ping']
2121
end
2222

23+
template_snmp = case zabbix_version
24+
when '4.0'
25+
['Template OS Linux SNMPv2']
26+
when '5.0'
27+
['Template OS Linux SNMP']
28+
else
29+
['Linux SNMP']
30+
end
31+
2332
pp1 = <<-EOS
2433
class { 'apache':
2534
mpm_module => 'prefork',
@@ -83,8 +92,31 @@ class { 'zabbix':
8392
apply_manifest(pp2, catch_changes: true)
8493
end
8594

95+
# Zabbix version 4.0 doesn't support interface details hash
96+
if zabbix_version != '4.0'
97+
pp3 = <<-EOS
98+
zabbix_host { 'test3.example.com':
99+
ipaddress => '127.0.0.3',
100+
use_ip => false,
101+
port => 161,
102+
groups => ['Virtual machines'],
103+
templates => #{template_snmp},
104+
macros => [],
105+
interfacetype => 2,
106+
interfacedetails => {"version" => "2", "bulk" => "0", "community" => "public"},
107+
}
108+
EOS
109+
110+
it 'creates host with SNMP interface and details without errors' do
111+
apply_manifest(pp3, catch_failures: true)
112+
end
113+
it 'creates host with SNMP interface and details without changes' do
114+
apply_manifest(pp3, catch_changes: true)
115+
end
116+
end
117+
86118
let(:result_hosts) do
87-
zabbixapi('localhost', 'Admin', 'zabbix', 'host.get', selectParentTemplates: ['host'], selectInterfaces: %w[dns ip main port type useip], selectGroups: ['name'], output: ['host', '']).result
119+
zabbixapi('localhost', 'Admin', 'zabbix', 'host.get', selectParentTemplates: ['host'], selectInterfaces: %w[dns ip main port type useip details], selectGroups: ['name'], output: ['host', '']).result
88120
end
89121

90122
context 'test1.example.com' do
@@ -150,6 +182,44 @@ class { 'zabbix':
150182
expect(test2['parentTemplates'].map { |t| t['host'] }.sort).to eq(template.sort)
151183
end
152184
end
185+
186+
# Zabbix version 4.0 doesn't support interface details hash
187+
if zabbix_version != '4.0'
188+
context 'test3.example.com' do
189+
let(:test3) { result_hosts.select { |h| h['host'] == 'test3.example.com' }.first }
190+
191+
it 'is created' do
192+
expect(test3['host']).to eq('test3.example.com')
193+
end
194+
it 'is in group Virtual machines' do
195+
expect(test3['groups'].map { |g| g['name'] }).to eq(['Virtual machines'])
196+
end
197+
it 'has a correct interface dns configured' do
198+
expect(test3['interfaces'][0]['dns']).to eq('test3.example.com')
199+
end
200+
it 'has a correct interface ip configured' do
201+
expect(test3['interfaces'][0]['ip']).to eq('127.0.0.3')
202+
end
203+
it 'has a correct interface main configured' do
204+
expect(test3['interfaces'][0]['main']).to eq('1')
205+
end
206+
it 'has a correct interface port configured' do
207+
expect(test3['interfaces'][0]['port']).to eq('161')
208+
end
209+
it 'has a correct interface type configured' do
210+
expect(test3['interfaces'][0]['type']).to eq('2')
211+
end
212+
it 'has a correct interface details configured' do
213+
expect(test3['interfaces'][0]['details']).to eq('version' => '2', 'bulk' => '0', 'community' => 'public')
214+
end
215+
it 'has a correct interface useip configured' do
216+
expect(test3['interfaces'][0]['useip']).to eq('0')
217+
end
218+
it 'has templates attached' do
219+
expect(test3['parentTemplates'].map { |t| t['host'] }.sort).to eq(template_snmp.sort)
220+
end
221+
end
222+
end
153223
end
154224
end
155225
end

0 commit comments

Comments
 (0)