Last active
December 31, 2023 16:19
-
-
Save CodeKJ/5e773d407c48b6343f778573dfe181ea to your computer and use it in GitHub Desktop.
Docker container healthcheck and automatic restart based on Uptime Kuma monitor name in metrics endpoint.
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
#!/bin/bash | |
# docker-compose example: | |
#version: "3.9" | |
#services: | |
# qbittorrent: | |
# image: lscr.io/linuxserver/qbittorrent:latest | |
# environment: | |
# - UPTIME_KUMA_API_KEY=your_api_key | |
# volumes: | |
# - ./healthcheck.sh:/scripts/healthcheck.sh | |
# healthcheck: | |
# test: [ "CMD", "/bin/bash", "/scripts/healthcheck.sh", "http://uptime-kuma:3001", "qBittorrent" ] | |
# interval: 10s | |
# timeout: 10s | |
# retries: 3 | |
if [ $# -ne 2 ]; then | |
echo "Usage: $0 <UPTIME_KUMA_URL> <MONITOR_NAME>" | |
exit 1 | |
fi | |
if [ -z "$UPTIME_KUMA_API_KEY" ]; then | |
echo "Environment variable <UPTIME_KUMA_API_KEY> is not set." | |
exit 1 | |
fi | |
UPTIME_KUMA_URL=$1 | |
MONITOR_NAME=$2 | |
# Make http request and find specific monitor status. | |
RESPONSE=$(curl -su ":${UPTIME_KUMA_API_KEY}" "$UPTIME_KUMA_URL/metrics" | grep "monitor_status{monitor_name=\"${MONITOR_NAME}\"") | |
# Extract status (1 = UP, 0 = DOWN, 2 = PENDING, 3 = MAINTENANCE) | |
STATUS="${RESPONSE: -1}" | |
# Down/Pending/Maintenance | |
if [[ $STATUS != "1" ]]; then | |
echo "Healthcheck FAILED" | |
# Send signal to kill process. | |
kill 1 | |
exit 1 | |
fi | |
# UP | |
echo "Healthcheck OK" | |
exit 0 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment