33require 'yaml'
44
55# Helper module to encapsulate custom fact injection
6- # rubocop:disable Metrics/ModuleLength
76module EsFacts
87 # Add a fact to the catalog of host facts
98 def self . add_fact ( prefix , key , value )
@@ -15,9 +14,7 @@ def self.add_fact(prefix, key, value)
1514
1615 def self . ssl? ( config )
1716 tls_keys = [
18- 'xpack.security.http.ssl.enabled' ,
19- 'shield.http.ssl' ,
20- 'searchguard.ssl.http.enabled'
17+ 'xpack.security.http.ssl.enabled'
2118 ]
2219
2320 tls_keys . any? { |key | ( config . key? key ) && ( config [ key ] == true ) }
@@ -28,13 +25,9 @@ def self.get_httpport(config)
2825 enabled = 'http.enabled'
2926 httpport = 'http.port'
3027
31- if !config [ enabled ] . nil? && config [ enabled ] == 'false'
32- false
33- elsif !config [ httpport ] . nil?
34- { config [ httpport ] => ssl? ( config ) }
35- else
36- { '9200' => ssl? ( config ) }
37- end
28+ return false , false if !config [ enabled ] . nil? && config [ enabled ] == 'false'
29+ return config [ httpport ] , ssl? ( config ) unless config [ httpport ] . nil?
30+ [ '9200' , ssl? ( config ) ]
3831 end
3932
4033 # Entrypoint for custom fact populator
@@ -46,7 +39,6 @@ def self.get_httpport(config)
4639 def self . run
4740 dir_prefix = '/etc/elasticsearch'
4841 # httpports is a hash of port_number => ssl?
49- httpports = { }
5042 transportports = [ ]
5143 http_bound_addresses = [ ]
5244 transport_bound_addresses = [ ]
@@ -56,34 +48,26 @@ def self.run
5648 # only when the directory exists we need to process the stuff
5749 return unless File . directory? ( dir_prefix )
5850
59- Dir . foreach ( dir_prefix ) do |dir |
60- next if dir == '.'
61-
62- if File . readable? ( "#{ dir_prefix } /#{ dir } /elasticsearch.yml" )
63- config_data = YAML . load_file ( "#{ dir_prefix } /#{ dir } /elasticsearch.yml" )
64- httpport = get_httpport ( config_data )
65- httpports . merge! httpport if httpport
66- end
51+ if File . readable? ( "#{ dir_prefix } /elasticsearch.yml" )
52+ config_data = YAML . load_file ( "#{ dir_prefix } /elasticsearch.yml" )
53+ httpport , ssl = get_httpport ( config_data )
6754 end
6855
6956 begin
70- if httpports . keys . count > 0
71-
72- add_fact ( 'elasticsearch' , 'ports' , httpports . keys . join ( ',' ) )
73-
74- httpports . each_pair do |httpport , ssl |
75- next if ssl
57+ add_fact ( 'elasticsearch' , 'port' , httpport )
7658
77- key_prefix = "elasticsearch_#{ httpport } "
59+ unless ssl
60+ key_prefix = 'elasticsearch'
61+ # key_prefix = "elasticsearch_#{httpport}"
7862
79- uri = URI ( "http://localhost:#{ httpport } " )
80- http = Net ::HTTP . new ( uri . host , uri . port )
81- http . read_timeout = 10
82- http . open_timeout = 2
83- response = http . get ( '/' )
84- json_data = JSON . parse ( response . body )
85- next if json_data [ 'status' ] && json_data [ 'status' ] != 200
63+ uri = URI ( "http://localhost:#{ httpport } " )
64+ http = Net ::HTTP . new ( uri . host , uri . port )
65+ http . read_timeout = 10
66+ http . open_timeout = 2
67+ response = http . get ( '/' )
68+ json_data = JSON . parse ( response . body )
8669
70+ if json_data [ 'status' ] && json_data [ 'status' ] == 200
8771 add_fact ( key_prefix , 'name' , json_data [ 'name' ] )
8872 add_fact ( key_prefix , 'version' , json_data [ 'version' ] [ 'number' ] )
8973
@@ -122,12 +106,18 @@ def self.run
122106 transport_publish_addresses << nodes_data [ 'transport' ] [ 'publish_address' ] unless nodes_data [ 'transport' ] [ 'publish_address' ] . nil?
123107 transportports << nodes_data [ 'settings' ] [ 'transport' ] [ 'tcp' ] [ 'port' ] unless nodes_data [ 'settings' ] [ 'transport' ] [ 'tcp' ] . nil? or nodes_data [ 'settings' ] [ 'transport' ] [ 'tcp' ] [ 'port' ] . nil?
124108
125- node = { 'http_ports' => httpports . keys ,
126- 'transport_ports' => transportports ,
127- 'http_bound_addresses' => http_bound_addresses ,
128- 'transport_bound_addresses' => transport_bound_addresses ,
129- 'transport_publish_addresses' => transport_publish_addresses ,
130- json_data [ 'name' ] => { 'settings' => nodes_data [ 'settings' ] , 'http' => nodes_data [ 'http' ] , 'transport' => nodes_data [ 'transport' ] } }
109+ node = {
110+ 'http_ports' => httpports . keys ,
111+ 'transport_ports' => transportports ,
112+ 'http_bound_addresses' => http_bound_addresses ,
113+ 'transport_bound_addresses' => transport_bound_addresses ,
114+ 'transport_publish_addresses' => transport_publish_addresses ,
115+ json_data [ 'name' ] => {
116+ 'settings' => nodes_data [ 'settings' ] ,
117+ 'http' => nodes_data [ 'http' ] ,
118+ 'transport' => nodes_data [ 'transport' ]
119+ }
120+ }
131121 nodes . merge! node
132122 end
133123 end
0 commit comments