bin/metrics-es-cluster.rb in sensu-plugins-elasticsearch-0.1.1 vs bin/metrics-es-cluster.rb in sensu-plugins-elasticsearch-0.1.2
- old
+ new
@@ -29,10 +29,11 @@
#
require 'sensu-plugin/metric/cli'
require 'rest-client'
require 'json'
+require 'base64'
#
# ES Cluster Metrics
#
class ESClusterMetrics < Sensu::Plugin::Metric::CLI::Graphite
@@ -60,16 +61,31 @@
short: '-t SECS',
long: '--timeout SECS',
proc: proc(&:to_i),
default: 30
+ option :user,
+ description: 'Elasticsearch User',
+ short: '-u USER',
+ long: '--user USER'
+
+ option :password,
+ description: 'Elasticsearch Password',
+ short: '-P PASS',
+ long: '--password PASS'
+
def acquire_es_version
info = get_es_resource('/')
info['version']['number']
end
def get_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'