#!/usr/bin/python3 

import sys, getopt, json
if sys.version_info[0]>2:
  from urllib.request import urlopen, URLError
  from io import BytesIO as StringIO
else:
  from urllib2 import urlopen, URLError
  from StringIO import StringIO

try:
  opts, files = getopt.gnu_getopt(sys.argv[1:], 'H:p:', [])
  argv = dict(opts)
except getopt.GetoptError as err:
  print("Error: %s" % err)
  sys.exit(1)

reply = "error"
exit_code = 2
URL = "http://%s:%s/_cluster/health" % (argv['-H'], argv.get('-p', 9200))
try:
  data = json.load(urlopen(URL))
  status = data["status"]
  if status=="green" or status=="yellow":
    reply = "OK"
    exit_code = 0
except Exception as err:
  print("Elasticsearch CRITICAL - %s" % err)
  sys.exit(2)

print(
  "Elastic %s - status=%s" % (reply, status)
)
sys.exit(exit_code)
