Skip to content

Instantly share code, notes, and snippets.

@khoubyari
Created March 25, 2016 17:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save khoubyari/be3e1593573074366d91 to your computer and use it in GitHub Desktop.
Save khoubyari/be3e1593573074366d91 to your computer and use it in GitHub Desktop.
Python server plugin for monitoring a Spring Boot microservice health check
import os, sys, requests, json, argparse
if __name__=='__main__':
fileName = os.path.basename(__file__)
parser = argparse.ArgumentParser(description='Monitor a spring boot healthcheck.',
epilog='Examples: "' + fileName + ' -i localhost:8080 -s myService" OR "'+fileName+' -i myhost.example.com:8001"')
parser.add_argument('-i', '--host', help="The hostname or hostname:port portion of the service)", required=True)
parser.add_argument('-t', '--https', help="Turn on SSL (use HTTPS instead of HTTP)", action='store_true', default=False)
parser.add_argument('-s', '--service', help="The case-sensitive name of the service for including the service-specific section inside the health check response", required=False)
args = vars(parser.parse_args())
transport = 'http';
if (args['https']):
transport = 'https';
if (args['service']):
serviceName = args['service']
## Call the spring-boot health check on /health
## remember that the actuator managemept APIs can be optionally exposed on a different port
## the host parameter can contain an optional port (e.g. myhost:8080)
hcReq = requests.get(transport+'://'+args['host']+'/health')
if hcReq.status_code != 200:
## If a non-OK response, print CRITICAL with the response code and optional service-specific section from response JSON if cound
resp = json.loads(hcReq.content)
returnString = "CRITICAL :: HTTP " + str(hcReq.status_code)
if (args['service']) and (resp[args['service']] is not 'null'):
returnString += " :: Service " + args['service'] + " returned : " + json.dumps(resp[args['service']])
print(str(returnString))
sys.exit(2)
else:
resp = json.loads(hcReq.content)
print("OK")
sys.exit(0)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment