Class: PrometheusExporterHcloudSd::DiscoveryConfig

Inherits:
Object
  • Object
show all
Defined in:
lib/prometheus_exporter_hcloud_sd/discovery_config.rb

Constant Summary collapse

PORTS =
{
  "9104" => :mysql,
  "9121" => :redis,
  "9114" => :elastic_search,
  "9187" => :postgres,
  "9127" => :pgbouncer
}.freeze

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ DiscoveryConfig

Returns a new instance of DiscoveryConfig.



19
20
21
22
23
24
# File 'lib/prometheus_exporter_hcloud_sd/discovery_config.rb', line 19

def initialize(options)
  @additional_ports = options[:exporter].each_with_object({}) do |exporter, acc|
    service, port = exporter.split("=")
    acc[port] = service.to_sym
  end
end

Class Method Details

.generate(options) ⇒ Object



15
16
17
# File 'lib/prometheus_exporter_hcloud_sd/discovery_config.rb', line 15

def self.generate(options)
  new(options).generate
end

Instance Method Details

#discover_exporters(server_ips:, port:) ⇒ Object



40
41
42
43
44
45
46
# File 'lib/prometheus_exporter_hcloud_sd/discovery_config.rb', line 40

def discover_exporters(server_ips:, port:)
  server_ips.collect do |ip|
    command = "timeout 1 nc -zv #{ip} #{port}"
    _, status = Open3.capture2e(command)
    status.success? ? "#{ip}:#{port}" : nil
  end.compact
end

#generateObject



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

def generate
  server_ips = PrometheusExporterHcloudSd::HetznerServers.private_ips
  output = ""

  PORTS.merge(@additional_ports).each do |port, service|
    available_servers = discover_exporters(server_ips: server_ips, port: port)

    next if available_servers.none?

    output << target_configuration(service: service, available_servers: available_servers)
  end
  output
end

#target_configuration(service:, available_servers:) ⇒ Object



48
49
50
51
52
53
54
# File 'lib/prometheus_exporter_hcloud_sd/discovery_config.rb', line 48

def target_configuration(service:, available_servers:)
  <<~LINE
    - targets: #{available_servers.to_json}
      labels:
        job: "#{service}"
  LINE
end