Add new module elasticsearch for listsearch
[mirror/dsa-puppet.git] / 3rdparty / modules / elasticsearch / lib / facter / es_facts.rb
1 require 'net/http'
2 require 'json'
3 require 'yaml'
4
5 module EsFacts
6
7   def self.add_fact(prefix, key, value)
8     key = "#{prefix}_#{key}".to_sym
9     ::Facter.add(key) do
10       setcode { value }
11     end
12   end
13
14   def self.run
15
16     dir_prefix = '/etc/elasticsearch'
17     ports = []
18
19     # only when the directory exists we need to process the stuff
20     if File.directory?(dir_prefix)
21
22       Dir.foreach(dir_prefix) { |dir| 
23         next if dir == '.'
24         if File.exists?("#{dir_prefix}/#{dir}/elasticsearch.yml")
25           config_data = YAML.load_file("#{dir_prefix}/#{dir}/elasticsearch.yml")
26           unless config_data['http'].nil?
27             next if config_data['http']['enabled'] == 'false'
28             if config_data['http']['port'].nil?
29               port = "9200"
30             else
31               port = config_data['http']['port']
32             end
33           else
34             port = "9200"
35           end
36           ports << port
37         end
38       }
39
40       begin
41         if ports.count > 0
42
43           add_fact('elasticsearch', 'ports', ports.join(",") )
44           ports.each do |port|
45
46             key_prefix = "elasticsearch_#{port}"
47
48             uri = URI("http://localhost:#{port}")
49             json_data = JSON.parse(Net::HTTP.get(uri))
50             next if json_data['status'] && json_data['status'] != 200
51
52             add_fact(key_prefix, 'name', json_data['name'])
53             add_fact(key_prefix, 'version', json_data['version']['number'])
54
55             uri2 = URI("http://localhost:#{port}/_nodes/#{json_data['name']}")
56             json_data_node = JSON.parse(Net::HTTP.get(uri2))
57
58             add_fact(key_prefix, 'cluster_name', json_data_node['cluster_name'])
59             node_data = json_data_node['nodes'].first
60
61             add_fact(key_prefix, 'node_id', node_data[0])
62
63             nodes_data = json_data_node['nodes'][node_data[0]]
64
65             process = nodes_data['process']
66             add_fact(key_prefix, 'mlockall', process['mlockall'])
67
68             plugins = nodes_data['plugins']
69
70             plugin_names = []
71             plugins.each do |plugin|
72               plugin_names << plugin['name']
73
74              plugin.each do |key, value|
75                 prefix = "#{key_prefix}_plugin_#{plugin['name']}"
76                 add_fact(prefix, key, value) unless key == 'name'
77               end
78             end
79             add_fact(key_prefix, 'plugins', plugin_names.join(","))
80
81           end
82
83         end
84       rescue
85       end
86
87     end
88
89   end
90
91 end
92
93 EsFacts.run