watchdog.sh 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382
  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. log_to_redis() {
  32. redis-cli -h redis LPUSH WATCHDOG_LOG "{\"time\":\"$(date +%s)\",\"message\":\"$(printf '%s' "${1}")\"}"
  33. }
  34. function mail_error() {
  35. [[ -z ${1} ]] && return 1
  36. [[ -z ${2} ]] && return 2
  37. RCPT_DOMAIN=$(echo ${1} | awk -F @ {'print $NF'})
  38. RCPT_MX=$(dig +short ${RCPT_DOMAIN} mx | sort -n | awk '{print $2; exit}')
  39. if [[ -z ${RCPT_MX} ]]; then
  40. log_to_redis "Cannot determine MX for ${1}, skipping email notification..."
  41. echo "Cannot determine MX for ${1}"
  42. return 1
  43. fi
  44. ./smtp-cli --missing-modules-ok \
  45. --subject="Watchdog: ${2} service hit the error rate limit" \
  46. --body-plain="Service was restarted, please check your mailcow installation." \
  47. --to=${1} \
  48. --from="watchdog@${MAILCOW_HOSTNAME}" \
  49. --server="${RCPT_MX}" \
  50. --hello-host=${MAILCOW_HOSTNAME}
  51. }
  52. get_container_ip() {
  53. # ${1} is container
  54. CONTAINER_ID=
  55. CONTAINER_IP=
  56. LOOP_C=1
  57. until [[ ${CONTAINER_IP} =~ ^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}$ ]] || [[ ${LOOP_C} -gt 5 ]]; do
  58. sleep 1
  59. CONTAINER_ID=$(curl --silent http://dockerapi:8080/containers/json | jq -r ".[] | {name: .Config.Labels[\"com.docker.compose.service\"], id: .Id}" | jq -rc "select( .name | tostring | contains(\"${1}\")) | .id")
  60. if [[ ! -z ${CONTAINER_ID} ]]; then
  61. CONTAINER_IP=$(curl --silent http://dockerapi:8080/containers/${CONTAINER_ID}/json | jq -r '.NetworkSettings.Networks[].IPAddress')
  62. fi
  63. LOOP_C=$((LOOP_C + 1))
  64. done
  65. [[ ${LOOP_C} -gt 5 ]] && echo 240.0.0.0 || echo ${CONTAINER_IP}
  66. }
  67. # Check functions
  68. nginx_checks() {
  69. err_count=0
  70. diff_c=0
  71. THRESHOLD=16
  72. # Reduce error count by 2 after restarting an unhealthy container
  73. trap "[ ${err_count} -gt 1 ] && err_count=$(( ${err_count} - 2 ))" USR1
  74. while [ ${err_count} -lt ${THRESHOLD} ]; do
  75. host_ip=$(get_container_ip nginx-mailcow)
  76. err_c_cur=${err_count}
  77. /usr/lib/nagios/plugins/check_ping -4 -H ${host_ip} -w 2000,10% -c 4000,100% -p2 1>&2; err_count=$(( ${err_count} + $? ))
  78. /usr/lib/nagios/plugins/check_http -4 -H ${host_ip} -u / -p 8081 1>&2; err_count=$(( ${err_count} + $? ))
  79. [ ${err_c_cur} -eq ${err_count} ] && [ ! $((${err_count} - 1)) -lt 0 ] && err_count=$((${err_count} - 1)) diff_c=1
  80. [ ${err_c_cur} -ne ${err_count} ] && diff_c=$(( ${err_c_cur} - ${err_count} ))
  81. progress "Nginx" ${THRESHOLD} $(( ${THRESHOLD} - ${err_count} )) ${diff_c}
  82. diff_c=0
  83. sleep $(( ( RANDOM % 30 ) + 10 ))
  84. done
  85. return 1
  86. }
  87. mysql_checks() {
  88. err_count=0
  89. diff_c=0
  90. THRESHOLD=12
  91. # Reduce error count by 2 after restarting an unhealthy container
  92. trap "[ ${err_count} -gt 1 ] && err_count=$(( ${err_count} - 2 ))" USR1
  93. while [ ${err_count} -lt ${THRESHOLD} ]; do
  94. host_ip=$(get_container_ip mysql-mailcow)
  95. err_c_cur=${err_count}
  96. /usr/lib/nagios/plugins/check_mysql -H ${host_ip} -P 3306 -u ${DBUSER} -p ${DBPASS} -d ${DBNAME} 1>&2; err_count=$(( ${err_count} + $? ))
  97. /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} + $? ))
  98. [ ${err_c_cur} -eq ${err_count} ] && [ ! $((${err_count} - 1)) -lt 0 ] && err_count=$((${err_count} - 1)) diff_c=1
  99. [ ${err_c_cur} -ne ${err_count} ] && diff_c=$(( ${err_c_cur} - ${err_count} ))
  100. progress "MySQL/MariaDB" ${THRESHOLD} $(( ${THRESHOLD} - ${err_count} )) ${diff_c}
  101. diff_c=0
  102. sleep $(( ( RANDOM % 30 ) + 10 ))
  103. done
  104. return 1
  105. }
  106. sogo_checks() {
  107. err_count=0
  108. diff_c=0
  109. THRESHOLD=20
  110. # Reduce error count by 2 after restarting an unhealthy container
  111. trap "[ ${err_count} -gt 1 ] && err_count=$(( ${err_count} - 2 ))" USR1
  112. while [ ${err_count} -lt ${THRESHOLD} ]; do
  113. host_ip=$(get_container_ip sogo-mailcow)
  114. err_c_cur=${err_count}
  115. /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} + $? ))
  116. /usr/lib/nagios/plugins/check_http -4 -H ${host_ip} -u /SOGo.index/ -p 20000 -R "SOGo\sGroupware" 1>&2; err_count=$(( ${err_count} + $? ))
  117. [ ${err_c_cur} -eq ${err_count} ] && [ ! $((${err_count} - 1)) -lt 0 ] && err_count=$((${err_count} - 1)) diff_c=1
  118. [ ${err_c_cur} -ne ${err_count} ] && diff_c=$(( ${err_c_cur} - ${err_count} ))
  119. progress "SOGo" ${THRESHOLD} $(( ${THRESHOLD} - ${err_count} )) ${diff_c}
  120. diff_c=0
  121. sleep $(( ( RANDOM % 30 ) + 10 ))
  122. done
  123. return 1
  124. }
  125. postfix_checks() {
  126. err_count=0
  127. diff_c=0
  128. THRESHOLD=16
  129. # Reduce error count by 2 after restarting an unhealthy container
  130. trap "[ ${err_count} -gt 1 ] && err_count=$(( ${err_count} - 2 ))" USR1
  131. while [ ${err_count} -lt ${THRESHOLD} ]; do
  132. host_ip=$(get_container_ip postfix-mailcow)
  133. err_c_cur=${err_count}
  134. /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} + $? ))
  135. /usr/lib/nagios/plugins/check_smtp -4 -H ${host_ip} -p 589 -S 1>&2; err_count=$(( ${err_count} + $? ))
  136. [ ${err_c_cur} -eq ${err_count} ] && [ ! $((${err_count} - 1)) -lt 0 ] && err_count=$((${err_count} - 1)) diff_c=1
  137. [ ${err_c_cur} -ne ${err_count} ] && diff_c=$(( ${err_c_cur} - ${err_count} ))
  138. progress "Postfix" ${THRESHOLD} $(( ${THRESHOLD} - ${err_count} )) ${diff_c}
  139. diff_c=0
  140. sleep $(( ( RANDOM % 30 ) + 10 ))
  141. done
  142. return 1
  143. }
  144. dovecot_checks() {
  145. err_count=0
  146. diff_c=0
  147. THRESHOLD=24
  148. # Reduce error count by 2 after restarting an unhealthy container
  149. trap "[ ${err_count} -gt 1 ] && err_count=$(( ${err_count} - 2 ))" USR1
  150. while [ ${err_count} -lt ${THRESHOLD} ]; do
  151. host_ip=$(get_container_ip dovecot-mailcow)
  152. err_c_cur=${err_count}
  153. /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} + $? ))
  154. /usr/lib/nagios/plugins/check_imap -4 -H ${host_ip} -p 993 -S -e "OK " 1>&2; err_count=$(( ${err_count} + $? ))
  155. /usr/lib/nagios/plugins/check_imap -4 -H ${host_ip} -p 143 -e "OK " 1>&2; err_count=$(( ${err_count} + $? ))
  156. /usr/lib/nagios/plugins/check_tcp -4 -H ${host_ip} -p 10001 -e "VERSION" 1>&2; err_count=$(( ${err_count} + $? ))
  157. /usr/lib/nagios/plugins/check_tcp -4 -H ${host_ip} -p 4190 -e "Dovecot ready" 1>&2; err_count=$(( ${err_count} + $? ))
  158. [ ${err_c_cur} -eq ${err_count} ] && [ ! $((${err_count} - 1)) -lt 0 ] && err_count=$((${err_count} - 1)) diff_c=1
  159. [ ${err_c_cur} -ne ${err_count} ] && diff_c=$(( ${err_c_cur} - ${err_count} ))
  160. progress "Dovecot" ${THRESHOLD} $(( ${THRESHOLD} - ${err_count} )) ${diff_c}
  161. diff_c=0
  162. sleep $(( ( RANDOM % 30 ) + 10 ))
  163. done
  164. return 1
  165. }
  166. phpfpm_checks() {
  167. err_count=0
  168. diff_c=0
  169. THRESHOLD=10
  170. # Reduce error count by 2 after restarting an unhealthy container
  171. trap "[ ${err_count} -gt 1 ] && err_count=$(( ${err_count} - 2 ))" USR1
  172. while [ ${err_count} -lt ${THRESHOLD} ]; do
  173. host_ip=$(get_container_ip php-fpm-mailcow)
  174. err_c_cur=${err_count}
  175. cgi-fcgi -bind -connect ${host_ip}:9000 | grep "Content-type" 1>&2; err_count=$(( ${err_count} + ($? * 2)))
  176. /usr/lib/nagios/plugins/check_ping -4 -H ${host_ip} -w 2000,10% -c 4000,100% -p2 1>&2; err_count=$(( ${err_count} + $? ))
  177. [ ${err_c_cur} -eq ${err_count} ] && [ ! $((${err_count} - 1)) -lt 0 ] && err_count=$((${err_count} - 1)) diff_c=1
  178. [ ${err_c_cur} -ne ${err_count} ] && diff_c=$(( ${err_c_cur} - ${err_count} ))
  179. progress "PHP-FPM" ${THRESHOLD} $(( ${THRESHOLD} - ${err_count} )) ${diff_c}
  180. diff_c=0
  181. sleep $(( ( RANDOM % 30 ) + 10 ))
  182. done
  183. return 1
  184. }
  185. rspamd_checks() {
  186. err_count=0
  187. diff_c=0
  188. THRESHOLD=10
  189. # Reduce error count by 2 after restarting an unhealthy container
  190. trap "[ ${err_count} -gt 1 ] && err_count=$(( ${err_count} - 2 ))" USR1
  191. while [ ${err_count} -lt ${THRESHOLD} ]; do
  192. host_ip=$(get_container_ip rspamd-mailcow)
  193. err_c_cur=${err_count}
  194. SCORE=$(curl --silent ${host_ip}:11333/scan -d '
  195. To: null@localhost
  196. From: watchdog@localhost
  197. Empty
  198. ' | jq -rc .required_score)
  199. if [[ ${SCORE} != "9999" ]]; then
  200. echo "Rspamd settings check failed" 1>&2
  201. err_count=$(( ${err_count} + 1))
  202. else
  203. echo "Rspamd settings check succeeded" 1>&2
  204. fi
  205. /usr/lib/nagios/plugins/check_ping -4 -H ${host_ip} -w 2000,10% -c 4000,100% -p2 1>&2; err_count=$(( ${err_count} + $? ))
  206. [ ${err_c_cur} -eq ${err_count} ] && [ ! $((${err_count} - 1)) -lt 0 ] && err_count=$((${err_count} - 1)) diff_c=1
  207. [ ${err_c_cur} -ne ${err_count} ] && diff_c=$(( ${err_c_cur} - ${err_count} ))
  208. progress "Rspamd" ${THRESHOLD} $(( ${THRESHOLD} - ${err_count} )) ${diff_c}
  209. diff_c=0
  210. sleep $(( ( RANDOM % 30 ) + 10 ))
  211. done
  212. return 1
  213. }
  214. dns_checks() {
  215. err_count=0
  216. diff_c=0
  217. THRESHOLD=28
  218. # Reduce error count by 2 after restarting an unhealthy container
  219. trap "[ ${err_count} -gt 1 ] && err_count=$(( ${err_count} - 2 ))" USR1
  220. while [ ${err_count} -lt ${THRESHOLD} ]; do
  221. host_ip=$(get_container_ip unbound-mailcow)
  222. err_c_cur=${err_count}
  223. /usr/lib/nagios/plugins/check_dns -H google.com 1>&2; err_count=$(( ${err_count} + ($? * 2)))
  224. /usr/lib/nagios/plugins/check_dns -s ${host_ip} -H google.com 1>&2; err_count=$(( ${err_count} + ($? * 2)))
  225. dig +dnssec org. @${host_ip} | grep -E 'flags:.+ad' 1>&2; err_count=$(( ${err_count} + ($? * 2)))
  226. [ ${err_c_cur} -eq ${err_count} ] && [ ! $((${err_count} - 1)) -lt 0 ] && err_count=$((${err_count} - 1)) diff_c=1
  227. [ ${err_c_cur} -ne ${err_count} ] && diff_c=$(( ${err_c_cur} - ${err_count} ))
  228. progress "Unbound" ${THRESHOLD} $(( ${THRESHOLD} - ${err_count} )) ${diff_c}
  229. diff_c=0
  230. sleep $(( ( RANDOM % 30 ) + 10 ))
  231. done
  232. return 1
  233. }
  234. # Create watchdog agents
  235. (
  236. while true; do
  237. if ! nginx_checks; then
  238. log_to_redis "Nginx hit error limit"
  239. [[ ! -z ${WATCHDOG_NOTIFY_EMAIL} ]] && mail_error "${WATCHDOG_NOTIFY_EMAIL}" "nginx-mailcow"
  240. echo -e "\e[31m$(date) - Nginx hit error limit\e[0m"
  241. echo nginx-mailcow > /tmp/com_pipe
  242. fi
  243. done
  244. ) &
  245. BACKGROUND_TASKS+=($!)
  246. (
  247. while true; do
  248. if ! mysql_checks; then
  249. log_to_redis "MySQL hit error limit"
  250. [[ ! -z ${WATCHDOG_NOTIFY_EMAIL} ]] && mail_error "${WATCHDOG_NOTIFY_EMAIL}" "mysql-mailcow"
  251. echo -e "\e[31m$(date) - MySQL hit error limit\e[0m"
  252. echo mysql-mailcow > /tmp/com_pipe
  253. fi
  254. done
  255. ) &
  256. BACKGROUND_TASKS+=($!)
  257. (
  258. while true; do
  259. if ! phpfpm_checks; then
  260. log_to_redis "PHP-FPM hit error limit"
  261. [[ ! -z ${WATCHDOG_NOTIFY_EMAIL} ]] && mail_error "${WATCHDOG_NOTIFY_EMAIL}" "php-fpm-mailcow"
  262. echo -e "\e[31m$(date) - PHP-FPM hit error limit\e[0m"
  263. echo php-fpm-mailcow > /tmp/com_pipe
  264. fi
  265. done
  266. ) &
  267. BACKGROUND_TASKS+=($!)
  268. (
  269. while true; do
  270. if ! sogo_checks; then
  271. log_to_redis "SOGo hit error limit"
  272. [[ ! -z ${WATCHDOG_NOTIFY_EMAIL} ]] && mail_error "${WATCHDOG_NOTIFY_EMAIL}" "sogo-mailcow"
  273. echo -e "\e[31m$(date) - SOGo hit error limit\e[0m"
  274. echo sogo-mailcow > /tmp/com_pipe
  275. fi
  276. done
  277. ) &
  278. BACKGROUND_TASKS+=($!)
  279. (
  280. while true; do
  281. if ! postfix_checks; then
  282. log_to_redis "Postfix hit error limit"
  283. [[ ! -z ${WATCHDOG_NOTIFY_EMAIL} ]] && mail_error "${WATCHDOG_NOTIFY_EMAIL}" "postfix-mailcow"
  284. echo -e "\e[31m$(date) - Postfix hit error limit\e[0m"
  285. echo postfix-mailcow > /tmp/com_pipe
  286. fi
  287. done
  288. ) &
  289. BACKGROUND_TASKS+=($!)
  290. (
  291. while true; do
  292. if ! dovecot_checks; then
  293. log_to_redis "Dovecot hit error limit"
  294. [[ ! -z ${WATCHDOG_NOTIFY_EMAIL} ]] && mail_error "${WATCHDOG_NOTIFY_EMAIL}" "dovecot-mailcow"
  295. echo -e "\e[31m$(date) - Dovecot hit error limit\e[0m"
  296. echo dovecot-mailcow > /tmp/com_pipe
  297. fi
  298. done
  299. ) &
  300. BACKGROUND_TASKS+=($!)
  301. (
  302. while true; do
  303. if ! dns_checks; then
  304. log_to_redis "Unbound hit error limit"
  305. [[ ! -z ${WATCHDOG_NOTIFY_EMAIL} ]] && mail_error "${WATCHDOG_NOTIFY_EMAIL}" "unbound-mailcow"
  306. echo -e "\e[31m$(date) - Unbound hit error limit\e[0m"
  307. #echo unbound-mailcow > /tmp/com_pipe
  308. fi
  309. done
  310. ) &
  311. BACKGROUND_TASKS+=($!)
  312. (
  313. while true; do
  314. if ! rspamd_checks; then
  315. log_to_redis "Rspamd hit error limit"
  316. [[ ! -z ${WATCHDOG_NOTIFY_EMAIL} ]] && mail_error "${WATCHDOG_NOTIFY_EMAIL}" "rspamd-mailcow"
  317. echo -e "\e[31m$(date) - Rspamd hit error limit\e[0m"
  318. echo rspamd-mailcow > /tmp/com_pipe
  319. fi
  320. done
  321. ) &
  322. BACKGROUND_TASKS+=($!)
  323. # Monitor watchdog agents, stop script when agents fails and wait for respawn by Docker (restart:always:n)
  324. (
  325. while true; do
  326. for bg_task in ${BACKGROUND_TASKS[*]}; do
  327. if ! kill -0 ${bg_task} 21>&2; then
  328. echo "Worker ${bg_task} died, stopping watchdog and waiting for respawn..."
  329. log_to_redis "Worker ${bg_task} died, stopping watchdog and waiting for respawn..."
  330. kill -TERM ${PARENT_PID}
  331. fi
  332. sleep 1
  333. done
  334. done
  335. ) &
  336. # Restart container when threshold limit reached
  337. while true; do
  338. CONTAINER_ID=
  339. read com_pipe_answer </tmp/com_pipe
  340. if [[ ${com_pipe_answer} =~ .+-mailcow ]]; then
  341. kill -STOP ${BACKGROUND_TASKS[*]}
  342. sleep 3
  343. CONTAINER_ID=$(curl --silent http://dockerapi:8080/containers/json | jq -r ".[] | {name: .Config.Labels[\"com.docker.compose.service\"], id: .Id}" | jq -rc "select( .name | tostring | contains(\"${com_pipe_answer}\")) | .id")
  344. if [[ ! -z ${CONTAINER_ID} ]]; then
  345. log_to_redis "Sending restart command to ${CONTAINER_ID}..."
  346. echo "Sending restart command to ${CONTAINER_ID}..."
  347. curl --silent -XPOST http://dockerapi:8080/containers/${CONTAINER_ID}/restart
  348. fi
  349. echo "Wait for restarted container to settle and continue watching..."
  350. sleep 30s
  351. kill -CONT ${BACKGROUND_TASKS[*]}
  352. kill -USR1 ${BACKGROUND_TASKS[*]}
  353. fi
  354. done