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



20
21
22
23
24
25
26
27
# File 'lib/new_relic/control/server_methods.rb', line 20

def api_server
  api_host = self['api_host'] || 'rpm.newrelic.com'
  @api_server ||=
    NewRelic::Control::Server.new \
  api_host,
  (self['api_port'] || self['port'] || (use_ssl? ? 443 : 80)).to_i,
  nil
end

#cert_file_pathObject

The path to the certificate file used to verify the SSL connection if verify_peer is enabled



80
81
82
# File 'lib/new_relic/control/server_methods.rb', line 80

def cert_file_path
  File.expand_path(File.join(newrelic_root, 'cert', 'cacert.pem'))
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



50
51
52
53
54
55
56
57
58
# File 'lib/new_relic/control/server_methods.rb', line 50

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 verify_certificate?
  return nil if host.nil? || host.downcase == "localhost"
  ip = resolve_ip_address(host)
  log.info "Resolved #{host} to #{ip}"
  ip
end

#http_connection(host = nil) ⇒ Object

Return the Net::HTTP with proxy configuration given the NewRelic::Control::Server object. Default is the collector but for api calls you need to pass api_server

Experimental support for SSL verification: swap ‘VERIFY_NONE’ for ‘VERIFY_PEER’ line to try it out If verification fails, uncomment the ‘http.ca_file’ line and it will use the included certificate.



91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/new_relic/control/server_methods.rb', line 91

def http_connection(host = nil)
  host ||= server
  # Proxy returns regular HTTP if @proxy_host is nil (the default)
  http_class = Net::HTTP::Proxy(proxy_server.name, proxy_server.port,
                                proxy_server.user, proxy_server.password)
  http = http_class.new(host.ip || host.name, host.port)
  log.debug("Http Connection opened to #{host.ip||host.name}:#{host.port}")
  if use_ssl?
    http.use_ssl = true
    if verify_certificate?
      http.verify_mode = OpenSSL::SSL::VERIFY_PEER
      http.ca_file = cert_file_path
    else
      http.verify_mode = OpenSSL::SSL::VERIFY_NONE
    end
  end
  http
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



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

def proxy_server
  @proxy_server ||=
    NewRelic::Control::ProxyServer.new self['proxy_host'], self['proxy_port'], self['proxy_user'], self['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.



64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/new_relic/control/server_methods.rb', line 64

def resolve_ip_address(host)
  Resolv.getaddress(host)
rescue => e
  log.warn("DNS Error caching IP address: #{e}")
  log.debug(e.backtrace.join("\n   "))
  begin
    log.info("Trying native DNS lookup since Resolv failed")
    IPSocket.getaddress(host)
  rescue => e
    log.error("Could not look up server address: #{e}")
    nil
  end
end

#serverObject



14
15
16
# File 'lib/new_relic/control/server_methods.rb', line 14

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



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

def server_from_host(hostname=nil)
  host = hostname || self['host'] || 'collector.newrelic.com'

  # if the host is not an IP address, turn it into one
  NewRelic::Control::Server.new host, (self['port'] || (use_ssl? ? 443 : 80)).to_i, convert_to_ip_address(host)
end