watchdog.sh 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550
  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. 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. rspamd_checks() {
  336. err_count=0
  337. diff_c=0
  338. THRESHOLD=5
  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. cat /dev/null > /tmp/rspamd-mailcow
  343. host_ip=$(get_container_ip rspamd-mailcow)
  344. err_c_cur=${err_count}
  345. SCORE=$(/usr/bin/curl -s --data-binary @- --unix-socket /var/lib/rspamd/rspamd.sock http://rspamd/scan -d '
  346. To: null@localhost
  347. From: watchdog@localhost
  348. Empty
  349. ' | jq -rc .required_score)
  350. if [[ ${SCORE} != "9999" ]]; then
  351. echo "Rspamd settings check failed" 2>> /tmp/rspamd-mailcow 1>&2
  352. err_count=$(( ${err_count} + 1))
  353. else
  354. echo "Rspamd settings check succeeded" 2>> /tmp/rspamd-mailcow 1>&2
  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 "Rspamd" ${THRESHOLD} $(( ${THRESHOLD} - ${err_count} )) ${diff_c}
  359. diff_c=0
  360. sleep $(( ( RANDOM % 30 ) + 10 ))
  361. done
  362. return 1
  363. }
  364. # Create watchdog agents
  365. (
  366. while true; do
  367. if ! nginx_checks; then
  368. log_msg "Nginx hit error limit"
  369. echo nginx-mailcow > /tmp/com_pipe
  370. fi
  371. done
  372. ) &
  373. BACKGROUND_TASKS+=($!)
  374. (
  375. while true; do
  376. if ! mysql_checks; then
  377. log_msg "MySQL hit error limit"
  378. echo mysql-mailcow > /tmp/com_pipe
  379. fi
  380. done
  381. ) &
  382. BACKGROUND_TASKS+=($!)
  383. (
  384. while true; do
  385. if ! phpfpm_checks; then
  386. log_msg "PHP-FPM hit error limit"
  387. echo php-fpm-mailcow > /tmp/com_pipe
  388. fi
  389. done
  390. ) &
  391. BACKGROUND_TASKS+=($!)
  392. (
  393. while true; do
  394. if ! sogo_checks; then
  395. log_msg "SOGo hit error limit"
  396. echo sogo-mailcow > /tmp/com_pipe
  397. fi
  398. done
  399. ) &
  400. BACKGROUND_TASKS+=($!)
  401. if [ ${CHECK_UNBOUND} -eq 1 ]; then
  402. (
  403. while true; do
  404. if ! unbound_checks; then
  405. log_msg "Unbound hit error limit"
  406. echo unbound-mailcow > /tmp/com_pipe
  407. fi
  408. done
  409. ) &
  410. BACKGROUND_TASKS+=($!)
  411. fi
  412. if [[ "${SKIP_CLAMD}" =~ ^([nN][oO]|[nN])+$ ]]; then
  413. (
  414. while true; do
  415. if ! clamd_checks; then
  416. log_msg "Clamd hit error limit"
  417. echo clamd-mailcow > /tmp/com_pipe
  418. fi
  419. done
  420. ) &
  421. BACKGROUND_TASKS+=($!)
  422. fi
  423. (
  424. while true; do
  425. if ! postfix_checks; then
  426. log_msg "Postfix hit error limit"
  427. echo postfix-mailcow > /tmp/com_pipe
  428. fi
  429. done
  430. ) &
  431. BACKGROUND_TASKS+=($!)
  432. (
  433. while true; do
  434. if ! dovecot_checks; then
  435. log_msg "Dovecot hit error limit"
  436. echo dovecot-mailcow > /tmp/com_pipe
  437. fi
  438. done
  439. ) &
  440. BACKGROUND_TASKS+=($!)
  441. (
  442. while true; do
  443. if ! rspamd_checks; then
  444. log_msg "Rspamd hit error limit"
  445. echo rspamd-mailcow > /tmp/com_pipe
  446. fi
  447. done
  448. ) &
  449. BACKGROUND_TASKS+=($!)
  450. (
  451. while true; do
  452. if ! ratelimit_checks; then
  453. log_msg "Ratelimit hit error limit"
  454. echo ratelimit > /tmp/com_pipe
  455. fi
  456. done
  457. ) &
  458. BACKGROUND_TASKS+=($!)
  459. # Monitor watchdog agents, stop script when agents fails and wait for respawn by Docker (restart:always:n)
  460. (
  461. while true; do
  462. for bg_task in ${BACKGROUND_TASKS[*]}; do
  463. if ! kill -0 ${bg_task} 1>&2; then
  464. log_msg "Worker ${bg_task} died, stopping watchdog and waiting for respawn..."
  465. kill -TERM 1
  466. fi
  467. sleep 10
  468. done
  469. done
  470. ) &
  471. # Monitor dockerapi
  472. (
  473. while true; do
  474. while nc -z dockerapi 443; do
  475. sleep 3
  476. done
  477. log_msg "Cannot find dockerapi-mailcow, waiting to recover..."
  478. kill -STOP ${BACKGROUND_TASKS[*]}
  479. until nc -z dockerapi 443; do
  480. sleep 3
  481. done
  482. kill -CONT ${BACKGROUND_TASKS[*]}
  483. kill -USR1 ${BACKGROUND_TASKS[*]}
  484. done
  485. ) &
  486. # Restart container when threshold limit reached
  487. while true; do
  488. CONTAINER_ID=
  489. HAS_INITDB=
  490. read com_pipe_answer </tmp/com_pipe
  491. if [[ ${com_pipe_answer} == "ratelimit" ]]; then
  492. log_msg "At least one ratelimit was applied"
  493. [[ ! -z ${WATCHDOG_NOTIFY_EMAIL} ]] && mail_error "${com_pipe_answer}" "No further information available."
  494. elif [[ ${com_pipe_answer} =~ .+-mailcow ]]; then
  495. kill -STOP ${BACKGROUND_TASKS[*]}
  496. sleep 3
  497. 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")
  498. if [[ ! -z ${CONTAINER_ID} ]]; then
  499. if [[ "${com_pipe_answer}" == "php-fpm-mailcow" ]]; then
  500. 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)
  501. fi
  502. S_RUNNING=$(($(date +%s) - $(curl --silent --insecure https://dockerapi/containers/${CONTAINER_ID}/json | jq .State.StartedAt | xargs -n1 date +%s -d)))
  503. if [ ${S_RUNNING} -lt 120 ]; then
  504. log_msg "Container is running for less than 120 seconds, skipping action..."
  505. elif [[ ! -z ${HAS_INITDB} ]]; then
  506. log_msg "Database is being initialized by php-fpm-mailcow, not restarting but delaying checks for a minute..."
  507. sleep 60
  508. else
  509. log_msg "Sending restart command to ${CONTAINER_ID}..."
  510. curl --silent --insecure -XPOST https://dockerapi/containers/${CONTAINER_ID}/restart
  511. [[ ! -z ${WATCHDOG_NOTIFY_EMAIL} ]] && mail_error "${com_pipe_answer}"
  512. log_msg "Wait for restarted container to settle and continue watching..."
  513. sleep 30
  514. fi
  515. fi
  516. kill -CONT ${BACKGROUND_TASKS[*]}
  517. kill -USR1 ${BACKGROUND_TASKS[*]}
  518. fi
  519. done