watchdog.sh 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510
  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 google.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. rspamd_checks() {
  309. err_count=0
  310. diff_c=0
  311. THRESHOLD=5
  312. # Reduce error count by 2 after restarting an unhealthy container
  313. trap "[ ${err_count} -gt 1 ] && err_count=$(( ${err_count} - 2 ))" USR1
  314. while [ ${err_count} -lt ${THRESHOLD} ]; do
  315. cat /dev/null > /tmp/rspamd-mailcow
  316. host_ip=$(get_container_ip rspamd-mailcow)
  317. err_c_cur=${err_count}
  318. SCORE=$(/usr/bin/curl -s --data-binary @- --unix-socket /var/lib/rspamd/rspamd.sock http://rspamd/scan -d '
  319. To: null@localhost
  320. From: watchdog@localhost
  321. Empty
  322. ' | jq -rc .required_score)
  323. if [[ ${SCORE} != "9999" ]]; then
  324. echo "Rspamd settings check failed" 2>> /tmp/rspamd-mailcow 1>&2
  325. err_count=$(( ${err_count} + 1))
  326. else
  327. echo "Rspamd settings check succeeded" 2>> /tmp/rspamd-mailcow 1>&2
  328. fi
  329. [ ${err_c_cur} -eq ${err_count} ] && [ ! $((${err_count} - 1)) -lt 0 ] && err_count=$((${err_count} - 1)) diff_c=1
  330. [ ${err_c_cur} -ne ${err_count} ] && diff_c=$(( ${err_c_cur} - ${err_count} ))
  331. progress "Rspamd" ${THRESHOLD} $(( ${THRESHOLD} - ${err_count} )) ${diff_c}
  332. diff_c=0
  333. sleep $(( ( RANDOM % 30 ) + 10 ))
  334. done
  335. return 1
  336. }
  337. # Create watchdog agents
  338. (
  339. while true; do
  340. if ! nginx_checks; then
  341. log_msg "Nginx hit error limit"
  342. echo nginx-mailcow > /tmp/com_pipe
  343. fi
  344. done
  345. ) &
  346. BACKGROUND_TASKS+=($!)
  347. (
  348. while true; do
  349. if ! mysql_checks; then
  350. log_msg "MySQL hit error limit"
  351. echo mysql-mailcow > /tmp/com_pipe
  352. fi
  353. done
  354. ) &
  355. BACKGROUND_TASKS+=($!)
  356. (
  357. while true; do
  358. if ! phpfpm_checks; then
  359. log_msg "PHP-FPM hit error limit"
  360. echo php-fpm-mailcow > /tmp/com_pipe
  361. fi
  362. done
  363. ) &
  364. BACKGROUND_TASKS+=($!)
  365. (
  366. while true; do
  367. if ! sogo_checks; then
  368. log_msg "SOGo hit error limit"
  369. echo sogo-mailcow > /tmp/com_pipe
  370. fi
  371. done
  372. ) &
  373. BACKGROUND_TASKS+=($!)
  374. if [ ${CHECK_UNBOUND} -eq 1 ]; then
  375. (
  376. while true; do
  377. if ! unbound_checks; then
  378. log_msg "Unbound hit error limit"
  379. echo unbound-mailcow > /tmp/com_pipe
  380. fi
  381. done
  382. ) &
  383. BACKGROUND_TASKS+=($!)
  384. fi
  385. if [[ "${SKIP_CLAMD}" =~ ^([nN][oO]|[nN])+$ ]]; then
  386. (
  387. while true; do
  388. if ! clamd_checks; then
  389. log_msg "Clamd hit error limit"
  390. echo clamd-mailcow > /tmp/com_pipe
  391. fi
  392. done
  393. ) &
  394. BACKGROUND_TASKS+=($!)
  395. fi
  396. (
  397. while true; do
  398. if ! postfix_checks; then
  399. log_msg "Postfix hit error limit"
  400. echo postfix-mailcow > /tmp/com_pipe
  401. fi
  402. done
  403. ) &
  404. BACKGROUND_TASKS+=($!)
  405. (
  406. while true; do
  407. if ! dovecot_checks; then
  408. log_msg "Dovecot hit error limit"
  409. echo dovecot-mailcow > /tmp/com_pipe
  410. fi
  411. done
  412. ) &
  413. BACKGROUND_TASKS+=($!)
  414. (
  415. while true; do
  416. if ! rspamd_checks; then
  417. log_msg "Rspamd hit error limit"
  418. echo rspamd-mailcow > /tmp/com_pipe
  419. fi
  420. done
  421. ) &
  422. BACKGROUND_TASKS+=($!)
  423. # Monitor watchdog agents, stop script when agents fails and wait for respawn by Docker (restart:always:n)
  424. (
  425. while true; do
  426. for bg_task in ${BACKGROUND_TASKS[*]}; do
  427. if ! kill -0 ${bg_task} 1>&2; then
  428. log_msg "Worker ${bg_task} died, stopping watchdog and waiting for respawn..."
  429. kill -TERM 1
  430. fi
  431. sleep 10
  432. done
  433. done
  434. ) &
  435. # Monitor dockerapi
  436. (
  437. while true; do
  438. while nc -z dockerapi 443; do
  439. sleep 3
  440. done
  441. log_msg "Cannot find dockerapi-mailcow, waiting to recover..."
  442. kill -STOP ${BACKGROUND_TASKS[*]}
  443. until nc -z dockerapi 443; do
  444. sleep 3
  445. done
  446. kill -CONT ${BACKGROUND_TASKS[*]}
  447. kill -USR1 ${BACKGROUND_TASKS[*]}
  448. done
  449. ) &
  450. # Restart container when threshold limit reached
  451. while true; do
  452. CONTAINER_ID=
  453. HAS_INITDB=
  454. read com_pipe_answer </tmp/com_pipe
  455. if [[ ${com_pipe_answer} =~ .+-mailcow ]]; then
  456. kill -STOP ${BACKGROUND_TASKS[*]}
  457. sleep 3
  458. 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")
  459. if [[ ! -z ${CONTAINER_ID} ]]; then
  460. if [[ "${com_pipe_answer}" == "php-fpm-mailcow" ]]; then
  461. 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)
  462. fi
  463. S_RUNNING=$(($(date +%s) - $(curl --silent --insecure https://dockerapi/containers/${CONTAINER_ID}/json | jq .State.StartedAt | xargs -n1 date +%s -d)))
  464. if [ ${S_RUNNING} -lt 120 ]; then
  465. log_msg "Container is running for less than 120 seconds, skipping action..."
  466. elif [[ ! -z ${HAS_INITDB} ]]; then
  467. log_msg "Database is being initialized by php-fpm-mailcow, not restarting but delaying checks for a minute..."
  468. sleep 60
  469. else
  470. log_msg "Sending restart command to ${CONTAINER_ID}..."
  471. curl --silent --insecure -XPOST https://dockerapi/containers/${CONTAINER_ID}/restart
  472. [[ ! -z ${WATCHDOG_NOTIFY_EMAIL} ]] && mail_error "${com_pipe_answer}"
  473. log_msg "Wait for restarted container to settle and continue watching..."
  474. sleep 30
  475. fi
  476. fi
  477. kill -CONT ${BACKGROUND_TASKS[*]}
  478. kill -USR1 ${BACKGROUND_TASKS[*]}
  479. fi
  480. done