Module: NewRelic::Control::ServerMethods

Included in:
NewRelic::Control
Defined in:
lib/new_relic/control/server_methods.rb

Overview

Contains methods that deal with connecting to the server

Instance Method Summary collapse

Instance Method Details

#api_serverObject

the server we should contact for api requests, like uploading deployments and the like



24
25
26
# File 'lib/new_relic/control/server_methods.rb', line 24

def api_server
  @api_server ||= NewRelic::Control::Server.new(Agent.config[:api_host], Agent.config[:api_port], nil)
end

#convert_to_ip_address(host) ⇒ Object

Check to see if we need to look up the IP address If it’s an IP address already, we pass it through. If it’s nil, or localhost, we don’t bother. Otherwise, use ‘resolve_ip_address` to find one



52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/new_relic/control/server_methods.rb', line 52

def convert_to_ip_address(host)
  # here we leave it as a host name since the cert verification
  # needs it in host form
  return host if Agent.config[:ssl]
  # We won't talk directly to the host, so no need to resolve if proxy configured
  return host if Agent.config[:proxy_host]
  return nil if host.nil? || host.downcase == "localhost"
  ip = resolve_ip_address(host)

  ::NewRelic::Agent.logger.debug "Resolved #{host} to #{ip}"
  ip
end

#proxy_serverObject

a new instances of the proxy server - this passes through if there is no proxy, otherwise it has proxy configuration information pulled from the config file



31
32
33
34
35
36
# File 'lib/new_relic/control/server_methods.rb', line 31

def proxy_server
  @proxy_server ||= NewRelic::Control::ProxyServer.new(Agent.config[:proxy_host],
                                                       Agent.config[:proxy_port],
                                                       Agent.config[:proxy_user],
                                                       Agent.config[:proxy_pass])
end

#resolve_ip_address(host) ⇒ Object

Look up the ip address of the host using the pure ruby lookup to prevent blocking. If that fails, fall back to the regular IPSocket library. Return nil if we can’t find the host ip address and don’t have a good default.



69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/new_relic/control/server_methods.rb', line 69

def resolve_ip_address(host)
  Resolv.getaddress(host)
rescue => e
  ::NewRelic::Agent.logger.warn("DNS Error caching IP address:", e)
  begin
    ::NewRelic::Agent.logger.debug("Trying native DNS lookup since Resolv failed")
    IPSocket.getaddress(host)
  rescue => e
    ::NewRelic::Agent.logger.error("Could not look up server address: #{e}")
    nil
  end
end

#serverObject



18
19
20
# File 'lib/new_relic/control/server_methods.rb', line 18

def server
  @remote_server ||= server_from_host(nil)
end

#server_from_host(hostname = nil) ⇒ Object

turns a hostname into an ip address and returns a NewRelic::Control::Server that contains the configuration info



40
41
42
43
44
45
46
# File 'lib/new_relic/control/server_methods.rb', line 40

def server_from_host(hostname=nil)
  host = hostname || Agent.config[:host]

  # if the host is not an IP address, turn it into one
  NewRelic::Control::Server.new(host, Agent.config[:port],
                                convert_to_ip_address(host))
end