Class: Puppet::Provider::Zabbix

Inherits:
Puppet::Provider
  • Object
show all
Defined in:
lib/puppet/provider/zabbix.rb

Overview

zabbix provider type for puppet

Class Method Summary collapse

Class Method Details

.a_number?(s) ⇒ Boolean

Is it a number?

Returns:

  • (Boolean)


53
54
55
# File 'lib/puppet/provider/zabbix.rb', line 53

def self.a_number?(s)
  s.to_s.match(%r{\A[+-]?\d+?(\.\d+)?\Z}).nil? ? false : true
end

.check_host(host, zabbix_url, zabbix_user, zabbix_pass, apache_use_ssl) ⇒ Object

Check if host exists. When error raised, return false.



22
23
24
25
26
27
# File 'lib/puppet/provider/zabbix.rb', line 22

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)
rescue Puppet::ExecutionFailure
  false
end

.check_proxy(host, zabbix_url, zabbix_user, zabbix_pass, apache_use_ssl) ⇒ Object

Check if proxy exists. When error raised, return false.



30
31
32
33
34
35
36
# File 'lib/puppet/provider/zabbix.rb', line 30

def self.check_proxy(host, zabbix_url, zabbix_user, zabbix_pass, apache_use_ssl)
  require_zabbix
  zbx = create_connection(zabbix_url, zabbix_user, zabbix_pass, apache_use_ssl)
  zbx.proxies.get_id(host: host)
rescue Puppet::ExecutionFailure
  false
end

.check_template_in_host(host, template, zabbix_url, zabbix_user, zabbix_pass, apache_use_ssl) ⇒ Object

Check if given template name exists in current host.



45
46
47
48
49
50
# File 'lib/puppet/provider/zabbix.rb', line 45

def self.check_template_in_host(host, template, zabbix_url, zabbix_user, zabbix_pass, apache_use_ssl)
  zbx = create_connection(zabbix_url, zabbix_user, zabbix_pass, apache_use_ssl)
  template_id = get_template_id(zbx, template)
  template_array = zbx.templates.get_ids_by_host(hostids: [zbx.hosts.get_id(host: host)])
  template_array.include?(template_id.to_s)
end

.create_connection(zabbix_url, zabbix_user, zabbix_pass, apache_use_ssl) ⇒ Object

Create the api connection



9
10
11
12
13
14
15
16
17
18
19
# File 'lib/puppet/provider/zabbix.rb', line 9

def self.create_connection(zabbix_url, zabbix_user, zabbix_pass, apache_use_ssl)
  protocol = apache_use_ssl ? 'https' : 'http'
  zbx = ZabbixApi.connect(
    url: "#{protocol}://#{zabbix_url}/api_jsonrpc.php",
    user: zabbix_user,
    password: zabbix_pass,
    http_user: zabbix_user,
    http_password: zabbix_pass
  )
  zbx
end

.get_template_id(zbx, template) ⇒ Object

Get the template id from the name.



39
40
41
42
# File 'lib/puppet/provider/zabbix.rb', line 39

def self.get_template_id(zbx, template)
  return template if a_number?(template)
  zbx.templates.get_id(host: template)
end

.require_zabbixObject

Require the zabbixapi gem



4
5
6
# File 'lib/puppet/provider/zabbix.rb', line 4

def self.require_zabbix
  require 'zabbixapi'
end