Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for defining host's macros #671

Merged
merged 6 commits into from
Apr 14, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions lib/puppet/provider/zabbix_host/ruby.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ def self.instances
selectParentTemplates: ['host'],
selectInterfaces: %w[interfaceid type main ip port useip],
selectGroups: ['name'],
selectMacros: %w[macro value],
output: %w[host proxy_hostid]
}
)
Expand All @@ -28,6 +29,7 @@ def self.instances
groups: h['groups'].map { |g| g['name'] },
group_create: nil,
templates: h['parentTemplates'].map { |x| x['host'] },
macros: h['macros'].map { |macro| { macro['macro'] => macro['value'] } },
proxy: proxies.select { |_name, id| id == h['proxy_hostid'] }.keys.first
)
end
Expand Down Expand Up @@ -174,6 +176,17 @@ def templates=(array)
)
end

def macros=(array)
macroarray = array.map { |macro| { 'macro' => macro.first[0], 'value' => macro.first[1] } }
zbx.query(
method: 'host.update',
params: {
hostid: @property_hash[:id],
macros: macroarray
}
)
end

def proxy=(string)
zbx.hosts.create_or_update(
host: @resource[:hostname],
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 @@ -97,6 +97,13 @@ def insync?(is)
end
end

newproperty(:macros, array_matching: :all) do
desc 'Array of hashes (macros) which should be loaded for this host.'
def insync?(is)
is.sort_by(&:first) == should.sort_by(&:first)
end
end

newproperty(:proxy) do
desc 'Whether it is monitored by an proxy or not.'
end
Expand Down
5 changes: 5 additions & 0 deletions manifests/agent.pp
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,9 @@
# [*zbx_templates*]
# List of templates which will be added when host is configured.
#
# [*zbx_macros*]
# List of macros which will be added when host is configured.
#
# [*agent_configfile_path*]
# Agent config file path defaults to /etc/zabbix/zabbix_agentd.conf
#
Expand Down Expand Up @@ -249,6 +252,7 @@
Variant[String[1],Array[String[1]]] $zbx_groups = $zabbix::params::agent_zbx_groups,
$zbx_group_create = $zabbix::params::agent_zbx_group_create,
$zbx_templates = $zabbix::params::agent_zbx_templates,
Array[Hash] $zbx_macros = [],
$agent_configfile_path = $zabbix::params::agent_configfile_path,
$pidfile = $zabbix::params::agent_pidfile,
$servicename = $zabbix::params::agent_servicename,
Expand Down Expand Up @@ -378,6 +382,7 @@
groups => [$groups].flatten(),
group_create => $zbx_group_create,
templates => $zbx_templates,
macros => $zbx_macros,
proxy => $use_proxy,
}
}
Expand Down
2 changes: 2 additions & 0 deletions manifests/resources/agent.pp
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
Array[String[1]] $groups = undef,
$group_create = undef,
$templates = undef,
$macros = undef,
$proxy = undef,
) {
if $group and $groups {
Expand All @@ -42,6 +43,7 @@
groups => $groups,
group_create => $group_create,
templates => $templates,
macros => $macros,
proxy => $proxy,
}
}
2 changes: 2 additions & 0 deletions spec/acceptance/zabbix_host_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -48,13 +48,15 @@ class { 'zabbix':
groups => ['TestgroupOne'],
group_create => true,
templates => [ 'Template OS Linux', ],
macros => [],
}
zabbix_host { 'test2.example.com':
ipaddress => '127.0.0.2',
use_ip => false,
port => 1050,
groups => ['Virtual machines'],
templates => [ 'Template OS Linux', 'Template ICMP Ping', ],
macros => [],
}
EOS

Expand Down
1 change: 1 addition & 0 deletions spec/types/zabbix_host_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
it { is_expected.to be_valid_type.with_properties('port') }
it { is_expected.to be_valid_type.with_properties('proxy') }
it { is_expected.to be_valid_type.with_properties('templates') }
it { is_expected.to be_valid_type.with_properties('macros') }
it { is_expected.to be_valid_type.with_properties('use_ip') }

it { is_expected.to be_valid_type.with_parameters('hostname') }
Expand Down
2 changes: 1 addition & 1 deletion spec/unit/puppet/provider/zabbix_host/ruby.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
end
end

[:create, :exists?, :destroy, :get_groupids, :get_templateids, :ipaddress, :use_ip, :port, :groups, :templates, :proxy].each do |method|
[:create, :exists?, :destroy, :get_groupids, :get_templateids, :ipaddress, :use_ip, :port, :groups, :templates, :macros, :proxy].each do |method|
it "should respond to the instance method #{method}" do
expect(described_class.new).to respond_to(method)
end
Expand Down
12 changes: 12 additions & 0 deletions spec/unit/puppet/type/zabbix_host_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
:port,
:proxy,
:templates,
:macros,
:use_ip
].each do |param|
it "should have a #{param} property" do
Expand Down Expand Up @@ -123,6 +124,17 @@
end
end

describe 'macros' do
it_behaves_like 'validated property', :macros, nil, [{ 'macro1' => 'value1' }, { 'macro2' => 'value2' }]
it_behaves_like 'array_matching property', :macros

let(:object) { described_class.new(name: 'nobody', macros: [{ 'macro1' => 'value1' }, { 'macro2' => 'value2' }]) }

it 'ignores order of array' do
expect(object.property(:macros).insync?([{ 'macro1' => 'value1' }, { 'macro2' => 'value2' }])).to be true
end
end

describe 'use_ip' do
it_behaves_like 'boolean property', :use_ip, nil
end
Expand Down