Merge branch 'apprise'

This commit is contained in:
Tyler Pace
2023-04-18 18:07:43 -07:00
3 changed files with 56 additions and 1 deletions
+3 -1
View File
@@ -10,7 +10,9 @@ ENV AUTOHEAL_CONTAINER_LABEL=autoheal \
AUTOHEAL_INTERVAL=5 \
AUTOHEAL_DEFAULT_STOP_TIMEOUT=10 \
DOCKER_SOCK=/var/run/docker.sock \
CURL_TIMEOUT=30
CURL_TIMEOUT=30 \
WEBHOOK_URL="" \
APPRISE_URL=""
HEALTHCHECK --interval=5s CMD pgrep -f autoheal || exit 1
+12
View File
@@ -52,6 +52,16 @@ If you need the timezone to match the local machine, you can map the `/etc/local
docker run ... -v /etc/localtime:/etc/localtime:ro
```
### Apprise configuration
a) Start the Apprise docker container. See: https://hub.docker.com/r/caronc/apprise
b) Create an Apprise configuration for Autoheal to use:
http://localhost:8000/cfg/autoheal
See the Apprise wiki for configuration details: https://github.com/caronc/apprise/wiki
c) Set the `APPRISE_URL` environment variable to use the Apprise configuration:
`APPRISE_URL="http://localhost:8000/notify/autoheal"`
## ENV Defaults
```
@@ -61,6 +71,8 @@ AUTOHEAL_START_PERIOD=0 # wait 0 seconds before first health check
AUTOHEAL_DEFAULT_STOP_TIMEOUT=10 # Docker waits max 10 seconds (the Docker default) for a container to stop before killing during restarts (container overridable via label, see below)
DOCKER_SOCK=/var/run/docker.sock # Unix socket for curl requests to Docker API
CURL_TIMEOUT=30 # --max-time seconds for curl requests to Docker API
WEBHOOK_URL="" # post message to the webhook if a container was restarted (or restart failed)
APPRISE_URL="" # post message to Apprise if a container was restarted (or restart failed)
```
### Optional Container Labels
+41
View File
@@ -7,6 +7,8 @@ set -o pipefail
DOCKER_SOCK=${DOCKER_SOCK:-/var/run/docker.sock}
UNIX_SOCK=""
CURL_TIMEOUT=${CURL_TIMEOUT:-30}
WEBHOOK_URL=${WEBHOOK_URL:-""}
APPRISE_URL=${APPRISE_URL:-""}
# only use unix domain socket if no TCP endpoint is defined
case "${DOCKER_SOCK}" in
@@ -58,6 +60,42 @@ restart_container() {
docker_curl -f -X POST "${HTTP_ENDPOINT}/containers/${container_id}/restart?t=${timeout}"
}
notify_webhook() {
local text="$@"
if [ -n "$WEBHOOK_URL" ]
then
# execute webhook requests as background process to prevent healer from blocking
curl -s -X POST -H "Content-type: application/json" -d "$(generate_webhook_payload $text)" $WEBHOOK_URL
fi
if [ -n "$APPRISE_URL" ]
then
# execute webhook requests as background process to prevent healer from blocking
curl -s -X POST -H "Content-type: application/json" -d "$(generate_apprise_payload $text)" $APPRISE_URL
fi
}
# https://towardsdatascience.com/proper-ways-to-pass-environment-variables-in-json-for-curl-post-f797d2698bf3
generate_webhook_payload() {
local text="$@"
cat <<EOF
{
"text":"$text"
}
EOF
}
generate_apprise_payload() {
local text="$@"
cat <<EOF
{
"title":"Autoheal",
"body":"$text"
}
EOF
}
# SIGTERM-handler
term_handler() {
exit 143 # 128 + 15 -- SIGTERM
@@ -102,6 +140,9 @@ then
if ! restart_container "$CONTAINER_ID" "$TIMEOUT"
then
echo "$DATE Restarting container $CONTAINER_SHORT_ID failed" >&2
notify_webhook "Container ${CONTAINER_NAME:1} (${CONTAINER_SHORT_ID}) found to be unhealthy. Failed to restart the container!" &
else
notify_webhook "Container ${CONTAINER_NAME:1} (${CONTAINER_SHORT_ID}) found to be unhealthy. Successfully restarted the container!" &
fi
fi
done