Python script for Nagios to check active primary shards on Elasticsearch cluster
#!/usr/bin/env python
import argparse
import requests
import sys
parser = argparse.ArgumentParser()
parser.add_argument('-e', '--elasticsearch_host', dest='elasticsearch_host', help='Elasticsearch host (FQDN).')
parser.add_argument('-w', '--warning', dest='threshold_warning', help='WARNING threshold (greater than or equal to).')
parser.add_argument('-c', '--critical', dest='threshold_critical', help='CRITICAL threshold (greater than or equal to).')
args = parser.parse_args()
es = args.elasticsearch_host
warning = args.threshold_warning
critical = args.threshold_critical
try:
req = requests.get(es)
data = req.json()
current = data['active_primary_shards']
except requests.exceptions.RequestException as e:
print "Unknow error {}".format(e)
sys.exit(3)
if int(current) > int(warning) and int(current) < int(critical):
print "WARNING - Number of active shards is: {}".format(current)
sys.exit(1)
elif int(current) > int(critical):
print "CRITICAL - Number of active shards is: {}".format(current)
sys.exit(2)
else:
print "OK - Number of active shards is: {}".format(current)