Search

Page tree
Skip to end of metadata
Go to start of metadata

Example code for monitoring ElasticSerach status

#!/usr/bin/python

import urllib2, json

#url='http://localhost:9200/_nodes/hipchat.home.soenke-martens.de/stats'
url='http://localhost:9200/_nodes/thread_pool'

req = urllib2.Request(url)
opener = urllib2.build_opener()
f = opener.open(req)
j = json.loads(f.read())
#print json
print json.dumps(j, indent=4, sort_keys=True)
#print json['nodes']['-koYKPKhSRGgcNrmIKN3-Q']['thread_pool']
#!/usr/bin/python

import urllib2, json, sys
url='http://localhost:9200/_nodes/thread_pool'

req = urllib2.Request(url)
opener = urllib2.build_opener()
f = opener.open(req)
j = json.loads(f.read())

#print json.dumps(j, indent=4, sort_keys=True)

#print dir(j)
# clear', 'copy', 'fromkeys', 'get', 'has_key', 'items', 'iteritems', 'iterkeys', 'itervalues', 'keys', 'pop', 'popitem', 'setdefault', 'update', 'values', 'viewitems', 'viewkeys', 'viewvalues'

#print j.keys()
# print dir(j['nodes'])
# 'clear', 'copy', 'fromkeys', 'get', 'has_key', 'items', 'iteritems', 'iterkeys', 'itervalues', 'keys', 'pop', 'popitem', 'setdefault', 'update', 'values', 'viewitems', 'viewkeys', 'viewvalues'

node0 = j['nodes'].keys()[0]
#print node0

node0_data = j['nodes'].get(node0)
#print node0_data

node0_bulk = node0_data['thread_pool']['bulk']
node0_flush = node0_data['thread_pool']['flush']
node0_generic = node0_data['thread_pool']['generic']
node0_get = node0_data['thread_pool']['get']
node0_index = node0_data['thread_pool']['index']
node0_listener = node0_data['thread_pool']['listener']
node0_management = node0_data['thread_pool']['management']
node0_merge = node0_data['thread_pool']['merge']
node0_optimize = node0_data['thread_pool']['optimize']
node0_percolate = node0_data['thread_pool']['percolate']
node0_refresh = node0_data['thread_pool']['refresh']
node0_search = node0_data['thread_pool']['search']
node0_snapshot = node0_data['thread_pool']['snapshot']

#print node0_index
#{u'max': 4, u'queue_size': u'200', u'type': u'fixed', u'min': 4}

node0_index_min = node0_index['min']
node0_index_max = node0_index['max']
node0_index_size = node0_index['queue_size']

index_warn_percent = 60
index_crit_percent = 80
index_current_percent = 100 * int(node0_index_max) / int(node0_index_size)

perfdata=" | index_queue=%d" % (index_current_percent)

if index_current_percent >= index_crit_percent:
  print "CRITICAL: Index queue at %d of %d %% %s" % (index_current_percent, index_crit_percent, perfdata)
  sys.exit(2)
elif index_current_percent >= index_warn_percent:
  print "WARN: Index queue at at %d of %d %% %s" % (index_current_percent, index_warn_percent, perfdata)
  sys.exit(1)
elif index_current_percent >= 0:
  print "OK: Index queue at %d %% %s" % (index_current_percent, perfdata)
  sys.exit(0)
else:
  print "UNKNOWN: Current queue percent at %s" % str(index_current_percent)
  sys.exit(3)
fi
print "UNKNOWN: Script does not return a queue size"
sys.exit(4)