watchdog.sh 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971
  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. # Do not attempt to write to slave
  23. if [[ ! -z ${REDIS_SLAVEOF_IP} ]]; then
  24. REDIS_CMDLINE="redis-cli -h ${REDIS_SLAVEOF_IP} -p ${REDIS_SLAVEOF_PORT}"
  25. else
  26. REDIS_CMDLINE="redis-cli -h redis -p 6379"
  27. fi
  28. until [[ $(${REDIS_CMDLINE} PING) == "PONG" ]]; do
  29. echo "Waiting for Redis..."
  30. sleep 2
  31. done
  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. err_c_cur=${err_count}
  276. /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} + $? ))
  277. /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} + $? ))
  278. [ ${err_c_cur} -eq ${err_count} ] && [ ! $((${err_count} - 1)) -lt 0 ] && err_count=$((${err_count} - 1)) diff_c=1
  279. [ ${err_c_cur} -ne ${err_count} ] && diff_c=$(( ${err_c_cur} - ${err_count} ))
  280. progress "MySQL/MariaDB" ${THRESHOLD} $(( ${THRESHOLD} - ${err_count} )) ${diff_c}
  281. if [[ $? == 10 ]]; then
  282. diff_c=0
  283. sleep 1
  284. else
  285. diff_c=0
  286. sleep $(( ( RANDOM % 60 ) + 20 ))
  287. fi
  288. done
  289. return 1
  290. }
  291. mysql_repl_checks() {
  292. err_count=0
  293. diff_c=0
  294. THRESHOLD=${MYSQL_REPLICATION_THRESHOLD}
  295. # Reduce error count by 2 after restarting an unhealthy container
  296. trap "[ ${err_count} -gt 1 ] && err_count=$(( ${err_count} - 2 ))" USR1
  297. while [ ${err_count} -lt ${THRESHOLD} ]; do
  298. touch /tmp/mysql_repl_checks; echo "$(tail -50 /tmp/mysql_repl_checks)" > /tmp/mysql_repl_checks
  299. err_c_cur=${err_count}
  300. /usr/lib/nagios/plugins/check_mysql_slavestatus.sh -S /var/run/mysqld/mysqld.sock -u root -p ${DBROOT} 2>> /tmp/mysql_repl_checks 1>&2; err_count=$(( ${err_count} + $? ))
  301. [ ${err_c_cur} -eq ${err_count} ] && [ ! $((${err_count} - 1)) -lt 0 ] && err_count=$((${err_count} - 1)) diff_c=1
  302. [ ${err_c_cur} -ne ${err_count} ] && diff_c=$(( ${err_c_cur} - ${err_count} ))
  303. progress "MySQL/MariaDB replication" ${THRESHOLD} $(( ${THRESHOLD} - ${err_count} )) ${diff_c}
  304. if [[ $? == 10 ]]; then
  305. diff_c=0
  306. sleep 60
  307. else
  308. diff_c=0
  309. sleep $(( ( RANDOM % 60 ) + 20 ))
  310. fi
  311. done
  312. return 1
  313. }
  314. sogo_checks() {
  315. err_count=0
  316. diff_c=0
  317. THRESHOLD=${SOGO_THRESHOLD}
  318. # Reduce error count by 2 after restarting an unhealthy container
  319. trap "[ ${err_count} -gt 1 ] && err_count=$(( ${err_count} - 2 ))" USR1
  320. while [ ${err_count} -lt ${THRESHOLD} ]; do
  321. touch /tmp/sogo-mailcow; echo "$(tail -50 /tmp/sogo-mailcow)" > /tmp/sogo-mailcow
  322. host_ip=$(get_container_ip sogo-mailcow)
  323. err_c_cur=${err_count}
  324. /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} + $? ))
  325. [ ${err_c_cur} -eq ${err_count} ] && [ ! $((${err_count} - 1)) -lt 0 ] && err_count=$((${err_count} - 1)) diff_c=1
  326. [ ${err_c_cur} -ne ${err_count} ] && diff_c=$(( ${err_c_cur} - ${err_count} ))
  327. progress "SOGo" ${THRESHOLD} $(( ${THRESHOLD} - ${err_count} )) ${diff_c}
  328. if [[ $? == 10 ]]; then
  329. diff_c=0
  330. sleep 1
  331. else
  332. diff_c=0
  333. sleep $(( ( RANDOM % 60 ) + 20 ))
  334. fi
  335. done
  336. return 1
  337. }
  338. postfix_checks() {
  339. err_count=0
  340. diff_c=0
  341. THRESHOLD=${POSTFIX_THRESHOLD}
  342. # Reduce error count by 2 after restarting an unhealthy container
  343. trap "[ ${err_count} -gt 1 ] && err_count=$(( ${err_count} - 2 ))" USR1
  344. while [ ${err_count} -lt ${THRESHOLD} ]; do
  345. touch /tmp/postfix-mailcow; echo "$(tail -50 /tmp/postfix-mailcow)" > /tmp/postfix-mailcow
  346. host_ip=$(get_container_ip postfix-mailcow)
  347. err_c_cur=${err_count}
  348. /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} + $? ))
  349. /usr/lib/nagios/plugins/check_smtp -4 -H ${host_ip} -p 589 -S 2>> /tmp/postfix-mailcow 1>&2; err_count=$(( ${err_count} + $? ))
  350. [ ${err_c_cur} -eq ${err_count} ] && [ ! $((${err_count} - 1)) -lt 0 ] && err_count=$((${err_count} - 1)) diff_c=1
  351. [ ${err_c_cur} -ne ${err_count} ] && diff_c=$(( ${err_c_cur} - ${err_count} ))
  352. progress "Postfix" ${THRESHOLD} $(( ${THRESHOLD} - ${err_count} )) ${diff_c}
  353. if [[ $? == 10 ]]; then
  354. diff_c=0
  355. sleep 1
  356. else
  357. diff_c=0
  358. sleep $(( ( RANDOM % 60 ) + 20 ))
  359. fi
  360. done
  361. return 1
  362. }
  363. clamd_checks() {
  364. err_count=0
  365. diff_c=0
  366. THRESHOLD=${CLAMD_THRESHOLD}
  367. # Reduce error count by 2 after restarting an unhealthy container
  368. trap "[ ${err_count} -gt 1 ] && err_count=$(( ${err_count} - 2 ))" USR1
  369. while [ ${err_count} -lt ${THRESHOLD} ]; do
  370. touch /tmp/clamd-mailcow; echo "$(tail -50 /tmp/clamd-mailcow)" > /tmp/clamd-mailcow
  371. host_ip=$(get_container_ip clamd-mailcow)
  372. err_c_cur=${err_count}
  373. /usr/lib/nagios/plugins/check_clamd -4 -H ${host_ip} 2>> /tmp/clamd-mailcow 1>&2; err_count=$(( ${err_count} + $? ))
  374. [ ${err_c_cur} -eq ${err_count} ] && [ ! $((${err_count} - 1)) -lt 0 ] && err_count=$((${err_count} - 1)) diff_c=1
  375. [ ${err_c_cur} -ne ${err_count} ] && diff_c=$(( ${err_c_cur} - ${err_count} ))
  376. progress "Clamd" ${THRESHOLD} $(( ${THRESHOLD} - ${err_count} )) ${diff_c}
  377. if [[ $? == 10 ]]; then
  378. diff_c=0
  379. sleep 1
  380. else
  381. diff_c=0
  382. sleep $(( ( RANDOM % 120 ) + 20 ))
  383. fi
  384. done
  385. return 1
  386. }
  387. dovecot_checks() {
  388. err_count=0
  389. diff_c=0
  390. THRESHOLD=${DOVECOT_THRESHOLD}
  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. touch /tmp/dovecot-mailcow; echo "$(tail -50 /tmp/dovecot-mailcow)" > /tmp/dovecot-mailcow
  395. host_ip=$(get_container_ip dovecot-mailcow)
  396. err_c_cur=${err_count}
  397. /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} + $? ))
  398. /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} + $? ))
  399. /usr/lib/nagios/plugins/check_imap -4 -H ${host_ip} -p 143 -e "OK " 2>> /tmp/dovecot-mailcow 1>&2; err_count=$(( ${err_count} + $? ))
  400. /usr/lib/nagios/plugins/check_tcp -4 -H ${host_ip} -p 10001 -e "VERSION" 2>> /tmp/dovecot-mailcow 1>&2; err_count=$(( ${err_count} + $? ))
  401. /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} + $? ))
  402. [ ${err_c_cur} -eq ${err_count} ] && [ ! $((${err_count} - 1)) -lt 0 ] && err_count=$((${err_count} - 1)) diff_c=1
  403. [ ${err_c_cur} -ne ${err_count} ] && diff_c=$(( ${err_c_cur} - ${err_count} ))
  404. progress "Dovecot" ${THRESHOLD} $(( ${THRESHOLD} - ${err_count} )) ${diff_c}
  405. if [[ $? == 10 ]]; then
  406. diff_c=0
  407. sleep 1
  408. else
  409. diff_c=0
  410. sleep $(( ( RANDOM % 60 ) + 20 ))
  411. fi
  412. done
  413. return 1
  414. }
  415. phpfpm_checks() {
  416. err_count=0
  417. diff_c=0
  418. THRESHOLD=${PHPFPM_THRESHOLD}
  419. # Reduce error count by 2 after restarting an unhealthy container
  420. trap "[ ${err_count} -gt 1 ] && err_count=$(( ${err_count} - 2 ))" USR1
  421. while [ ${err_count} -lt ${THRESHOLD} ]; do
  422. touch /tmp/php-fpm-mailcow; echo "$(tail -50 /tmp/php-fpm-mailcow)" > /tmp/php-fpm-mailcow
  423. host_ip=$(get_container_ip php-fpm-mailcow)
  424. err_c_cur=${err_count}
  425. /usr/lib/nagios/plugins/check_tcp -H ${host_ip} -p 9001 2>> /tmp/php-fpm-mailcow 1>&2; err_count=$(( ${err_count} + $? ))
  426. /usr/lib/nagios/plugins/check_tcp -H ${host_ip} -p 9002 2>> /tmp/php-fpm-mailcow 1>&2; err_count=$(( ${err_count} + $? ))
  427. [ ${err_c_cur} -eq ${err_count} ] && [ ! $((${err_count} - 1)) -lt 0 ] && err_count=$((${err_count} - 1)) diff_c=1
  428. [ ${err_c_cur} -ne ${err_count} ] && diff_c=$(( ${err_c_cur} - ${err_count} ))
  429. progress "PHP-FPM" ${THRESHOLD} $(( ${THRESHOLD} - ${err_count} )) ${diff_c}
  430. if [[ $? == 10 ]]; then
  431. diff_c=0
  432. sleep 1
  433. else
  434. diff_c=0
  435. sleep $(( ( RANDOM % 60 ) + 20 ))
  436. fi
  437. done
  438. return 1
  439. }
  440. ratelimit_checks() {
  441. err_count=0
  442. diff_c=0
  443. THRESHOLD=${RATELIMIT_THRESHOLD}
  444. RL_LOG_STATUS=$(redis-cli -h redis LRANGE RL_LOG 0 0 | jq .qid)
  445. # Reduce error count by 2 after restarting an unhealthy container
  446. trap "[ ${err_count} -gt 1 ] && err_count=$(( ${err_count} - 2 ))" USR1
  447. while [ ${err_count} -lt ${THRESHOLD} ]; do
  448. err_c_cur=${err_count}
  449. RL_LOG_STATUS_PREV=${RL_LOG_STATUS}
  450. RL_LOG_STATUS=$(redis-cli -h redis LRANGE RL_LOG 0 0 | jq .qid)
  451. if [[ ${RL_LOG_STATUS_PREV} != ${RL_LOG_STATUS} ]]; then
  452. err_count=$(( ${err_count} + 1 ))
  453. echo 'Last 10 applied ratelimits (may overlap with previous reports).' > /tmp/ratelimit
  454. echo 'Full ratelimit buckets can be emptied by deleting the ratelimit hash from within mailcow UI (see /debug -> Protocols -> Ratelimit):' >> /tmp/ratelimit
  455. echo >> /tmp/ratelimit
  456. redis-cli --raw -h redis LRANGE RL_LOG 0 10 | jq . >> /tmp/ratelimit
  457. fi
  458. [ ${err_c_cur} -eq ${err_count} ] && [ ! $((${err_count} - 1)) -lt 0 ] && err_count=$((${err_count} - 1)) diff_c=1
  459. [ ${err_c_cur} -ne ${err_count} ] && diff_c=$(( ${err_c_cur} - ${err_count} ))
  460. progress "Ratelimit" ${THRESHOLD} $(( ${THRESHOLD} - ${err_count} )) ${diff_c}
  461. if [[ $? == 10 ]]; then
  462. diff_c=0
  463. sleep 1
  464. else
  465. diff_c=0
  466. sleep $(( ( RANDOM % 60 ) + 20 ))
  467. fi
  468. done
  469. return 1
  470. }
  471. fail2ban_checks() {
  472. err_count=0
  473. diff_c=0
  474. THRESHOLD=${FAIL2BAN_THRESHOLD}
  475. F2B_LOG_STATUS=($(${REDIS_CMDLINE} --raw HKEYS F2B_ACTIVE_BANS))
  476. F2B_RES=
  477. # Reduce error count by 2 after restarting an unhealthy container
  478. trap "[ ${err_count} -gt 1 ] && err_count=$(( ${err_count} - 2 ))" USR1
  479. while [ ${err_count} -lt ${THRESHOLD} ]; do
  480. err_c_cur=${err_count}
  481. F2B_LOG_STATUS_PREV=(${F2B_LOG_STATUS[@]})
  482. F2B_LOG_STATUS=($(${REDIS_CMDLINE} --raw HKEYS F2B_ACTIVE_BANS))
  483. array_diff F2B_RES F2B_LOG_STATUS F2B_LOG_STATUS_PREV
  484. if [[ ! -z "${F2B_RES}" ]]; then
  485. err_count=$(( ${err_count} + 1 ))
  486. echo -n "${F2B_RES[@]}" | tr -cd "[a-fA-F0-9.:/] " | timeout 3s ${REDIS_CMDLINE} -x SET F2B_RES > /dev/null
  487. if [ $? -ne 0 ]; then
  488. ${REDIS_CMDLINE} -x DEL F2B_RES
  489. fi
  490. fi
  491. [ ${err_c_cur} -eq ${err_count} ] && [ ! $((${err_count} - 1)) -lt 0 ] && err_count=$((${err_count} - 1)) diff_c=1
  492. [ ${err_c_cur} -ne ${err_count} ] && diff_c=$(( ${err_c_cur} - ${err_count} ))
  493. progress "Fail2ban" ${THRESHOLD} $(( ${THRESHOLD} - ${err_count} )) ${diff_c}
  494. if [[ $? == 10 ]]; then
  495. diff_c=0
  496. sleep 1
  497. else
  498. diff_c=0
  499. sleep $(( ( RANDOM % 60 ) + 20 ))
  500. fi
  501. done
  502. return 1
  503. }
  504. acme_checks() {
  505. err_count=0
  506. diff_c=0
  507. THRESHOLD=${ACME_THRESHOLD}
  508. ACME_LOG_STATUS=$(redis-cli -h redis GET ACME_FAIL_TIME)
  509. if [[ -z "${ACME_LOG_STATUS}" ]]; then
  510. ${REDIS_CMDLINE} SET ACME_FAIL_TIME 0
  511. ACME_LOG_STATUS=0
  512. fi
  513. # Reduce error count by 2 after restarting an unhealthy container
  514. trap "[ ${err_count} -gt 1 ] && err_count=$(( ${err_count} - 2 ))" USR1
  515. while [ ${err_count} -lt ${THRESHOLD} ]; do
  516. err_c_cur=${err_count}
  517. ACME_LOG_STATUS_PREV=${ACME_LOG_STATUS}
  518. ACME_LC=0
  519. until [[ ! -z ${ACME_LOG_STATUS} ]] || [ ${ACME_LC} -ge 3 ]; do
  520. ACME_LOG_STATUS=$(redis-cli -h redis GET ACME_FAIL_TIME 2> /dev/null)
  521. sleep 3
  522. ACME_LC=$((ACME_LC+1))
  523. done
  524. if [[ ${ACME_LOG_STATUS_PREV} != ${ACME_LOG_STATUS} ]]; then
  525. err_count=$(( ${err_count} + 1 ))
  526. fi
  527. [ ${err_c_cur} -eq ${err_count} ] && [ ! $((${err_count} - 1)) -lt 0 ] && err_count=$((${err_count} - 1)) diff_c=1
  528. [ ${err_c_cur} -ne ${err_count} ] && diff_c=$(( ${err_c_cur} - ${err_count} ))
  529. progress "ACME" ${THRESHOLD} $(( ${THRESHOLD} - ${err_count} )) ${diff_c}
  530. if [[ $? == 10 ]]; then
  531. diff_c=0
  532. sleep 1
  533. else
  534. diff_c=0
  535. sleep $(( ( RANDOM % 60 ) + 20 ))
  536. fi
  537. done
  538. return 1
  539. }
  540. ipv6nat_checks() {
  541. err_count=0
  542. diff_c=0
  543. THRESHOLD=${IPV6NAT_THRESHOLD}
  544. # Reduce error count by 2 after restarting an unhealthy container
  545. trap "[ ${err_count} -gt 1 ] && err_count=$(( ${err_count} - 2 ))" USR1
  546. while [ ${err_count} -lt ${THRESHOLD} ]; do
  547. err_c_cur=${err_count}
  548. CONTAINERS=$(curl --silent --insecure https://dockerapi/containers/json)
  549. 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")
  550. if [[ ! -z ${IPV6NAT_CONTAINER_ID} ]]; then
  551. 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)"
  552. 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)"
  553. DIFFERENCE_START_TIME=$(expr ${LATEST_IPV6NAT} - ${LATEST_STARTED} 2>/dev/null)
  554. if [[ "${DIFFERENCE_START_TIME}" -lt 30 ]]; then
  555. err_count=$(( ${err_count} + 1 ))
  556. fi
  557. fi
  558. [ ${err_c_cur} -eq ${err_count} ] && [ ! $((${err_count} - 1)) -lt 0 ] && err_count=$((${err_count} - 1)) diff_c=1
  559. [ ${err_c_cur} -ne ${err_count} ] && diff_c=$(( ${err_c_cur} - ${err_count} ))
  560. progress "IPv6 NAT" ${THRESHOLD} $(( ${THRESHOLD} - ${err_count} )) ${diff_c}
  561. if [[ $? == 10 ]]; then
  562. diff_c=0
  563. sleep 30
  564. else
  565. diff_c=0
  566. sleep 300
  567. fi
  568. done
  569. return 1
  570. }
  571. rspamd_checks() {
  572. err_count=0
  573. diff_c=0
  574. THRESHOLD=${RSPAMD_THRESHOLD}
  575. # Reduce error count by 2 after restarting an unhealthy container
  576. trap "[ ${err_count} -gt 1 ] && err_count=$(( ${err_count} - 2 ))" USR1
  577. while [ ${err_count} -lt ${THRESHOLD} ]; do
  578. touch /tmp/rspamd-mailcow; echo "$(tail -50 /tmp/rspamd-mailcow)" > /tmp/rspamd-mailcow
  579. host_ip=$(get_container_ip rspamd-mailcow)
  580. err_c_cur=${err_count}
  581. SCORE=$(echo 'To: null@localhost
  582. From: watchdog@localhost
  583. Empty
  584. ' | usr/bin/curl -s --data-binary @- --unix-socket /var/lib/rspamd/rspamd.sock http://rspamd/scan | jq -rc .default.required_score)
  585. if [[ ${SCORE} != "9999" ]]; then
  586. echo "Rspamd settings check failed" 2>> /tmp/rspamd-mailcow 1>&2
  587. err_count=$(( ${err_count} + 1))
  588. else
  589. echo "Rspamd settings check succeeded" 2>> /tmp/rspamd-mailcow 1>&2
  590. fi
  591. [ ${err_c_cur} -eq ${err_count} ] && [ ! $((${err_count} - 1)) -lt 0 ] && err_count=$((${err_count} - 1)) diff_c=1
  592. [ ${err_c_cur} -ne ${err_count} ] && diff_c=$(( ${err_c_cur} - ${err_count} ))
  593. progress "Rspamd" ${THRESHOLD} $(( ${THRESHOLD} - ${err_count} )) ${diff_c}
  594. if [[ $? == 10 ]]; then
  595. diff_c=0
  596. sleep 1
  597. else
  598. diff_c=0
  599. sleep $(( ( RANDOM % 60 ) + 20 ))
  600. fi
  601. done
  602. return 1
  603. }
  604. olefy_checks() {
  605. err_count=0
  606. diff_c=0
  607. THRESHOLD=${OLEFY_THRESHOLD}
  608. # Reduce error count by 2 after restarting an unhealthy container
  609. trap "[ ${err_count} -gt 1 ] && err_count=$(( ${err_count} - 2 ))" USR1
  610. while [ ${err_count} -lt ${THRESHOLD} ]; do
  611. touch /tmp/olefy-mailcow; echo "$(tail -50 /tmp/olefy-mailcow)" > /tmp/olefy-mailcow
  612. host_ip=$(get_container_ip olefy-mailcow)
  613. err_c_cur=${err_count}
  614. /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} + $? ))
  615. [ ${err_c_cur} -eq ${err_count} ] && [ ! $((${err_count} - 1)) -lt 0 ] && err_count=$((${err_count} - 1)) diff_c=1
  616. [ ${err_c_cur} -ne ${err_count} ] && diff_c=$(( ${err_c_cur} - ${err_count} ))
  617. progress "Olefy" ${THRESHOLD} $(( ${THRESHOLD} - ${err_count} )) ${diff_c}
  618. if [[ $? == 10 ]]; then
  619. diff_c=0
  620. sleep 1
  621. else
  622. diff_c=0
  623. sleep $(( ( RANDOM % 60 ) + 20 ))
  624. fi
  625. done
  626. return 1
  627. }
  628. # Notify about start
  629. if [[ ! -z ${WATCHDOG_NOTIFY_EMAIL} ]]; then
  630. mail_error "watchdog-mailcow" "Watchdog started monitoring mailcow."
  631. fi
  632. # Create watchdog agents
  633. (
  634. while true; do
  635. if ! nginx_checks; then
  636. log_msg "Nginx hit error limit"
  637. echo nginx-mailcow > /tmp/com_pipe
  638. fi
  639. done
  640. ) &
  641. PID=$!
  642. echo "Spawned nginx_checks with PID ${PID}"
  643. BACKGROUND_TASKS+=(${PID})
  644. if [[ ${WATCHDOG_EXTERNAL_CHECKS} =~ ^([yY][eE][sS]|[yY])+$ ]]; then
  645. (
  646. while true; do
  647. if ! external_checks; then
  648. log_msg "External checks hit error limit"
  649. echo external_checks > /tmp/com_pipe
  650. fi
  651. done
  652. ) &
  653. PID=$!
  654. echo "Spawned external_checks with PID ${PID}"
  655. BACKGROUND_TASKS+=(${PID})
  656. fi
  657. if [[ ${WATCHDOG_MYSQL_REPLICATION_CHECKS} =~ ^([yY][eE][sS]|[yY])+$ ]]; then
  658. (
  659. while true; do
  660. if ! mysql_repl_checks; then
  661. log_msg "MySQL replication check hit error limit"
  662. echo mysql_repl_checks > /tmp/com_pipe
  663. fi
  664. done
  665. ) &
  666. PID=$!
  667. echo "Spawned mysql_repl_checks with PID ${PID}"
  668. BACKGROUND_TASKS+=(${PID})
  669. fi
  670. (
  671. while true; do
  672. if ! mysql_checks; then
  673. log_msg "MySQL hit error limit"
  674. echo mysql-mailcow > /tmp/com_pipe
  675. fi
  676. done
  677. ) &
  678. PID=$!
  679. echo "Spawned mysql_checks with PID ${PID}"
  680. BACKGROUND_TASKS+=(${PID})
  681. (
  682. while true; do
  683. if ! redis_checks; then
  684. log_msg "Local Redis hit error limit"
  685. echo redis-mailcow > /tmp/com_pipe
  686. fi
  687. done
  688. ) &
  689. PID=$!
  690. echo "Spawned redis_checks with PID ${PID}"
  691. BACKGROUND_TASKS+=(${PID})
  692. (
  693. while true; do
  694. if ! phpfpm_checks; then
  695. log_msg "PHP-FPM hit error limit"
  696. echo php-fpm-mailcow > /tmp/com_pipe
  697. fi
  698. done
  699. ) &
  700. PID=$!
  701. echo "Spawned phpfpm_checks with PID ${PID}"
  702. BACKGROUND_TASKS+=(${PID})
  703. (
  704. while true; do
  705. if ! sogo_checks; then
  706. log_msg "SOGo hit error limit"
  707. echo sogo-mailcow > /tmp/com_pipe
  708. fi
  709. done
  710. ) &
  711. PID=$!
  712. echo "Spawned sogo_checks with PID ${PID}"
  713. BACKGROUND_TASKS+=(${PID})
  714. if [ ${CHECK_UNBOUND} -eq 1 ]; then
  715. (
  716. while true; do
  717. if ! unbound_checks; then
  718. log_msg "Unbound hit error limit"
  719. echo unbound-mailcow > /tmp/com_pipe
  720. fi
  721. done
  722. ) &
  723. PID=$!
  724. echo "Spawned unbound_checks with PID ${PID}"
  725. BACKGROUND_TASKS+=(${PID})
  726. fi
  727. if [[ "${SKIP_CLAMD}" =~ ^([nN][oO]|[nN])+$ ]]; then
  728. (
  729. while true; do
  730. if ! clamd_checks; then
  731. log_msg "Clamd hit error limit"
  732. echo clamd-mailcow > /tmp/com_pipe
  733. fi
  734. done
  735. ) &
  736. PID=$!
  737. echo "Spawned clamd_checks with PID ${PID}"
  738. BACKGROUND_TASKS+=(${PID})
  739. fi
  740. (
  741. while true; do
  742. if ! postfix_checks; then
  743. log_msg "Postfix hit error limit"
  744. echo postfix-mailcow > /tmp/com_pipe
  745. fi
  746. done
  747. ) &
  748. PID=$!
  749. echo "Spawned postfix_checks with PID ${PID}"
  750. BACKGROUND_TASKS+=(${PID})
  751. (
  752. while true; do
  753. if ! dovecot_checks; then
  754. log_msg "Dovecot hit error limit"
  755. echo dovecot-mailcow > /tmp/com_pipe
  756. fi
  757. done
  758. ) &
  759. PID=$!
  760. echo "Spawned dovecot_checks with PID ${PID}"
  761. BACKGROUND_TASKS+=(${PID})
  762. (
  763. while true; do
  764. if ! rspamd_checks; then
  765. log_msg "Rspamd hit error limit"
  766. echo rspamd-mailcow > /tmp/com_pipe
  767. fi
  768. done
  769. ) &
  770. PID=$!
  771. echo "Spawned rspamd_checks with PID ${PID}"
  772. BACKGROUND_TASKS+=(${PID})
  773. (
  774. while true; do
  775. if ! ratelimit_checks; then
  776. log_msg "Ratelimit hit error limit"
  777. echo ratelimit > /tmp/com_pipe
  778. fi
  779. done
  780. ) &
  781. PID=$!
  782. echo "Spawned ratelimit_checks with PID ${PID}"
  783. BACKGROUND_TASKS+=(${PID})
  784. (
  785. while true; do
  786. if ! fail2ban_checks; then
  787. log_msg "Fail2ban hit error limit"
  788. echo fail2ban > /tmp/com_pipe
  789. fi
  790. done
  791. ) &
  792. PID=$!
  793. echo "Spawned fail2ban_checks with PID ${PID}"
  794. BACKGROUND_TASKS+=(${PID})
  795. (
  796. while true; do
  797. if ! olefy_checks; then
  798. log_msg "Olefy hit error limit"
  799. echo olefy-mailcow > /tmp/com_pipe
  800. fi
  801. done
  802. ) &
  803. PID=$!
  804. echo "Spawned olefy_checks with PID ${PID}"
  805. BACKGROUND_TASKS+=(${PID})
  806. (
  807. while true; do
  808. if ! acme_checks; then
  809. log_msg "ACME client hit error limit"
  810. echo acme-mailcow > /tmp/com_pipe
  811. fi
  812. done
  813. ) &
  814. PID=$!
  815. echo "Spawned acme_checks with PID ${PID}"
  816. BACKGROUND_TASKS+=(${PID})
  817. (
  818. while true; do
  819. if ! ipv6nat_checks; then
  820. log_msg "IPv6 NAT warning: ipv6nat-mailcow container was not started at least 30s after siblings (not an error)"
  821. echo ipv6nat-mailcow > /tmp/com_pipe
  822. fi
  823. done
  824. ) &
  825. PID=$!
  826. echo "Spawned ipv6nat_checks with PID ${PID}"
  827. BACKGROUND_TASKS+=(${PID})
  828. # Monitor watchdog agents, stop script when agents fails and wait for respawn by Docker (restart:always:n)
  829. (
  830. while true; do
  831. for bg_task in ${BACKGROUND_TASKS[*]}; do
  832. if ! kill -0 ${bg_task} 1>&2; then
  833. log_msg "Worker ${bg_task} died, stopping watchdog and waiting for respawn..."
  834. kill -TERM 1
  835. fi
  836. sleep 10
  837. done
  838. done
  839. ) &
  840. # Monitor dockerapi
  841. (
  842. while true; do
  843. while nc -z dockerapi 443; do
  844. sleep 3
  845. done
  846. log_msg "Cannot find dockerapi-mailcow, waiting to recover..."
  847. kill -STOP ${BACKGROUND_TASKS[*]}
  848. until nc -z dockerapi 443; do
  849. sleep 3
  850. done
  851. kill -CONT ${BACKGROUND_TASKS[*]}
  852. kill -USR1 ${BACKGROUND_TASKS[*]}
  853. done
  854. ) &
  855. # Actions when threshold limit is reached
  856. while true; do
  857. CONTAINER_ID=
  858. HAS_INITDB=
  859. read com_pipe_answer </tmp/com_pipe
  860. if [ -s "/tmp/${com_pipe_answer}" ]; then
  861. cat "/tmp/${com_pipe_answer}"
  862. fi
  863. if [[ ${com_pipe_answer} == "ratelimit" ]]; then
  864. log_msg "At least one ratelimit was applied"
  865. [[ ! -z ${WATCHDOG_NOTIFY_EMAIL} ]] && mail_error "${com_pipe_answer}"
  866. elif [[ ${com_pipe_answer} == "external_checks" ]]; then
  867. log_msg "Your mailcow is an open relay!"
  868. [[ ! -z ${WATCHDOG_NOTIFY_EMAIL} ]] && mail_error "${com_pipe_answer}" "Please stop mailcow now and check your network configuration!"
  869. elif [[ ${com_pipe_answer} == "mysql_repl_checks" ]]; then
  870. log_msg "MySQL replication is not working properly"
  871. [[ ! -z ${WATCHDOG_NOTIFY_EMAIL} ]] && mail_error "${com_pipe_answer}"
  872. elif [[ ${com_pipe_answer} == "acme-mailcow" ]]; then
  873. log_msg "acme-mailcow did not complete successfully"
  874. [[ ! -z ${WATCHDOG_NOTIFY_EMAIL} ]] && mail_error "${com_pipe_answer}" "Please check acme-mailcow for further information."
  875. elif [[ ${com_pipe_answer} == "fail2ban" ]]; then
  876. F2B_RES=($(timeout 4s ${REDIS_CMDLINE} --raw GET F2B_RES 2> /dev/null))
  877. if [[ ! -z "${F2B_RES}" ]]; then
  878. ${REDIS_CMDLINE} DEL F2B_RES > /dev/null
  879. host=
  880. for host in "${F2B_RES[@]}"; do
  881. log_msg "Banned ${host}"
  882. rm /tmp/fail2ban 2> /dev/null
  883. timeout 2s whois "${host}" > /tmp/fail2ban
  884. [[ ! -z ${WATCHDOG_NOTIFY_EMAIL} ]] && [[ ${WATCHDOG_NOTIFY_BAN} =~ ^([yY][eE][sS]|[yY])+$ ]] && mail_error "${com_pipe_answer}" "IP ban: ${host}"
  885. done
  886. fi
  887. elif [[ ${com_pipe_answer} =~ .+-mailcow ]]; then
  888. kill -STOP ${BACKGROUND_TASKS[*]}
  889. sleep 10
  890. 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")
  891. if [[ ! -z ${CONTAINER_ID} ]]; then
  892. if [[ "${com_pipe_answer}" == "php-fpm-mailcow" ]]; then
  893. 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)
  894. fi
  895. S_RUNNING=$(($(date +%s) - $(curl --silent --insecure https://dockerapi/containers/${CONTAINER_ID}/json | jq .State.StartedAt | xargs -n1 date +%s -d)))
  896. if [ ${S_RUNNING} -lt 360 ]; then
  897. log_msg "Container is running for less than 360 seconds, skipping action..."
  898. elif [[ ! -z ${HAS_INITDB} ]]; then
  899. log_msg "Database is being initialized by php-fpm-mailcow, not restarting but delaying checks for a minute..."
  900. sleep 60
  901. else
  902. log_msg "Sending restart command to ${CONTAINER_ID}..."
  903. curl --silent --insecure -XPOST https://dockerapi/containers/${CONTAINER_ID}/restart
  904. if [[ ${com_pipe_answer} != "ipv6nat-mailcow" ]]; then
  905. [[ ! -z ${WATCHDOG_NOTIFY_EMAIL} ]] && mail_error "${com_pipe_answer}"
  906. fi
  907. log_msg "Wait for restarted container to settle and continue watching..."
  908. sleep 35
  909. fi
  910. fi
  911. kill -CONT ${BACKGROUND_TASKS[*]}
  912. sleep 1
  913. kill -USR1 ${BACKGROUND_TASKS[*]}
  914. fi
  915. done