Class: Philae::HttpProbe

Inherits:
Object
  • Object
show all
Defined in:
lib/philae/http_probe.rb

Direct Known Subclasses

NSQProbe

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, uri, username = nil, password = nil, tls_context = nil) ⇒ HttpProbe

tls_context:

cert: "path/to/cert",
key: "path/to/key",
ca: "path/to/ca"



14
15
16
17
18
19
20
# File 'lib/philae/http_probe.rb', line 14

def initialize(name, uri, username = nil, password = nil, tls_context = nil)
  @name = name
  @uri = uri
  @username = username
  @password = password
  @tls_context = tls_context
end

Instance Attribute Details

#nameObject (readonly)

Returns the value of attribute name.



6
7
8
# File 'lib/philae/http_probe.rb', line 6

def name
  @name
end

Instance Method Details

#checkObject



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/philae/http_probe.rb', line 22

def check
  begin
    uri = URI(@uri)
    http = Net::HTTP.new(uri.host, uri.port)
    if uri.scheme == "https"
      http.use_ssl = true
      if !@tls_context.nil?
        http.cert = OpenSSL::X509::Certificate.new(File.read(@tls_context[:cert]))
        http.key = OpenSSL::PKey::RSA.new(File.read(@tls_context[:key]))
        http.ca_file = @tls_context[:ca]
        http.verify_mode = OpenSSL::SSL::VERIFY_PEER
      end
    end
    req = Net::HTTP::Get.new(uri)
    req.basic_auth @username, @password if !@username.nil? || !@password.nil?
    resp = http.start do |httpconn|
      httpconn.request(req)
    end
    return { healthy: false, comment: 'Invalid response code', code: resp.code.to_s } if resp.code.to_s[0] != '2' && resp.code.to_s[0] != '3'
  rescue => e
    return { healthy: false, comment: 'Unable to contact server', error: "#{e.class}: #{e.message}" }
  end

  return { healthy: true, comment: '' }
end