-
Notifications
You must be signed in to change notification settings - Fork 0
/
exporter.rb
87 lines (74 loc) · 3.04 KB
/
exporter.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
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
# REQUIREMENTS
require 'prometheus_exporter'
require 'prometheus_exporter/server'
# client allows instrumentation to send info to server
require 'prometheus_exporter/client'
require 'prometheus_exporter/instrumentation'
require 'net/http'
require 'openssl'
require 'json'
require 'yaml'
#METHODS
def underscore name
name.gsub(/::/, '/').gsub(/([A-Z]+)([A-Z][a-z])/,'\1_\2').gsub(/([a-z\d])([A-Z])/,'\1_\2').tr("-", "_").downcase
end
#VARIABLES
configuration = YAML.load_file('conf/exporter.yml')
bind = configuration['exporter']['bind'] || '0.0.0.0'
port = configuration['exporter']['port'] || 7629
interval = configuration['exporter']['interval'] || 60
verbose = configuration['exporter']['verbose'] || false
components = configuration['components'] || []
default_metrics = YAML.load_file('metrics/metrics.yml')
values = {}
# bind is the address, on which the webserver will listen
# port is the port that will provide the /metrics route
server = PrometheusExporter::Server::WebServer.new bind: bind , port: port , verbose: verbose
server.start
#Instance a client and metrics to collect
client = PrometheusExporter::LocalClient.new(collector: server.collector)
# Filter and keep metrics to fill
metrics = default_metrics.select {|k, v| components.keys.include? k}
components.each do |component|
if component.last['include']
metrics[component.first].select!{|k, v| component.last['include'].include? k}
end
end
#Instanciate metrics
metrics.each do |component|
component_name = component.first
component.last.each do |metric|
metric_name = metric.first
formatted_key = underscore(metric_name)
values["#{component_name}_#{formatted_key}"] = client.register( metrics[component_name][metric_name]['metricType'].to_sym, "#{component_name}_#{formatted_key}", metrics[component_name][metric_name]['desc'])
end
end
#Set metrics
while true
components.each do |component|
name = component.first
uri = URI(component.last['url'])
Net::HTTP.start(uri.host, uri.port,
:use_ssl => uri.scheme == 'https',
:verify_mode => OpenSSL::SSL::VERIFY_NONE) do |http|
request = Net::HTTP::Get.new uri.request_uri
if component.last['basic_auth']
request.basic_auth component.last['basic_auth']['username'], component.last['basic_auth']['password']
end
response = http.request request # Net::HTTPResponse object
result = JSON.parse(response.body)
if name == 'yarn'
result['clusterMetrics'].select{|k, v| metrics[name].keys.include? k}.each do |metric|
formatted_key = underscore( metric.first )
values["#{name}_#{formatted_key}"].observe( metric.last )
end
else
result["beans"].map{ |obj| obj.select{ |k,v| metrics[name].keys.include? k}}.reject(&:empty?).reduce( Hash.new, :merge ).each do |metric|
formatted_key = underscore( metric.first )
values["#{name}_#{formatted_key}"].observe( metric.last )
end
end
end
end
sleep interval
end