Module: NewRelic::Control::ServerMethods

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

Instance Method Summary collapse

Instance Method Details

#api_serverObject



17
18
19
20
21
22
23
24
# File 'lib/new_relic/control/server_methods.rb', line 17

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

#convert_to_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.



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/new_relic/control/server_methods.rb', line 42

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"
  # Fall back to known ip address in the common case
  ip_address = '65.74.177.195' if host.downcase == 'collector.newrelic.com'
  begin
    ip_address = Resolv.getaddress(host)
    log.info "Resolved #{host} to #{ip_address}"
  rescue => e
    log.warn "DNS Error caching IP address: #{e}"
    log.debug e.backtrace.join("\n   ")
    ip_address = IPSocket::getaddress host rescue ip_address
  end
  ip_address
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.



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

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 = File.join(File.dirname(__FILE__), '..', '..', 'cert', 'cacert.pem')
    else
      http.verify_mode = OpenSSL::SSL::VERIFY_NONE
    end
  end
  http
end

#proxy_serverObject



26
27
28
29
# File 'lib/new_relic/control/server_methods.rb', line 26

def proxy_server
  @proxy_server ||=
    NewRelic::Control::ProxyServer.new self['proxy_host'], self['proxy_port'], self['proxy_user'], self['proxy_pass']
end

#serverObject



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

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

#server_from_host(hostname = nil) ⇒ Object



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

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