watchdog.sh 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338
  1. #!/bin/bash
  2. trap "exit" INT TERM
  3. trap "kill 0" EXIT
  4. PARENT_PID=$$
  5. # Prepare
  6. BACKGROUND_TASKS=()
  7. if [[ "${USE_WATCHDOG}" =~ ^([nN][oO]|[nN])+$ ]]; then
  8. echo -e "$(date) - USE_WATCHDOG=n, skipping watchdog..."
  9. sleep 365d
  10. exec $(readlink -f "$0")
  11. fi
  12. # Checks pipe their corresponding container name in this pipe
  13. if [[ ! -p /tmp/com_pipe ]]; then
  14. mkfifo /tmp/com_pipe
  15. fi
  16. # Common functions
  17. progress() {
  18. SERVICE=${1}
  19. TOTAL=${2}
  20. CURRENT=${3}
  21. DIFF=${4}
  22. [[ -z ${DIFF} ]] && DIFF=0
  23. [[ -z ${TOTAL} || -z ${CURRENT} ]] && return
  24. [[ ${CURRENT} -gt ${TOTAL} ]] && return
  25. [[ ${CURRENT} -lt 0 ]] && CURRENT=0
  26. PERCENT=$(( 200 * ${CURRENT} / ${TOTAL} % 2 + 100 * ${CURRENT} / ${TOTAL} ))
  27. echo -ne "$(date) - ${SERVICE} health level: \e[7m${PERCENT}%\e[0m (${CURRENT}/${TOTAL}), health trend: "
  28. [[ ${DIFF} =~ ^-[1-9] ]] && echo -en '[\e[41m \e[0m] ' || echo -en '[\e[42m \e[0m] '
  29. echo "(${DIFF})"
  30. }
  31. get_container_ip() {
  32. # ${1} is container
  33. CONTAINER_ID=
  34. CONTAINER_IP=
  35. LOOP_C=1
  36. until [[ ${CONTAINER_IP} =~ ^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}$ ]] || [[ ${LOOP_C} -gt 5 ]]; do
  37. sleep 1
  38. CONTAINER_ID=$(curl --silent --unix-socket /var/run/docker.sock http/containers/json?all=1 | jq -rc "map(select(.Names[] | contains (\"${1}\"))) | .[] .Id")
  39. CONTAINER_IP=$(curl --silent --unix-socket /var/run/docker.sock http/containers/${CONTAINER_ID}/json | jq -r '.NetworkSettings.Networks[].IPAddress')
  40. LOOP_C=$((LOOP_C + 1))
  41. done
  42. [[ ${LOOP_C} -gt 5 ]] && echo 240.0.0.0 || echo ${CONTAINER_IP}
  43. }
  44. # Check functions
  45. nginx_checks() {
  46. err_count=0
  47. diff_c=0
  48. THRESHOLD=16
  49. # Reduce error count by 2 after restarting an unhealthy container
  50. trap "[ ${err_count} -gt 1 ] && err_count=$(( ${err_count} - 2 ))" USR1
  51. while [ ${err_count} -lt ${THRESHOLD} ]; do
  52. host_ip=$(get_container_ip nginx-mailcow)
  53. err_c_cur=${err_count}
  54. /usr/lib/nagios/plugins/check_ping -4 -H ${host_ip} -w 2000,10% -c 4000,100% -p2 1>&2; err_count=$(( ${err_count} + $? ))
  55. /usr/lib/nagios/plugins/check_http -4 -H ${host_ip} -u / -p 8081 1>&2; err_count=$(( ${err_count} + $? ))
  56. [ ${err_c_cur} -eq ${err_count} ] && [ ! $((${err_count} - 1)) -lt 0 ] && err_count=$((${err_count} - 1)) diff_c=1
  57. [ ${err_c_cur} -ne ${err_count} ] && diff_c=$(( ${err_c_cur} - ${err_count} ))
  58. progress "Nginx" ${THRESHOLD} $(( ${THRESHOLD} - ${err_count} )) ${diff_c}
  59. diff_c=0
  60. sleep $(( ( RANDOM % 30 ) + 10 ))
  61. done
  62. return 1
  63. }
  64. mysql_checks() {
  65. err_count=0
  66. diff_c=0
  67. THRESHOLD=12
  68. # Reduce error count by 2 after restarting an unhealthy container
  69. trap "[ ${err_count} -gt 1 ] && err_count=$(( ${err_count} - 2 ))" USR1
  70. while [ ${err_count} -lt ${THRESHOLD} ]; do
  71. host_ip=$(get_container_ip mysql-mailcow)
  72. err_c_cur=${err_count}
  73. /usr/lib/nagios/plugins/check_mysql -H ${host_ip} -P 3306 -u ${DBUSER} -p ${DBPASS} -d ${DBNAME} 1>&2; err_count=$(( ${err_count} + $? ))
  74. /usr/lib/nagios/plugins/check_mysql_query -H ${host_ip} -P 3306 -u ${DBUSER} -p ${DBPASS} -d ${DBNAME} -q "SELECT COUNT(*) FROM information_schema.tables" 1>&2; err_count=$(( ${err_count} + $? ))
  75. [ ${err_c_cur} -eq ${err_count} ] && [ ! $((${err_count} - 1)) -lt 0 ] && err_count=$((${err_count} - 1)) diff_c=1
  76. [ ${err_c_cur} -ne ${err_count} ] && diff_c=$(( ${err_c_cur} - ${err_count} ))
  77. progress "MySQL/MariaDB" ${THRESHOLD} $(( ${THRESHOLD} - ${err_count} )) ${diff_c}
  78. diff_c=0
  79. sleep $(( ( RANDOM % 30 ) + 10 ))
  80. done
  81. return 1
  82. }
  83. sogo_checks() {
  84. err_count=0
  85. diff_c=0
  86. THRESHOLD=20
  87. # Reduce error count by 2 after restarting an unhealthy container
  88. trap "[ ${err_count} -gt 1 ] && err_count=$(( ${err_count} - 2 ))" USR1
  89. while [ ${err_count} -lt ${THRESHOLD} ]; do
  90. host_ip=$(get_container_ip sogo-mailcow)
  91. err_c_cur=${err_count}
  92. /usr/lib/nagios/plugins/check_http -4 -H ${host_ip} -u /WebServerResources/css/theme-default.css -p 9192 -R md-default-theme 1>&2; err_count=$(( ${err_count} + $? ))
  93. /usr/lib/nagios/plugins/check_http -4 -H ${host_ip} -u /SOGo.index/ -p 20000 -R "SOGo\sGroupware" 1>&2; err_count=$(( ${err_count} + $? ))
  94. [ ${err_c_cur} -eq ${err_count} ] && [ ! $((${err_count} - 1)) -lt 0 ] && err_count=$((${err_count} - 1)) diff_c=1
  95. [ ${err_c_cur} -ne ${err_count} ] && diff_c=$(( ${err_c_cur} - ${err_count} ))
  96. progress "SOGo" ${THRESHOLD} $(( ${THRESHOLD} - ${err_count} )) ${diff_c}
  97. diff_c=0
  98. sleep $(( ( RANDOM % 30 ) + 10 ))
  99. done
  100. return 1
  101. }
  102. postfix_checks() {
  103. err_count=0
  104. diff_c=0
  105. THRESHOLD=16
  106. # Reduce error count by 2 after restarting an unhealthy container
  107. trap "[ ${err_count} -gt 1 ] && err_count=$(( ${err_count} - 2 ))" USR1
  108. while [ ${err_count} -lt ${THRESHOLD} ]; do
  109. host_ip=$(get_container_ip postfix-mailcow)
  110. err_c_cur=${err_count}
  111. /usr/lib/nagios/plugins/check_smtp -4 -H ${host_ip} -p 589 -f watchdog -C "RCPT TO:null@localhost" -C DATA -C . -R 250 1>&2; err_count=$(( ${err_count} + $? ))
  112. /usr/lib/nagios/plugins/check_smtp -4 -H ${host_ip} -p 589 -S 1>&2; err_count=$(( ${err_count} + $? ))
  113. [ ${err_c_cur} -eq ${err_count} ] && [ ! $((${err_count} - 1)) -lt 0 ] && err_count=$((${err_count} - 1)) diff_c=1
  114. [ ${err_c_cur} -ne ${err_count} ] && diff_c=$(( ${err_c_cur} - ${err_count} ))
  115. progress "Postfix" ${THRESHOLD} $(( ${THRESHOLD} - ${err_count} )) ${diff_c}
  116. diff_c=0
  117. sleep $(( ( RANDOM % 30 ) + 10 ))
  118. done
  119. return 1
  120. }
  121. dovecot_checks() {
  122. err_count=0
  123. diff_c=0
  124. THRESHOLD=24
  125. # Reduce error count by 2 after restarting an unhealthy container
  126. trap "[ ${err_count} -gt 1 ] && err_count=$(( ${err_count} - 2 ))" USR1
  127. while [ ${err_count} -lt ${THRESHOLD} ]; do
  128. host_ip=$(get_container_ip dovecot-mailcow)
  129. err_c_cur=${err_count}
  130. /usr/lib/nagios/plugins/check_smtp -4 -H ${host_ip} -p 24 -f "watchdog" -C "RCPT TO:<watchdog@invalid>" -L -R "User doesn't exist" 1>&2; err_count=$(( ${err_count} + $? ))
  131. /usr/lib/nagios/plugins/check_imap -4 -H ${host_ip} -p 993 -S -e "OK " 1>&2; err_count=$(( ${err_count} + $? ))
  132. /usr/lib/nagios/plugins/check_imap -4 -H ${host_ip} -p 143 -e "OK " 1>&2; err_count=$(( ${err_count} + $? ))
  133. /usr/lib/nagios/plugins/check_tcp -4 -H ${host_ip} -p 10001 -e "VERSION" 1>&2; err_count=$(( ${err_count} + $? ))
  134. /usr/lib/nagios/plugins/check_tcp -4 -H ${host_ip} -p 4190 -e "Dovecot ready" 1>&2; err_count=$(( ${err_count} + $? ))
  135. [ ${err_c_cur} -eq ${err_count} ] && [ ! $((${err_count} - 1)) -lt 0 ] && err_count=$((${err_count} - 1)) diff_c=1
  136. [ ${err_c_cur} -ne ${err_count} ] && diff_c=$(( ${err_c_cur} - ${err_count} ))
  137. progress "Dovecot" ${THRESHOLD} $(( ${THRESHOLD} - ${err_count} )) ${diff_c}
  138. diff_c=0
  139. sleep $(( ( RANDOM % 30 ) + 10 ))
  140. done
  141. return 1
  142. }
  143. phpfpm_checks() {
  144. err_count=0
  145. diff_c=0
  146. THRESHOLD=10
  147. # Reduce error count by 2 after restarting an unhealthy container
  148. trap "[ ${err_count} -gt 1 ] && err_count=$(( ${err_count} - 2 ))" USR1
  149. while [ ${err_count} -lt ${THRESHOLD} ]; do
  150. host_ip=$(get_container_ip php-fpm-mailcow)
  151. err_c_cur=${err_count}
  152. cgi-fcgi -bind -connect ${host_ip}:9000 | grep PHP 1>&2; err_count=$(( ${err_count} + ($? * 2)))
  153. /usr/lib/nagios/plugins/check_ping -4 -H ${host_ip} -w 2000,10% -c 4000,100% -p2 1>&2; err_count=$(( ${err_count} + $? ))
  154. [ ${err_c_cur} -eq ${err_count} ] && [ ! $((${err_count} - 1)) -lt 0 ] && err_count=$((${err_count} - 1)) diff_c=1
  155. [ ${err_c_cur} -ne ${err_count} ] && diff_c=$(( ${err_c_cur} - ${err_count} ))
  156. progress "PHP-FPM" ${THRESHOLD} $(( ${THRESHOLD} - ${err_count} )) ${diff_c}
  157. diff_c=0
  158. sleep $(( ( RANDOM % 30 ) + 10 ))
  159. done
  160. return 1
  161. }
  162. rspamd_checks() {
  163. err_count=0
  164. diff_c=0
  165. THRESHOLD=10
  166. # Reduce error count by 2 after restarting an unhealthy container
  167. trap "[ ${err_count} -gt 1 ] && err_count=$(( ${err_count} - 2 ))" USR1
  168. while [ ${err_count} -lt ${THRESHOLD} ]; do
  169. host_ip=$(get_container_ip rspamd-mailcow)
  170. err_c_cur=${err_count}
  171. SCORE=$(curl --silent ${host_ip}:11333/scan -d '
  172. To: null@localhost
  173. From: watchdog@localhost
  174. Empty
  175. ' | jq -rc .required_score)
  176. if [[ ${SCORE} != "9999" ]]; then
  177. echo "Rspamd settings check failed" 1>&2
  178. err_count=$(( ${err_count} + 1))
  179. else
  180. echo "Rspamd settings check succeeded" 1>&2
  181. fi
  182. /usr/lib/nagios/plugins/check_ping -4 -H ${host_ip} -w 2000,10% -c 4000,100% -p2 1>&2; err_count=$(( ${err_count} + $? ))
  183. [ ${err_c_cur} -eq ${err_count} ] && [ ! $((${err_count} - 1)) -lt 0 ] && err_count=$((${err_count} - 1)) diff_c=1
  184. [ ${err_c_cur} -ne ${err_count} ] && diff_c=$(( ${err_c_cur} - ${err_count} ))
  185. progress "Rspamd" ${THRESHOLD} $(( ${THRESHOLD} - ${err_count} )) ${diff_c}
  186. diff_c=0
  187. sleep $(( ( RANDOM % 30 ) + 10 ))
  188. done
  189. return 1
  190. }
  191. dns_checks() {
  192. err_count=0
  193. diff_c=0
  194. THRESHOLD=28
  195. # Reduce error count by 2 after restarting an unhealthy container
  196. trap "[ ${err_count} -gt 1 ] && err_count=$(( ${err_count} - 2 ))" USR1
  197. while [ ${err_count} -lt ${THRESHOLD} ]; do
  198. host_ip=$(get_container_ip unbound-mailcow)
  199. err_c_cur=${err_count}
  200. /usr/lib/nagios/plugins/check_dns -H google.com 1>&2; err_count=$(( ${err_count} + ($? * 2)))
  201. /usr/lib/nagios/plugins/check_dns -s ${host_ip} -H google.com 1>&2; err_count=$(( ${err_count} + ($? * 2)))
  202. dig +dnssec org. @${host_ip} | grep -E 'flags:.+ad' 1>&2; err_count=$(( ${err_count} + ($? * 2)))
  203. [ ${err_c_cur} -eq ${err_count} ] && [ ! $((${err_count} - 1)) -lt 0 ] && err_count=$((${err_count} - 1)) diff_c=1
  204. [ ${err_c_cur} -ne ${err_count} ] && diff_c=$(( ${err_c_cur} - ${err_count} ))
  205. progress "Unbound" ${THRESHOLD} $(( ${THRESHOLD} - ${err_count} )) ${diff_c}
  206. diff_c=0
  207. sleep $(( ( RANDOM % 30 ) + 10 ))
  208. done
  209. return 1
  210. }
  211. # Create watchdog agents
  212. (
  213. while true; do
  214. if ! nginx_checks; then
  215. echo -e "\e[31m$(date) - Nginx hit error limit\e[0m"
  216. echo nginx-mailcow > /tmp/com_pipe
  217. fi
  218. done
  219. ) &
  220. BACKGROUND_TASKS+=($!)
  221. (
  222. while true; do
  223. if ! mysql_checks; then
  224. echo -e "\e[31m$(date) - MySQL hit error limit\e[0m"
  225. echo mysql-mailcow > /tmp/com_pipe
  226. fi
  227. done
  228. ) &
  229. BACKGROUND_TASKS+=($!)
  230. (
  231. while true; do
  232. if ! phpfpm_checks; then
  233. echo -e "\e[31m$(date) - PHP-FPM hit error limit\e[0m"
  234. echo php-fpm-mailcow > /tmp/com_pipe
  235. fi
  236. done
  237. ) &
  238. BACKGROUND_TASKS+=($!)
  239. (
  240. while true; do
  241. if ! sogo_checks; then
  242. echo -e "\e[31m$(date) - SOGo hit error limit\e[0m"
  243. echo sogo-mailcow > /tmp/com_pipe
  244. fi
  245. done
  246. ) &
  247. BACKGROUND_TASKS+=($!)
  248. (
  249. while true; do
  250. if ! postfix_checks; then
  251. echo -e "\e[31m$(date) - Postfix hit error limit\e[0m"
  252. echo postfix-mailcow > /tmp/com_pipe
  253. fi
  254. done
  255. ) &
  256. BACKGROUND_TASKS+=($!)
  257. (
  258. while true; do
  259. if ! dovecot_checks; then
  260. echo -e "\e[31m$(date) - Dovecot hit error limit\e[0m"
  261. echo dovecot-mailcow > /tmp/com_pipe
  262. fi
  263. done
  264. ) &
  265. BACKGROUND_TASKS+=($!)
  266. (
  267. while true; do
  268. if ! dns_checks; then
  269. echo -e "\e[31m$(date) - Unbound hit error limit\e[0m"
  270. #echo unbound-mailcow > /tmp/com_pipe
  271. fi
  272. done
  273. ) &
  274. BACKGROUND_TASKS+=($!)
  275. (
  276. while true; do
  277. if ! rspamd_checks; then
  278. echo -e "\e[31m$(date) - Rspamd hit error limit\e[0m"
  279. echo rspamd-mailcow > /tmp/com_pipe
  280. fi
  281. done
  282. ) &
  283. BACKGROUND_TASKS+=($!)
  284. # Monitor watchdog agents, stop script when agents fails and wait for respawn by Docker (restart:always:n)
  285. (
  286. while true; do
  287. for bg_task in ${BACKGROUND_TASKS[*]}; do
  288. if ! kill -0 ${bg_task} 21>&2; then
  289. echo "Worker ${bg_task} died, stopping watchdog and waiting for respawn..."
  290. kill -TERM ${PARENT_PID}
  291. fi
  292. sleep 1
  293. done
  294. done
  295. ) &
  296. # Restart container when threshold limit reached
  297. while true; do
  298. CONTAINER_ID=
  299. read com_pipe_answer </tmp/com_pipe
  300. if [[ ${com_pipe_answer} =~ .+-mailcow ]]; then
  301. kill -STOP ${BACKGROUND_TASKS[*]}
  302. sleep 3
  303. CONTAINER_ID=$(curl --silent --unix-socket /var/run/docker.sock http/containers/json?all=1 | jq -rc "map(select(.Names[] | contains (\"${com_pipe_answer}\"))) | .[] .Id")
  304. if [[ ! -z ${CONTAINER_ID} ]]; then
  305. echo "Sending restart command to ${CONTAINER_ID}..."
  306. curl --silent --unix-socket /var/run/docker.sock -XPOST http/containers/${CONTAINER_ID}/restart
  307. fi
  308. echo "Wait for restarted container to settle and continue watching..."
  309. sleep 30s
  310. kill -CONT ${BACKGROUND_TASKS[*]}
  311. kill -USR1 ${BACKGROUND_TASKS[*]}
  312. fi
  313. done