Added support for Slack / MS Teams webhooks

This commit is contained in:
Dominik Deutsch
2021-05-12 14:16:27 +02:00
parent 50de4c7688
commit 7545e77c1e
3 changed files with 25 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_SLACK="" \
WEBHOOK_TEAMS=""
HEALTHCHECK --interval=5s CMD pgrep -f autoheal || exit 1
+2
View File
@@ -60,6 +60,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_SLACK="" # post message to the slack webhook if a container was restarted (or restart failed)
WEBHOOK_TEAMS="" # post message to the teams webhook if a container was restarted (or restart failed)
```
### Optional Container Labels
+20
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_TEAMS=${WEBHOOK_TEAMS:-""}
WEBHOOK_SLACK=${WEBHOOK_SLACK:-""}
# only use unix domain socket if no TCP endpoint is defined
case "${DOCKER_SOCK}" in
@@ -56,6 +58,21 @@ restart_container() {
docker_curl -f -X POST "${HTTP_ENDPOINT}/containers/${container_id}/restart?t=${timeout}"
}
notify_webhook() {
local text="$1"
local data="{\"text\": \"${text}\"}"
if [ -n "$WEBHOOK_SLACK" ]
then
curl -X POST -H "Content-type: application/json" -d $data $WEBHOOK_SLACK
fi
if [ -n "$WEBHOOK_TEAMS" ]
then
curl -X POST -H "Content-Type: application/json" -d $data $WEBHOOK_TEAMS
fi
}
# SIGTERM-handler
term_handler() {
exit 143 # 128 + 15 -- SIGTERM
@@ -94,6 +111,9 @@ if [ "$1" = "autoheal" ] && [ -e "$DOCKER_SOCK" ];then
if ! restart_container "$CONTAINER_ID" "$TIMEOUT"
then
echo "$DATE Restarting container $CONTAINER_SHORT_ID failed" >&2
notify_webhook "Container $CONTAINER_NAME (${CONTAINER_SHORT_ID}) found to be unhealthy. Failed to restart the container."
else
notify_webhook "Container $CONTAINER_NAME (${CONTAINER_SHORT_ID}) found to be unhealthy. Successfully restarted the container"
fi
fi
done