class SensuPluginsHabitat::Check::Base

A base class with some common options that the actual plugins can inherit from.

Public Instance Methods

get(url) click to toggle source

Use net/http to do a GET on a URL and return the response object. If the connection is refused or times out, consider it an automatic CRITICAL, since the check presumably won’t be able to proceed.

@param url [String] the full URL to GET @return [Net::HTTPResponse] the HTTP response object for processing raise [CRITICAL] if the connection fails

# File lib/sensu_plugins_habitat/check/base.rb, line 116
def get(url)
  uri = URI(url)

  resp = Net::HTTP.start(*http_params_for(uri)).get(uri)

  resp.is_a?(Net::HTTPRedirection) ? get(resp.header['location']) : resp
rescue Errno::ECONNREFUSED, Net::OpenTimeout => e
  critical("Connection to the supervisor API failed: #{e.message}")
end
hab_get(endpoint) click to toggle source

Fetch and return a given endpoint from the Habitat supervisor API.

@param endpoint [String] an API endpoint @return [Net::HTTPResponse]

# File lib/sensu_plugins_habitat/check/base.rb, line 102
def hab_get(endpoint)
  server = "#{config[:protocol]}://#{config[:host]}:#{config[:port]}"
  get(File.join(server, endpoint))
end
http_params_for(uri) click to toggle source

Assemble the array of params for Net::HTTP.start, according to the check config and URI object being retrieved.

@param uri [URI] the URI object being fetched @return [Array] the array of params for Net::HTTP.start

# File lib/sensu_plugins_habitat/check/base.rb, line 133
def http_params_for(uri)
  conn_opts = {
    use_ssl: uri.scheme == 'https',
    open_timeout: config[:timeout],
    read_timeout: config[:timeout],
    verify_mode: (OpenSSL::SSL::VERIFY_NONE if config[:insecure]),
    ca_file: config[:capath]
  }.compact

  [uri.host, uri.port, conn_opts]
end
run() click to toggle source

This should never be run, but Sensu complains if we don’t define a run method with an exit.

# File lib/sensu_plugins_habitat/check/base.rb, line 92
def run
  exit 0
end