This is the script I came up with to check on docker containers and notify Healtchecks. See my video on Healthchecks for a brief walkthrough, and the Healthchecks website for documentation and further information.
The Healthchecks developer reached out to me and had some helpful advice on some features I was overlooking. One is that you can append /fail to the ping url and it will instantly notify of the failure instead of waiting for the timer to lapse. And the other was that instead of using the uuid URLS using the slug URLS are easier to keep track of. So with that in mind I have changed my script, but I’ll leave the first version further down below. This version requires the container name and Healthchecks name to match whereas if you manually add each with the separate uuid URL then it would not require this.
#!/usr/bin/env python3 import os import socket import requests #change these config options server = "thinkcentre.lan:7055" #domain:port or ip:port of healthchecks server ping_key = "Htu_MIMlIxuhHMnF-Ffo1A" #ping key obtained on healthchecks settings page containers = ['sabnzbd', 'sonarr', 'deluge'] #container names to check for, container name and healthcheck name must match #function for checking on status def checkstatus(container): stream = os.popen('docker ps | grep ' + container) output = stream.read() url = 'http://' + server + '/ping/' + ping_key + '/' + container #puts together the url to ping if bool(output): requests.get(url, timeout=10) #if variable is set, ping healthcheck success else: requests.get(url + '/fail', timeout=10) #if container down, ping healthcheck fail #run the checks for i in containers: checkstatus(i)
And my first version below, although, I am changing the print line to the /fail ping because that should have been obvious to me from the beginning… had I read the documentation. Oops.
#!/usr/bin/env python3 import os import socket import requests #function for checking on status def checkstatus(container, healthchecksurl): stream = os.popen('docker ps | grep ' + container) output = stream.read() if bool(output): requests.get(healthchecksurl, timeout=10) else: #print(container + " is down!") requests.get(healthchecksurl + "/fail", timeout=10) #pass docker service name, healthchecks ping url for each docker container to check checkstatus("paperless", "http://thinkcentre.lan:7055/ping/90fdd24c-ba0a-494b-8711-0661d1e14171") checkstatus("airsonic", "http://thinkcentre.lan:7055/ping/3c899b70-3bbb-4fc8-814a-d3fb647b94c6") checkstatus("smtptelegram", "http://thinkcentre.lan:7055/ping/017fbe01-9299-498f-a152-d0ff3b614b56") checkstatus("deluge", "http://thinkcentre.lan:7055/ping/50126e53-ff1c-41d9-9e49-1f463ba6fd49") checkstatus("sonarr", "http://thinkcentre.lan:7055/ping/d86999d0-9214-4afc-aaff-4f3b076e6ca0") checkstatus("sabnzbd", "http://thinkcentre.lan:7055/ping/90c86ddd-4e9a-4f5c-a83c-ab28f63f545b")