Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: ntfy integration #107

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,11 @@ AUTOHEAL_DEFAULT_STOP_TIMEOUT=10 # Docker waits max 10 seconds (the Docker def
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)

NTFY_URL="" # https://docs.ntfy.sh/
NTFY_TITLE="autoheal alert: unhealthy container"
NTFY_PRIORITY="urgent" # https://docs.ntfy.sh/publish/#message-priority
NTFY_TAGS="skull,rotating_light,warning" # https://docs.ntfy.sh/emojis/
```

### Optional Container Labels
Expand Down
25 changes: 24 additions & 1 deletion docker-entrypoint
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,12 @@ UNIX_SOCK=""
CURL_TIMEOUT=${CURL_TIMEOUT:-30}
WEBHOOK_URL=${WEBHOOK_URL:-""}

NTFY_URL=${NTFY_URL:-""}
NTFY_TITLE=${NTFY_TITLE:-"autoheal alert: unhealthy container"}
NTFY_PRIORITY=${NTFY_PRIORITY:-"urgent"} # https://docs.ntfy.sh/publish/#message-priority
NTFY_TAGS=${NTFY_TAGS:-"skull,rotating_light,warning"} # https://docs.ntfy.sh/emojis/


# only use unix domain socket if no TCP endpoint is defined
case "${DOCKER_SOCK}" in
"tcp://"*) HTTP_ENDPOINT="$(echo ${DOCKER_SOCK} | sed 's#tcp://#https://#')"
Expand Down Expand Up @@ -67,6 +73,21 @@ notify_webhook() {
fi
}

ntfy_webhook() {
local text="$@"

if [ -n "$NTFY_URL" ]
then
# execute webhook requests as background process to prevent healer from blocking
curl -X POST \
-H "Title: $NTFY_TITLE" \
-H "Priority: $NTFY_PRIORITY" \
-H "Tags: $NTFY_TAGS" \
-d "$text" \
$NTFY_URL
fi
}

# https://towardsdatascience.com/proper-ways-to-pass-environment-variables-in-json-for-curl-post-f797d2698bf3
generate_webhook_payload() {
local text="$@"
Expand Down Expand Up @@ -116,8 +137,10 @@ if [ "$1" = "autoheal" ] && [ -e "$DOCKER_SOCK" ];then
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
ntfy_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!" &
ntfy_webhook "Container ${CONTAINER_NAME:1} (${CONTAINER_SHORT_ID}) found to be unhealthy. Successfully restarted the container!" &
fi
fi
done
Expand Down