2
0

watchdog.sh 21 KB

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