bin/check-es-heap.rb in sensu-plugins-elasticsearch-1.7.1 vs bin/check-es-heap.rb in sensu-plugins-elasticsearch-1.8.0
- old
+ new
@@ -88,10 +88,16 @@
option :https,
description: 'Enables HTTPS',
short: '-e',
long: '--https'
+ option :all,
+ description: 'Check all nodes in the ES cluster',
+ short: '-a',
+ long: '--all',
+ default: false
+
def acquire_es_version
info = acquire_es_resource('/')
info['version']['number']
end
@@ -118,48 +124,69 @@
warning 'Service is unavailable'
rescue JSON::ParserError
warning 'Elasticsearch API returned invalid JSON'
end
- def acquire_heap_data(return_max = false)
- stats = if Gem::Version.new(acquire_es_version) >= Gem::Version.new('1.0.0')
- acquire_es_resource('/_nodes/_local/stats')
- else
- acquire_es_resource('/_cluster/nodes/_local/stats')
- end
- node = stats['nodes'].keys.first
- begin
- if return_max
- return stats['nodes'][node]['jvm']['mem']['heap_used_in_bytes'], stats['nodes'][node]['jvm']['mem']['heap_max_in_bytes']
+ def acquire_stats
+ if Gem::Version.new(acquire_es_version) >= Gem::Version.new('1.0.0')
+ if config[:all]
+ acquire_es_resource('/_nodes/stats')
else
- stats['nodes'][node]['jvm']['mem']['heap_used_in_bytes']
+ acquire_es_resource('/_nodes/_local/stats')
end
- rescue
- warning 'Failed to obtain heap used in bytes'
+ elsif config[:all]
+ acquire_es_resource('/_cluster/nodes/stats')
+ else
+ acquire_es_resource('/_cluster/nodes/_local/stats')
end
end
- def run
+ def acquire_heap_data(node)
+ return node['jvm']['mem']['heap_used_in_bytes'], node['jvm']['mem']['heap_max_in_bytes']
+ rescue
+ warning 'Failed to obtain heap used in bytes'
+ end
+
+ def acquire_heap_usage(heap_used, heap_max, node_name)
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]
- critical
- elsif heap_used_ratio >= config[:warn]
- warning
- else
- ok
- end
+ heap_usage = ((100 * heap_used) / heap_max).to_i
+ output = if config[:all]
+ "Node #{node_name}: Heap used in bytes #{heap_used} (#{heap_usage}% full)\n"
+ else
+ "Heap used in bytes #{heap_used} (#{heap_usage}% full)"
+ end
else
- heap_used = acquire_heap_data(false)
- message "Heap used in bytes #{heap_used}"
- if heap_used >= config[:crit]
- critical
- elsif heap_used >= config[:warn]
- warning
- else
- ok
+ heap_usage = heap_used
+ output = config[:all] ? "Node #{node_name}: Heap used in bytes #{heap_used}\n" : "Heap used in bytes #{heap_used}"
+ end
+ [heap_usage, output]
+ end
+
+ def run
+ stats = acquire_stats
+ status = { crit: '', warn: '', ok: '' }
+
+ # Check all the nodes in the cluster, alert if any of the nodes have heap usage above thresholds
+ stats['nodes'].each do |_, node|
+ heap_used, heap_max = acquire_heap_data(node)
+ heap_usage, output = acquire_heap_usage(heap_used, heap_max, node['name'])
+ if heap_usage >= config[:crit]
+ status[:crit] += output
+ elsif heap_usage >= config[:warn]
+ status[:warn] += output
+ elsif !config[:all]
+ status[:ok] += output
end
+ end
+
+ if !status[:crit].empty?
+ message status[:crit]
+ critical
+ elsif !status[:warn].empty?
+ message status[:warn]
+ warning
+ else
+ message status[:ok]
+ ok
end
end
end