Class: SensuPluginsHabitat::Check::Base

Inherits:
Sensu::Plugin::Check::CLI
  • Object
show all
Defined in:
lib/sensu_plugins_habitat/check/base.rb

Overview

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

Instance Method Summary collapse

Instance Method Details

#get(url) ⇒ Net::HTTPResponse

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.

raise [CRITICAL] if the connection fails

Parameters:

  • url (String)

    the full URL to GET

Returns:

  • (Net::HTTPResponse)

    the HTTP response object for processing



116
117
118
119
120
121
122
123
124
# 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) ⇒ Net::HTTPResponse

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

Parameters:

  • endpoint (String)

    an API endpoint

Returns:

  • (Net::HTTPResponse)


102
103
104
105
# 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) ⇒ Array

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

Parameters:

  • uri (URI)

    the URI object being fetched

Returns:

  • (Array)

    the array of params for Net::HTTP.start



133
134
135
136
137
138
139
140
141
142
143
# 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

#runObject

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



92
93
94
# File 'lib/sensu_plugins_habitat/check/base.rb', line 92

def run
  exit 0
end