-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
docker_container_healthchecks.sh
executable file
·59 lines (54 loc) · 2.53 KB
/
docker_container_healthchecks.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
#!/usr/bin/env bash
#
# Script to check container status and report to Discord if any of them are not running
# Tronyx
# Define some variables
tempDir='/tmp/'
containerNamesFile="${tempDir}container_names.txt"
# Exclude containers you do not want to be checked
exclude=("container-1" "container-2" "container-3")
# Your webhook URL for the Discord channel you want alerts sent to
discordWebhookURL=''
# Your Discord numeric user ID
# To find your user ID just type \@<username> or \@<role>, like so \@username#1337
# It will look something like <@123492578063015834> and you NEED the exclamation point like below
discordUserID='<@!123492578063015834>'
# Function to create list of Docker containers
create_containers_list() {
docker ps -a --format '{{.Names}}' | sort > "${containerNamesFile}"
}
# Function to check Docker containers
check_containers() {
if [ -f ${containerNamesFile} ]; then
if [ -s ${containerNamesFile} ]; then
while IFS= read -r container; do
if [[ ! ${exclude[*]} =~ ${container} ]]; then
containerStatus=$(docker inspect "${container}" | jq .[].State.Status | tr -d '"')
if [ "${containerStatus}" = 'running' ];then
:
elif [ "${containerStatus}" = 'exited' ];then
curl -s -H "Content-Type: application/json" -X POST -d '{"content": "'"${discordUserID}"' The '"${container}"' container is currently stopped!"}' "${discordWebhookURL}"
elif [ "${containerStatus}" = 'dead' ];then
curl -s -H "Content-Type: application/json" -X POST -d '{"content": "'"${discordUserID}"' The '"${container}"' container is currently dead!"}' "${discordWebhookURL}"
elif [ "${containerStatus}" = 'restarting' ];then
curl -s -H "Content-Type: application/json" -X POST -d '{"content": "'"${discordUserID}"' The '"${container}"' container is currently restarting!"}' "${discordWebhookURL}"
else
curl -s -H "Content-Type: application/json" -X POST -d '{"content": "'"${discordUserID}"' The '"${container}"' container currently has an unknown status!"}' "${discordWebhookURL}"
fi
fi
done < <(cat "${containerNamesFile}")
else
echo "There are currently no Docker containers on this Server!"
exit 0
fi
else
echo "Unable to find ${containerNamesFile}!"
exit 1
fi
}
# Main function to run all other functions
main() {
create_containers_list
check_containers
}
main