11# zabbix provider type for puppet
22class Puppet ::Provider ::Zabbix < Puppet ::Provider
3+ # This method is vendored from the AWS SDK, rather than including an
4+ # extra library just to parse an ini file
5+ # Copied from https://github.com/puppetlabs/puppetlabs-aws/blob/2d34b1602bdd564b3f882f683dc000878f539343/lib/puppet_x/puppetlabs/aws.rb#L120
6+ def ini_parse ( file )
7+ current_section = { }
8+ map = { }
9+ file . rewind
10+ file . each_line do |line |
11+ line = line . split ( %r{^|\s ;} ) . first # remove comments
12+ section = line . match ( %r{^\s *\[ ([^\[ \] ]+)\] \s *$} ) unless line . nil?
13+ if section
14+ current_section = section [ 1 ]
15+ elsif current_section
16+ item = line . match ( %r{^\s *(.+?)\s *=\s *(.+?)\s *$} ) unless line . nil?
17+ if item
18+ map [ current_section ] = map [ current_section ] || { }
19+ map [ current_section ] [ item [ 1 ] ] = item [ 2 ]
20+ end
21+ end
22+ end
23+ map
24+ end
25+
26+ def api_config
27+ @api_config ||= ini_parse ( File . new ( '/etc/zabbix/api.conf' ) )
28+ end
29+
30+ def zbx
31+ @zbx ||= create_connection
32+ end
33+
334 # Create the api connection
4- def self . create_connection ( zabbix_url , zabbix_user , zabbix_pass , apache_use_ssl )
5- protocol = apache_use_ssl ? 'https' : 'http'
35+ def create_connection
36+ protocol = api_config [ 'default' ] [ ' apache_use_ssl' ] == 'true' ? 'https' : 'http'
637 zbx = ZabbixApi . connect (
7- url : "#{ protocol } ://#{ zabbix_url } /api_jsonrpc.php" ,
8- user : zabbix_user ,
9- password : zabbix_pass ,
10- http_user : zabbix_user ,
11- http_password : zabbix_pass
38+ url : "#{ protocol } ://#{ api_config [ 'default' ] [ ' zabbix_url' ] } /api_jsonrpc.php" ,
39+ user : api_config [ 'default' ] [ ' zabbix_user' ] ,
40+ password : api_config [ 'default' ] [ ' zabbix_pass' ] ,
41+ http_user : api_config [ 'default' ] [ ' zabbix_user' ] ,
42+ http_password : api_config [ 'default' ] [ ' zabbix_pass' ]
1243 )
1344 zbx
1445 end
1546
1647 # Check if host exists. When error raised, return false.
17- def self . check_host ( host , zabbix_url , zabbix_user , zabbix_pass , apache_use_ssl )
18- zbx = create_connection ( zabbix_url , zabbix_user , zabbix_pass , apache_use_ssl )
48+ def check_host ( host )
1949 zbx . query (
2050 method : 'host.get' ,
2151 params : {
@@ -31,29 +61,27 @@ def self.check_host(host, zabbix_url, zabbix_user, zabbix_pass, apache_use_ssl)
3161 end
3262
3363 # Check if proxy exists. When error raised, return false.
34- def self . check_proxy ( host , zabbix_url , zabbix_user , zabbix_pass , apache_use_ssl )
35- zbx = create_connection ( zabbix_url , zabbix_user , zabbix_pass , apache_use_ssl )
64+ def check_proxy ( host )
3665 zbx . proxies . get_id ( host : host )
3766 rescue Puppet ::ExecutionFailure
3867 false
3968 end
4069
4170 # Get the template id from the name.
42- def self . get_template_id ( zbx , template )
71+ def get_template_id ( zbx , template )
4372 return template if a_number? ( template )
4473 zbx . templates . get_id ( host : template )
4574 end
4675
4776 # Check if given template name exists in current host.
48- def self . check_template_in_host ( host , template , zabbix_url , zabbix_user , zabbix_pass , apache_use_ssl )
49- zbx = create_connection ( zabbix_url , zabbix_user , zabbix_pass , apache_use_ssl )
77+ def check_template_in_host ( host )
5078 template_id = get_template_id ( zbx , template )
5179 template_array = zbx . templates . get_ids_by_host ( hostids : [ zbx . hosts . get_id ( host : host ) ] )
5280 template_array . include? ( template_id . to_s )
5381 end
5482
5583 # Is it a number?
56- def self . a_number? ( s )
84+ def a_number? ( s )
5785 s . to_s . match ( %r{\A [+-]?\d +?(\. \d +)?\Z } ) . nil? ? false : true
5886 end
5987end
0 commit comments