Module: EsFacts

Defined in:
lib/facter/es_facts.rb

Overview

Helper module to encapsulate custom fact injection

Class Method Summary collapse

Class Method Details

.add_fact(prefix, key, value) ⇒ Object

Add a fact to the catalog of host facts



9
10
11
12
13
14
# File 'lib/facter/es_facts.rb', line 9

def self.add_fact(prefix, key, value)
  key = "#{prefix}_#{key}".to_sym
  ::Facter.add(key) do
    setcode { value }
  end
end

.get_port(config) ⇒ Object

Helper to determine the instance port number



27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/facter/es_facts.rb', line 27

def self.get_port(config)
  enabled = 'http.enabled'
  port = 'http.port'

  if not config[enabled].nil? and config[enabled] == 'false'
    false
  elsif not config[port].nil?
    { config[port] => ssl?(config) }
  else
    { '9200' => ssl?(config) }
  end
end

.runObject

Entrypoint for custom fact populator



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
# File 'lib/facter/es_facts.rb', line 41

def self.run
  dir_prefix = '/etc/elasticsearch'
  # Ports is a hash of port_number => ssl?
  ports = {}

  # only when the directory exists we need to process the stuff
  return unless File.directory?(dir_prefix)

  Dir.foreach(dir_prefix) do |dir|
    next if dir == '.'

    if File.readable?("#{dir_prefix}/#{dir}/elasticsearch.yml")
      config_data = YAML.load_file("#{dir_prefix}/#{dir}/elasticsearch.yml")
      port = get_port(config_data)
      next unless port
      ports.merge! port
    end
  end

  begin
    if ports.keys.count > 0

      add_fact('elasticsearch', 'ports', ports.keys.join(','))
      ports.each_pair do |port, ssl|
        next if ssl

        key_prefix = "elasticsearch_#{port}"

        uri = URI("http://localhost:#{port}")
        http = Net::HTTP.new(uri.host, uri.port)
        http.read_timeout = 10
        http.open_timeout = 2
        response = http.get('/')
        json_data = JSON.parse(response.body)
        next if json_data['status'] && json_data['status'] != 200

        add_fact(key_prefix, 'name', json_data['name'])
        add_fact(key_prefix, 'version', json_data['version']['number'])

        uri2 = URI("http://localhost:#{port}/_nodes/#{json_data['name']}")
        http2 = Net::HTTP.new(uri2.host, uri2.port)
        http2.read_timeout = 10
        http2.open_timeout = 2
        response2 = http2.get(uri2.path)
        json_data_node = JSON.parse(response2.body)

        add_fact(key_prefix, 'cluster_name', json_data_node['cluster_name'])
        node_data = json_data_node['nodes'].first

        add_fact(key_prefix, 'node_id', node_data[0])

        nodes_data = json_data_node['nodes'][node_data[0]]

        process = nodes_data['process']
        add_fact(key_prefix, 'mlockall', process['mlockall'])

        plugins = nodes_data['plugins']

        plugin_names = []
        plugins.each do |plugin|
          plugin_names << plugin['name']

          plugin.each do |key, value|
            prefix = "#{key_prefix}_plugin_#{plugin['name']}"
            add_fact(prefix, key, value) unless key == 'name'
          end
        end
        add_fact(key_prefix, 'plugins', plugin_names.join(','))
      end
    end
  rescue
  end
end

.ssl?(config) ⇒ Boolean

Returns:

  • (Boolean)


16
17
18
19
20
21
22
23
24
# File 'lib/facter/es_facts.rb', line 16

def self.ssl?(config)
  tls_keys = [
    'xpack.security.http.ssl.enabled',
    'shield.http.ssl',
    'searchguard.ssl.http.enabled'
  ]

  tls_keys.any? { |key| config.key? key and config[key] == true }
end