watchdog.sh 23 KB

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