watchdog.sh 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927
  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 30
  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. # Wait for containers
  18. while ! mysqladmin status --socket=/var/run/mysqld/mysqld.sock -u${DBUSER} -p${DBPASS} --silent; do
  19. echo "Waiting for SQL..."
  20. sleep 2
  21. done
  22. until [[ $(redis-cli -h redis-mailcow PING) == "PONG" ]]; do
  23. echo "Waiting for Redis..."
  24. sleep 2
  25. done
  26. # Do not attempt to write to slave
  27. if [[ ! -z ${REDIS_SLAVEOF_IP} ]]; then
  28. REDIS_CMDLINE="redis-cli -h ${REDIS_SLAVEOF_IP} -p ${REDIS_SLAVEOF_PORT}"
  29. else
  30. REDIS_CMDLINE="redis-cli -h redis -p 6379"
  31. fi
  32. ${REDIS_CMDLINE} DEL F2B_RES > /dev/null
  33. # Common functions
  34. get_ipv6(){
  35. local IPV6=
  36. local IPV6_SRCS=
  37. local TRY=
  38. IPV6_SRCS[0]="ip6.korves.net"
  39. IPV6_SRCS[1]="ip6.mailcow.email"
  40. until [[ ! -z ${IPV6} ]] || [[ ${TRY} -ge 10 ]]; do
  41. IPV6=$(curl --connect-timeout 3 -m 10 -L6s ${IPV6_SRCS[$RANDOM % ${#IPV6_SRCS[@]} ]} | grep "^\([0-9a-fA-F]\{0,4\}:\)\{1,7\}[0-9a-fA-F]\{0,4\}$")
  42. [[ ! -z ${TRY} ]] && sleep 1
  43. TRY=$((TRY+1))
  44. done
  45. echo ${IPV6}
  46. }
  47. array_diff() {
  48. # https://stackoverflow.com/questions/2312762, Alex Offshore
  49. eval local ARR1=\(\"\${$2[@]}\"\)
  50. eval local ARR2=\(\"\${$3[@]}\"\)
  51. local IFS=$'\n'
  52. mapfile -t $1 < <(comm -23 <(echo "${ARR1[*]}" | sort) <(echo "${ARR2[*]}" | sort))
  53. }
  54. progress() {
  55. SERVICE=${1}
  56. TOTAL=${2}
  57. CURRENT=${3}
  58. DIFF=${4}
  59. [[ -z ${DIFF} ]] && DIFF=0
  60. [[ -z ${TOTAL} || -z ${CURRENT} ]] && return
  61. [[ ${CURRENT} -gt ${TOTAL} ]] && return
  62. [[ ${CURRENT} -lt 0 ]] && CURRENT=0
  63. PERCENT=$(( 200 * ${CURRENT} / ${TOTAL} % 2 + 100 * ${CURRENT} / ${TOTAL} ))
  64. ${REDIS_CMDLINE} LPUSH WATCHDOG_LOG "{\"time\":\"$(date +%s)\",\"service\":\"${SERVICE}\",\"lvl\":\"${PERCENT}\",\"hpnow\":\"${CURRENT}\",\"hptotal\":\"${TOTAL}\",\"hpdiff\":\"${DIFF}\"}" > /dev/null
  65. log_msg "${SERVICE} health level: ${PERCENT}% (${CURRENT}/${TOTAL}), health trend: ${DIFF}" no_redis
  66. # Return 10 to indicate a dead service
  67. [ ${CURRENT} -le 0 ] && return 10
  68. }
  69. log_msg() {
  70. if [[ ${2} != "no_redis" ]]; then
  71. ${REDIS_CMDLINE} LPUSH WATCHDOG_LOG "{\"time\":\"$(date +%s)\",\"message\":\"$(printf '%s' "${1}" | \
  72. tr '\r\n%&;$"_[]{}-' ' ')\"}" > /dev/null
  73. fi
  74. echo $(date) $(printf '%s\n' "${1}")
  75. }
  76. function mail_error() {
  77. [[ -z ${1} ]] && return 1
  78. [[ -z ${2} ]] && BODY="Service was restarted on $(date), please check your mailcow installation." || BODY="$(date) - ${2}"
  79. WATCHDOG_NOTIFY_EMAIL=$(echo "${WATCHDOG_NOTIFY_EMAIL}" | sed 's/"//;s|"$||')
  80. # Some exceptions for subject and body formats
  81. if [[ ${1} == "fail2ban" ]]; then
  82. SUBJECT="${BODY}"
  83. BODY="Please see netfilter-mailcow for more details and triggered rules."
  84. else
  85. SUBJECT="Watchdog ALERT: ${1}"
  86. fi
  87. IFS=',' read -r -a MAIL_RCPTS <<< "${WATCHDOG_NOTIFY_EMAIL}"
  88. for rcpt in "${MAIL_RCPTS[@]}"; do
  89. RCPT_DOMAIN=
  90. #RCPT_MX=
  91. RCPT_DOMAIN=$(echo ${rcpt} | awk -F @ {'print $NF'})
  92. # Latest smtp-cli looks up mx via dns
  93. #RCPT_MX=$(dig +short ${RCPT_DOMAIN} mx | sort -n | awk '{print $2; exit}')
  94. #if [[ -z ${RCPT_MX} ]]; then
  95. # log_msg "Cannot determine MX for ${rcpt}, skipping email notification..."
  96. # return 1
  97. #fi
  98. [ -f "/tmp/${1}" ] && BODY="/tmp/${1}"
  99. timeout 10s ./smtp-cli --missing-modules-ok \
  100. --charset=UTF-8 \
  101. --subject="${SUBJECT}" \
  102. --body-plain="${BODY}" \
  103. --to=${rcpt} \
  104. --from="watchdog@${MAILCOW_HOSTNAME}" \
  105. --hello-host=${MAILCOW_HOSTNAME} \
  106. --ipv4
  107. #--server="${RCPT_MX}"
  108. log_msg "Sent notification email to ${rcpt}"
  109. done
  110. }
  111. get_container_ip() {
  112. # ${1} is container
  113. CONTAINER_ID=()
  114. CONTAINER_IPS=()
  115. CONTAINER_IP=
  116. LOOP_C=1
  117. until [[ ${CONTAINER_IP} =~ ^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}$ ]] || [[ ${LOOP_C} -gt 5 ]]; do
  118. if [ ${IP_BY_DOCKER_API} -eq 0 ]; then
  119. CONTAINER_IP=$(dig a "${1}" +short)
  120. else
  121. sleep 0.5
  122. # get long container id for exact match
  123. 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"))
  124. # returned id can have multiple elements (if scaled), shuffle for random test
  125. CONTAINER_ID=($(printf "%s\n" "${CONTAINER_ID[@]}" | shuf))
  126. if [[ ! -z ${CONTAINER_ID} ]]; then
  127. for matched_container in "${CONTAINER_ID[@]}"; do
  128. CONTAINER_IPS=($(curl --silent --insecure https://dockerapi/containers/${matched_container}/json | jq -r '.NetworkSettings.Networks[].IPAddress'))
  129. for ip_match in "${CONTAINER_IPS[@]}"; do
  130. # grep will do nothing if one of these vars is empty
  131. [[ -z ${ip_match} ]] && continue
  132. [[ -z ${IPV4_NETWORK} ]] && continue
  133. # only return ips that are part of our network
  134. if ! grep -q ${IPV4_NETWORK} <(echo ${ip_match}); then
  135. continue
  136. else
  137. CONTAINER_IP=${ip_match}
  138. break
  139. fi
  140. done
  141. [[ ! -z ${CONTAINER_IP} ]] && break
  142. done
  143. fi
  144. fi
  145. LOOP_C=$((LOOP_C + 1))
  146. done
  147. [[ ${LOOP_C} -gt 5 ]] && echo 240.0.0.0 || echo ${CONTAINER_IP}
  148. }
  149. # One-time check
  150. if grep -qi "$(echo ${IPV6_NETWORK} | cut -d: -f1-3)" <<< "$(ip a s)"; then
  151. if [[ -z "$(get_ipv6)" ]]; then
  152. mail_error "ipv6-config" "enable_ipv6 is true in docker-compose.yml, but an IPv6 link could not be established. Please verify your IPv6 connection."
  153. fi
  154. fi
  155. external_checks() {
  156. err_count=0
  157. diff_c=0
  158. THRESHOLD=${EXTERNAL_CHECKS_THRESHOLD}
  159. # Reduce error count by 2 after restarting an unhealthy container
  160. GUID=$(mysql -u${DBUSER} -p${DBPASS} ${DBNAME} -e "SELECT version FROM versions WHERE application = 'GUID'" -BN)
  161. trap "[ ${err_count} -gt 1 ] && err_count=$(( ${err_count} - 2 ))" USR1
  162. while [ ${err_count} -lt ${THRESHOLD} ]; do
  163. err_c_cur=${err_count}
  164. CHECK_REPONSE="$(curl --connect-timeout 3 -m 10 -4 -s https://checks.mailcow.email -X POST -dguid=${GUID} 2> /dev/null)"
  165. if [[ ! -z "${CHECK_REPONSE}" ]] && [[ "$(echo ${CHECK_REPONSE} | jq -r .response)" == "critical" ]]; then
  166. echo ${CHECK_REPONSE} | jq -r .out > /tmp/external_checks
  167. err_count=$(( ${err_count} + 1 ))
  168. fi
  169. CHECK_REPONSE6="$(curl --connect-timeout 3 -m 10 -6 -s https://checks.mailcow.email -X POST -dguid=${GUID} 2> /dev/null)"
  170. if [[ ! -z "${CHECK_REPONSE6}" ]] && [[ "$(echo ${CHECK_REPONSE6} | jq -r .response)" == "critical" ]]; then
  171. echo ${CHECK_REPONSE} | jq -r .out > /tmp/external_checks
  172. err_count=$(( ${err_count} + 1 ))
  173. fi
  174. [ ${err_c_cur} -eq ${err_count} ] && [ ! $((${err_count} - 1)) -lt 0 ] && err_count=$((${err_count} - 1)) diff_c=1
  175. [ ${err_c_cur} -ne ${err_count} ] && diff_c=$(( ${err_c_cur} - ${err_count} ))
  176. progress "External checks" ${THRESHOLD} $(( ${THRESHOLD} - ${err_count} )) ${diff_c}
  177. if [[ $? == 10 ]]; then
  178. diff_c=0
  179. sleep 60
  180. else
  181. diff_c=0
  182. sleep $(( ( RANDOM % 20 ) + 120 ))
  183. fi
  184. done
  185. return 1
  186. }
  187. nginx_checks() {
  188. err_count=0
  189. diff_c=0
  190. THRESHOLD=${NGINX_THRESHOLD}
  191. # Reduce error count by 2 after restarting an unhealthy container
  192. trap "[ ${err_count} -gt 1 ] && err_count=$(( ${err_count} - 2 ))" USR1
  193. while [ ${err_count} -lt ${THRESHOLD} ]; do
  194. touch /tmp/nginx-mailcow; echo "$(tail -50 /tmp/nginx-mailcow)" > /tmp/nginx-mailcow
  195. host_ip=$(get_container_ip nginx-mailcow)
  196. err_c_cur=${err_count}
  197. /usr/lib/nagios/plugins/check_http -4 -H ${host_ip} -u / -p 8081 2>> /tmp/nginx-mailcow 1>&2; err_count=$(( ${err_count} + $? ))
  198. [ ${err_c_cur} -eq ${err_count} ] && [ ! $((${err_count} - 1)) -lt 0 ] && err_count=$((${err_count} - 1)) diff_c=1
  199. [ ${err_c_cur} -ne ${err_count} ] && diff_c=$(( ${err_c_cur} - ${err_count} ))
  200. progress "Nginx" ${THRESHOLD} $(( ${THRESHOLD} - ${err_count} )) ${diff_c}
  201. if [[ $? == 10 ]]; then
  202. diff_c=0
  203. sleep 1
  204. else
  205. diff_c=0
  206. sleep $(( ( RANDOM % 60 ) + 20 ))
  207. fi
  208. done
  209. return 1
  210. }
  211. unbound_checks() {
  212. err_count=0
  213. diff_c=0
  214. THRESHOLD=${UNBOUND_THRESHOLD}
  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. touch /tmp/unbound-mailcow; echo "$(tail -50 /tmp/unbound-mailcow)" > /tmp/unbound-mailcow
  219. host_ip=$(get_container_ip unbound-mailcow)
  220. err_c_cur=${err_count}
  221. /usr/lib/nagios/plugins/check_dns -s ${host_ip} -H stackoverflow.com 2>> /tmp/unbound-mailcow 1>&2; err_count=$(( ${err_count} + $? ))
  222. DNSSEC=$(dig com +dnssec | egrep 'flags:.+ad')
  223. if [[ -z ${DNSSEC} ]]; then
  224. echo "DNSSEC failure" 2>> /tmp/unbound-mailcow 1>&2
  225. err_count=$(( ${err_count} + 1))
  226. else
  227. echo "DNSSEC check succeeded" 2>> /tmp/unbound-mailcow 1>&2
  228. fi
  229. [ ${err_c_cur} -eq ${err_count} ] && [ ! $((${err_count} - 1)) -lt 0 ] && err_count=$((${err_count} - 1)) diff_c=1
  230. [ ${err_c_cur} -ne ${err_count} ] && diff_c=$(( ${err_c_cur} - ${err_count} ))
  231. progress "Unbound" ${THRESHOLD} $(( ${THRESHOLD} - ${err_count} )) ${diff_c}
  232. if [[ $? == 10 ]]; then
  233. diff_c=0
  234. sleep 1
  235. else
  236. diff_c=0
  237. sleep $(( ( RANDOM % 60 ) + 20 ))
  238. fi
  239. done
  240. return 1
  241. }
  242. redis_checks() {
  243. # A check for the local redis container
  244. err_count=0
  245. diff_c=0
  246. THRESHOLD=${REDIS_THRESHOLD}
  247. # Reduce error count by 2 after restarting an unhealthy container
  248. trap "[ ${err_count} -gt 1 ] && err_count=$(( ${err_count} - 2 ))" USR1
  249. while [ ${err_count} -lt ${THRESHOLD} ]; do
  250. touch /tmp/redis-mailcow; echo "$(tail -50 /tmp/redis-mailcow)" > /tmp/redis-mailcow
  251. host_ip=$(get_container_ip redis-mailcow)
  252. err_c_cur=${err_count}
  253. /usr/lib/nagios/plugins/check_tcp -4 -H redis-mailcow -p 6379 -E -s "PING\n" -q "QUIT" -e "PONG" 2>> /tmp/redis-mailcow 1>&2; err_count=$(( ${err_count} + $? ))
  254. [ ${err_c_cur} -eq ${err_count} ] && [ ! $((${err_count} - 1)) -lt 0 ] && err_count=$((${err_count} - 1)) diff_c=1
  255. [ ${err_c_cur} -ne ${err_count} ] && diff_c=$(( ${err_c_cur} - ${err_count} ))
  256. progress "Redis" ${THRESHOLD} $(( ${THRESHOLD} - ${err_count} )) ${diff_c}
  257. if [[ $? == 10 ]]; then
  258. diff_c=0
  259. sleep 1
  260. else
  261. diff_c=0
  262. sleep $(( ( RANDOM % 60 ) + 20 ))
  263. fi
  264. done
  265. return 1
  266. }
  267. mysql_checks() {
  268. err_count=0
  269. diff_c=0
  270. THRESHOLD=${MYSQL_THRESHOLD}
  271. # Reduce error count by 2 after restarting an unhealthy container
  272. trap "[ ${err_count} -gt 1 ] && err_count=$(( ${err_count} - 2 ))" USR1
  273. while [ ${err_count} -lt ${THRESHOLD} ]; do
  274. touch /tmp/mysql-mailcow; echo "$(tail -50 /tmp/mysql-mailcow)" > /tmp/mysql-mailcow
  275. host_ip=$(get_container_ip mysql-mailcow)
  276. err_c_cur=${err_count}
  277. /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} + $? ))
  278. /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} + $? ))
  279. [ ${err_c_cur} -eq ${err_count} ] && [ ! $((${err_count} - 1)) -lt 0 ] && err_count=$((${err_count} - 1)) diff_c=1
  280. [ ${err_c_cur} -ne ${err_count} ] && diff_c=$(( ${err_c_cur} - ${err_count} ))
  281. progress "MySQL/MariaDB" ${THRESHOLD} $(( ${THRESHOLD} - ${err_count} )) ${diff_c}
  282. if [[ $? == 10 ]]; then
  283. diff_c=0
  284. sleep 1
  285. else
  286. diff_c=0
  287. sleep $(( ( RANDOM % 60 ) + 20 ))
  288. fi
  289. done
  290. return 1
  291. }
  292. sogo_checks() {
  293. err_count=0
  294. diff_c=0
  295. THRESHOLD=${SOGO_THRESHOLD}
  296. # Reduce error count by 2 after restarting an unhealthy container
  297. trap "[ ${err_count} -gt 1 ] && err_count=$(( ${err_count} - 2 ))" USR1
  298. while [ ${err_count} -lt ${THRESHOLD} ]; do
  299. touch /tmp/sogo-mailcow; echo "$(tail -50 /tmp/sogo-mailcow)" > /tmp/sogo-mailcow
  300. host_ip=$(get_container_ip sogo-mailcow)
  301. err_c_cur=${err_count}
  302. /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} + $? ))
  303. [ ${err_c_cur} -eq ${err_count} ] && [ ! $((${err_count} - 1)) -lt 0 ] && err_count=$((${err_count} - 1)) diff_c=1
  304. [ ${err_c_cur} -ne ${err_count} ] && diff_c=$(( ${err_c_cur} - ${err_count} ))
  305. progress "SOGo" ${THRESHOLD} $(( ${THRESHOLD} - ${err_count} )) ${diff_c}
  306. if [[ $? == 10 ]]; then
  307. diff_c=0
  308. sleep 1
  309. else
  310. diff_c=0
  311. sleep $(( ( RANDOM % 60 ) + 20 ))
  312. fi
  313. done
  314. return 1
  315. }
  316. postfix_checks() {
  317. err_count=0
  318. diff_c=0
  319. THRESHOLD=${POSTFIX_THRESHOLD}
  320. # Reduce error count by 2 after restarting an unhealthy container
  321. trap "[ ${err_count} -gt 1 ] && err_count=$(( ${err_count} - 2 ))" USR1
  322. while [ ${err_count} -lt ${THRESHOLD} ]; do
  323. touch /tmp/postfix-mailcow; echo "$(tail -50 /tmp/postfix-mailcow)" > /tmp/postfix-mailcow
  324. host_ip=$(get_container_ip postfix-mailcow)
  325. err_c_cur=${err_count}
  326. /usr/lib/nagios/plugins/check_smtp -4 -H ${host_ip} -p 589 -f "watchdog@invalid" -C "RCPT TO:watchdog@localhost" -C DATA -C . -R 250 2>> /tmp/postfix-mailcow 1>&2; err_count=$(( ${err_count} + $? ))
  327. /usr/lib/nagios/plugins/check_smtp -4 -H ${host_ip} -p 589 -S 2>> /tmp/postfix-mailcow 1>&2; err_count=$(( ${err_count} + $? ))
  328. [ ${err_c_cur} -eq ${err_count} ] && [ ! $((${err_count} - 1)) -lt 0 ] && err_count=$((${err_count} - 1)) diff_c=1
  329. [ ${err_c_cur} -ne ${err_count} ] && diff_c=$(( ${err_c_cur} - ${err_count} ))
  330. progress "Postfix" ${THRESHOLD} $(( ${THRESHOLD} - ${err_count} )) ${diff_c}
  331. if [[ $? == 10 ]]; then
  332. diff_c=0
  333. sleep 1
  334. else
  335. diff_c=0
  336. sleep $(( ( RANDOM % 60 ) + 20 ))
  337. fi
  338. done
  339. return 1
  340. }
  341. clamd_checks() {
  342. err_count=0
  343. diff_c=0
  344. THRESHOLD=${CLAMD_THRESHOLD}
  345. # Reduce error count by 2 after restarting an unhealthy container
  346. trap "[ ${err_count} -gt 1 ] && err_count=$(( ${err_count} - 2 ))" USR1
  347. while [ ${err_count} -lt ${THRESHOLD} ]; do
  348. touch /tmp/clamd-mailcow; echo "$(tail -50 /tmp/clamd-mailcow)" > /tmp/clamd-mailcow
  349. host_ip=$(get_container_ip clamd-mailcow)
  350. err_c_cur=${err_count}
  351. /usr/lib/nagios/plugins/check_clamd -4 -H ${host_ip} 2>> /tmp/clamd-mailcow 1>&2; err_count=$(( ${err_count} + $? ))
  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 "Clamd" ${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 $(( ( RANDOM % 120 ) + 20 ))
  361. fi
  362. done
  363. return 1
  364. }
  365. dovecot_checks() {
  366. err_count=0
  367. diff_c=0
  368. THRESHOLD=${DOVECOT_THRESHOLD}
  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. touch /tmp/dovecot-mailcow; echo "$(tail -50 /tmp/dovecot-mailcow)" > /tmp/dovecot-mailcow
  373. host_ip=$(get_container_ip dovecot-mailcow)
  374. err_c_cur=${err_count}
  375. /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} + $? ))
  376. /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} + $? ))
  377. /usr/lib/nagios/plugins/check_imap -4 -H ${host_ip} -p 143 -e "OK " 2>> /tmp/dovecot-mailcow 1>&2; err_count=$(( ${err_count} + $? ))
  378. /usr/lib/nagios/plugins/check_tcp -4 -H ${host_ip} -p 10001 -e "VERSION" 2>> /tmp/dovecot-mailcow 1>&2; err_count=$(( ${err_count} + $? ))
  379. /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} + $? ))
  380. [ ${err_c_cur} -eq ${err_count} ] && [ ! $((${err_count} - 1)) -lt 0 ] && err_count=$((${err_count} - 1)) diff_c=1
  381. [ ${err_c_cur} -ne ${err_count} ] && diff_c=$(( ${err_c_cur} - ${err_count} ))
  382. progress "Dovecot" ${THRESHOLD} $(( ${THRESHOLD} - ${err_count} )) ${diff_c}
  383. if [[ $? == 10 ]]; then
  384. diff_c=0
  385. sleep 1
  386. else
  387. diff_c=0
  388. sleep $(( ( RANDOM % 60 ) + 20 ))
  389. fi
  390. done
  391. return 1
  392. }
  393. phpfpm_checks() {
  394. err_count=0
  395. diff_c=0
  396. THRESHOLD=${PHPFPM_THRESHOLD}
  397. # Reduce error count by 2 after restarting an unhealthy container
  398. trap "[ ${err_count} -gt 1 ] && err_count=$(( ${err_count} - 2 ))" USR1
  399. while [ ${err_count} -lt ${THRESHOLD} ]; do
  400. touch /tmp/php-fpm-mailcow; echo "$(tail -50 /tmp/php-fpm-mailcow)" > /tmp/php-fpm-mailcow
  401. host_ip=$(get_container_ip php-fpm-mailcow)
  402. err_c_cur=${err_count}
  403. /usr/lib/nagios/plugins/check_tcp -H ${host_ip} -p 9001 2>> /tmp/php-fpm-mailcow 1>&2; err_count=$(( ${err_count} + $? ))
  404. /usr/lib/nagios/plugins/check_tcp -H ${host_ip} -p 9002 2>> /tmp/php-fpm-mailcow 1>&2; err_count=$(( ${err_count} + $? ))
  405. [ ${err_c_cur} -eq ${err_count} ] && [ ! $((${err_count} - 1)) -lt 0 ] && err_count=$((${err_count} - 1)) diff_c=1
  406. [ ${err_c_cur} -ne ${err_count} ] && diff_c=$(( ${err_c_cur} - ${err_count} ))
  407. progress "PHP-FPM" ${THRESHOLD} $(( ${THRESHOLD} - ${err_count} )) ${diff_c}
  408. if [[ $? == 10 ]]; then
  409. diff_c=0
  410. sleep 1
  411. else
  412. diff_c=0
  413. sleep $(( ( RANDOM % 60 ) + 20 ))
  414. fi
  415. done
  416. return 1
  417. }
  418. ratelimit_checks() {
  419. err_count=0
  420. diff_c=0
  421. THRESHOLD=${RATELIMIT_THRESHOLD}
  422. RL_LOG_STATUS=$(redis-cli -h redis LRANGE RL_LOG 0 0 | jq .qid)
  423. # Reduce error count by 2 after restarting an unhealthy container
  424. trap "[ ${err_count} -gt 1 ] && err_count=$(( ${err_count} - 2 ))" USR1
  425. while [ ${err_count} -lt ${THRESHOLD} ]; do
  426. err_c_cur=${err_count}
  427. RL_LOG_STATUS_PREV=${RL_LOG_STATUS}
  428. RL_LOG_STATUS=$(redis-cli -h redis LRANGE RL_LOG 0 0 | jq .qid)
  429. if [[ ${RL_LOG_STATUS_PREV} != ${RL_LOG_STATUS} ]]; then
  430. err_count=$(( ${err_count} + 1 ))
  431. fi
  432. [ ${err_c_cur} -eq ${err_count} ] && [ ! $((${err_count} - 1)) -lt 0 ] && err_count=$((${err_count} - 1)) diff_c=1
  433. [ ${err_c_cur} -ne ${err_count} ] && diff_c=$(( ${err_c_cur} - ${err_count} ))
  434. progress "Ratelimit" ${THRESHOLD} $(( ${THRESHOLD} - ${err_count} )) ${diff_c}
  435. if [[ $? == 10 ]]; then
  436. diff_c=0
  437. sleep 1
  438. else
  439. diff_c=0
  440. sleep $(( ( RANDOM % 60 ) + 20 ))
  441. fi
  442. done
  443. return 1
  444. }
  445. fail2ban_checks() {
  446. err_count=0
  447. diff_c=0
  448. THRESHOLD=${FAIL2BAN_THRESHOLD}
  449. F2B_LOG_STATUS=($(${REDIS_CMDLINE} --raw HKEYS F2B_ACTIVE_BANS))
  450. F2B_RES=
  451. # Reduce error count by 2 after restarting an unhealthy container
  452. trap "[ ${err_count} -gt 1 ] && err_count=$(( ${err_count} - 2 ))" USR1
  453. while [ ${err_count} -lt ${THRESHOLD} ]; do
  454. err_c_cur=${err_count}
  455. F2B_LOG_STATUS_PREV=(${F2B_LOG_STATUS[@]})
  456. F2B_LOG_STATUS=($(${REDIS_CMDLINE} --raw HKEYS F2B_ACTIVE_BANS))
  457. array_diff F2B_RES F2B_LOG_STATUS F2B_LOG_STATUS_PREV
  458. if [[ ! -z "${F2B_RES}" ]]; then
  459. err_count=$(( ${err_count} + 1 ))
  460. echo -n "${F2B_RES[@]}" | tr -cd "[a-fA-F0-9.:/] " | timeout 3s ${REDIS_CMDLINE} -x SET F2B_RES > /dev/null
  461. if [ $? -ne 0 ]; then
  462. ${REDIS_CMDLINE} -x DEL F2B_RES
  463. fi
  464. fi
  465. [ ${err_c_cur} -eq ${err_count} ] && [ ! $((${err_count} - 1)) -lt 0 ] && err_count=$((${err_count} - 1)) diff_c=1
  466. [ ${err_c_cur} -ne ${err_count} ] && diff_c=$(( ${err_c_cur} - ${err_count} ))
  467. progress "Fail2ban" ${THRESHOLD} $(( ${THRESHOLD} - ${err_count} )) ${diff_c}
  468. if [[ $? == 10 ]]; then
  469. diff_c=0
  470. sleep 1
  471. else
  472. diff_c=0
  473. sleep $(( ( RANDOM % 60 ) + 20 ))
  474. fi
  475. done
  476. return 1
  477. }
  478. acme_checks() {
  479. err_count=0
  480. diff_c=0
  481. THRESHOLD=${ACME_THRESHOLD}
  482. ACME_LOG_STATUS=$(redis-cli -h redis GET ACME_FAIL_TIME)
  483. if [[ -z "${ACME_LOG_STATUS}" ]]; then
  484. ${REDIS_CMDLINE} SET ACME_FAIL_TIME 0
  485. ACME_LOG_STATUS=0
  486. fi
  487. # Reduce error count by 2 after restarting an unhealthy container
  488. trap "[ ${err_count} -gt 1 ] && err_count=$(( ${err_count} - 2 ))" USR1
  489. while [ ${err_count} -lt ${THRESHOLD} ]; do
  490. err_c_cur=${err_count}
  491. ACME_LOG_STATUS_PREV=${ACME_LOG_STATUS}
  492. ACME_LC=0
  493. until [[ ! -z ${ACME_LOG_STATUS} ]] || [ ${ACME_LC} -ge 3 ]; do
  494. ACME_LOG_STATUS=$(redis-cli -h redis GET ACME_FAIL_TIME 2> /dev/null)
  495. sleep 3
  496. ACME_LC=$((ACME_LC+1))
  497. done
  498. if [[ ${ACME_LOG_STATUS_PREV} != ${ACME_LOG_STATUS} ]]; then
  499. err_count=$(( ${err_count} + 1 ))
  500. fi
  501. [ ${err_c_cur} -eq ${err_count} ] && [ ! $((${err_count} - 1)) -lt 0 ] && err_count=$((${err_count} - 1)) diff_c=1
  502. [ ${err_c_cur} -ne ${err_count} ] && diff_c=$(( ${err_c_cur} - ${err_count} ))
  503. progress "ACME" ${THRESHOLD} $(( ${THRESHOLD} - ${err_count} )) ${diff_c}
  504. if [[ $? == 10 ]]; then
  505. diff_c=0
  506. sleep 1
  507. else
  508. diff_c=0
  509. sleep $(( ( RANDOM % 60 ) + 20 ))
  510. fi
  511. done
  512. return 1
  513. }
  514. ipv6nat_checks() {
  515. err_count=0
  516. diff_c=0
  517. THRESHOLD=${IPV6NAT_THRESHOLD}
  518. # Reduce error count by 2 after restarting an unhealthy container
  519. trap "[ ${err_count} -gt 1 ] && err_count=$(( ${err_count} - 2 ))" USR1
  520. while [ ${err_count} -lt ${THRESHOLD} ]; do
  521. err_c_cur=${err_count}
  522. CONTAINERS=$(curl --silent --insecure https://dockerapi/containers/json)
  523. 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")
  524. if [[ ! -z ${IPV6NAT_CONTAINER_ID} ]]; then
  525. 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)"
  526. 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)"
  527. DIFFERENCE_START_TIME=$(expr ${LATEST_IPV6NAT} - ${LATEST_STARTED} 2>/dev/null)
  528. if [[ "${DIFFERENCE_START_TIME}" -lt 30 ]]; then
  529. err_count=$(( ${err_count} + 1 ))
  530. fi
  531. fi
  532. [ ${err_c_cur} -eq ${err_count} ] && [ ! $((${err_count} - 1)) -lt 0 ] && err_count=$((${err_count} - 1)) diff_c=1
  533. [ ${err_c_cur} -ne ${err_count} ] && diff_c=$(( ${err_c_cur} - ${err_count} ))
  534. progress "IPv6 NAT" ${THRESHOLD} $(( ${THRESHOLD} - ${err_count} )) ${diff_c}
  535. if [[ $? == 10 ]]; then
  536. diff_c=0
  537. sleep 30
  538. else
  539. diff_c=0
  540. sleep 300
  541. fi
  542. done
  543. return 1
  544. }
  545. rspamd_checks() {
  546. err_count=0
  547. diff_c=0
  548. THRESHOLD=${RSPAMD_THRESHOLD}
  549. # Reduce error count by 2 after restarting an unhealthy container
  550. trap "[ ${err_count} -gt 1 ] && err_count=$(( ${err_count} - 2 ))" USR1
  551. while [ ${err_count} -lt ${THRESHOLD} ]; do
  552. touch /tmp/rspamd-mailcow; echo "$(tail -50 /tmp/rspamd-mailcow)" > /tmp/rspamd-mailcow
  553. host_ip=$(get_container_ip rspamd-mailcow)
  554. err_c_cur=${err_count}
  555. SCORE=$(echo 'To: null@localhost
  556. From: watchdog@localhost
  557. Empty
  558. ' | usr/bin/curl -s --data-binary @- --unix-socket /var/lib/rspamd/rspamd.sock http://rspamd/scan | jq -rc .default.required_score)
  559. if [[ ${SCORE} != "9999" ]]; then
  560. echo "Rspamd settings check failed" 2>> /tmp/rspamd-mailcow 1>&2
  561. err_count=$(( ${err_count} + 1))
  562. else
  563. echo "Rspamd settings check succeeded" 2>> /tmp/rspamd-mailcow 1>&2
  564. fi
  565. [ ${err_c_cur} -eq ${err_count} ] && [ ! $((${err_count} - 1)) -lt 0 ] && err_count=$((${err_count} - 1)) diff_c=1
  566. [ ${err_c_cur} -ne ${err_count} ] && diff_c=$(( ${err_c_cur} - ${err_count} ))
  567. progress "Rspamd" ${THRESHOLD} $(( ${THRESHOLD} - ${err_count} )) ${diff_c}
  568. if [[ $? == 10 ]]; then
  569. diff_c=0
  570. sleep 1
  571. else
  572. diff_c=0
  573. sleep $(( ( RANDOM % 60 ) + 20 ))
  574. fi
  575. done
  576. return 1
  577. }
  578. olefy_checks() {
  579. err_count=0
  580. diff_c=0
  581. THRESHOLD=${OLEFY_THRESHOLD}
  582. # Reduce error count by 2 after restarting an unhealthy container
  583. trap "[ ${err_count} -gt 1 ] && err_count=$(( ${err_count} - 2 ))" USR1
  584. while [ ${err_count} -lt ${THRESHOLD} ]; do
  585. touch /tmp/olefy-mailcow; echo "$(tail -50 /tmp/olefy-mailcow)" > /tmp/olefy-mailcow
  586. host_ip=$(get_container_ip olefy-mailcow)
  587. err_c_cur=${err_count}
  588. /usr/lib/nagios/plugins/check_tcp -4 -H ${host_ip} -p 10055 -s "PING\n" 2>> /tmp/olefy-mailcow 1>&2; err_count=$(( ${err_count} + $? ))
  589. [ ${err_c_cur} -eq ${err_count} ] && [ ! $((${err_count} - 1)) -lt 0 ] && err_count=$((${err_count} - 1)) diff_c=1
  590. [ ${err_c_cur} -ne ${err_count} ] && diff_c=$(( ${err_c_cur} - ${err_count} ))
  591. progress "Olefy" ${THRESHOLD} $(( ${THRESHOLD} - ${err_count} )) ${diff_c}
  592. if [[ $? == 10 ]]; then
  593. diff_c=0
  594. sleep 1
  595. else
  596. diff_c=0
  597. sleep $(( ( RANDOM % 60 ) + 20 ))
  598. fi
  599. done
  600. return 1
  601. }
  602. # Notify about start
  603. if [[ ! -z ${WATCHDOG_NOTIFY_EMAIL} ]]; then
  604. mail_error "watchdog-mailcow" "Watchdog started monitoring mailcow."
  605. fi
  606. # Create watchdog agents
  607. (
  608. while true; do
  609. if ! nginx_checks; then
  610. log_msg "Nginx hit error limit"
  611. echo nginx-mailcow > /tmp/com_pipe
  612. fi
  613. done
  614. ) &
  615. PID=$!
  616. echo "Spawned nginx_checks with PID ${PID}"
  617. BACKGROUND_TASKS+=(${PID})
  618. if [[ ${WATCHDOG_EXTERNAL_CHECKS} =~ ^([yY][eE][sS]|[yY])+$ ]]; then
  619. (
  620. while true; do
  621. if ! external_checks; then
  622. log_msg "External checks hit error limit"
  623. echo external_checks > /tmp/com_pipe
  624. fi
  625. done
  626. ) &
  627. PID=$!
  628. echo "Spawned external_checks with PID ${PID}"
  629. BACKGROUND_TASKS+=(${PID})
  630. fi
  631. (
  632. while true; do
  633. if ! mysql_checks; then
  634. log_msg "MySQL hit error limit"
  635. echo mysql-mailcow > /tmp/com_pipe
  636. fi
  637. done
  638. ) &
  639. PID=$!
  640. echo "Spawned mysql_checks with PID ${PID}"
  641. BACKGROUND_TASKS+=(${PID})
  642. (
  643. while true; do
  644. if ! redis_checks; then
  645. log_msg "Local Redis hit error limit"
  646. echo redis-mailcow > /tmp/com_pipe
  647. fi
  648. done
  649. ) &
  650. PID=$!
  651. echo "Spawned redis_checks with PID ${PID}"
  652. BACKGROUND_TASKS+=(${PID})
  653. (
  654. while true; do
  655. if ! phpfpm_checks; then
  656. log_msg "PHP-FPM hit error limit"
  657. echo php-fpm-mailcow > /tmp/com_pipe
  658. fi
  659. done
  660. ) &
  661. PID=$!
  662. echo "Spawned phpfpm_checks with PID ${PID}"
  663. BACKGROUND_TASKS+=(${PID})
  664. (
  665. while true; do
  666. if ! sogo_checks; then
  667. log_msg "SOGo hit error limit"
  668. echo sogo-mailcow > /tmp/com_pipe
  669. fi
  670. done
  671. ) &
  672. PID=$!
  673. echo "Spawned sogo_checks with PID ${PID}"
  674. BACKGROUND_TASKS+=(${PID})
  675. if [ ${CHECK_UNBOUND} -eq 1 ]; then
  676. (
  677. while true; do
  678. if ! unbound_checks; then
  679. log_msg "Unbound hit error limit"
  680. echo unbound-mailcow > /tmp/com_pipe
  681. fi
  682. done
  683. ) &
  684. PID=$!
  685. echo "Spawned unbound_checks with PID ${PID}"
  686. BACKGROUND_TASKS+=(${PID})
  687. fi
  688. if [[ "${SKIP_CLAMD}" =~ ^([nN][oO]|[nN])+$ ]]; then
  689. (
  690. while true; do
  691. if ! clamd_checks; then
  692. log_msg "Clamd hit error limit"
  693. echo clamd-mailcow > /tmp/com_pipe
  694. fi
  695. done
  696. ) &
  697. PID=$!
  698. echo "Spawned clamd_checks with PID ${PID}"
  699. BACKGROUND_TASKS+=(${PID})
  700. fi
  701. (
  702. while true; do
  703. if ! postfix_checks; then
  704. log_msg "Postfix hit error limit"
  705. echo postfix-mailcow > /tmp/com_pipe
  706. fi
  707. done
  708. ) &
  709. PID=$!
  710. echo "Spawned postfix_checks with PID ${PID}"
  711. BACKGROUND_TASKS+=(${PID})
  712. (
  713. while true; do
  714. if ! dovecot_checks; then
  715. log_msg "Dovecot hit error limit"
  716. echo dovecot-mailcow > /tmp/com_pipe
  717. fi
  718. done
  719. ) &
  720. PID=$!
  721. echo "Spawned dovecot_checks with PID ${PID}"
  722. BACKGROUND_TASKS+=(${PID})
  723. (
  724. while true; do
  725. if ! rspamd_checks; then
  726. log_msg "Rspamd hit error limit"
  727. echo rspamd-mailcow > /tmp/com_pipe
  728. fi
  729. done
  730. ) &
  731. PID=$!
  732. echo "Spawned rspamd_checks with PID ${PID}"
  733. BACKGROUND_TASKS+=(${PID})
  734. (
  735. while true; do
  736. if ! ratelimit_checks; then
  737. log_msg "Ratelimit hit error limit"
  738. echo ratelimit > /tmp/com_pipe
  739. fi
  740. done
  741. ) &
  742. PID=$!
  743. echo "Spawned ratelimit_checks with PID ${PID}"
  744. BACKGROUND_TASKS+=(${PID})
  745. (
  746. while true; do
  747. if ! fail2ban_checks; then
  748. log_msg "Fail2ban hit error limit"
  749. echo fail2ban > /tmp/com_pipe
  750. fi
  751. done
  752. ) &
  753. PID=$!
  754. echo "Spawned fail2ban_checks with PID ${PID}"
  755. BACKGROUND_TASKS+=(${PID})
  756. (
  757. while true; do
  758. if ! olefy_checks; then
  759. log_msg "Olefy hit error limit"
  760. echo olefy-mailcow > /tmp/com_pipe
  761. fi
  762. done
  763. ) &
  764. PID=$!
  765. echo "Spawned olefy_checks with PID ${PID}"
  766. BACKGROUND_TASKS+=(${PID})
  767. (
  768. while true; do
  769. if ! acme_checks; then
  770. log_msg "ACME client hit error limit"
  771. echo acme-mailcow > /tmp/com_pipe
  772. fi
  773. done
  774. ) &
  775. PID=$!
  776. echo "Spawned acme_checks with PID ${PID}"
  777. BACKGROUND_TASKS+=(${PID})
  778. (
  779. while true; do
  780. if ! ipv6nat_checks; then
  781. log_msg "IPv6 NAT warning: ipv6nat-mailcow container was not started at least 30s after siblings (not an error)"
  782. echo ipv6nat-mailcow > /tmp/com_pipe
  783. fi
  784. done
  785. ) &
  786. PID=$!
  787. echo "Spawned ipv6nat_checks with PID ${PID}"
  788. BACKGROUND_TASKS+=(${PID})
  789. # Monitor watchdog agents, stop script when agents fails and wait for respawn by Docker (restart:always:n)
  790. (
  791. while true; do
  792. for bg_task in ${BACKGROUND_TASKS[*]}; do
  793. if ! kill -0 ${bg_task} 1>&2; then
  794. log_msg "Worker ${bg_task} died, stopping watchdog and waiting for respawn..."
  795. kill -TERM 1
  796. fi
  797. sleep 10
  798. done
  799. done
  800. ) &
  801. # Monitor dockerapi
  802. (
  803. while true; do
  804. while nc -z dockerapi 443; do
  805. sleep 3
  806. done
  807. log_msg "Cannot find dockerapi-mailcow, waiting to recover..."
  808. kill -STOP ${BACKGROUND_TASKS[*]}
  809. until nc -z dockerapi 443; do
  810. sleep 3
  811. done
  812. kill -CONT ${BACKGROUND_TASKS[*]}
  813. kill -USR1 ${BACKGROUND_TASKS[*]}
  814. done
  815. ) &
  816. # Actions when threshold limit is reached
  817. while true; do
  818. CONTAINER_ID=
  819. HAS_INITDB=
  820. read com_pipe_answer </tmp/com_pipe
  821. if [ -s "/tmp/${com_pipe_answer}" ]; then
  822. cat "/tmp/${com_pipe_answer}"
  823. fi
  824. if [[ ${com_pipe_answer} == "ratelimit" ]]; then
  825. log_msg "At least one ratelimit was applied"
  826. [[ ! -z ${WATCHDOG_NOTIFY_EMAIL} ]] && mail_error "${com_pipe_answer}" "Please see mailcow UI logs for further information."
  827. elif [[ ${com_pipe_answer} == "external_checks" ]]; then
  828. log_msg "Your mailcow is an open relay!"
  829. [[ ! -z ${WATCHDOG_NOTIFY_EMAIL} ]] && mail_error "${com_pipe_answer}" "Please stop mailcow now and check your network configuration!"
  830. elif [[ ${com_pipe_answer} == "acme-mailcow" ]]; then
  831. log_msg "acme-mailcow did not complete successfully"
  832. [[ ! -z ${WATCHDOG_NOTIFY_EMAIL} ]] && mail_error "${com_pipe_answer}" "Please check acme-mailcow for further information."
  833. elif [[ ${com_pipe_answer} == "fail2ban" ]]; then
  834. F2B_RES=($(timeout 4s ${REDIS_CMDLINE} --raw GET F2B_RES 2> /dev/null))
  835. if [[ ! -z "${F2B_RES}" ]]; then
  836. ${REDIS_CMDLINE} DEL F2B_RES > /dev/null
  837. host=
  838. for host in "${F2B_RES[@]}"; do
  839. log_msg "Banned ${host}"
  840. rm /tmp/fail2ban 2> /dev/null
  841. timeout 2s whois "${host}" > /tmp/fail2ban
  842. [[ ! -z ${WATCHDOG_NOTIFY_EMAIL} ]] && [[ ${WATCHDOG_NOTIFY_BAN} =~ ^([yY][eE][sS]|[yY])+$ ]] && mail_error "${com_pipe_answer}" "IP ban: ${host}"
  843. done
  844. fi
  845. elif [[ ${com_pipe_answer} =~ .+-mailcow ]]; then
  846. kill -STOP ${BACKGROUND_TASKS[*]}
  847. sleep 10
  848. 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")
  849. if [[ ! -z ${CONTAINER_ID} ]]; then
  850. if [[ "${com_pipe_answer}" == "php-fpm-mailcow" ]]; then
  851. 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)
  852. fi
  853. S_RUNNING=$(($(date +%s) - $(curl --silent --insecure https://dockerapi/containers/${CONTAINER_ID}/json | jq .State.StartedAt | xargs -n1 date +%s -d)))
  854. if [ ${S_RUNNING} -lt 360 ]; then
  855. log_msg "Container is running for less than 360 seconds, skipping action..."
  856. elif [[ ! -z ${HAS_INITDB} ]]; then
  857. log_msg "Database is being initialized by php-fpm-mailcow, not restarting but delaying checks for a minute..."
  858. sleep 60
  859. else
  860. log_msg "Sending restart command to ${CONTAINER_ID}..."
  861. curl --silent --insecure -XPOST https://dockerapi/containers/${CONTAINER_ID}/restart
  862. if [[ ${com_pipe_answer} != "ipv6nat-mailcow" ]]; then
  863. [[ ! -z ${WATCHDOG_NOTIFY_EMAIL} ]] && mail_error "${com_pipe_answer}"
  864. fi
  865. log_msg "Wait for restarted container to settle and continue watching..."
  866. sleep 35
  867. fi
  868. fi
  869. kill -CONT ${BACKGROUND_TASKS[*]}
  870. sleep 1
  871. kill -USR1 ${BACKGROUND_TASKS[*]}
  872. fi
  873. done