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

Instance Method Summary collapse

Class Method Details

.api_configObject



30
31
32
# File 'lib/puppet/provider/zabbix.rb', line 30

def self.api_config
  @api_config ||= ini_parse(File.new('/etc/zabbix/api.conf'))
end

.create_connectionObject

Create the api connection



47
48
49
50
51
52
53
54
55
56
57
# File 'lib/puppet/provider/zabbix.rb', line 47

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

.ini_parse(file) ⇒ Object

This method is vendored from the AWS SDK, rather than including an extra library just to parse an ini file Copied from github.com/puppetlabs/puppetlabs-aws/blob/2d34b1602bdd564b3f882f683dc000878f539343/lib/puppet_x/puppetlabs/aws.rb#L120



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/puppet/provider/zabbix.rb', line 6

def self.ini_parse(file)
  current_section = {}
  map = {}
  file.rewind
  file.each_line do |line|
    line = line.split(%r{^|\s;}).first # remove comments
    section = line.match(%r{^\s*\[([^\[\]]+)\]\s*$}) unless line.nil?
    if section
      current_section = section[1]
    elsif current_section
      item = line.match(%r{^\s*(.+?)\s*=\s*(.+?)\s*$}) unless line.nil?
      if item
        map[current_section] = map[current_section] || {}
        map[current_section][item[1]] = item[2]
      end
    end
  end
  map
end

.zbxObject



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

def self.zbx
  @zbx ||= create_connection
end

Instance Method Details

#a_number?(s) ⇒ Boolean

Is it a number?

Returns:

  • (Boolean)


104
105
106
# File 'lib/puppet/provider/zabbix.rb', line 104

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

#api_configObject



34
35
36
# File 'lib/puppet/provider/zabbix.rb', line 34

def api_config
  self.class.api_config
end

#check_host(host) ⇒ Object

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



64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/puppet/provider/zabbix.rb', line 64

def check_host(host)
  zbx.query(
    method: 'host.get',
    params: {
      filter: {
        'host' => [host]
      },
      selectParentTemplates: ['host'],
      output: ['host']
    }
  )
rescue Puppet::ExecutionFailure
  false
end

#check_proxy(host) ⇒ Object

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



80
81
82
83
84
# File 'lib/puppet/provider/zabbix.rb', line 80

def check_proxy(host)
  zbx.proxies.get_id(host: host)
rescue Puppet::ExecutionFailure
  false
end

#check_template_in_host(host, template) ⇒ Object

Check if given template name exists in current host.



93
94
95
96
97
# File 'lib/puppet/provider/zabbix.rb', line 93

def check_template_in_host(host, template)
  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_connectionObject



59
60
61
# File 'lib/puppet/provider/zabbix.rb', line 59

def create_connection
  self.class.create_connection
end

#get_template_id(zbx, template) ⇒ Object

Get the template id from the name.



87
88
89
90
# File 'lib/puppet/provider/zabbix.rb', line 87

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

#ini_parse(file) ⇒ Object



26
27
28
# File 'lib/puppet/provider/zabbix.rb', line 26

def ini_parse(file)
  self.class.ini_parse(file)
end

#transform_to_array_hash(key, value_array) ⇒ Object



99
100
101
# File 'lib/puppet/provider/zabbix.rb', line 99

def transform_to_array_hash(key, value_array)
  value_array.map { |a| { key => a } }
end

#zbxObject



42
43
44
# File 'lib/puppet/provider/zabbix.rb', line 42

def zbx
  self.class.zbx
end