-
Notifications
You must be signed in to change notification settings - Fork 0
/
watchduck.sh
123 lines (111 loc) · 17.5 KB
/
watchduck.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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
#!/bin/zsh
printf "%b\n" " ▄╥╗╦╦╦╗╗╖╓"
printf "%b\n" " ▄▓████▓╝▀╩╙╙╙╩▒╠▒═"
printf "%b\n" " ▓█▀▀╙ ▄▄▄▄╓ ╙╚╦▒┐"
printf "%b\n" " ╓▀ ╓▄▄▓▓█████████▓▄ └▒╗"
printf "%b\n" " ╓▒╓▄▓███████▓╬▓▓█╬╫╣▓▓▓▓▄ ╚▓"
printf "%b\n" " ▓▓████╩ └└╙╣▓▓▓█└▀▓▄"
printf "%b\n" " \033[32m╔╦╓▒╝\033[0m\033[33m╩▓╩╪╫▄╔╗╓\033[0m ╟███▓└ └▀██▒ └┐"
printf "%b\n" " \033[32m┌╔▄▄╫╣▒▒▌▓╟▐▒▒▒\033[0m\033[33m▌▀╪╢╣│▌╙▌\033[0m ███╩░░░─┘ ╠▒▒ ▀▓╦└═╓"
printf "%b\n" " \033[32m╗▄▒▌▓╟╟░▌▌▓╟▐▒▒▌▓╟╟╣╬▒\033[0m\033[33m▓╗╪▄╣╓╠╙▌\033[0m ▐██╬▒▒▒▒▒░┐░ ┌╓░░░╓░▀█▀╠░░░ ═┐"
printf "%b\n" " \033[32m╣▒▌▌█╟▐▒▌▌▓╟▐▒▒▌█╫╣╣╬▓\033[0m\033[33m▓╣▓╣╣╠╠▀▌\033[0m ╞█▓▓╬╣▒╠▒╠╦▒░▒▒▒╠▒╬╣╬▒▒║████▓▓▄░┐┌ ▐═┌"
printf "%b\n" " \033[32m╣▒▌██╫╫▒▓██▓╣▓███████\033[0m\033[33m██▓█▓▓╬╬╬▌\033[0m └█▓▓▓╣╬▒╬▒╩╠╫╣▓▓▓▓█▓╨╙└└ ╙╙▀▀▀▓▓▄▄▄▄▄▄▄▄═"
printf "%b\n" " \033[32m█▓████╣▓█████████▀█▓█╝█░▒██╬╫╬▓▄▄\033[0m ▓▓█▓▓╬╬▒░╠ ╠╚╬▓██▌"
printf "%b\n" " ███████▀▀▓▓╨█\033[32m▓▒▓╩█▓▓▌░╟▄▄╣▌▄▐ ┤\033[0m█\033[34m▀▒╗╫▒▄▀\033[0m ╙▓█▓▓▓╬▐░░░╔░╚╠╣▓"
printf "%b\n" " \033[34m┌─╦▐░╬\033[0m▓█▀▀\033[31m╦╓▒╫▐─▓└▌╘ ▐╒\033[0m█\033[32m▄▓▓▓▓▓▓▌╠╫╢╚╢▌└╙╙│\033[0m█\033[34m═╤▄▄▄ ╟\033[0m ╫█▓▓▌╬╬╠▒░└└ ░╠╬▄"
printf "%b\n" " \033[34m╟▌▀╙▒▓▒▄▄╣\033[0m\033[31m╙▒╟ ▌╠ ╠▐ ╚ ░ ▐░\033[0m█\033[32m▒▒▓▒▓▓▓▓▓▓▀▀╫▓▀▓▀╪\033[0m█\033[34m▄▄▒╔░ ╟\033[0m ▐╣╫▓╣▒╬░╦░░╒░╒╠╣"
printf "%b\n" " \033[34m╞▀╠╠╡▒▓▓█\033[0m█\033[31m▐▒╟ ▒▐ ╠▐ ╠ ▒ ░▐░\033[0m█\033[32m▓╬▓╬▓██▓▓▓▓▓╢▓▀▓╣▓\033[0m█\033[34m▒╢╩╠▓▀▓─\033[0m ╫▓▓█▓╬▒└▒╢╟▒▄▒╣╩▌╖"
printf "%b\n" " \033[34m╞▀╬╣╠▓██\033[0m██\033[31m▐▒╫░▌║░▌░▒╫░▒░▒╫╬\033[0m█\033[32m█▓█▓████▓█▓███▓▓╬▓\033[0m█▓▓\033[34m█╝▀╙\033[0m╠╩╣▒╢▄╔─╩██▓██▓╣▓▓▓▀╙└ └═╓"
printf "%b\n" " ▐\033[34m▓▓▓█\033[0m█████\033[31m▐╬╫╠▌╢▒▌╠▒╣▒▓╣╣▓▓\033[0m█\033[32m██████████████▓█\033[0m████▓▒█╞▌║▒ ▐ ╣╙╩╟▓███▓▓▓╙ ╓ ▀▄"
printf "%b\n" " ▐\033[34m████\033[0m█████\033[31m╣╝▓▓▓╝╬█▓██▓█████\033[0m█\033[32m███▀▀▓▌\033[0m▓╨▓\033[34m╙▌▐░╟─▌╙╙\033[0m▌▒▓╬╫╩╣▓ ▒╙╙▐ ▀═▓████╙ ╔╗▄╚╓╧│╕╡╚▀▄"
printf "%b\n" " ╨▀▀▓╩▄└\033[31m▓ ▓▄╠─╠└└╟▓▒▀\033[0m▀╬▓╟╨▓\033[34m╙▒╟\033[0m ╬ ▌▐ ╠ ▌ ╣\033[0m▐╪╟\033[34m ▌▀▀▓╗▒▄▄▄▄╠\033[0m─▀╤╗▒╓▄ █▓█▌╦╣▓╬╩▒▄┴ ╒└ ═ ─ ═"
printf "%b\n" " ▓█╥╡▐▒▐ \033[31m╟ ╠ ╩ ╠ ╞ │╗╞ ╠▒▒\033[0m╫\033[34m╝▌╟▐─▌▐─╬ ░║ ▒ ╠╘ ╠ ▌ ▒\033[0m▐░\033[34m╟░▓▓╬▓▄▒▄▒░\033[0m ╟▒▀║╣╣╓▌┌▓█╣▓██▓▓╣▒║╙══ ╒ ▐ ╔╜└═"
printf "%b\n" " ▌╫\033[31m ▌▐─▐ ╙ ░ ░ │ ╠ ║ ╞ ╠╣▓\033[0m█\033[34m▐▌╟▐ ▌▐ ╬ ░║ ▒ ╠▐ ╠ ▒ ▒▐│║╠▌▄╓▌░▓╬╝╬╠╟\033[0m▒▌▓╬▓▒▌╠▌█████╣╣╠╢┘╙─ ╕ ─ ─╨▄"
printf "%b\n" " ▌\033[31m▓░▌▐═▐ ▐ ░ ▐ │ ╠┌╞ ░░▓▓\033[0m██\033[34m▒▌╟▐─▌▐ ╬ ▒║ ╬ ▒▐╕╠ ▒░▒▐▒╫╬▌┐│▌╩╣╬╠▀▀▓\033[0m▓█╫╢█████████▓╬▓▄▒╞└╚░▌╓ └ ╓┐╔▀"
printf "%b\n" " ▌\033[31m▓░▌╔▒▐░║░▐░╠ ╠ ║│╠░▐▒▓█\033[0m██\033[34m╣▌╟▐▒▌▐░▓▐▒╟╦╬▒╬║╬▓▒▓╣╬╣╬▓╬▓╬╠▓▀╣▀▀▓╪▓\033[0m███████████▓█▓╣▒▒╨▓▄╓▄╩▄▀─╕▄ ╦ ┌└▄"
printf "%b\n" " \033[31m╢▓░▌╠▒║░╟░╢░╟░╟░╫▒╫▒╠▒▓█\033[0m██\033[34m╣▓╫╬▒▓╣╬▓╣╬╣╬▓╬▓╣╬▓╬█▓██▓█╣█╣╢▓╬▓╬╢▓╬▓\033[0m█╩╬▓╫╬╩▀███▓▒▓▒╠╫▄░╓╝ ╛╓╓┘ ╘ ▄"
printf "%b\n" " \033[31m╫█▒▌╬▒╫▒╫▒╣╠╫▒╫▒╫╠╫╬╢▒█\033[0m███\033[34m╣██▓▓██▓█▓▓█\033[0m████████████▀▀▀▀╠▓╚╝██╩╠▌▀▀▀█╬▓▒▀ ╙█▓█╬█▒░░▓ ╕ ╓─ ═┐╗ ▐▀"
printf "%b\n" " ▌\033[31m█╣▓╬▓▓▓█▓▓▓▓▓▓▓█▓███▓\033[0m████\033[34m███\033[0m████▀▀▀▀╠▌╩╫╜╬╙███──╫ ╟╟▀ ╠ ╫ ╟╠ ▌╙██▓╡▄ ▀▓╠╠╬╬╜╙▀═▌ ─ ▐└└ ╖└▌ ▌"
printf "%b\n" " ╣█\033[31m╫█▓▓╝\033[0m▀▀▀█▓▓████████▓▌╫▀╫╛▐▒▀─╙─├ ▐ ▒▒╞ ██▒╗╗╣╗╝╞▀▀╠╙╙█─└╙┘╟╠═╬═╣██▌╕▌ ╙▌╜╩▒╔╔▌ ╙└═▄═└╕ ╡ └ └╣"
printf "%b\n" " ╟▌─╫─▌▓ ╟ ╟███─▌▐▒▐─└ └─ │ ▌▒│ ██╡ ▐ ╙╞╥┌╠╓▄▓╓╓┌┐╠╞ ▌ ╟▓╬╫▌ ▓ └╡░░╠ └▀▀└└═╜ ╔╓╬─"
printf "%b\n" " ╣▄└═╥▓─╒┐╓╓ ╟▄╗╣╗╡▌▀▀▀╙╫╝╣█─▌▐▒▐ │ ═─╞ ██┤▀╚╠╙╟╫╙└╬╤ █╓└▒▒╚╞╙│└▓▒╚╚█▌ █ ╪▒╣▒╕ └▒╙ ╙▀╪▄┌▄▀▀▀▌ ▐═"
printf "%b\n" " ╬ ┐ ╙▓═╩╠▓█░ ▐ │▌╓▐▄▄╫─╟█ ▌╞▒▐ └ ╒ ╒ ─ ═ ╞░██╪▀▀╣▀╣╞╩╙╠╙╚█╠ ─▒╡╟╠▓▒▓█▓╩╠╟▌╙▓▌╚╨╠▒╙▀▌│ │╠ ▒┌▄ ╓╩▀▓"
printf "%b\n" " ▀┐ ▀▌╥╠█╬╠╟└╫▌ ╠┌ ╫▒║█▒▌▐▒▐▒▐─▐ ─▐ ░ ▒░▒░║░██▓▄╢▓▐▓╞▓╬▓▄▄█╝╪╬▓╣█╫▓▄█▀╙ ╙▓╣▓ ╚╕░▀▄▒▄▄▓░░┐─└▐╙╙╠┌░▌"
printf "%b\n" " ╙▀▒▒▒╧╣█▓█▀▀╟▀╠▌╙╠││▓▒╫█▒▌▐▒▐▒▐▒▐▒▐▒▐▒░░╔░╔▒░▒▐▒▒╟▒██▓▄▄╫▄╣╫▌╗▓╣╗█▓▒╬▓▓█╝▌╝ ╙▄╟▌ ╔▌░░╙╣▒▓▓▒░╔▄╣░░╠█╣▀"
printf "%b\n" " ▀▓▓▓▓██▓▄█╣▌█▀╣╝▀╫███▒▌║▒║▒▐▒║▒║▒║▒╣▒║▒║▒╠▒║▒▒╢╬████▓█▓▓╝╝╝█▓╬▓█████▄═▌┌ ▄▀╚ ══╙▄█▌ ╟▓▒▒▒▒▌└╙╠▌░▒╙╟╬╬▌"
printf "%b\n" " ╙╬▒═╝████▓▓▓▓▓▓╬▓╬╬████▌█╣▓╣╩╝╫╫▓▀▀╬╠▓▓▓▓╫╝╨╬╬▓╬▓╛ ░░╙└╙╖▄▓▓▄▓▓██▀╙▄═│─╟▀ ┌ ▄╛╟▌█▓▄▒▄▓╫╫▒║▓▓▄╬▓█▄▄█╬▓"
printf "%b\n" " └▀╗▄▄╫█▓▌╜╜╙╙╙╚╚╛└ ▐▓▓▓▄▄▄╟▓▓▓║▄╗▄█╩▀▓▀▓██╘▓▀╝╫████╞╬█▄▄▄╠╠╠████▀╠█╛░▄▄▄▒▄░╓▄██▓▌╩██▓▄▄▓▀╫╬╣▒╠▓▓█╬╣╬╬█"
printf "%b\n" " ╙▀█▓░▓▓▓██╣╢███▌▄╟╝██▒╩▓█▄██▓█▌▌╢╣▓▄╣███╬ ╠░██▌╣░╟╫█████▀▄╧└ └╙╠╩ ╙╙│▄╩╣└▀▀▓█▓╬▓╠▒▓╫▓█▓█╣▓╬╣█▀"
printf "%b\n" " █████▐▒╙▒██▐▄╙▒██▐▒╟╟██████╙▒╬██████▄▓▄████▄▓▓█████▀└ ▄──│ ┌ ▄█▓█┌╦▓█▓██▓▓▌╠╬██▓█▓▓█▓▀"
printf "%b\n" " ╙╝███▄▓████▓▓██████████████████▓╬╬╠████▀▀▓▓▓████▌ ╓═░ ▄▄██▒▒░░▄▓█╬╣└└╩╟▓╣█▓╠███▓╬╬▓████╩─"
printf "%b\n" " ▀█████████████▓▓▓█████▀▀╬╬╬╬╬╬╬╬╣█▓▒░▄▄▓▓▀╬▓█▓▀▓╬╚╩╢▒▓▓████▓╬█▄▓██╬╠╣██╬▓███▒╚█▓╝▀"
printf "%b\n" " └╙╝▓▓█▓╬╬╬█▒╙╠╬╣███▓╬╬╢▒╬▄▓██╬╡▒╠╠╩╩╙░░░║▓▒╠╠▄▓██████╬╠╠╬█╬╬╬╬█▓██████▓██╩▌═╙"
printf "%b\n" " └╙╝█▓╬╬╢▓╬╬╬╫▓╬███████████▓▄▄░▄▒▄▄▒╬███████╣╬╬╣█╬██████████████╬█╝▀╙└"
printf "%b\n" " ╙▀▓▓╣▓▓▓█████████████████▓╬╬╫╬▓▓██████████████████▓██╝▀▀└"
printf "%b\n" " └╙╝╬▓▓▓█▓▓▓████████████████████████████▓▀▀▀╙╙└"
printf "%b\n" " └╙▀▀▀▀▀▀▀▀╙████╩▌╙ └████▀"
printf "%b\n" " ╟██▄ ▌ ╙██▓╦▌"
printf "%b\n" " ██░╙─ ▀▓╬╦╙╖"
printf "%b\n" " ╟▓▒ ▌ └╬▓╬╦╙"
printf "%b\n" " ▐╩╬─╞ ▀╬▓▓ ╘"
printf "%b\n" " ▐▒╬╬ ▄ ▓╬█▓ ╣▒╗─┐┌"
printf "%b\n" " ▐╬▓▓░╟ ╙▀╚╙█▓▄╙▀╬╩╙┘└└└╙╙╙╙╚▀▀▒╥"
printf "%b\n" " ──────░░░░░░░░▒▒▒▒▒▒▒▒╗▄▄▄▄▄▄▄▓█▓██▄╙▀▓╣╣╗╗╡╚╚╚╠╠╠▀▀▀▀▓▓╗▄▄▄▄▄▄╦╖╖╓ ▌"
printf "%b\n" " ╙╙╙╙╙╙╚╚╩╩╩▀▀▀▀▀▀▀▀██▓▓▄▄▄╓╔┬┌ ╓▄▄╝╝╗"
printf "%b\n" " └└╙└╙╙└"
echo "░▒▓█▓▒░░▒▓█▓▒░░▒▓█▓▒░░▒▓██████▓▒░▒▓████████▓▒░▒▓██████▓▒░░▒▓█▓▒░░▒▓█▓▒░▒▓███████▓▒░░▒▓█▓▒░░▒▓█▓▒░░▒▓██████▓▒░░▒▓█▓▒░░▒▓█▓▒░ "
echo "░▒▓█▓▒░░▒▓█▓▒░░▒▓█▓▒░▒▓█▓▒░░▒▓█▓▒░ ░▒▓█▓▒░ ░▒▓█▓▒░░▒▓█▓▒░▒▓█▓▒░░▒▓█▓▒░▒▓█▓▒░░▒▓█▓▒░▒▓█▓▒░░▒▓█▓▒░▒▓█▓▒░░▒▓█▓▒░▒▓█▓▒░░▒▓█▓▒░ "
echo "░▒▓█▓▒░░▒▓█▓▒░░▒▓█▓▒░▒▓█▓▒░░▒▓█▓▒░ ░▒▓█▓▒░ ░▒▓█▓▒░ ░▒▓█▓▒░░▒▓█▓▒░▒▓█▓▒░░▒▓█▓▒░▒▓█▓▒░░▒▓█▓▒░▒▓█▓▒░ ░▒▓█▓▒░░▒▓█▓▒░ "
echo "░▒▓█▓▒░░▒▓█▓▒░░▒▓█▓▒░▒▓████████▓▒░ ░▒▓█▓▒░ ░▒▓█▓▒░ ░▒▓████████▓▒░▒▓█▓▒░░▒▓█▓▒░▒▓█▓▒░░▒▓█▓▒░▒▓█▓▒░ ░▒▓███████▓▒░ "
echo "░▒▓█▓▒░░▒▓█▓▒░░▒▓█▓▒░▒▓█▓▒░░▒▓█▓▒░ ░▒▓█▓▒░ ░▒▓█▓▒░ ░▒▓█▓▒░░▒▓█▓▒░▒▓█▓▒░░▒▓█▓▒░▒▓█▓▒░░▒▓█▓▒░▒▓█▓▒░ ░▒▓█▓▒░░▒▓█▓▒░ "
echo "░▒▓█▓▒░░▒▓█▓▒░░▒▓█▓▒░▒▓█▓▒░░▒▓█▓▒░ ░▒▓█▓▒░ ░▒▓█▓▒░░▒▓█▓▒░▒▓█▓▒░░▒▓█▓▒░▒▓█▓▒░░▒▓█▓▒░▒▓█▓▒░░▒▓█▓▒░▒▓█▓▒░░▒▓█▓▒░▒▓█▓▒░░▒▓█▓▒░ "
echo " ░▒▓█████████████▓▒░░▒▓█▓▒░░▒▓█▓▒░ ░▒▓█▓▒░ ░▒▓██████▓▒░░▒▓█▓▒░░▒▓█▓▒░▒▓███████▓▒░ ░▒▓██████▓▒░ ░▒▓██████▓▒░░▒▓█▓▒░░▒▓█▓▒░ "
# Declare an associative array for container names and their health check URLs
typeset -A containers
containers["portainer"]="https://192.168.1.100:9443"
# containers["homeassistant"]="http://192.168.1.100:8123"
# Add more containers here
RESTART_THRESHOLD=3 # Number of consecutive failures before a container is restarted
WAIT_AFTER_RESTART=30 # Seconds to wait after restarting a container for services to stabilize
# Basically a logtee for dummie dumdums
log_message() {
echo "$1"
logger -t "Docker containers Watchdog" -p "daemon.warn" "$1"
}
# Function to monitor and restart a container if necessary
monitor_container() {
local container_name=$1
local health_check_url=$2
local failures=0
while true; do
if curl --insecure --silent --fail $health_check_url > /dev/null; then
# Health check passed
failures=0
else
# Health check failed
((failures++))
log_message "Health check failed for $container_name ($failures)"
fi
# Check if failures reached the threshold
if [ $failures -ge $RESTART_THRESHOLD ]; then
log_message "Restarting $container_name due to health check failure."
docker restart $container_name
failures=0 # Reset failures after restart
log_message "Waiting $WAIT_AFTER_RESTART seconds for $container_name services to stabilize after restart."
sleep $WAIT_AFTER_RESTART
fi
sleep 60 # Wait for 60 seconds before the next check
done
}
# Soooo brutal \m/
kill_all_children() {
log_message "Killing all children"
pkill -P $$
exit # Ensure exit on SIGINT trap catch
}
trap kill_all_children SIGINT EXIT
# Loop through all containers and start monitoring in the background
for container_name in "${(@k)containers}"; do
monitor_container $container_name ${containers[$container_name]} &
done
wait # Wait for all background jobs to finish (they won't in this case)