Class: SensuPluginsConsul::Check::Base

Inherits:
Sensu::Plugin::Check::CLI
  • Object
show all
Defined in:
lib/sensu-plugins-consul/check/base.rb

Instance Method Summary collapse

Instance Method Details

#consul_get(endpoint) ⇒ Object

Fetch and return the parsed JSON data from a specified Consul API endpoint.



90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/sensu-plugins-consul/check/base.rb', line 90

def consul_get(endpoint)
  url = "#{config[:protocol]}://#{config[:server]}:#{config[:port]}/" \
        "v1/#{endpoint}"
  options = { timeout: config[:timeout],
              verify_ssl: !config[:insecure],
              ssl_ca_file: config[:capath],
              headers: { 'X-Consul-Token' => config[:token] } }

  JSON.parse(RestClient::Resource.new(url, options).get)
rescue Errno::ECONNREFUSED
  critical 'Consul is not responding'
rescue RestClient::RequestTimeout
  critical 'Consul connection timed out'
rescue RestClient::Exception => e
  if e.message.include?('403')
    critical 'ACL token is not authorized to access resource'
  else
    unknown "Consul returned: #{e}"
  end
end