bin/check-es-heap.rb in sensu-plugins-elasticsearch-0.1.1 vs bin/check-es-heap.rb in sensu-plugins-elasticsearch-0.1.2
- old
+ new
@@ -27,10 +27,11 @@
#
require 'sensu-plugin/check/cli'
require 'rest-client'
require 'json'
+require 'base64'
#
# ES Heap
#
class ESHeap < Sensu::Plugin::Check::CLI
@@ -72,27 +73,42 @@
short: '-P',
long: '--percentage',
description: 'Use the WARNING and CRITICAL threshold numbers as percentage indicators of the total heap available',
default: false
+ option :user,
+ description: 'Elasticsearch User',
+ short: '-u USER',
+ long: '--user USER'
+
+ option :password,
+ description: 'Elasticsearch Password',
+ short: '-W PASS',
+ long: '--password PASS'
+
def acquire_es_version
info = acquire_es_resource('/')
info['version']['number']
end
def acquire_es_resource(resource)
- r = RestClient::Resource.new("http://#{config[:host]}:#{config[:port]}#{resource}", timeout: config[:timeout])
+ headers = {}
+ if config[:user] && config[:password]
+ auth = 'Basic ' + Base64.encode64("#{config[:user]}:#{config[:password]}").chomp
+ headers = { 'Authorization' => auth }
+ end
+ r = RestClient::Resource.new("http://#{config[:host]}:#{config[:port]}#{resource}", timeout: config[:timeout], headers: headers)
JSON.parse(r.get)
rescue Errno::ECONNREFUSED
warning 'Connection refused'
rescue RestClient::RequestTimeout
warning 'Connection timed out'
rescue JSON::ParserError
warning 'Elasticsearch API returned invalid JSON'
end
- def acquire_heap_data(return_max = false) # rubocop:disable all
+ def acquire_heap_data(return_max = false)
if Gem::Version.new(acquire_es_version) >= Gem::Version.new('1.0.0')
stats = acquire_es_resource('/_nodes/_local/stats?jvm=true')
node = stats['nodes'].keys.first
else
stats = acquire_es_resource('/_cluster/nodes/_local/stats?jvm=true')
@@ -107,10 +123,10 @@
rescue
warning 'Failed to obtain heap used in bytes'
end
end
- def run # rubocop:disable all
+ def run
if config[:percentage]
heap_used, heap_max = acquire_heap_data(true)
heap_used_ratio = ((100 * heap_used) / heap_max).to_i
message "Heap used in bytes #{heap_used} (#{heap_used_ratio}% full)"
if heap_used_ratio >= config[:crit]