Sha256: 3f2abf949ba611d1e7a82390259c5e7ef9e93fd1dca34b41d32002b660dbb586

Contents?: true

Size: 951 Bytes

Versions: 1

Compression:

Stored size: 951 Bytes

Contents

module Hcheck
  module Checks
    # ping check module
    # implements status
    # include ping check dependencies
    # returns ok only if the response code is 2** or 3**
    module Ping
      # @config { url }
      def status(config)
        url = URI.parse(config[:url])
        request = build_request(url)

        case request.request_head(url.path)
        when Net::HTTPSuccess, Net::HTTPRedirection, Net::HTTPInformation
          'ok'
        else
          'bad'
        end
      rescue Net::ReadTimeout, Errno::ECONNREFUSED => e
        Hcheck.logger.error "[HCheck] Ping Fail #{e.message}"
        'bad'
      end

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

      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
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
hcheck-0.1.0 lib/hcheck/checks/ping.rb