watchdog.sh 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479
  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. }
  29. log_msg() {
  30. if [[ ${2} != "no_redis" ]]; then
  31. redis-cli -h redis LPUSH WATCHDOG_LOG "{\"time\":\"$(date +%s)\",\"message\":\"$(printf '%s' "${1}" | \
  32. tr '%&;$"_[]{}-\r\n' ' ')\"}" > /dev/null
  33. fi
  34. echo $(date) $(printf '%s\n' "${1}")
  35. }
  36. function mail_error() {
  37. [[ -z ${1} ]] && return 1
  38. [[ -z ${2} ]] && return 2
  39. [[ -z ${3} ]] && BODY="Service was restarted, please check your mailcow installation." || BODY="${3}"
  40. RCPT_DOMAIN=$(echo ${1} | awk -F @ {'print $NF'})
  41. RCPT_MX=$(dig +short ${RCPT_DOMAIN} mx | sort -n | awk '{print $2; exit}')
  42. if [[ -z ${RCPT_MX} ]]; then
  43. log_msg "Cannot determine MX for ${1}, skipping email notification..."
  44. return 1
  45. fi
  46. ./smtp-cli --missing-modules-ok \
  47. --subject="Watchdog: ${2} hit the error rate limit" \
  48. --body-plain="${BODY}" \
  49. --to=${1} \
  50. --from="watchdog@${MAILCOW_HOSTNAME}" \
  51. --server="${RCPT_MX}" \
  52. --hello-host=${MAILCOW_HOSTNAME}
  53. log_msg "Sent notification email to ${1}"
  54. }
  55. get_container_ip() {
  56. # ${1} is container
  57. CONTAINER_ID=()
  58. CONTAINER_IPS=()
  59. CONTAINER_IP=
  60. LOOP_C=1
  61. until [[ ${CONTAINER_IP} =~ ^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}$ ]] || [[ ${LOOP_C} -gt 5 ]]; do
  62. if [ ${IP_BY_DOCKER_API} -eq 0 ]; then
  63. CONTAINER_IP=$(dig a "${1}" +short)
  64. else
  65. sleep 0.5
  66. # get long container id for exact match
  67. 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"))
  68. # returned id can have multiple elements (if scaled), shuffle for random test
  69. CONTAINER_ID=($(printf "%s\n" "${CONTAINER_ID[@]}" | shuf))
  70. if [[ ! -z ${CONTAINER_ID} ]]; then
  71. for matched_container in "${CONTAINER_ID[@]}"; do
  72. CONTAINER_IPS=($(curl --silent --insecure https://dockerapi/containers/${matched_container}/json | jq -r '.NetworkSettings.Networks[].IPAddress'))
  73. for ip_match in "${CONTAINER_IPS[@]}"; do
  74. # grep will do nothing if one of these vars is empty
  75. [[ -z ${ip_match} ]] && continue
  76. [[ -z ${IPV4_NETWORK} ]] && continue
  77. # only return ips that are part of our network
  78. if ! grep -q ${IPV4_NETWORK} <(echo ${ip_match}); then
  79. continue
  80. else
  81. CONTAINER_IP=${ip_match}
  82. break
  83. fi
  84. done
  85. [[ ! -z ${CONTAINER_IP} ]] && break
  86. done
  87. fi
  88. fi
  89. LOOP_C=$((LOOP_C + 1))
  90. done
  91. [[ ${LOOP_C} -gt 5 ]] && echo 240.0.0.0 || echo ${CONTAINER_IP}
  92. }
  93. nginx_checks() {
  94. err_count=0
  95. diff_c=0
  96. THRESHOLD=16
  97. # Reduce error count by 2 after restarting an unhealthy container
  98. trap "[ ${err_count} -gt 1 ] && err_count=$(( ${err_count} - 2 ))" USR1
  99. while [ ${err_count} -lt ${THRESHOLD} ]; do
  100. host_ip=$(get_container_ip nginx-mailcow)
  101. err_c_cur=${err_count}
  102. /usr/lib/nagios/plugins/check_http -4 -H ${host_ip} -u / -p 8081 1>&2; err_count=$(( ${err_count} + $? ))
  103. [ ${err_c_cur} -eq ${err_count} ] && [ ! $((${err_count} - 1)) -lt 0 ] && err_count=$((${err_count} - 1)) diff_c=1
  104. [ ${err_c_cur} -ne ${err_count} ] && diff_c=$(( ${err_c_cur} - ${err_count} ))
  105. progress "Nginx" ${THRESHOLD} $(( ${THRESHOLD} - ${err_count} )) ${diff_c}
  106. diff_c=0
  107. sleep $(( ( RANDOM % 30 ) + 10 ))
  108. done
  109. return 1
  110. }
  111. unbound_checks() {
  112. err_count=0
  113. diff_c=0
  114. THRESHOLD=8
  115. # Reduce error count by 2 after restarting an unhealthy container
  116. trap "[ ${err_count} -gt 1 ] && err_count=$(( ${err_count} - 2 ))" USR1
  117. while [ ${err_count} -lt ${THRESHOLD} ]; do
  118. host_ip=$(get_container_ip unbound-mailcow)
  119. err_c_cur=${err_count}
  120. /usr/lib/nagios/plugins/check_dns -s ${host_ip} -H google.com 1>&2; err_count=$(( ${err_count} + $? ))
  121. DNSSEC=$(dig com +dnssec | egrep 'flags:.+ad')
  122. if [[ -z ${DNSSEC} ]]; then
  123. echo "DNSSEC failure" 1>&2
  124. err_count=$(( ${err_count} + 1))
  125. else
  126. echo "DNSSEC check succeeded" 1>&2
  127. fi
  128. [ ${err_c_cur} -eq ${err_count} ] && [ ! $((${err_count} - 1)) -lt 0 ] && err_count=$((${err_count} - 1)) diff_c=1
  129. [ ${err_c_cur} -ne ${err_count} ] && diff_c=$(( ${err_c_cur} - ${err_count} ))
  130. progress "Unbound" ${THRESHOLD} $(( ${THRESHOLD} - ${err_count} )) ${diff_c}
  131. diff_c=0
  132. sleep $(( ( RANDOM % 30 ) + 10 ))
  133. done
  134. return 1
  135. }
  136. mysql_checks() {
  137. err_count=0
  138. diff_c=0
  139. THRESHOLD=12
  140. # Reduce error count by 2 after restarting an unhealthy container
  141. trap "[ ${err_count} -gt 1 ] && err_count=$(( ${err_count} - 2 ))" USR1
  142. while [ ${err_count} -lt ${THRESHOLD} ]; do
  143. host_ip=$(get_container_ip mysql-mailcow)
  144. err_c_cur=${err_count}
  145. /usr/lib/nagios/plugins/check_mysql -s /var/run/mysqld/mysqld.sock -u ${DBUSER} -p ${DBPASS} -d ${DBNAME} 1>&2; err_count=$(( ${err_count} + $? ))
  146. /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" 1>&2; err_count=$(( ${err_count} + $? ))
  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 "MySQL/MariaDB" ${THRESHOLD} $(( ${THRESHOLD} - ${err_count} )) ${diff_c}
  150. diff_c=0
  151. sleep $(( ( RANDOM % 30 ) + 10 ))
  152. done
  153. return 1
  154. }
  155. sogo_checks() {
  156. err_count=0
  157. diff_c=0
  158. THRESHOLD=10
  159. # Reduce error count by 2 after restarting an unhealthy container
  160. trap "[ ${err_count} -gt 1 ] && err_count=$(( ${err_count} - 2 ))" USR1
  161. while [ ${err_count} -lt ${THRESHOLD} ]; do
  162. host_ip=$(get_container_ip sogo-mailcow)
  163. err_c_cur=${err_count}
  164. /usr/lib/nagios/plugins/check_http -4 -H ${host_ip} -u /SOGo.index/ -p 20000 -R "SOGo\.MainUI" 1>&2; err_count=$(( ${err_count} + $? ))
  165. [ ${err_c_cur} -eq ${err_count} ] && [ ! $((${err_count} - 1)) -lt 0 ] && err_count=$((${err_count} - 1)) diff_c=1
  166. [ ${err_c_cur} -ne ${err_count} ] && diff_c=$(( ${err_c_cur} - ${err_count} ))
  167. progress "SOGo" ${THRESHOLD} $(( ${THRESHOLD} - ${err_count} )) ${diff_c}
  168. diff_c=0
  169. sleep $(( ( RANDOM % 30 ) + 10 ))
  170. done
  171. return 1
  172. }
  173. postfix_checks() {
  174. err_count=0
  175. diff_c=0
  176. THRESHOLD=8
  177. # Reduce error count by 2 after restarting an unhealthy container
  178. trap "[ ${err_count} -gt 1 ] && err_count=$(( ${err_count} - 2 ))" USR1
  179. while [ ${err_count} -lt ${THRESHOLD} ]; do
  180. host_ip=$(get_container_ip postfix-mailcow)
  181. err_c_cur=${err_count}
  182. /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 1>&2; err_count=$(( ${err_count} + $? ))
  183. /usr/lib/nagios/plugins/check_smtp -4 -H ${host_ip} -p 589 -S 1>&2; err_count=$(( ${err_count} + $? ))
  184. [ ${err_c_cur} -eq ${err_count} ] && [ ! $((${err_count} - 1)) -lt 0 ] && err_count=$((${err_count} - 1)) diff_c=1
  185. [ ${err_c_cur} -ne ${err_count} ] && diff_c=$(( ${err_c_cur} - ${err_count} ))
  186. progress "Postfix" ${THRESHOLD} $(( ${THRESHOLD} - ${err_count} )) ${diff_c}
  187. diff_c=0
  188. sleep $(( ( RANDOM % 30 ) + 10 ))
  189. done
  190. return 1
  191. }
  192. clamd_checks() {
  193. err_count=0
  194. diff_c=0
  195. THRESHOLD=5
  196. # Reduce error count by 2 after restarting an unhealthy container
  197. trap "[ ${err_count} -gt 1 ] && err_count=$(( ${err_count} - 2 ))" USR1
  198. while [ ${err_count} -lt ${THRESHOLD} ]; do
  199. host_ip=$(get_container_ip clamd-mailcow)
  200. err_c_cur=${err_count}
  201. /usr/lib/nagios/plugins/check_clamd -4 -H ${host_ip} 1>&2; err_count=$(( ${err_count} + $? ))
  202. [ ${err_c_cur} -eq ${err_count} ] && [ ! $((${err_count} - 1)) -lt 0 ] && err_count=$((${err_count} - 1)) diff_c=1
  203. [ ${err_c_cur} -ne ${err_count} ] && diff_c=$(( ${err_c_cur} - ${err_count} ))
  204. progress "Clamd" ${THRESHOLD} $(( ${THRESHOLD} - ${err_count} )) ${diff_c}
  205. diff_c=0
  206. # Don't check Clamd too often
  207. sleep 1800
  208. done
  209. return 1
  210. }
  211. dovecot_checks() {
  212. err_count=0
  213. diff_c=0
  214. THRESHOLD=20
  215. # Reduce error count by 2 after restarting an unhealthy container
  216. trap "[ ${err_count} -gt 1 ] && err_count=$(( ${err_count} - 2 ))" USR1
  217. while [ ${err_count} -lt ${THRESHOLD} ]; do
  218. host_ip=$(get_container_ip dovecot-mailcow)
  219. err_c_cur=${err_count}
  220. /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" 1>&2; err_count=$(( ${err_count} + $? ))
  221. /usr/lib/nagios/plugins/check_imap -4 -H ${host_ip} -p 993 -S -e "OK " 1>&2; err_count=$(( ${err_count} + $? ))
  222. /usr/lib/nagios/plugins/check_imap -4 -H ${host_ip} -p 143 -e "OK " 1>&2; err_count=$(( ${err_count} + $? ))
  223. /usr/lib/nagios/plugins/check_tcp -4 -H ${host_ip} -p 10001 -e "VERSION" 1>&2; err_count=$(( ${err_count} + $? ))
  224. /usr/lib/nagios/plugins/check_tcp -4 -H ${host_ip} -p 4190 -e "Dovecot ready" 1>&2; err_count=$(( ${err_count} + $? ))
  225. [ ${err_c_cur} -eq ${err_count} ] && [ ! $((${err_count} - 1)) -lt 0 ] && err_count=$((${err_count} - 1)) diff_c=1
  226. [ ${err_c_cur} -ne ${err_count} ] && diff_c=$(( ${err_c_cur} - ${err_count} ))
  227. progress "Dovecot" ${THRESHOLD} $(( ${THRESHOLD} - ${err_count} )) ${diff_c}
  228. diff_c=0
  229. sleep $(( ( RANDOM % 30 ) + 10 ))
  230. done
  231. return 1
  232. }
  233. phpfpm_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. host_ip=$(get_container_ip php-fpm-mailcow)
  241. err_c_cur=${err_count}
  242. nc -z ${host_ip} 9001 ; err_count=$(( ${err_count} + ($? * 2)))
  243. nc -z ${host_ip} 9002 ; err_count=$(( ${err_count} + ($? * 2)))
  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 "PHP-FPM" ${THRESHOLD} $(( ${THRESHOLD} - ${err_count} )) ${diff_c}
  247. diff_c=0
  248. sleep $(( ( RANDOM % 30 ) + 10 ))
  249. done
  250. return 1
  251. }
  252. cert_checks() {
  253. err_count=0
  254. diff_c=0
  255. THRESHOLD=1
  256. # Reduce error count by 2 after restarting an unhealthy container
  257. trap "[ ${err_count} -gt 1 ] && err_count=$(( ${err_count} - 2 ))" USR1
  258. while [ ${err_count} -lt ${THRESHOLD} ]; do
  259. host_ip=$(get_container_ip nginx-mailcow)
  260. err_c_cur=${err_count}
  261. /usr/lib/nagios/plugins/check_http -H ${host_ip} -p ${HTTPS_PORT} -C 15 1>&2; err_count=$(( ${err_count} + $? ))
  262. [ ${err_c_cur} -eq ${err_count} ] && [ ! $((${err_count} - 1)) -lt 0 ] && err_count=$((${err_count} - 1)) diff_c=1
  263. [ ${err_c_cur} -ne ${err_count} ] && diff_c=$(( ${err_c_cur} - ${err_count} ))
  264. progress "TLS certificate" ${THRESHOLD} $(( ${THRESHOLD} - ${err_count} )) ${diff_c}
  265. diff_c=0
  266. # Sleep 1 day, fixme: 1 day lag
  267. sleep 86400
  268. done
  269. return 1
  270. }
  271. rspamd_checks() {
  272. err_count=0
  273. diff_c=0
  274. THRESHOLD=5
  275. # Reduce error count by 2 after restarting an unhealthy container
  276. trap "[ ${err_count} -gt 1 ] && err_count=$(( ${err_count} - 2 ))" USR1
  277. while [ ${err_count} -lt ${THRESHOLD} ]; do
  278. host_ip=$(get_container_ip rspamd-mailcow)
  279. err_c_cur=${err_count}
  280. SCORE=$(/usr/bin/curl -s --data-binary @- --unix-socket /var/lib/rspamd/rspamd.sock http://rspamd/scan -d '
  281. To: null@localhost
  282. From: watchdog@localhost
  283. Empty
  284. ' | jq -rc .required_score)
  285. if [[ ${SCORE} != "9999" ]]; then
  286. echo "Rspamd settings check failed" 1>&2
  287. err_count=$(( ${err_count} + 1))
  288. else
  289. echo "Rspamd settings check succeeded" 1>&2
  290. fi
  291. [ ${err_c_cur} -eq ${err_count} ] && [ ! $((${err_count} - 1)) -lt 0 ] && err_count=$((${err_count} - 1)) diff_c=1
  292. [ ${err_c_cur} -ne ${err_count} ] && diff_c=$(( ${err_c_cur} - ${err_count} ))
  293. progress "Rspamd" ${THRESHOLD} $(( ${THRESHOLD} - ${err_count} )) ${diff_c}
  294. diff_c=0
  295. sleep $(( ( RANDOM % 30 ) + 10 ))
  296. done
  297. return 1
  298. }
  299. # Create watchdog agents
  300. (
  301. while true; do
  302. if ! nginx_checks; then
  303. log_msg "Nginx hit error limit"
  304. [[ ! -z ${WATCHDOG_NOTIFY_EMAIL} ]] && mail_error "${WATCHDOG_NOTIFY_EMAIL}" "nginx-mailcow"
  305. echo nginx-mailcow > /tmp/com_pipe
  306. fi
  307. done
  308. ) &
  309. BACKGROUND_TASKS+=($!)
  310. (
  311. while true; do
  312. if ! mysql_checks; then
  313. log_msg "MySQL hit error limit"
  314. [[ ! -z ${WATCHDOG_NOTIFY_EMAIL} ]] && mail_error "${WATCHDOG_NOTIFY_EMAIL}" "mysql-mailcow"
  315. echo mysql-mailcow > /tmp/com_pipe
  316. fi
  317. done
  318. ) &
  319. BACKGROUND_TASKS+=($!)
  320. (
  321. while true; do
  322. if ! phpfpm_checks; then
  323. log_msg "PHP-FPM hit error limit"
  324. [[ ! -z ${WATCHDOG_NOTIFY_EMAIL} ]] && mail_error "${WATCHDOG_NOTIFY_EMAIL}" "php-fpm-mailcow"
  325. echo php-fpm-mailcow > /tmp/com_pipe
  326. fi
  327. done
  328. ) &
  329. BACKGROUND_TASKS+=($!)
  330. (
  331. while true; do
  332. if ! sogo_checks; then
  333. log_msg "SOGo hit error limit"
  334. [[ ! -z ${WATCHDOG_NOTIFY_EMAIL} ]] && mail_error "${WATCHDOG_NOTIFY_EMAIL}" "sogo-mailcow"
  335. echo sogo-mailcow > /tmp/com_pipe
  336. fi
  337. done
  338. ) &
  339. BACKGROUND_TASKS+=($!)
  340. if [ ${CHECK_UNBOUND} -eq 1 ]; then
  341. (
  342. while true; do
  343. if ! unbound_checks; then
  344. log_msg "Unbound hit error limit"
  345. [[ ! -z ${WATCHDOG_NOTIFY_EMAIL} ]] && mail_error "${WATCHDOG_NOTIFY_EMAIL}" "unbound-mailcow"
  346. echo unbound-mailcow > /tmp/com_pipe
  347. fi
  348. done
  349. ) &
  350. BACKGROUND_TASKS+=($!)
  351. fi
  352. if [[ "${SKIP_CLAMD}" =~ ^([nN][oO]|[nN])+$ ]]; then
  353. (
  354. while true; do
  355. if ! clamd_checks; then
  356. log_msg "Clamd hit error limit"
  357. [[ ! -z ${WATCHDOG_NOTIFY_EMAIL} ]] && mail_error "${WATCHDOG_NOTIFY_EMAIL}" "clamd-mailcow"
  358. echo clamd-mailcow > /tmp/com_pipe
  359. fi
  360. done
  361. ) &
  362. BACKGROUND_TASKS+=($!)
  363. fi
  364. (
  365. while true; do
  366. if ! cert_checks; then
  367. log_msg "TLS certificate hit error limit"
  368. [[ ! -z ${WATCHDOG_NOTIFY_EMAIL} ]] && mail_error "${WATCHDOG_NOTIFY_EMAIL}" "TLS check" "TLS certificate expires soon!"
  369. fi
  370. done
  371. ) &
  372. BACKGROUND_TASKS+=($!)
  373. (
  374. while true; do
  375. if ! postfix_checks; then
  376. log_msg "Postfix hit error limit"
  377. [[ ! -z ${WATCHDOG_NOTIFY_EMAIL} ]] && mail_error "${WATCHDOG_NOTIFY_EMAIL}" "postfix-mailcow"
  378. echo postfix-mailcow > /tmp/com_pipe
  379. fi
  380. done
  381. ) &
  382. BACKGROUND_TASKS+=($!)
  383. (
  384. while true; do
  385. if ! dovecot_checks; then
  386. log_msg "Dovecot hit error limit"
  387. [[ ! -z ${WATCHDOG_NOTIFY_EMAIL} ]] && mail_error "${WATCHDOG_NOTIFY_EMAIL}" "dovecot-mailcow"
  388. echo dovecot-mailcow > /tmp/com_pipe
  389. fi
  390. done
  391. ) &
  392. BACKGROUND_TASKS+=($!)
  393. (
  394. while true; do
  395. if ! rspamd_checks; then
  396. log_msg "Rspamd hit error limit"
  397. [[ ! -z ${WATCHDOG_NOTIFY_EMAIL} ]] && mail_error "${WATCHDOG_NOTIFY_EMAIL}" "rspamd-mailcow"
  398. echo rspamd-mailcow > /tmp/com_pipe
  399. fi
  400. done
  401. ) &
  402. BACKGROUND_TASKS+=($!)
  403. # Monitor watchdog agents, stop script when agents fails and wait for respawn by Docker (restart:always:n)
  404. (
  405. while true; do
  406. for bg_task in ${BACKGROUND_TASKS[*]}; do
  407. if ! kill -0 ${bg_task} 1>&2; then
  408. log_msg "Worker ${bg_task} died, stopping watchdog and waiting for respawn..."
  409. kill -TERM 1
  410. fi
  411. sleep 10
  412. done
  413. done
  414. ) &
  415. # Monitor dockerapi
  416. (
  417. while true; do
  418. while nc -z dockerapi 443; do
  419. sleep 3
  420. done
  421. log_msg "Cannot find dockerapi-mailcow, waiting to recover..."
  422. kill -STOP ${BACKGROUND_TASKS[*]}
  423. until nc -z dockerapi 443; do
  424. sleep 3
  425. done
  426. kill -CONT ${BACKGROUND_TASKS[*]}
  427. kill -USR1 ${BACKGROUND_TASKS[*]}
  428. done
  429. ) &
  430. # Restart container when threshold limit reached
  431. while true; do
  432. CONTAINER_ID=
  433. read com_pipe_answer </tmp/com_pipe
  434. if [[ ${com_pipe_answer} =~ .+-mailcow ]]; then
  435. kill -STOP ${BACKGROUND_TASKS[*]}
  436. sleep 3
  437. 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")
  438. if [[ ! -z ${CONTAINER_ID} ]]; then
  439. log_msg "Sending restart command to ${CONTAINER_ID}..."
  440. curl --silent --insecure -XPOST https://dockerapi/containers/${CONTAINER_ID}/restart
  441. fi
  442. log_msg "Wait for restarted container to settle and continue watching..."
  443. sleep 30s
  444. kill -CONT ${BACKGROUND_TASKS[*]}
  445. kill -USR1 ${BACKGROUND_TASKS[*]}
  446. fi
  447. done