-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathchealth.sh
executable file
·45 lines (38 loc) · 1.39 KB
/
chealth.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
#!/bin/sh
# Function to check the health status of a container
check_health_status() {
container_name="$1"
health_status=$(docker inspect --format='{{.State.Health.Status}}' "$container_name")
echo "$health_status"
}
# Function to restart a container
restart_container() {
container_name="$1"
echo "Restarting container: $container_name"
docker restart "$container_name"
}
# Function to log errors to journalctl
log_error() {
error_message="$1"
echo "Error: $error_message" >&2
echo "$(date +'%Y-%m-%d %H:%M:%S') - DockerStatus: $error_message" | logger -p user.err
}
# Main loop
while true; do
# Read container names from chealth.conf file
containers_file="chealth.conf"
if [ -f "$containers_file" ]; then
while IFS= read -r container; do
health_status=$(check_health_status "$container")
echo "$(date +'%Y-%m-%d %H:%M:%S') Container: $container, Health Status: $health_status"
if [ "$health_status" != "healthy" ]; then
restart_container "$container"
log_error "$(date +'%Y-%m-%d %H:%M:%S') Container '$container' is unhealthy and was restarted."
fi
done < "$containers_file"
else
echo "$(date +'%Y-%m-%d %H:%M:%S') Error: $containers_file not found. Please create the file and add container names, one per line."
exit 1
fi
sleep 30
done