Skip to content

Commit

Permalink
Improve performance of zabbix_host
Browse files Browse the repository at this point in the history
Before this commit, every template added to a host genarated addtional 5
API calls. This isn't needed as we can get all required information by
only one API call.

Fixes #558.
  • Loading branch information
baurmatt committed Nov 2, 2018
1 parent f6ade70 commit 555b6be
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 6 deletions.
11 changes: 10 additions & 1 deletion lib/puppet/provider/zabbix.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,16 @@ def self.create_connection(zabbix_url, zabbix_user, zabbix_pass, apache_use_ssl)
# Check if host exists. When error raised, return false.
def self.check_host(host, zabbix_url, zabbix_user, zabbix_pass, apache_use_ssl)
zbx = create_connection(zabbix_url, zabbix_user, zabbix_pass, apache_use_ssl)
zbx.hosts.get_id(host: host)
zbx.query(
method: 'host.get',
params: {
filter: {
'host' => [host]
},
selectParentTemplates: ['host'],
output: ['host']
}
)
rescue Puppet::ExecutionFailure
false
end
Expand Down
20 changes: 15 additions & 5 deletions lib/puppet/provider/zabbix_host/ruby.rb
Original file line number Diff line number Diff line change
Expand Up @@ -89,12 +89,22 @@ def exists?

templates = [templates] unless templates.is_a?(Array)

res = []
res.push(self.class.check_host(host, zabbix_url, zabbix_user, zabbix_pass, apache_use_ssl))
templates.each do |template|
res.push(self.class.check_template_in_host(host, template, zabbix_url, zabbix_user, zabbix_pass, apache_use_ssl))
host = self.class.check_host(host, zabbix_url, zabbix_user, zabbix_pass, apache_use_ssl)
if host.any?
template_ids = host[0]['parentTemplates'].map { |x| x['templateid'] }
template_names = host[0]['parentTemplates'].map { |x| x['host'] }
res = []
templates.each do |template|
if self.class.a_number?(template)
res.push(template_ids.include?(template))
else
res.push(template_names.include?(template))
end
end
res.all?
else
false
end
res.all?
end

def destroy
Expand Down

0 comments on commit 555b6be

Please sign in to comment.