class Instana::Backend::HostAgentLookup

Utility class to discover the agent that a given instance of the collector needs to communicate with. @since 1.197.0

Public Class Methods

new(host = ::Instana.config[:agent_host], port = ::Instana.config[:agent_port], destination: '00000000') click to toggle source
# File lib/instana/backend/host_agent_lookup.rb, line 12
def initialize(host = ::Instana.config[:agent_host], port = ::Instana.config[:agent_port], destination: '00000000')
  @host = host
  @port = port
  @destination = destination
end

Public Instance Methods

call() click to toggle source

@return [RequestClient, NilClass] the request client to use to communicate with the agent or nil if no agent could be found

# File lib/instana/backend/host_agent_lookup.rb, line 19
def call
  host_listening?(@host, @port) || host_listening?(default_gateway, @port)
end

Private Instance Methods

default_gateway() click to toggle source

@return [String] the default gateway to attempt to connect to or the @host if a default gateway can not be identified

# File lib/instana/backend/host_agent_lookup.rb, line 34
def default_gateway
  return @host unless File.exist?('/proc/self/net/route')

  routes = CSV.read(
    '/proc/self/net/route',
    headers: :first_row,
    col_sep: "\t",
    header_converters: [->(v) { v.strip }],
    converters: [->(v) { v.strip }]
  )

  route = routes.detect { |r| r['Destination'] == @destination }
  return @host unless route

  route['Gateway']
    .split(/([0-9A-Z]{2})/)
    .reject(&:empty?)
    .reverse
    .map { |s| s.to_i(16) }
    .join('.')
end
host_listening?(host, port) click to toggle source

@return [RequestClient, nil] the request client if it responds to '/' with a success

# File lib/instana/backend/host_agent_lookup.rb, line 26
def host_listening?(host, port)
  client = RequestClient.new(host, port)
  client.send_request('GET', '/').ok? ? client : nil
rescue Net::OpenTimeout, Errno::ECONNREFUSED => _e
  nil
end