Module: Hcheck::Checks::Ping

Defined in:
lib/hcheck/checks/ping.rb

Overview

ping check module implements status include ping check dependencies returns ok only if the response code is 2** or 3**

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(_base) ⇒ Object



20
21
22
# File 'lib/hcheck/checks/ping.rb', line 20

def self.included(_base)
  require 'net/http'
end

Instance Method Details

#build_request(url) ⇒ Object



24
25
26
27
28
29
30
31
# File 'lib/hcheck/checks/ping.rb', line 24

def build_request(url)
  req = Net::HTTP.new(url.host, url.port)
  req.use_ssl = true if url.scheme == 'https'
  req.read_timeout = 5 # seconds

  url.path = '/' if url.path.empty?
  req
end

#fail_with(response, url) ⇒ Object



33
34
35
36
# File 'lib/hcheck/checks/ping.rb', line 33

def fail_with(response, url)
  raise Hcheck::Errors::HTTPError,
        [response.class, response.code, response.message].join(' ') + " from #{url}"
end

#status(config) ⇒ Object



9
10
11
12
13
14
15
16
17
18
# File 'lib/hcheck/checks/ping.rb', line 9

def status(config)
  url = URI.parse(config[:url])
  request = build_request(url)

  res = request.request_head(url.path)

  ok_responses = [Net::HTTPSuccess, Net::HTTPInformation, Net::HTTPMovedPermanently]

  fail_with(res, url) unless ok_responses.include?(res.class)
end