Created
August 7, 2020 12:21
-
-
Save jgis/b7b70052917dcdc26f6eca7e2d5e6a6c to your computer and use it in GitHub Desktop.
Nagios/Icinga check script for Dropwizard Healthchecks
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
# Check dropwizard health check page. | |
# Reports the number of healthy services. | |
# The only parameter is the url to the healthcheck page. i.e. https://host/service/metrics/healthcheck | |
# for use in icinga2, define a CheckCommand: | |
# | |
# object CheckCommand "dropwizard_health" { | |
# import "plugin-check-command" | |
# command = [ SysconfDir + "/icinga2/scripts/check_dropwizard_health.py", "$url$" ] | |
# } | |
# | |
# and a Service: | |
# | |
# object Service "<appname>_health" { | |
# import "generic-service" | |
# check_command = "dropwizard_health" | |
# display_name = "<Application> Health" | |
# host_name = "hostname" | |
# vars.url = "http://application-host/metrics/healthcheck" | |
# } | |
import os | |
import sys | |
import requests | |
url = sys.argv[1] | |
count = 0 | |
healthy = 0 | |
r = requests.get(url) | |
if r.status_code == 200: | |
s = r.json() | |
for part, state in s.items(): | |
count = count + 1 | |
if state['healthy'] == True: | |
healthy = healthy + 1 | |
msg = str(healthy) + " of " + str(count) + " services are healthy." | |
state = "UNKNOWN" | |
unhealthy = count - healthy | |
if healthy == count: | |
state = "OK" | |
exit = 0 | |
else: | |
state = "WARNING" | |
exit = 1 | |
print ("HEALTH " + state + ": " + msg + " |healthy=" + str(healthy) + ";" + str(unhealthy) + ";0;" + str(count)) | |
sys.exit(exit) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment