watchdog.sh 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758
  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 10
  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. redis-cli -h redis-mailcow DEL F2B_RES > /dev/null
  18. # Common functions
  19. array_diff() {
  20. # https://stackoverflow.com/questions/2312762, Alex Offshore
  21. eval local ARR1=\(\"\${$2[@]}\"\)
  22. eval local ARR2=\(\"\${$3[@]}\"\)
  23. local IFS=$'\n'
  24. mapfile -t $1 < <(comm -23 <(echo "${ARR1[*]}" | sort) <(echo "${ARR2[*]}" | sort))
  25. }
  26. progress() {
  27. SERVICE=${1}
  28. TOTAL=${2}
  29. CURRENT=${3}
  30. DIFF=${4}
  31. [[ -z ${DIFF} ]] && DIFF=0
  32. [[ -z ${TOTAL} || -z ${CURRENT} ]] && return
  33. [[ ${CURRENT} -gt ${TOTAL} ]] && return
  34. [[ ${CURRENT} -lt 0 ]] && CURRENT=0
  35. PERCENT=$(( 200 * ${CURRENT} / ${TOTAL} % 2 + 100 * ${CURRENT} / ${TOTAL} ))
  36. redis-cli -h redis LPUSH WATCHDOG_LOG "{\"time\":\"$(date +%s)\",\"service\":\"${SERVICE}\",\"lvl\":\"${PERCENT}\",\"hpnow\":\"${CURRENT}\",\"hptotal\":\"${TOTAL}\",\"hpdiff\":\"${DIFF}\"}" > /dev/null
  37. log_msg "${SERVICE} health level: ${PERCENT}% (${CURRENT}/${TOTAL}), health trend: ${DIFF}" no_redis
  38. # Return 10 to indicate a dead service
  39. [ ${CURRENT} -le 0 ] && return 10
  40. }
  41. log_msg() {
  42. if [[ ${2} != "no_redis" ]]; then
  43. redis-cli -h redis LPUSH WATCHDOG_LOG "{\"time\":\"$(date +%s)\",\"message\":\"$(printf '%s' "${1}" | \
  44. tr '\r\n%&;$"_[]{}-' ' ')\"}" > /dev/null
  45. fi
  46. echo $(date) $(printf '%s\n' "${1}")
  47. }
  48. function mail_error() {
  49. [[ -z ${1} ]] && return 1
  50. [[ -z ${2} ]] && BODY="Service was restarted on $(date), please check your mailcow installation." || BODY="$(date) - ${2}"
  51. WATCHDOG_NOTIFY_EMAIL=$(echo "${WATCHDOG_NOTIFY_EMAIL}" | sed 's/"//;s|"$||')
  52. # Some exceptions for subject and body formats
  53. if [[ ${1} == "fail2ban" ]]; then
  54. SUBJECT="${BODY}"
  55. BODY="Please see netfilter-mailcow for more details and triggered rules."
  56. else
  57. SUBJECT="Watchdog ALERT: ${1}"
  58. fi
  59. IFS=',' read -r -a MAIL_RCPTS <<< "${WATCHDOG_NOTIFY_EMAIL}"
  60. for rcpt in "${MAIL_RCPTS[@]}"; do
  61. RCPT_DOMAIN=
  62. RCPT_MX=
  63. RCPT_DOMAIN=$(echo ${rcpt} | awk -F @ {'print $NF'})
  64. RCPT_MX=$(dig +short ${RCPT_DOMAIN} mx | sort -n | awk '{print $2; exit}')
  65. if [[ -z ${RCPT_MX} ]]; then
  66. log_msg "Cannot determine MX for ${rcpt}, skipping email notification..."
  67. return 1
  68. fi
  69. [ -f "/tmp/${1}" ] && BODY="/tmp/${1}"
  70. ./smtp-cli --missing-modules-ok \
  71. --charset=UTF-8 \
  72. --subject="${SUBJECT}" \
  73. --body-plain="${BODY}" \
  74. --to=${rcpt} \
  75. --from="watchdog@${MAILCOW_HOSTNAME}" \
  76. --server="${RCPT_MX}" \
  77. --hello-host=${MAILCOW_HOSTNAME}
  78. log_msg "Sent notification email to ${rcpt}"
  79. done
  80. }
  81. get_container_ip() {
  82. # ${1} is container
  83. CONTAINER_ID=()
  84. CONTAINER_IPS=()
  85. CONTAINER_IP=
  86. LOOP_C=1
  87. until [[ ${CONTAINER_IP} =~ ^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}$ ]] || [[ ${LOOP_C} -gt 5 ]]; do
  88. if [ ${IP_BY_DOCKER_API} -eq 0 ]; then
  89. CONTAINER_IP=$(dig a "${1}" +short)
  90. else
  91. sleep 0.5
  92. # get long container id for exact match
  93. 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"))
  94. # returned id can have multiple elements (if scaled), shuffle for random test
  95. CONTAINER_ID=($(printf "%s\n" "${CONTAINER_ID[@]}" | shuf))
  96. if [[ ! -z ${CONTAINER_ID} ]]; then
  97. for matched_container in "${CONTAINER_ID[@]}"; do
  98. CONTAINER_IPS=($(curl --silent --insecure https://dockerapi/containers/${matched_container}/json | jq -r '.NetworkSettings.Networks[].IPAddress'))
  99. for ip_match in "${CONTAINER_IPS[@]}"; do
  100. # grep will do nothing if one of these vars is empty
  101. [[ -z ${ip_match} ]] && continue
  102. [[ -z ${IPV4_NETWORK} ]] && continue
  103. # only return ips that are part of our network
  104. if ! grep -q ${IPV4_NETWORK} <(echo ${ip_match}); then
  105. continue
  106. else
  107. CONTAINER_IP=${ip_match}
  108. break
  109. fi
  110. done
  111. [[ ! -z ${CONTAINER_IP} ]] && break
  112. done
  113. fi
  114. fi
  115. LOOP_C=$((LOOP_C + 1))
  116. done
  117. [[ ${LOOP_C} -gt 5 ]] && echo 240.0.0.0 || echo ${CONTAINER_IP}
  118. }
  119. nginx_checks() {
  120. err_count=0
  121. diff_c=0
  122. THRESHOLD=5
  123. # Reduce error count by 2 after restarting an unhealthy container
  124. trap "[ ${err_count} -gt 1 ] && err_count=$(( ${err_count} - 2 ))" USR1
  125. while [ ${err_count} -lt ${THRESHOLD} ]; do
  126. touch /tmp/nginx-mailcow; echo "$(tail -50 /tmp/nginx-mailcow)" > /tmp/nginx-mailcow
  127. host_ip=$(get_container_ip nginx-mailcow)
  128. err_c_cur=${err_count}
  129. /usr/lib/nagios/plugins/check_http -4 -H ${host_ip} -u / -p 8081 2>> /tmp/nginx-mailcow 1>&2; err_count=$(( ${err_count} + $? ))
  130. [ ${err_c_cur} -eq ${err_count} ] && [ ! $((${err_count} - 1)) -lt 0 ] && err_count=$((${err_count} - 1)) diff_c=1
  131. [ ${err_c_cur} -ne ${err_count} ] && diff_c=$(( ${err_c_cur} - ${err_count} ))
  132. progress "Nginx" ${THRESHOLD} $(( ${THRESHOLD} - ${err_count} )) ${diff_c}
  133. if [[ $? == 10 ]]; then
  134. diff_c=0
  135. sleep 1
  136. else
  137. diff_c=0
  138. sleep $(( ( RANDOM % 60 ) + 20 ))
  139. fi
  140. done
  141. return 1
  142. }
  143. unbound_checks() {
  144. err_count=0
  145. diff_c=0
  146. THRESHOLD=5
  147. # Reduce error count by 2 after restarting an unhealthy container
  148. trap "[ ${err_count} -gt 1 ] && err_count=$(( ${err_count} - 2 ))" USR1
  149. while [ ${err_count} -lt ${THRESHOLD} ]; do
  150. touch /tmp/unbound-mailcow; echo "$(tail -50 /tmp/unbound-mailcow)" > /tmp/unbound-mailcow
  151. host_ip=$(get_container_ip unbound-mailcow)
  152. err_c_cur=${err_count}
  153. /usr/lib/nagios/plugins/check_dns -s ${host_ip} -H stackoverflow.com 2>> /tmp/unbound-mailcow 1>&2; err_count=$(( ${err_count} + $? ))
  154. DNSSEC=$(dig com +dnssec | egrep 'flags:.+ad')
  155. if [[ -z ${DNSSEC} ]]; then
  156. echo "DNSSEC failure" 2>> /tmp/unbound-mailcow 1>&2
  157. err_count=$(( ${err_count} + 1))
  158. else
  159. echo "DNSSEC check succeeded" 2>> /tmp/unbound-mailcow 1>&2
  160. fi
  161. [ ${err_c_cur} -eq ${err_count} ] && [ ! $((${err_count} - 1)) -lt 0 ] && err_count=$((${err_count} - 1)) diff_c=1
  162. [ ${err_c_cur} -ne ${err_count} ] && diff_c=$(( ${err_c_cur} - ${err_count} ))
  163. progress "Unbound" ${THRESHOLD} $(( ${THRESHOLD} - ${err_count} )) ${diff_c}
  164. if [[ $? == 10 ]]; then
  165. diff_c=0
  166. sleep 1
  167. else
  168. diff_c=0
  169. sleep $(( ( RANDOM % 60 ) + 20 ))
  170. fi
  171. done
  172. return 1
  173. }
  174. mysql_checks() {
  175. err_count=0
  176. diff_c=0
  177. THRESHOLD=5
  178. # Reduce error count by 2 after restarting an unhealthy container
  179. trap "[ ${err_count} -gt 1 ] && err_count=$(( ${err_count} - 2 ))" USR1
  180. while [ ${err_count} -lt ${THRESHOLD} ]; do
  181. touch /tmp/mysql-mailcow; echo "$(tail -50 /tmp/mysql-mailcow)" > /tmp/mysql-mailcow
  182. host_ip=$(get_container_ip mysql-mailcow)
  183. err_c_cur=${err_count}
  184. /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} + $? ))
  185. /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} + $? ))
  186. [ ${err_c_cur} -eq ${err_count} ] && [ ! $((${err_count} - 1)) -lt 0 ] && err_count=$((${err_count} - 1)) diff_c=1
  187. [ ${err_c_cur} -ne ${err_count} ] && diff_c=$(( ${err_c_cur} - ${err_count} ))
  188. progress "MySQL/MariaDB" ${THRESHOLD} $(( ${THRESHOLD} - ${err_count} )) ${diff_c}
  189. if [[ $? == 10 ]]; then
  190. diff_c=0
  191. sleep 1
  192. else
  193. diff_c=0
  194. sleep $(( ( RANDOM % 60 ) + 20 ))
  195. fi
  196. done
  197. return 1
  198. }
  199. sogo_checks() {
  200. err_count=0
  201. diff_c=0
  202. THRESHOLD=5
  203. # Reduce error count by 2 after restarting an unhealthy container
  204. trap "[ ${err_count} -gt 1 ] && err_count=$(( ${err_count} - 2 ))" USR1
  205. while [ ${err_count} -lt ${THRESHOLD} ]; do
  206. touch /tmp/sogo-mailcow; echo "$(tail -50 /tmp/sogo-mailcow)" > /tmp/sogo-mailcow
  207. host_ip=$(get_container_ip sogo-mailcow)
  208. err_c_cur=${err_count}
  209. /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} + $? ))
  210. [ ${err_c_cur} -eq ${err_count} ] && [ ! $((${err_count} - 1)) -lt 0 ] && err_count=$((${err_count} - 1)) diff_c=1
  211. [ ${err_c_cur} -ne ${err_count} ] && diff_c=$(( ${err_c_cur} - ${err_count} ))
  212. progress "SOGo" ${THRESHOLD} $(( ${THRESHOLD} - ${err_count} )) ${diff_c}
  213. if [[ $? == 10 ]]; then
  214. diff_c=0
  215. sleep 1
  216. else
  217. diff_c=0
  218. sleep $(( ( RANDOM % 60 ) + 20 ))
  219. fi
  220. done
  221. return 1
  222. }
  223. postfix_checks() {
  224. err_count=0
  225. diff_c=0
  226. THRESHOLD=8
  227. # Reduce error count by 2 after restarting an unhealthy container
  228. trap "[ ${err_count} -gt 1 ] && err_count=$(( ${err_count} - 2 ))" USR1
  229. while [ ${err_count} -lt ${THRESHOLD} ]; do
  230. touch /tmp/postfix-mailcow; echo "$(tail -50 /tmp/postfix-mailcow)" > /tmp/postfix-mailcow
  231. host_ip=$(get_container_ip postfix-mailcow)
  232. err_c_cur=${err_count}
  233. /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} + $? ))
  234. /usr/lib/nagios/plugins/check_smtp -4 -H ${host_ip} -p 589 -S 2>> /tmp/postfix-mailcow 1>&2; err_count=$(( ${err_count} + $? ))
  235. [ ${err_c_cur} -eq ${err_count} ] && [ ! $((${err_count} - 1)) -lt 0 ] && err_count=$((${err_count} - 1)) diff_c=1
  236. [ ${err_c_cur} -ne ${err_count} ] && diff_c=$(( ${err_c_cur} - ${err_count} ))
  237. progress "Postfix" ${THRESHOLD} $(( ${THRESHOLD} - ${err_count} )) ${diff_c}
  238. if [[ $? == 10 ]]; then
  239. diff_c=0
  240. sleep 1
  241. else
  242. diff_c=0
  243. sleep $(( ( RANDOM % 60 ) + 20 ))
  244. fi
  245. done
  246. return 1
  247. }
  248. clamd_checks() {
  249. err_count=0
  250. diff_c=0
  251. THRESHOLD=10
  252. # Reduce error count by 2 after restarting an unhealthy container
  253. trap "[ ${err_count} -gt 1 ] && err_count=$(( ${err_count} - 2 ))" USR1
  254. while [ ${err_count} -lt ${THRESHOLD} ]; do
  255. touch /tmp/clamd-mailcow; echo "$(tail -50 /tmp/clamd-mailcow)" > /tmp/clamd-mailcow
  256. host_ip=$(get_container_ip clamd-mailcow)
  257. err_c_cur=${err_count}
  258. /usr/lib/nagios/plugins/check_clamd -4 -H ${host_ip} 2>> /tmp/clamd-mailcow 1>&2; err_count=$(( ${err_count} + $? ))
  259. [ ${err_c_cur} -eq ${err_count} ] && [ ! $((${err_count} - 1)) -lt 0 ] && err_count=$((${err_count} - 1)) diff_c=1
  260. [ ${err_c_cur} -ne ${err_count} ] && diff_c=$(( ${err_c_cur} - ${err_count} ))
  261. progress "Clamd" ${THRESHOLD} $(( ${THRESHOLD} - ${err_count} )) ${diff_c}
  262. if [[ $? == 10 ]]; then
  263. diff_c=0
  264. sleep 1
  265. else
  266. diff_c=0
  267. sleep $(( ( RANDOM % 120 ) + 20 ))
  268. fi
  269. done
  270. return 1
  271. }
  272. dovecot_checks() {
  273. err_count=0
  274. diff_c=0
  275. THRESHOLD=12
  276. # Reduce error count by 2 after restarting an unhealthy container
  277. trap "[ ${err_count} -gt 1 ] && err_count=$(( ${err_count} - 2 ))" USR1
  278. while [ ${err_count} -lt ${THRESHOLD} ]; do
  279. touch /tmp/dovecot-mailcow; echo "$(tail -50 /tmp/dovecot-mailcow)" > /tmp/dovecot-mailcow
  280. host_ip=$(get_container_ip dovecot-mailcow)
  281. err_c_cur=${err_count}
  282. /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} + $? ))
  283. /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} + $? ))
  284. /usr/lib/nagios/plugins/check_imap -4 -H ${host_ip} -p 143 -e "OK " 2>> /tmp/dovecot-mailcow 1>&2; err_count=$(( ${err_count} + $? ))
  285. /usr/lib/nagios/plugins/check_tcp -4 -H ${host_ip} -p 10001 -e "VERSION" 2>> /tmp/dovecot-mailcow 1>&2; err_count=$(( ${err_count} + $? ))
  286. /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} + $? ))
  287. [ ${err_c_cur} -eq ${err_count} ] && [ ! $((${err_count} - 1)) -lt 0 ] && err_count=$((${err_count} - 1)) diff_c=1
  288. [ ${err_c_cur} -ne ${err_count} ] && diff_c=$(( ${err_c_cur} - ${err_count} ))
  289. progress "Dovecot" ${THRESHOLD} $(( ${THRESHOLD} - ${err_count} )) ${diff_c}
  290. if [[ $? == 10 ]]; then
  291. diff_c=0
  292. sleep 1
  293. else
  294. diff_c=0
  295. sleep $(( ( RANDOM % 60 ) + 20 ))
  296. fi
  297. done
  298. return 1
  299. }
  300. phpfpm_checks() {
  301. err_count=0
  302. diff_c=0
  303. THRESHOLD=5
  304. # Reduce error count by 2 after restarting an unhealthy container
  305. trap "[ ${err_count} -gt 1 ] && err_count=$(( ${err_count} - 2 ))" USR1
  306. while [ ${err_count} -lt ${THRESHOLD} ]; do
  307. touch /tmp/php-fpm-mailcow; echo "$(tail -50 /tmp/php-fpm-mailcow)" > /tmp/php-fpm-mailcow
  308. host_ip=$(get_container_ip php-fpm-mailcow)
  309. err_c_cur=${err_count}
  310. /usr/lib/nagios/plugins/check_tcp -H ${host_ip} -p 9001 2>> /tmp/php-fpm-mailcow 1>&2; err_count=$(( ${err_count} + $? ))
  311. /usr/lib/nagios/plugins/check_tcp -H ${host_ip} -p 9002 2>> /tmp/php-fpm-mailcow 1>&2; err_count=$(( ${err_count} + $? ))
  312. [ ${err_c_cur} -eq ${err_count} ] && [ ! $((${err_count} - 1)) -lt 0 ] && err_count=$((${err_count} - 1)) diff_c=1
  313. [ ${err_c_cur} -ne ${err_count} ] && diff_c=$(( ${err_c_cur} - ${err_count} ))
  314. progress "PHP-FPM" ${THRESHOLD} $(( ${THRESHOLD} - ${err_count} )) ${diff_c}
  315. if [[ $? == 10 ]]; then
  316. diff_c=0
  317. sleep 1
  318. else
  319. diff_c=0
  320. sleep $(( ( RANDOM % 60 ) + 20 ))
  321. fi
  322. done
  323. return 1
  324. }
  325. ratelimit_checks() {
  326. err_count=0
  327. diff_c=0
  328. THRESHOLD=1
  329. RL_LOG_STATUS=$(redis-cli -h redis LRANGE RL_LOG 0 0 | jq .qid)
  330. # Reduce error count by 2 after restarting an unhealthy container
  331. trap "[ ${err_count} -gt 1 ] && err_count=$(( ${err_count} - 2 ))" USR1
  332. while [ ${err_count} -lt ${THRESHOLD} ]; do
  333. err_c_cur=${err_count}
  334. RL_LOG_STATUS_PREV=${RL_LOG_STATUS}
  335. RL_LOG_STATUS=$(redis-cli -h redis LRANGE RL_LOG 0 0 | jq .qid)
  336. if [[ ${RL_LOG_STATUS_PREV} != ${RL_LOG_STATUS} ]]; then
  337. err_count=$(( ${err_count} + 1 ))
  338. fi
  339. [ ${err_c_cur} -eq ${err_count} ] && [ ! $((${err_count} - 1)) -lt 0 ] && err_count=$((${err_count} - 1)) diff_c=1
  340. [ ${err_c_cur} -ne ${err_count} ] && diff_c=$(( ${err_c_cur} - ${err_count} ))
  341. progress "Ratelimit" ${THRESHOLD} $(( ${THRESHOLD} - ${err_count} )) ${diff_c}
  342. if [[ $? == 10 ]]; then
  343. diff_c=0
  344. sleep 1
  345. else
  346. diff_c=0
  347. sleep $(( ( RANDOM % 60 ) + 20 ))
  348. fi
  349. done
  350. return 1
  351. }
  352. fail2ban_checks() {
  353. err_count=0
  354. diff_c=0
  355. THRESHOLD=1
  356. F2B_LOG_STATUS=($(redis-cli -h redis-mailcow --raw HKEYS F2B_ACTIVE_BANS))
  357. F2B_RES=
  358. # Reduce error count by 2 after restarting an unhealthy container
  359. trap "[ ${err_count} -gt 1 ] && err_count=$(( ${err_count} - 2 ))" USR1
  360. while [ ${err_count} -lt ${THRESHOLD} ]; do
  361. err_c_cur=${err_count}
  362. F2B_LOG_STATUS_PREV=(${F2B_LOG_STATUS[@]})
  363. F2B_LOG_STATUS=($(redis-cli -h redis-mailcow --raw HKEYS F2B_ACTIVE_BANS))
  364. array_diff F2B_RES F2B_LOG_STATUS F2B_LOG_STATUS_PREV
  365. if [[ ! -z "${F2B_RES}" ]]; then
  366. err_count=$(( ${err_count} + 1 ))
  367. echo -n "${F2B_RES[@]}" | redis-cli -x -h redis-mailcow SET F2B_RES > /dev/null
  368. fi
  369. [ ${err_c_cur} -eq ${err_count} ] && [ ! $((${err_count} - 1)) -lt 0 ] && err_count=$((${err_count} - 1)) diff_c=1
  370. [ ${err_c_cur} -ne ${err_count} ] && diff_c=$(( ${err_c_cur} - ${err_count} ))
  371. progress "Fail2ban" ${THRESHOLD} $(( ${THRESHOLD} - ${err_count} )) ${diff_c}
  372. if [[ $? == 10 ]]; then
  373. diff_c=0
  374. sleep 1
  375. else
  376. diff_c=0
  377. sleep $(( ( RANDOM % 60 ) + 20 ))
  378. fi
  379. done
  380. return 1
  381. }
  382. acme_checks() {
  383. err_count=0
  384. diff_c=0
  385. THRESHOLD=1
  386. ACME_LOG_STATUS=$(redis-cli -h redis GET ACME_FAIL_TIME)
  387. if [[ -z "${ACME_LOG_STATUS}" ]]; then
  388. redis-cli -h redis SET ACME_FAIL_TIME 0
  389. ACME_LOG_STATUS=0
  390. fi
  391. # Reduce error count by 2 after restarting an unhealthy container
  392. trap "[ ${err_count} -gt 1 ] && err_count=$(( ${err_count} - 2 ))" USR1
  393. while [ ${err_count} -lt ${THRESHOLD} ]; do
  394. err_c_cur=${err_count}
  395. ACME_LOG_STATUS_PREV=${ACME_LOG_STATUS}
  396. ACME_LOG_STATUS=$(redis-cli -h redis GET ACME_FAIL_TIME)
  397. if [[ ${ACME_LOG_STATUS_PREV} != ${ACME_LOG_STATUS} ]]; then
  398. err_count=$(( ${err_count} + 1 ))
  399. fi
  400. [ ${err_c_cur} -eq ${err_count} ] && [ ! $((${err_count} - 1)) -lt 0 ] && err_count=$((${err_count} - 1)) diff_c=1
  401. [ ${err_c_cur} -ne ${err_count} ] && diff_c=$(( ${err_c_cur} - ${err_count} ))
  402. progress "ACME" ${THRESHOLD} $(( ${THRESHOLD} - ${err_count} )) ${diff_c}
  403. if [[ $? == 10 ]]; then
  404. diff_c=0
  405. sleep 1
  406. else
  407. diff_c=0
  408. sleep $(( ( RANDOM % 60 ) + 20 ))
  409. fi
  410. done
  411. return 1
  412. }
  413. ipv6nat_checks() {
  414. err_count=0
  415. diff_c=0
  416. THRESHOLD=1
  417. # Reduce error count by 2 after restarting an unhealthy container
  418. trap "[ ${err_count} -gt 1 ] && err_count=$(( ${err_count} - 2 ))" USR1
  419. while [ ${err_count} -lt ${THRESHOLD} ]; do
  420. err_c_cur=${err_count}
  421. CONTAINERS=$(curl --silent --insecure https://dockerapi/containers/json)
  422. 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")
  423. if [[ ! -z ${IPV6NAT_CONTAINER_ID} ]]; then
  424. 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)"
  425. 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)"
  426. DIFFERENCE_START_TIME=$(expr ${LATEST_IPV6NAT} - ${LATEST_STARTED} 2>/dev/null)
  427. if [[ "${DIFFERENCE_START_TIME}" -lt 30 ]]; then
  428. err_count=$(( ${err_count} + 1 ))
  429. fi
  430. fi
  431. [ ${err_c_cur} -eq ${err_count} ] && [ ! $((${err_count} - 1)) -lt 0 ] && err_count=$((${err_count} - 1)) diff_c=1
  432. [ ${err_c_cur} -ne ${err_count} ] && diff_c=$(( ${err_c_cur} - ${err_count} ))
  433. progress "IPv6 NAT" ${THRESHOLD} $(( ${THRESHOLD} - ${err_count} )) ${diff_c}
  434. if [[ $? == 10 ]]; then
  435. diff_c=0
  436. sleep 30
  437. else
  438. diff_c=0
  439. sleep 300
  440. fi
  441. done
  442. return 1
  443. }
  444. rspamd_checks() {
  445. err_count=0
  446. diff_c=0
  447. THRESHOLD=5
  448. # Reduce error count by 2 after restarting an unhealthy container
  449. trap "[ ${err_count} -gt 1 ] && err_count=$(( ${err_count} - 2 ))" USR1
  450. while [ ${err_count} -lt ${THRESHOLD} ]; do
  451. touch /tmp/rspamd-mailcow; echo "$(tail -50 /tmp/rspamd-mailcow)" > /tmp/rspamd-mailcow
  452. host_ip=$(get_container_ip rspamd-mailcow)
  453. err_c_cur=${err_count}
  454. SCORE=$(echo 'To: null@localhost
  455. From: watchdog@localhost
  456. Empty
  457. ' | usr/bin/curl -s --data-binary @- --unix-socket /var/lib/rspamd/rspamd.sock http://rspamd/scan | jq -rc .required_score)
  458. if [[ ${SCORE} != "9999" ]]; then
  459. echo "Rspamd settings check failed" 2>> /tmp/rspamd-mailcow 1>&2
  460. err_count=$(( ${err_count} + 1))
  461. else
  462. echo "Rspamd settings check succeeded" 2>> /tmp/rspamd-mailcow 1>&2
  463. fi
  464. [ ${err_c_cur} -eq ${err_count} ] && [ ! $((${err_count} - 1)) -lt 0 ] && err_count=$((${err_count} - 1)) diff_c=1
  465. [ ${err_c_cur} -ne ${err_count} ] && diff_c=$(( ${err_c_cur} - ${err_count} ))
  466. progress "Rspamd" ${THRESHOLD} $(( ${THRESHOLD} - ${err_count} )) ${diff_c}
  467. if [[ $? == 10 ]]; then
  468. diff_c=0
  469. sleep 1
  470. else
  471. diff_c=0
  472. sleep $(( ( RANDOM % 60 ) + 20 ))
  473. fi
  474. done
  475. return 1
  476. }
  477. olefy_checks() {
  478. err_count=0
  479. diff_c=0
  480. THRESHOLD=5
  481. # Reduce error count by 2 after restarting an unhealthy container
  482. trap "[ ${err_count} -gt 1 ] && err_count=$(( ${err_count} - 2 ))" USR1
  483. while [ ${err_count} -lt ${THRESHOLD} ]; do
  484. touch /tmp/olefy-mailcow; echo "$(tail -50 /tmp/olefy-mailcow)" > /tmp/olefy-mailcow
  485. host_ip=$(get_container_ip olefy-mailcow)
  486. err_c_cur=${err_count}
  487. /usr/lib/nagios/plugins/check_tcp -4 -H ${host_ip} -p 10055 2>> /tmp/olefy-mailcow 1>&2; err_count=$(( ${err_count} + $? ))
  488. [ ${err_c_cur} -eq ${err_count} ] && [ ! $((${err_count} - 1)) -lt 0 ] && err_count=$((${err_count} - 1)) diff_c=1
  489. [ ${err_c_cur} -ne ${err_count} ] && diff_c=$(( ${err_c_cur} - ${err_count} ))
  490. progress "Olefy" ${THRESHOLD} $(( ${THRESHOLD} - ${err_count} )) ${diff_c}
  491. if [[ $? == 10 ]]; then
  492. diff_c=0
  493. sleep 1
  494. else
  495. diff_c=0
  496. sleep $(( ( RANDOM % 60 ) + 20 ))
  497. fi
  498. done
  499. return 1
  500. }
  501. # Notify about start
  502. [[ ! -z ${WATCHDOG_NOTIFY_EMAIL} ]] && mail_error "watchdog-mailcow" "Watchdog started monitoring mailcow."
  503. # Create watchdog agents
  504. (
  505. while true; do
  506. if ! nginx_checks; then
  507. log_msg "Nginx hit error limit"
  508. echo nginx-mailcow > /tmp/com_pipe
  509. fi
  510. done
  511. ) &
  512. BACKGROUND_TASKS+=($!)
  513. (
  514. while true; do
  515. if ! mysql_checks; then
  516. log_msg "MySQL hit error limit"
  517. echo mysql-mailcow > /tmp/com_pipe
  518. fi
  519. done
  520. ) &
  521. BACKGROUND_TASKS+=($!)
  522. (
  523. while true; do
  524. if ! phpfpm_checks; then
  525. log_msg "PHP-FPM hit error limit"
  526. echo php-fpm-mailcow > /tmp/com_pipe
  527. fi
  528. done
  529. ) &
  530. BACKGROUND_TASKS+=($!)
  531. (
  532. while true; do
  533. if ! sogo_checks; then
  534. log_msg "SOGo hit error limit"
  535. echo sogo-mailcow > /tmp/com_pipe
  536. fi
  537. done
  538. ) &
  539. BACKGROUND_TASKS+=($!)
  540. if [ ${CHECK_UNBOUND} -eq 1 ]; then
  541. (
  542. while true; do
  543. if ! unbound_checks; then
  544. log_msg "Unbound hit error limit"
  545. echo unbound-mailcow > /tmp/com_pipe
  546. fi
  547. done
  548. ) &
  549. BACKGROUND_TASKS+=($!)
  550. fi
  551. if [[ "${SKIP_CLAMD}" =~ ^([nN][oO]|[nN])+$ ]]; then
  552. (
  553. while true; do
  554. if ! clamd_checks; then
  555. log_msg "Clamd hit error limit"
  556. echo clamd-mailcow > /tmp/com_pipe
  557. fi
  558. done
  559. ) &
  560. BACKGROUND_TASKS+=($!)
  561. fi
  562. (
  563. while true; do
  564. if ! postfix_checks; then
  565. log_msg "Postfix hit error limit"
  566. echo postfix-mailcow > /tmp/com_pipe
  567. fi
  568. done
  569. ) &
  570. BACKGROUND_TASKS+=($!)
  571. (
  572. while true; do
  573. if ! dovecot_checks; then
  574. log_msg "Dovecot hit error limit"
  575. echo dovecot-mailcow > /tmp/com_pipe
  576. fi
  577. done
  578. ) &
  579. BACKGROUND_TASKS+=($!)
  580. (
  581. while true; do
  582. if ! rspamd_checks; then
  583. log_msg "Rspamd hit error limit"
  584. echo rspamd-mailcow > /tmp/com_pipe
  585. fi
  586. done
  587. ) &
  588. BACKGROUND_TASKS+=($!)
  589. (
  590. while true; do
  591. if ! ratelimit_checks; then
  592. log_msg "Ratelimit hit error limit"
  593. echo ratelimit > /tmp/com_pipe
  594. fi
  595. done
  596. ) &
  597. BACKGROUND_TASKS+=($!)
  598. (
  599. while true; do
  600. if ! fail2ban_checks; then
  601. log_msg "Fail2ban hit error limit"
  602. echo fail2ban > /tmp/com_pipe
  603. fi
  604. done
  605. ) &
  606. BACKGROUND_TASKS+=($!)
  607. (
  608. while true; do
  609. if ! olefy_checks; then
  610. log_msg "Olefy hit error limit"
  611. echo olefy-mailcow > /tmp/com_pipe
  612. fi
  613. done
  614. ) &
  615. BACKGROUND_TASKS+=($!)
  616. (
  617. while true; do
  618. if ! acme_checks; then
  619. log_msg "ACME client hit error limit"
  620. echo acme-mailcow > /tmp/com_pipe
  621. fi
  622. done
  623. ) &
  624. BACKGROUND_TASKS+=($!)
  625. (
  626. while true; do
  627. if ! ipv6nat_checks; then
  628. log_msg "IPv6 NAT warning: ipv6nat-mailcow container was not started at least 30s after siblings (not an error)"
  629. echo ipv6nat-mailcow > /tmp/com_pipe
  630. fi
  631. done
  632. ) &
  633. BACKGROUND_TASKS+=($!)
  634. # Monitor watchdog agents, stop script when agents fails and wait for respawn by Docker (restart:always:n)
  635. (
  636. while true; do
  637. for bg_task in ${BACKGROUND_TASKS[*]}; do
  638. if ! kill -0 ${bg_task} 1>&2; then
  639. log_msg "Worker ${bg_task} died, stopping watchdog and waiting for respawn..."
  640. kill -TERM 1
  641. fi
  642. sleep 10
  643. done
  644. done
  645. ) &
  646. # Monitor dockerapi
  647. (
  648. while true; do
  649. while nc -z dockerapi 443; do
  650. sleep 3
  651. done
  652. log_msg "Cannot find dockerapi-mailcow, waiting to recover..."
  653. kill -STOP ${BACKGROUND_TASKS[*]}
  654. until nc -z dockerapi 443; do
  655. sleep 3
  656. done
  657. kill -CONT ${BACKGROUND_TASKS[*]}
  658. kill -USR1 ${BACKGROUND_TASKS[*]}
  659. done
  660. ) &
  661. # Actions when threshold limit is reached
  662. while true; do
  663. CONTAINER_ID=
  664. HAS_INITDB=
  665. read com_pipe_answer </tmp/com_pipe
  666. if [ -s "/tmp/${com_pipe_answer}" ]; then
  667. cat "/tmp/${com_pipe_answer}"
  668. fi
  669. if [[ ${com_pipe_answer} == "ratelimit" ]]; then
  670. log_msg "At least one ratelimit was applied"
  671. [[ ! -z ${WATCHDOG_NOTIFY_EMAIL} ]] && mail_error "${com_pipe_answer}" "Please see mailcow UI logs for further information."
  672. elif [[ ${com_pipe_answer} == "acme-mailcow" ]]; then
  673. log_msg "acme-mailcow did not complete successfully"
  674. [[ ! -z ${WATCHDOG_NOTIFY_EMAIL} ]] && mail_error "${com_pipe_answer}" "Please check acme-mailcow for further information."
  675. elif [[ ${com_pipe_answer} == "fail2ban" ]]; then
  676. F2B_RES=($(redis-cli -h redis-mailcow --raw GET F2B_RES))
  677. redis-cli -h redis-mailcow DEL F2B_RES > /dev/null
  678. host=
  679. for host in "${F2B_RES[@]}"; do
  680. log_msg "Banned ${host}"
  681. rm /tmp/fail2ban 2> /dev/null
  682. timeout 2s whois ${host} > /tmp/fail2ban
  683. sleep 2.5
  684. [[ ! -z ${WATCHDOG_NOTIFY_EMAIL} ]] && [[ ${WATCHDOG_NOTIFY_BAN} =~ ^([yY][eE][sS]|[yY])+$ ]] && mail_error "${com_pipe_answer}" "IP ban: ${host}"
  685. done
  686. elif [[ ${com_pipe_answer} =~ .+-mailcow ]]; then
  687. kill -STOP ${BACKGROUND_TASKS[*]}
  688. sleep 10
  689. 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")
  690. if [[ ! -z ${CONTAINER_ID} ]]; then
  691. if [[ "${com_pipe_answer}" == "php-fpm-mailcow" ]]; then
  692. 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)
  693. fi
  694. S_RUNNING=$(($(date +%s) - $(curl --silent --insecure https://dockerapi/containers/${CONTAINER_ID}/json | jq .State.StartedAt | xargs -n1 date +%s -d)))
  695. if [ ${S_RUNNING} -lt 360 ]; then
  696. log_msg "Container is running for less than 360 seconds, skipping action..."
  697. elif [[ ! -z ${HAS_INITDB} ]]; then
  698. log_msg "Database is being initialized by php-fpm-mailcow, not restarting but delaying checks for a minute..."
  699. sleep 60
  700. else
  701. log_msg "Sending restart command to ${CONTAINER_ID}..."
  702. curl --silent --insecure -XPOST https://dockerapi/containers/${CONTAINER_ID}/restart
  703. if [[ ${com_pipe_answer} != "ipv6nat-mailcow" ]]; then
  704. [[ ! -z ${WATCHDOG_NOTIFY_EMAIL} ]] && mail_error "${com_pipe_answer}"
  705. fi
  706. log_msg "Wait for restarted container to settle and continue watching..."
  707. sleep 35
  708. fi
  709. fi
  710. kill -CONT ${BACKGROUND_TASKS[*]}
  711. sleep 1
  712. kill -USR1 ${BACKGROUND_TASKS[*]}
  713. fi
  714. done