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



32
33
34
# File 'lib/puppet/provider/zabbix.rb', line 32

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

.create_connectionObject

Create the api connection



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

def self.create_connection
  protocol = api_config['default']['apache_use_ssl'] == 'true' ? 'https' : 'http'
  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'],
    ignore_version: true
  )
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



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

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



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

def self.zbx
  @zbx ||= create_connection
end

Instance Method Details

#a_number?(value) ⇒ Boolean

Is it a number?

Returns:

  • (Boolean)


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

def a_number?(value)
  !value.to_s.match(%r{\A[+-]?\d+?(\.\d+)?\Z}).nil?
end

#api_configObject



36
37
38
# File 'lib/puppet/provider/zabbix.rb', line 36

def api_config
  self.class.api_config
end

#check_host(host) ⇒ Object

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



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

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

#check_template_in_host(host, template) ⇒ Object

Check if given template name exists in current host.



89
90
91
92
93
# File 'lib/puppet/provider/zabbix.rb', line 89

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



61
62
63
# File 'lib/puppet/provider/zabbix.rb', line 61

def create_connection
  self.class.create_connection
end

#get_template_id(zbx, template) ⇒ Object

Get the template id from the name.



82
83
84
85
86
# File 'lib/puppet/provider/zabbix.rb', line 82

def get_template_id(zbx, template)
  return template if a_number?(template)

  zbx.templates.get_id(host: template)
end

#ini_parse(file) ⇒ Object



28
29
30
# File 'lib/puppet/provider/zabbix.rb', line 28

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

#transform_to_array_hash(key, value_array) ⇒ Object



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

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

#zbxObject



44
45
46
# File 'lib/puppet/provider/zabbix.rb', line 44

def zbx
  self.class.zbx
end