watchdog.sh 27 KB

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