watchdog.sh 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645
  1. #!/bin/bash
  2. trap "exit" INT TERM
  3. trap "kill 0" EXIT
  4. # Prepare
  5. BACKGROUND_TASKS=()
  6. echo "Waiting for containers to settle..."
  7. sleep 10
  8. if [[ "${USE_WATCHDOG}" =~ ^([nN][oO]|[nN])+$ ]]; then
  9. echo -e "$(date) - USE_WATCHDOG=n, skipping watchdog..."
  10. sleep 365d
  11. exec $(readlink -f "$0")
  12. fi
  13. # Checks pipe their corresponding container name in this pipe
  14. if [[ ! -p /tmp/com_pipe ]]; then
  15. mkfifo /tmp/com_pipe
  16. fi
  17. # Common functions
  18. progress() {
  19. SERVICE=${1}
  20. TOTAL=${2}
  21. CURRENT=${3}
  22. DIFF=${4}
  23. [[ -z ${DIFF} ]] && DIFF=0
  24. [[ -z ${TOTAL} || -z ${CURRENT} ]] && return
  25. [[ ${CURRENT} -gt ${TOTAL} ]] && return
  26. [[ ${CURRENT} -lt 0 ]] && CURRENT=0
  27. PERCENT=$(( 200 * ${CURRENT} / ${TOTAL} % 2 + 100 * ${CURRENT} / ${TOTAL} ))
  28. redis-cli -h redis LPUSH WATCHDOG_LOG "{\"time\":\"$(date +%s)\",\"service\":\"${SERVICE}\",\"lvl\":\"${PERCENT}\",\"hpnow\":\"${CURRENT}\",\"hptotal\":\"${TOTAL}\",\"hpdiff\":\"${DIFF}\"}" > /dev/null
  29. log_msg "${SERVICE} health level: ${PERCENT}% (${CURRENT}/${TOTAL}), health trend: ${DIFF}" no_redis
  30. # Return 10 to indicate a dead service
  31. [ ${CURRENT} -le 0 ] && return 10
  32. }
  33. log_msg() {
  34. if [[ ${2} != "no_redis" ]]; then
  35. redis-cli -h redis LPUSH WATCHDOG_LOG "{\"time\":\"$(date +%s)\",\"message\":\"$(printf '%s' "${1}" | \
  36. tr '\r\n%&;$"_[]{}-' ' ')\"}" > /dev/null
  37. fi
  38. echo $(date) $(printf '%s\n' "${1}")
  39. }
  40. function mail_error() {
  41. [[ -z ${1} ]] && return 1
  42. [[ -z ${2} ]] && BODY="Service was restarted on $(date), please check your mailcow installation." || BODY="$(date) - ${2}"
  43. WATCHDOG_NOTIFY_EMAIL=$(echo "${WATCHDOG_NOTIFY_EMAIL}" | sed 's/"//;s|"$||')
  44. IFS=',' read -r -a MAIL_RCPTS <<< "${WATCHDOG_NOTIFY_EMAIL}"
  45. for rcpt in "${MAIL_RCPTS[@]}"; do
  46. RCPT_DOMAIN=
  47. RCPT_MX=
  48. RCPT_DOMAIN=$(echo ${rcpt} | awk -F @ {'print $NF'})
  49. RCPT_MX=$(dig +short ${RCPT_DOMAIN} mx | sort -n | awk '{print $2; exit}')
  50. if [[ -z ${RCPT_MX} ]]; then
  51. log_msg "Cannot determine MX for ${rcpt}, skipping email notification..."
  52. return 1
  53. fi
  54. [ -f "/tmp/${1}" ] && ATTACH="--attach /tmp/${1}@text/plain" || ATTACH=
  55. ./smtp-cli --missing-modules-ok \
  56. --subject="Watchdog: ${1} hit the error rate limit" \
  57. --body-plain="${BODY}" \
  58. --to=${rcpt} \
  59. --from="watchdog@${MAILCOW_HOSTNAME}" \
  60. --server="${RCPT_MX}" \
  61. --hello-host=${MAILCOW_HOSTNAME} \
  62. ${ATTACH}
  63. log_msg "Sent notification email to ${rcpt}"
  64. done
  65. }
  66. get_container_ip() {
  67. # ${1} is container
  68. CONTAINER_ID=()
  69. CONTAINER_IPS=()
  70. CONTAINER_IP=
  71. LOOP_C=1
  72. until [[ ${CONTAINER_IP} =~ ^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}$ ]] || [[ ${LOOP_C} -gt 5 ]]; do
  73. if [ ${IP_BY_DOCKER_API} -eq 0 ]; then
  74. CONTAINER_IP=$(dig a "${1}" +short)
  75. else
  76. sleep 0.5
  77. # get long container id for exact match
  78. CONTAINER_ID=($(curl --silent --insecure https://dockerapi/containers/json | jq -r ".[] | {name: .Config.Labels[\"com.docker.compose.service\"], id: .Id}" | jq -rc "select( .name | tostring == \"${1}\") | .id"))
  79. # returned id can have multiple elements (if scaled), shuffle for random test
  80. CONTAINER_ID=($(printf "%s\n" "${CONTAINER_ID[@]}" | shuf))
  81. if [[ ! -z ${CONTAINER_ID} ]]; then
  82. for matched_container in "${CONTAINER_ID[@]}"; do
  83. CONTAINER_IPS=($(curl --silent --insecure https://dockerapi/containers/${matched_container}/json | jq -r '.NetworkSettings.Networks[].IPAddress'))
  84. for ip_match in "${CONTAINER_IPS[@]}"; do
  85. # grep will do nothing if one of these vars is empty
  86. [[ -z ${ip_match} ]] && continue
  87. [[ -z ${IPV4_NETWORK} ]] && continue
  88. # only return ips that are part of our network
  89. if ! grep -q ${IPV4_NETWORK} <(echo ${ip_match}); then
  90. continue
  91. else
  92. CONTAINER_IP=${ip_match}
  93. break
  94. fi
  95. done
  96. [[ ! -z ${CONTAINER_IP} ]] && break
  97. done
  98. fi
  99. fi
  100. LOOP_C=$((LOOP_C + 1))
  101. done
  102. [[ ${LOOP_C} -gt 5 ]] && echo 240.0.0.0 || echo ${CONTAINER_IP}
  103. }
  104. nginx_checks() {
  105. err_count=0
  106. diff_c=0
  107. THRESHOLD=16
  108. # Reduce error count by 2 after restarting an unhealthy container
  109. trap "[ ${err_count} -gt 1 ] && err_count=$(( ${err_count} - 2 ))" USR1
  110. while [ ${err_count} -lt ${THRESHOLD} ]; do
  111. touch /tmp/nginx-mailcow; echo "$(tail -50 /tmp/nginx-mailcow)" > /tmp/nginx-mailcow
  112. host_ip=$(get_container_ip nginx-mailcow)
  113. err_c_cur=${err_count}
  114. /usr/lib/nagios/plugins/check_http -4 -H ${host_ip} -u / -p 8081 2>> /tmp/nginx-mailcow 1>&2; err_count=$(( ${err_count} + $? ))
  115. [ ${err_c_cur} -eq ${err_count} ] && [ ! $((${err_count} - 1)) -lt 0 ] && err_count=$((${err_count} - 1)) diff_c=1
  116. [ ${err_c_cur} -ne ${err_count} ] && diff_c=$(( ${err_c_cur} - ${err_count} ))
  117. progress "Nginx" ${THRESHOLD} $(( ${THRESHOLD} - ${err_count} )) ${diff_c}
  118. if [[ $? == 10 ]]; then
  119. diff_c=0
  120. sleep 1
  121. else
  122. diff_c=0
  123. sleep $(( ( RANDOM % 30 ) + 10 ))
  124. fi
  125. done
  126. return 1
  127. }
  128. unbound_checks() {
  129. err_count=0
  130. diff_c=0
  131. THRESHOLD=8
  132. # Reduce error count by 2 after restarting an unhealthy container
  133. trap "[ ${err_count} -gt 1 ] && err_count=$(( ${err_count} - 2 ))" USR1
  134. while [ ${err_count} -lt ${THRESHOLD} ]; do
  135. touch /tmp/unbound-mailcow; echo "$(tail -50 /tmp/unbound-mailcow)" > /tmp/unbound-mailcow
  136. host_ip=$(get_container_ip unbound-mailcow)
  137. err_c_cur=${err_count}
  138. /usr/lib/nagios/plugins/check_dns -s ${host_ip} -H stackoverflow.com 2>> /tmp/unbound-mailcow 1>&2; err_count=$(( ${err_count} + $? ))
  139. DNSSEC=$(dig com +dnssec | egrep 'flags:.+ad')
  140. if [[ -z ${DNSSEC} ]]; then
  141. echo "DNSSEC failure" 2>> /tmp/unbound-mailcow 1>&2
  142. err_count=$(( ${err_count} + 1))
  143. else
  144. echo "DNSSEC check succeeded" 2>> /tmp/unbound-mailcow 1>&2
  145. fi
  146. [ ${err_c_cur} -eq ${err_count} ] && [ ! $((${err_count} - 1)) -lt 0 ] && err_count=$((${err_count} - 1)) diff_c=1
  147. [ ${err_c_cur} -ne ${err_count} ] && diff_c=$(( ${err_c_cur} - ${err_count} ))
  148. progress "Unbound" ${THRESHOLD} $(( ${THRESHOLD} - ${err_count} )) ${diff_c}
  149. if [[ $? == 10 ]]; then
  150. diff_c=0
  151. sleep 1
  152. else
  153. diff_c=0
  154. sleep $(( ( RANDOM % 30 ) + 10 ))
  155. fi
  156. done
  157. return 1
  158. }
  159. mysql_checks() {
  160. err_count=0
  161. diff_c=0
  162. THRESHOLD=12
  163. # Reduce error count by 2 after restarting an unhealthy container
  164. trap "[ ${err_count} -gt 1 ] && err_count=$(( ${err_count} - 2 ))" USR1
  165. while [ ${err_count} -lt ${THRESHOLD} ]; do
  166. touch /tmp/mysql-mailcow; echo "$(tail -50 /tmp/mysql-mailcow)" > /tmp/mysql-mailcow
  167. host_ip=$(get_container_ip mysql-mailcow)
  168. err_c_cur=${err_count}
  169. /usr/lib/nagios/plugins/check_mysql -s /var/run/mysqld/mysqld.sock -u ${DBUSER} -p ${DBPASS} -d ${DBNAME} 2>> /tmp/mysql-mailcow 1>&2; err_count=$(( ${err_count} + $? ))
  170. /usr/lib/nagios/plugins/check_mysql_query -s /var/run/mysqld/mysqld.sock -u ${DBUSER} -p ${DBPASS} -d ${DBNAME} -q "SELECT COUNT(*) FROM information_schema.tables" 2>> /tmp/mysql-mailcow 1>&2; err_count=$(( ${err_count} + $? ))
  171. [ ${err_c_cur} -eq ${err_count} ] && [ ! $((${err_count} - 1)) -lt 0 ] && err_count=$((${err_count} - 1)) diff_c=1
  172. [ ${err_c_cur} -ne ${err_count} ] && diff_c=$(( ${err_c_cur} - ${err_count} ))
  173. progress "MySQL/MariaDB" ${THRESHOLD} $(( ${THRESHOLD} - ${err_count} )) ${diff_c}
  174. if [[ $? == 10 ]]; then
  175. diff_c=0
  176. sleep 1
  177. else
  178. diff_c=0
  179. sleep $(( ( RANDOM % 30 ) + 10 ))
  180. fi
  181. done
  182. return 1
  183. }
  184. sogo_checks() {
  185. err_count=0
  186. diff_c=0
  187. THRESHOLD=10
  188. # Reduce error count by 2 after restarting an unhealthy container
  189. trap "[ ${err_count} -gt 1 ] && err_count=$(( ${err_count} - 2 ))" USR1
  190. while [ ${err_count} -lt ${THRESHOLD} ]; do
  191. touch /tmp/sogo-mailcow; echo "$(tail -50 /tmp/sogo-mailcow)" > /tmp/sogo-mailcow
  192. host_ip=$(get_container_ip sogo-mailcow)
  193. err_c_cur=${err_count}
  194. /usr/lib/nagios/plugins/check_http -4 -H ${host_ip} -u /SOGo.index/ -p 20000 -R "SOGo\.MainUI" 2>> /tmp/sogo-mailcow 1>&2; err_count=$(( ${err_count} + $? ))
  195. [ ${err_c_cur} -eq ${err_count} ] && [ ! $((${err_count} - 1)) -lt 0 ] && err_count=$((${err_count} - 1)) diff_c=1
  196. [ ${err_c_cur} -ne ${err_count} ] && diff_c=$(( ${err_c_cur} - ${err_count} ))
  197. progress "SOGo" ${THRESHOLD} $(( ${THRESHOLD} - ${err_count} )) ${diff_c}
  198. if [[ $? == 10 ]]; then
  199. diff_c=0
  200. sleep 1
  201. else
  202. diff_c=0
  203. sleep $(( ( RANDOM % 30 ) + 10 ))
  204. fi
  205. done
  206. return 1
  207. }
  208. postfix_checks() {
  209. err_count=0
  210. diff_c=0
  211. THRESHOLD=8
  212. # Reduce error count by 2 after restarting an unhealthy container
  213. trap "[ ${err_count} -gt 1 ] && err_count=$(( ${err_count} - 2 ))" USR1
  214. while [ ${err_count} -lt ${THRESHOLD} ]; do
  215. touch /tmp/postfix-mailcow; echo "$(tail -50 /tmp/postfix-mailcow)" > /tmp/postfix-mailcow
  216. host_ip=$(get_container_ip postfix-mailcow)
  217. err_c_cur=${err_count}
  218. /usr/lib/nagios/plugins/check_smtp -4 -H ${host_ip} -p 589 -f "watchdog@invalid" -C "RCPT TO:null@localhost" -C DATA -C . -R 250 2>> /tmp/postfix-mailcow 1>&2; err_count=$(( ${err_count} + $? ))
  219. /usr/lib/nagios/plugins/check_smtp -4 -H ${host_ip} -p 589 -S 2>> /tmp/postfix-mailcow 1>&2; err_count=$(( ${err_count} + $? ))
  220. [ ${err_c_cur} -eq ${err_count} ] && [ ! $((${err_count} - 1)) -lt 0 ] && err_count=$((${err_count} - 1)) diff_c=1
  221. [ ${err_c_cur} -ne ${err_count} ] && diff_c=$(( ${err_c_cur} - ${err_count} ))
  222. progress "Postfix" ${THRESHOLD} $(( ${THRESHOLD} - ${err_count} )) ${diff_c}
  223. if [[ $? == 10 ]]; then
  224. diff_c=0
  225. sleep 1
  226. else
  227. diff_c=0
  228. sleep $(( ( RANDOM % 30 ) + 10 ))
  229. fi
  230. done
  231. return 1
  232. }
  233. clamd_checks() {
  234. err_count=0
  235. diff_c=0
  236. THRESHOLD=5
  237. # Reduce error count by 2 after restarting an unhealthy container
  238. trap "[ ${err_count} -gt 1 ] && err_count=$(( ${err_count} - 2 ))" USR1
  239. while [ ${err_count} -lt ${THRESHOLD} ]; do
  240. touch /tmp/clamd-mailcow; echo "$(tail -50 /tmp/clamd-mailcow)" > /tmp/clamd-mailcow
  241. host_ip=$(get_container_ip clamd-mailcow)
  242. err_c_cur=${err_count}
  243. /usr/lib/nagios/plugins/check_clamd -4 -H ${host_ip} 2>> /tmp/clamd-mailcow 1>&2; err_count=$(( ${err_count} + $? ))
  244. [ ${err_c_cur} -eq ${err_count} ] && [ ! $((${err_count} - 1)) -lt 0 ] && err_count=$((${err_count} - 1)) diff_c=1
  245. [ ${err_c_cur} -ne ${err_count} ] && diff_c=$(( ${err_c_cur} - ${err_count} ))
  246. progress "Clamd" ${THRESHOLD} $(( ${THRESHOLD} - ${err_count} )) ${diff_c}
  247. if [[ $? == 10 ]]; then
  248. diff_c=0
  249. sleep 1
  250. else
  251. diff_c=0
  252. sleep $(( ( RANDOM % 30 ) + 10 ))
  253. fi
  254. done
  255. return 1
  256. }
  257. dovecot_checks() {
  258. err_count=0
  259. diff_c=0
  260. THRESHOLD=20
  261. # Reduce error count by 2 after restarting an unhealthy container
  262. trap "[ ${err_count} -gt 1 ] && err_count=$(( ${err_count} - 2 ))" USR1
  263. while [ ${err_count} -lt ${THRESHOLD} ]; do
  264. touch /tmp/dovecot-mailcow; echo "$(tail -50 /tmp/dovecot-mailcow)" > /tmp/dovecot-mailcow
  265. host_ip=$(get_container_ip dovecot-mailcow)
  266. err_c_cur=${err_count}
  267. /usr/lib/nagios/plugins/check_smtp -4 -H ${host_ip} -p 24 -f "watchdog@invalid" -C "RCPT TO:<watchdog@invalid>" -L -R "User doesn't exist" 2>> /tmp/dovecot-mailcow 1>&2; err_count=$(( ${err_count} + $? ))
  268. /usr/lib/nagios/plugins/check_imap -4 -H ${host_ip} -p 993 -S -e "OK " 2>> /tmp/dovecot-mailcow 1>&2; err_count=$(( ${err_count} + $? ))
  269. /usr/lib/nagios/plugins/check_imap -4 -H ${host_ip} -p 143 -e "OK " 2>> /tmp/dovecot-mailcow 1>&2; err_count=$(( ${err_count} + $? ))
  270. /usr/lib/nagios/plugins/check_tcp -4 -H ${host_ip} -p 10001 -e "VERSION" 2>> /tmp/dovecot-mailcow 1>&2; err_count=$(( ${err_count} + $? ))
  271. /usr/lib/nagios/plugins/check_tcp -4 -H ${host_ip} -p 4190 -e "Dovecot ready" 2>> /tmp/dovecot-mailcow 1>&2; err_count=$(( ${err_count} + $? ))
  272. [ ${err_c_cur} -eq ${err_count} ] && [ ! $((${err_count} - 1)) -lt 0 ] && err_count=$((${err_count} - 1)) diff_c=1
  273. [ ${err_c_cur} -ne ${err_count} ] && diff_c=$(( ${err_c_cur} - ${err_count} ))
  274. progress "Dovecot" ${THRESHOLD} $(( ${THRESHOLD} - ${err_count} )) ${diff_c}
  275. if [[ $? == 10 ]]; then
  276. diff_c=0
  277. sleep 1
  278. else
  279. diff_c=0
  280. sleep $(( ( RANDOM % 30 ) + 10 ))
  281. fi
  282. done
  283. return 1
  284. }
  285. phpfpm_checks() {
  286. err_count=0
  287. diff_c=0
  288. THRESHOLD=5
  289. # Reduce error count by 2 after restarting an unhealthy container
  290. trap "[ ${err_count} -gt 1 ] && err_count=$(( ${err_count} - 2 ))" USR1
  291. while [ ${err_count} -lt ${THRESHOLD} ]; do
  292. touch /tmp/php-fpm-mailcow; echo "$(tail -50 /tmp/php-fpm-mailcow)" > /tmp/php-fpm-mailcow
  293. host_ip=$(get_container_ip php-fpm-mailcow)
  294. err_c_cur=${err_count}
  295. /usr/lib/nagios/plugins/check_tcp -H ${host_ip} -p 9001 2>> /tmp/php-fpm-mailcow 1>&2; err_count=$(( ${err_count} + $? ))
  296. /usr/lib/nagios/plugins/check_tcp -H ${host_ip} -p 9002 2>> /tmp/php-fpm-mailcow 1>&2; err_count=$(( ${err_count} + $? ))
  297. [ ${err_c_cur} -eq ${err_count} ] && [ ! $((${err_count} - 1)) -lt 0 ] && err_count=$((${err_count} - 1)) diff_c=1
  298. [ ${err_c_cur} -ne ${err_count} ] && diff_c=$(( ${err_c_cur} - ${err_count} ))
  299. progress "PHP-FPM" ${THRESHOLD} $(( ${THRESHOLD} - ${err_count} )) ${diff_c}
  300. if [[ $? == 10 ]]; then
  301. diff_c=0
  302. sleep 1
  303. else
  304. diff_c=0
  305. sleep $(( ( RANDOM % 30 ) + 10 ))
  306. fi
  307. done
  308. return 1
  309. }
  310. ratelimit_checks() {
  311. err_count=0
  312. diff_c=0
  313. THRESHOLD=1
  314. RL_LOG_STATUS=$(redis-cli -h redis LRANGE RL_LOG 0 0 | jq .qid)
  315. # Reduce error count by 2 after restarting an unhealthy container
  316. trap "[ ${err_count} -gt 1 ] && err_count=$(( ${err_count} - 2 ))" USR1
  317. while [ ${err_count} -lt ${THRESHOLD} ]; do
  318. err_c_cur=${err_count}
  319. RL_LOG_STATUS_PREV=${RL_LOG_STATUS}
  320. RL_LOG_STATUS=$(redis-cli -h redis LRANGE RL_LOG 0 0 | jq .qid)
  321. if [[ ${RL_LOG_STATUS_PREV} != ${RL_LOG_STATUS} ]]; then
  322. err_count=$(( ${err_count} + 1 ))
  323. fi
  324. [ ${err_c_cur} -eq ${err_count} ] && [ ! $((${err_count} - 1)) -lt 0 ] && err_count=$((${err_count} - 1)) diff_c=1
  325. [ ${err_c_cur} -ne ${err_count} ] && diff_c=$(( ${err_c_cur} - ${err_count} ))
  326. progress "Ratelimit" ${THRESHOLD} $(( ${THRESHOLD} - ${err_count} )) ${diff_c}
  327. if [[ $? == 10 ]]; then
  328. diff_c=0
  329. sleep 1
  330. else
  331. diff_c=0
  332. sleep $(( ( RANDOM % 30 ) + 10 ))
  333. fi
  334. done
  335. return 1
  336. }
  337. acme_checks() {
  338. err_count=0
  339. diff_c=0
  340. THRESHOLD=1
  341. ACME_LOG_STATUS=$(redis-cli -h redis GET ACME_FAIL_TIME)
  342. if [[ -z "${ACME_LOG_STATUS}" ]]; then
  343. redis-cli -h redis SET ACME_FAIL_TIME 0
  344. ACME_LOG_STATUS=0
  345. fi
  346. # Reduce error count by 2 after restarting an unhealthy container
  347. trap "[ ${err_count} -gt 1 ] && err_count=$(( ${err_count} - 2 ))" USR1
  348. while [ ${err_count} -lt ${THRESHOLD} ]; do
  349. err_c_cur=${err_count}
  350. ACME_LOG_STATUS_PREV=${ACME_LOG_STATUS}
  351. ACME_LOG_STATUS=$(redis-cli -h redis GET ACME_FAIL_TIME)
  352. if [[ ${ACME_LOG_STATUS_PREV} != ${ACME_LOG_STATUS} ]]; then
  353. err_count=$(( ${err_count} + 1 ))
  354. fi
  355. [ ${err_c_cur} -eq ${err_count} ] && [ ! $((${err_count} - 1)) -lt 0 ] && err_count=$((${err_count} - 1)) diff_c=1
  356. [ ${err_c_cur} -ne ${err_count} ] && diff_c=$(( ${err_c_cur} - ${err_count} ))
  357. progress "ACME" ${THRESHOLD} $(( ${THRESHOLD} - ${err_count} )) ${diff_c}
  358. if [[ $? == 10 ]]; then
  359. diff_c=0
  360. sleep 1
  361. else
  362. diff_c=0
  363. sleep $(( ( RANDOM % 30 ) + 10 ))
  364. fi
  365. done
  366. return 1
  367. }
  368. ipv6nat_checks() {
  369. err_count=0
  370. diff_c=0
  371. THRESHOLD=1
  372. # Reduce error count by 2 after restarting an unhealthy container
  373. trap "[ ${err_count} -gt 1 ] && err_count=$(( ${err_count} - 2 ))" USR1
  374. while [ ${err_count} -lt ${THRESHOLD} ]; do
  375. err_c_cur=${err_count}
  376. CONTAINERS=$(curl --silent --insecure https://dockerapi/containers/json)
  377. IPV6NAT_CONTAINER_ID=$(echo ${CONTAINERS} | jq -r ".[] | {name: .Config.Labels[\"com.docker.compose.service\"], id: .Id}" | jq -rc "select( .name | tostring | contains(\"ipv6nat-mailcow\")) | .id")
  378. if [[ ! -z ${IPV6NAT_CONTAINER_ID} ]]; then
  379. LATEST_STARTED="$(echo ${CONTAINERS} | jq -r ".[] | {name: .Config.Labels[\"com.docker.compose.service\"], StartedAt: .State.StartedAt}" | jq -rc "select( .name | tostring | contains(\"ipv6nat-mailcow\") | not)" | jq -rc .StartedAt | xargs -n1 date +%s -d | sort | tail -n1)"
  380. LATEST_IPV6NAT="$(echo ${CONTAINERS} | jq -r ".[] | {name: .Config.Labels[\"com.docker.compose.service\"], StartedAt: .State.StartedAt}" | jq -rc "select( .name | tostring | contains(\"ipv6nat-mailcow\"))" | jq -rc .StartedAt | xargs -n1 date +%s -d | sort | tail -n1)"
  381. DIFFERENCE_START_TIME=$(expr ${LATEST_IPV6NAT} - ${LATEST_STARTED} 2>/dev/null)
  382. if [[ "${DIFFERENCE_START_TIME}" -lt 30 ]]; then
  383. err_count=$(( ${err_count} + 1 ))
  384. fi
  385. fi
  386. [ ${err_c_cur} -eq ${err_count} ] && [ ! $((${err_count} - 1)) -lt 0 ] && err_count=$((${err_count} - 1)) diff_c=1
  387. [ ${err_c_cur} -ne ${err_count} ] && diff_c=$(( ${err_c_cur} - ${err_count} ))
  388. progress "IPv6 NAT" ${THRESHOLD} $(( ${THRESHOLD} - ${err_count} )) ${diff_c}
  389. if [[ $? == 10 ]]; then
  390. diff_c=0
  391. sleep 1
  392. else
  393. diff_c=0
  394. sleep 300
  395. fi
  396. done
  397. return 1
  398. }
  399. rspamd_checks() {
  400. err_count=0
  401. diff_c=0
  402. THRESHOLD=5
  403. # Reduce error count by 2 after restarting an unhealthy container
  404. trap "[ ${err_count} -gt 1 ] && err_count=$(( ${err_count} - 2 ))" USR1
  405. while [ ${err_count} -lt ${THRESHOLD} ]; do
  406. touch /tmp/rspamd-mailcow; echo "$(tail -50 /tmp/rspamd-mailcow)" > /tmp/rspamd-mailcow
  407. host_ip=$(get_container_ip rspamd-mailcow)
  408. err_c_cur=${err_count}
  409. SCORE=$(echo 'To: null@localhost
  410. From: watchdog@localhost
  411. Empty
  412. ' | usr/bin/curl -s --data-binary @- --unix-socket /var/lib/rspamd/rspamd.sock http://rspamd/scan | jq -rc .required_score)
  413. if [[ ${SCORE} != "9999" ]]; then
  414. echo "Rspamd settings check failed" 2>> /tmp/rspamd-mailcow 1>&2
  415. err_count=$(( ${err_count} + 1))
  416. else
  417. echo "Rspamd settings check succeeded" 2>> /tmp/rspamd-mailcow 1>&2
  418. fi
  419. [ ${err_c_cur} -eq ${err_count} ] && [ ! $((${err_count} - 1)) -lt 0 ] && err_count=$((${err_count} - 1)) diff_c=1
  420. [ ${err_c_cur} -ne ${err_count} ] && diff_c=$(( ${err_c_cur} - ${err_count} ))
  421. progress "Rspamd" ${THRESHOLD} $(( ${THRESHOLD} - ${err_count} )) ${diff_c}
  422. diff_c=0
  423. sleep $(( ( RANDOM % 30 ) + 10 ))
  424. done
  425. return 1
  426. }
  427. # Create watchdog agents
  428. (
  429. while true; do
  430. if ! nginx_checks; then
  431. log_msg "Nginx hit error limit"
  432. echo nginx-mailcow > /tmp/com_pipe
  433. fi
  434. done
  435. ) &
  436. BACKGROUND_TASKS+=($!)
  437. (
  438. while true; do
  439. if ! mysql_checks; then
  440. log_msg "MySQL hit error limit"
  441. echo mysql-mailcow > /tmp/com_pipe
  442. fi
  443. done
  444. ) &
  445. BACKGROUND_TASKS+=($!)
  446. (
  447. while true; do
  448. if ! phpfpm_checks; then
  449. log_msg "PHP-FPM hit error limit"
  450. echo php-fpm-mailcow > /tmp/com_pipe
  451. fi
  452. done
  453. ) &
  454. BACKGROUND_TASKS+=($!)
  455. (
  456. while true; do
  457. if ! sogo_checks; then
  458. log_msg "SOGo hit error limit"
  459. echo sogo-mailcow > /tmp/com_pipe
  460. fi
  461. done
  462. ) &
  463. BACKGROUND_TASKS+=($!)
  464. if [ ${CHECK_UNBOUND} -eq 1 ]; then
  465. (
  466. while true; do
  467. if ! unbound_checks; then
  468. log_msg "Unbound hit error limit"
  469. echo unbound-mailcow > /tmp/com_pipe
  470. fi
  471. done
  472. ) &
  473. BACKGROUND_TASKS+=($!)
  474. fi
  475. if [[ "${SKIP_CLAMD}" =~ ^([nN][oO]|[nN])+$ ]]; then
  476. (
  477. while true; do
  478. if ! clamd_checks; then
  479. log_msg "Clamd hit error limit"
  480. echo clamd-mailcow > /tmp/com_pipe
  481. fi
  482. done
  483. ) &
  484. BACKGROUND_TASKS+=($!)
  485. fi
  486. (
  487. while true; do
  488. if ! postfix_checks; then
  489. log_msg "Postfix hit error limit"
  490. echo postfix-mailcow > /tmp/com_pipe
  491. fi
  492. done
  493. ) &
  494. BACKGROUND_TASKS+=($!)
  495. (
  496. while true; do
  497. if ! dovecot_checks; then
  498. log_msg "Dovecot hit error limit"
  499. echo dovecot-mailcow > /tmp/com_pipe
  500. fi
  501. done
  502. ) &
  503. BACKGROUND_TASKS+=($!)
  504. (
  505. while true; do
  506. if ! rspamd_checks; then
  507. log_msg "Rspamd hit error limit"
  508. echo rspamd-mailcow > /tmp/com_pipe
  509. fi
  510. done
  511. ) &
  512. BACKGROUND_TASKS+=($!)
  513. (
  514. while true; do
  515. if ! ratelimit_checks; then
  516. log_msg "Ratelimit hit error limit"
  517. echo ratelimit > /tmp/com_pipe
  518. fi
  519. done
  520. ) &
  521. BACKGROUND_TASKS+=($!)
  522. (
  523. while true; do
  524. if ! acme_checks; then
  525. log_msg "ACME client hit error limit"
  526. echo acme-tiny > /tmp/com_pipe
  527. fi
  528. done
  529. ) &
  530. BACKGROUND_TASKS+=($!)
  531. (
  532. while true; do
  533. if ! ipv6nat_checks; then
  534. log_msg "IPv6 NAT warning: ipv6nat-mailcow container was not started at least 30s after siblings (not an error)"
  535. echo ipv6nat-mailcow > /tmp/com_pipe
  536. fi
  537. done
  538. ) &
  539. BACKGROUND_TASKS+=($!)
  540. # Monitor watchdog agents, stop script when agents fails and wait for respawn by Docker (restart:always:n)
  541. (
  542. while true; do
  543. for bg_task in ${BACKGROUND_TASKS[*]}; do
  544. if ! kill -0 ${bg_task} 1>&2; then
  545. log_msg "Worker ${bg_task} died, stopping watchdog and waiting for respawn..."
  546. kill -TERM 1
  547. fi
  548. sleep 10
  549. done
  550. done
  551. ) &
  552. # Monitor dockerapi
  553. (
  554. while true; do
  555. while nc -z dockerapi 443; do
  556. sleep 3
  557. done
  558. log_msg "Cannot find dockerapi-mailcow, waiting to recover..."
  559. kill -STOP ${BACKGROUND_TASKS[*]}
  560. until nc -z dockerapi 443; do
  561. sleep 3
  562. done
  563. kill -CONT ${BACKGROUND_TASKS[*]}
  564. kill -USR1 ${BACKGROUND_TASKS[*]}
  565. done
  566. ) &
  567. # Restart container when threshold limit reached
  568. while true; do
  569. CONTAINER_ID=
  570. HAS_INITDB=
  571. read com_pipe_answer </tmp/com_pipe
  572. if [ -s "/tmp/${com_pipe_answer}" ]; then
  573. cat "/tmp/${com_pipe_answer}"
  574. fi
  575. if [[ ${com_pipe_answer} == "ratelimit" ]]; then
  576. log_msg "At least one ratelimit was applied"
  577. [[ ! -z ${WATCHDOG_NOTIFY_EMAIL} ]] && mail_error "${com_pipe_answer}" "Please see mailcow UI logs for further information."
  578. elif [[ ${com_pipe_answer} == "acme-tiny" ]]; then
  579. log_msg "acme-tiny client returned non-zero exit code"
  580. [[ ! -z ${WATCHDOG_NOTIFY_EMAIL} ]] && mail_error "${com_pipe_answer}" "Please check acme-mailcow for ruther information."
  581. elif [[ ${com_pipe_answer} =~ .+-mailcow ]] || [[ ${com_pipe_answer} == "ipv6nat-mailcow" ]]; then
  582. kill -STOP ${BACKGROUND_TASKS[*]}
  583. sleep 3
  584. CONTAINER_ID=$(curl --silent --insecure https://dockerapi/containers/json | jq -r ".[] | {name: .Config.Labels[\"com.docker.compose.service\"], id: .Id}" | jq -rc "select( .name | tostring | contains(\"${com_pipe_answer}\")) | .id")
  585. if [[ ! -z ${CONTAINER_ID} ]]; then
  586. if [[ "${com_pipe_answer}" == "php-fpm-mailcow" ]]; then
  587. HAS_INITDB=$(curl --silent --insecure -XPOST https://dockerapi/containers/${CONTAINER_ID}/top | jq '.msg.Processes[] | contains(["php -c /usr/local/etc/php -f /web/inc/init_db.inc.php"])' | grep true)
  588. fi
  589. S_RUNNING=$(($(date +%s) - $(curl --silent --insecure https://dockerapi/containers/${CONTAINER_ID}/json | jq .State.StartedAt | xargs -n1 date +%s -d)))
  590. if [ ${S_RUNNING} -lt 120 ]; then
  591. log_msg "Container is running for less than 120 seconds, skipping action..."
  592. elif [[ ! -z ${HAS_INITDB} ]]; then
  593. log_msg "Database is being initialized by php-fpm-mailcow, not restarting but delaying checks for a minute..."
  594. sleep 60
  595. else
  596. log_msg "Sending restart command to ${CONTAINER_ID}..."
  597. curl --silent --insecure -XPOST https://dockerapi/containers/${CONTAINER_ID}/restart
  598. if [[ ${com_pipe_answer} != "ipv6nat-mailcow" ]]; then
  599. [[ ! -z ${WATCHDOG_NOTIFY_EMAIL} ]] && mail_error "${com_pipe_answer}"
  600. fi
  601. log_msg "Wait for restarted container to settle and continue watching..."
  602. sleep 35
  603. fi
  604. fi
  605. kill -CONT ${BACKGROUND_TASKS[*]}
  606. kill -USR1 ${BACKGROUND_TASKS[*]}
  607. fi
  608. done