watchdog.sh 36 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015
  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. dovecot_repl_checks() {
  416. err_count=0
  417. diff_c=0
  418. THRESHOLD=${DOVECOT_REPL_THRESHOLD}
  419. D_REPL_STATUS=$(redis-cli -h redis -r GET DOVECOT_REPL_HEALTH)
  420. # Reduce error count by 2 after restarting an unhealthy container
  421. trap "[ ${err_count} -gt 1 ] && err_count=$(( ${err_count} - 2 ))" USR1
  422. while [ ${err_count} -lt ${THRESHOLD} ]; do
  423. err_c_cur=${err_count}
  424. D_REPL_STATUS=$(redis-cli --raw -h redis GET DOVECOT_REPL_HEALTH)
  425. if [[ "${D_REPL_STATUS}" != "1" ]]; then
  426. err_count=$(( ${err_count} + 1 ))
  427. fi
  428. [ ${err_c_cur} -eq ${err_count} ] && [ ! $((${err_count} - 1)) -lt 0 ] && err_count=$((${err_count} - 1)) diff_c=1
  429. [ ${err_c_cur} -ne ${err_count} ] && diff_c=$(( ${err_c_cur} - ${err_count} ))
  430. progress "Dovecot replication" ${THRESHOLD} $(( ${THRESHOLD} - ${err_count} )) ${diff_c}
  431. if [[ $? == 10 ]]; then
  432. diff_c=0
  433. sleep 1
  434. else
  435. diff_c=0
  436. sleep $(( ( RANDOM % 60 ) + 20 ))
  437. fi
  438. done
  439. return 1
  440. }
  441. phpfpm_checks() {
  442. err_count=0
  443. diff_c=0
  444. THRESHOLD=${PHPFPM_THRESHOLD}
  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. touch /tmp/php-fpm-mailcow; echo "$(tail -50 /tmp/php-fpm-mailcow)" > /tmp/php-fpm-mailcow
  449. host_ip=$(get_container_ip php-fpm-mailcow)
  450. err_c_cur=${err_count}
  451. /usr/lib/nagios/plugins/check_tcp -H ${host_ip} -p 9001 2>> /tmp/php-fpm-mailcow 1>&2; err_count=$(( ${err_count} + $? ))
  452. /usr/lib/nagios/plugins/check_tcp -H ${host_ip} -p 9002 2>> /tmp/php-fpm-mailcow 1>&2; err_count=$(( ${err_count} + $? ))
  453. [ ${err_c_cur} -eq ${err_count} ] && [ ! $((${err_count} - 1)) -lt 0 ] && err_count=$((${err_count} - 1)) diff_c=1
  454. [ ${err_c_cur} -ne ${err_count} ] && diff_c=$(( ${err_c_cur} - ${err_count} ))
  455. progress "PHP-FPM" ${THRESHOLD} $(( ${THRESHOLD} - ${err_count} )) ${diff_c}
  456. if [[ $? == 10 ]]; then
  457. diff_c=0
  458. sleep 1
  459. else
  460. diff_c=0
  461. sleep $(( ( RANDOM % 60 ) + 20 ))
  462. fi
  463. done
  464. return 1
  465. }
  466. ratelimit_checks() {
  467. err_count=0
  468. diff_c=0
  469. THRESHOLD=${RATELIMIT_THRESHOLD}
  470. RL_LOG_STATUS=$(redis-cli -h redis LRANGE RL_LOG 0 0 | jq .qid)
  471. # Reduce error count by 2 after restarting an unhealthy container
  472. trap "[ ${err_count} -gt 1 ] && err_count=$(( ${err_count} - 2 ))" USR1
  473. while [ ${err_count} -lt ${THRESHOLD} ]; do
  474. err_c_cur=${err_count}
  475. RL_LOG_STATUS_PREV=${RL_LOG_STATUS}
  476. RL_LOG_STATUS=$(redis-cli -h redis LRANGE RL_LOG 0 0 | jq .qid)
  477. if [[ ${RL_LOG_STATUS_PREV} != ${RL_LOG_STATUS} ]]; then
  478. err_count=$(( ${err_count} + 1 ))
  479. echo 'Last 10 applied ratelimits (may overlap with previous reports).' > /tmp/ratelimit
  480. echo 'Full ratelimit buckets can be emptied by deleting the ratelimit hash from within mailcow UI (see /debug -> Protocols -> Ratelimit):' >> /tmp/ratelimit
  481. echo >> /tmp/ratelimit
  482. redis-cli --raw -h redis LRANGE RL_LOG 0 10 | jq . >> /tmp/ratelimit
  483. fi
  484. [ ${err_c_cur} -eq ${err_count} ] && [ ! $((${err_count} - 1)) -lt 0 ] && err_count=$((${err_count} - 1)) diff_c=1
  485. [ ${err_c_cur} -ne ${err_count} ] && diff_c=$(( ${err_c_cur} - ${err_count} ))
  486. progress "Ratelimit" ${THRESHOLD} $(( ${THRESHOLD} - ${err_count} )) ${diff_c}
  487. if [[ $? == 10 ]]; then
  488. diff_c=0
  489. sleep 1
  490. else
  491. diff_c=0
  492. sleep $(( ( RANDOM % 60 ) + 20 ))
  493. fi
  494. done
  495. return 1
  496. }
  497. fail2ban_checks() {
  498. err_count=0
  499. diff_c=0
  500. THRESHOLD=${FAIL2BAN_THRESHOLD}
  501. F2B_LOG_STATUS=($(${REDIS_CMDLINE} --raw HKEYS F2B_ACTIVE_BANS))
  502. F2B_RES=
  503. # Reduce error count by 2 after restarting an unhealthy container
  504. trap "[ ${err_count} -gt 1 ] && err_count=$(( ${err_count} - 2 ))" USR1
  505. while [ ${err_count} -lt ${THRESHOLD} ]; do
  506. err_c_cur=${err_count}
  507. F2B_LOG_STATUS_PREV=(${F2B_LOG_STATUS[@]})
  508. F2B_LOG_STATUS=($(${REDIS_CMDLINE} --raw HKEYS F2B_ACTIVE_BANS))
  509. array_diff F2B_RES F2B_LOG_STATUS F2B_LOG_STATUS_PREV
  510. if [[ ! -z "${F2B_RES}" ]]; then
  511. err_count=$(( ${err_count} + 1 ))
  512. echo -n "${F2B_RES[@]}" | tr -cd "[a-fA-F0-9.:/] " | timeout 3s ${REDIS_CMDLINE} -x SET F2B_RES > /dev/null
  513. if [ $? -ne 0 ]; then
  514. ${REDIS_CMDLINE} -x DEL F2B_RES
  515. fi
  516. fi
  517. [ ${err_c_cur} -eq ${err_count} ] && [ ! $((${err_count} - 1)) -lt 0 ] && err_count=$((${err_count} - 1)) diff_c=1
  518. [ ${err_c_cur} -ne ${err_count} ] && diff_c=$(( ${err_c_cur} - ${err_count} ))
  519. progress "Fail2ban" ${THRESHOLD} $(( ${THRESHOLD} - ${err_count} )) ${diff_c}
  520. if [[ $? == 10 ]]; then
  521. diff_c=0
  522. sleep 1
  523. else
  524. diff_c=0
  525. sleep $(( ( RANDOM % 60 ) + 20 ))
  526. fi
  527. done
  528. return 1
  529. }
  530. acme_checks() {
  531. err_count=0
  532. diff_c=0
  533. THRESHOLD=${ACME_THRESHOLD}
  534. ACME_LOG_STATUS=$(redis-cli -h redis GET ACME_FAIL_TIME)
  535. if [[ -z "${ACME_LOG_STATUS}" ]]; then
  536. ${REDIS_CMDLINE} SET ACME_FAIL_TIME 0
  537. ACME_LOG_STATUS=0
  538. fi
  539. # Reduce error count by 2 after restarting an unhealthy container
  540. trap "[ ${err_count} -gt 1 ] && err_count=$(( ${err_count} - 2 ))" USR1
  541. while [ ${err_count} -lt ${THRESHOLD} ]; do
  542. err_c_cur=${err_count}
  543. ACME_LOG_STATUS_PREV=${ACME_LOG_STATUS}
  544. ACME_LC=0
  545. until [[ ! -z ${ACME_LOG_STATUS} ]] || [ ${ACME_LC} -ge 3 ]; do
  546. ACME_LOG_STATUS=$(redis-cli -h redis GET ACME_FAIL_TIME 2> /dev/null)
  547. sleep 3
  548. ACME_LC=$((ACME_LC+1))
  549. done
  550. if [[ ${ACME_LOG_STATUS_PREV} != ${ACME_LOG_STATUS} ]]; then
  551. err_count=$(( ${err_count} + 1 ))
  552. fi
  553. [ ${err_c_cur} -eq ${err_count} ] && [ ! $((${err_count} - 1)) -lt 0 ] && err_count=$((${err_count} - 1)) diff_c=1
  554. [ ${err_c_cur} -ne ${err_count} ] && diff_c=$(( ${err_c_cur} - ${err_count} ))
  555. progress "ACME" ${THRESHOLD} $(( ${THRESHOLD} - ${err_count} )) ${diff_c}
  556. if [[ $? == 10 ]]; then
  557. diff_c=0
  558. sleep 1
  559. else
  560. diff_c=0
  561. sleep $(( ( RANDOM % 60 ) + 20 ))
  562. fi
  563. done
  564. return 1
  565. }
  566. ipv6nat_checks() {
  567. err_count=0
  568. diff_c=0
  569. THRESHOLD=${IPV6NAT_THRESHOLD}
  570. # Reduce error count by 2 after restarting an unhealthy container
  571. trap "[ ${err_count} -gt 1 ] && err_count=$(( ${err_count} - 2 ))" USR1
  572. while [ ${err_count} -lt ${THRESHOLD} ]; do
  573. err_c_cur=${err_count}
  574. CONTAINERS=$(curl --silent --insecure https://dockerapi/containers/json)
  575. 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")
  576. if [[ ! -z ${IPV6NAT_CONTAINER_ID} ]]; then
  577. 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)"
  578. 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)"
  579. DIFFERENCE_START_TIME=$(expr ${LATEST_IPV6NAT} - ${LATEST_STARTED} 2>/dev/null)
  580. if [[ "${DIFFERENCE_START_TIME}" -lt 30 ]]; then
  581. err_count=$(( ${err_count} + 1 ))
  582. fi
  583. fi
  584. [ ${err_c_cur} -eq ${err_count} ] && [ ! $((${err_count} - 1)) -lt 0 ] && err_count=$((${err_count} - 1)) diff_c=1
  585. [ ${err_c_cur} -ne ${err_count} ] && diff_c=$(( ${err_c_cur} - ${err_count} ))
  586. progress "IPv6 NAT" ${THRESHOLD} $(( ${THRESHOLD} - ${err_count} )) ${diff_c}
  587. if [[ $? == 10 ]]; then
  588. diff_c=0
  589. sleep 30
  590. else
  591. diff_c=0
  592. sleep 300
  593. fi
  594. done
  595. return 1
  596. }
  597. rspamd_checks() {
  598. err_count=0
  599. diff_c=0
  600. THRESHOLD=${RSPAMD_THRESHOLD}
  601. # Reduce error count by 2 after restarting an unhealthy container
  602. trap "[ ${err_count} -gt 1 ] && err_count=$(( ${err_count} - 2 ))" USR1
  603. while [ ${err_count} -lt ${THRESHOLD} ]; do
  604. touch /tmp/rspamd-mailcow; echo "$(tail -50 /tmp/rspamd-mailcow)" > /tmp/rspamd-mailcow
  605. host_ip=$(get_container_ip rspamd-mailcow)
  606. err_c_cur=${err_count}
  607. SCORE=$(echo 'To: null@localhost
  608. From: watchdog@localhost
  609. Empty
  610. ' | usr/bin/curl -s --data-binary @- --unix-socket /var/lib/rspamd/rspamd.sock http://rspamd/scan | jq -rc .default.required_score)
  611. if [[ ${SCORE} != "9999" ]]; then
  612. echo "Rspamd settings check failed" 2>> /tmp/rspamd-mailcow 1>&2
  613. err_count=$(( ${err_count} + 1))
  614. else
  615. echo "Rspamd settings check succeeded" 2>> /tmp/rspamd-mailcow 1>&2
  616. fi
  617. [ ${err_c_cur} -eq ${err_count} ] && [ ! $((${err_count} - 1)) -lt 0 ] && err_count=$((${err_count} - 1)) diff_c=1
  618. [ ${err_c_cur} -ne ${err_count} ] && diff_c=$(( ${err_c_cur} - ${err_count} ))
  619. progress "Rspamd" ${THRESHOLD} $(( ${THRESHOLD} - ${err_count} )) ${diff_c}
  620. if [[ $? == 10 ]]; then
  621. diff_c=0
  622. sleep 1
  623. else
  624. diff_c=0
  625. sleep $(( ( RANDOM % 60 ) + 20 ))
  626. fi
  627. done
  628. return 1
  629. }
  630. olefy_checks() {
  631. err_count=0
  632. diff_c=0
  633. THRESHOLD=${OLEFY_THRESHOLD}
  634. # Reduce error count by 2 after restarting an unhealthy container
  635. trap "[ ${err_count} -gt 1 ] && err_count=$(( ${err_count} - 2 ))" USR1
  636. while [ ${err_count} -lt ${THRESHOLD} ]; do
  637. touch /tmp/olefy-mailcow; echo "$(tail -50 /tmp/olefy-mailcow)" > /tmp/olefy-mailcow
  638. host_ip=$(get_container_ip olefy-mailcow)
  639. err_c_cur=${err_count}
  640. /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} + $? ))
  641. [ ${err_c_cur} -eq ${err_count} ] && [ ! $((${err_count} - 1)) -lt 0 ] && err_count=$((${err_count} - 1)) diff_c=1
  642. [ ${err_c_cur} -ne ${err_count} ] && diff_c=$(( ${err_c_cur} - ${err_count} ))
  643. progress "Olefy" ${THRESHOLD} $(( ${THRESHOLD} - ${err_count} )) ${diff_c}
  644. if [[ $? == 10 ]]; then
  645. diff_c=0
  646. sleep 1
  647. else
  648. diff_c=0
  649. sleep $(( ( RANDOM % 60 ) + 20 ))
  650. fi
  651. done
  652. return 1
  653. }
  654. # Notify about start
  655. if [[ ! -z ${WATCHDOG_NOTIFY_EMAIL} ]]; then
  656. mail_error "watchdog-mailcow" "Watchdog started monitoring mailcow."
  657. fi
  658. # Create watchdog agents
  659. (
  660. while true; do
  661. if ! nginx_checks; then
  662. log_msg "Nginx hit error limit"
  663. echo nginx-mailcow > /tmp/com_pipe
  664. fi
  665. done
  666. ) &
  667. PID=$!
  668. echo "Spawned nginx_checks with PID ${PID}"
  669. BACKGROUND_TASKS+=(${PID})
  670. if [[ ${WATCHDOG_EXTERNAL_CHECKS} =~ ^([yY][eE][sS]|[yY])+$ ]]; then
  671. (
  672. while true; do
  673. if ! external_checks; then
  674. log_msg "External checks hit error limit"
  675. echo external_checks > /tmp/com_pipe
  676. fi
  677. done
  678. ) &
  679. PID=$!
  680. echo "Spawned external_checks with PID ${PID}"
  681. BACKGROUND_TASKS+=(${PID})
  682. fi
  683. if [[ ${WATCHDOG_MYSQL_REPLICATION_CHECKS} =~ ^([yY][eE][sS]|[yY])+$ ]]; then
  684. (
  685. while true; do
  686. if ! mysql_repl_checks; then
  687. log_msg "MySQL replication check hit error limit"
  688. echo mysql_repl_checks > /tmp/com_pipe
  689. fi
  690. done
  691. ) &
  692. PID=$!
  693. echo "Spawned mysql_repl_checks with PID ${PID}"
  694. BACKGROUND_TASKS+=(${PID})
  695. fi
  696. (
  697. while true; do
  698. if ! mysql_checks; then
  699. log_msg "MySQL hit error limit"
  700. echo mysql-mailcow > /tmp/com_pipe
  701. fi
  702. done
  703. ) &
  704. PID=$!
  705. echo "Spawned mysql_checks with PID ${PID}"
  706. BACKGROUND_TASKS+=(${PID})
  707. (
  708. while true; do
  709. if ! redis_checks; then
  710. log_msg "Local Redis hit error limit"
  711. echo redis-mailcow > /tmp/com_pipe
  712. fi
  713. done
  714. ) &
  715. PID=$!
  716. echo "Spawned redis_checks with PID ${PID}"
  717. BACKGROUND_TASKS+=(${PID})
  718. (
  719. while true; do
  720. if ! phpfpm_checks; then
  721. log_msg "PHP-FPM hit error limit"
  722. echo php-fpm-mailcow > /tmp/com_pipe
  723. fi
  724. done
  725. ) &
  726. PID=$!
  727. echo "Spawned phpfpm_checks with PID ${PID}"
  728. BACKGROUND_TASKS+=(${PID})
  729. if [[ "${SKIP_SOGO}" =~ ^([nN][oO]|[nN])+$ ]]; then
  730. (
  731. while true; do
  732. if ! sogo_checks; then
  733. log_msg "SOGo hit error limit"
  734. echo sogo-mailcow > /tmp/com_pipe
  735. fi
  736. done
  737. ) &
  738. PID=$!
  739. echo "Spawned sogo_checks with PID ${PID}"
  740. BACKGROUND_TASKS+=(${PID})
  741. fi
  742. if [ ${CHECK_UNBOUND} -eq 1 ]; then
  743. (
  744. while true; do
  745. if ! unbound_checks; then
  746. log_msg "Unbound hit error limit"
  747. echo unbound-mailcow > /tmp/com_pipe
  748. fi
  749. done
  750. ) &
  751. PID=$!
  752. echo "Spawned unbound_checks with PID ${PID}"
  753. BACKGROUND_TASKS+=(${PID})
  754. fi
  755. if [[ "${SKIP_CLAMD}" =~ ^([nN][oO]|[nN])+$ ]]; then
  756. (
  757. while true; do
  758. if ! clamd_checks; then
  759. log_msg "Clamd hit error limit"
  760. echo clamd-mailcow > /tmp/com_pipe
  761. fi
  762. done
  763. ) &
  764. PID=$!
  765. echo "Spawned clamd_checks with PID ${PID}"
  766. BACKGROUND_TASKS+=(${PID})
  767. fi
  768. (
  769. while true; do
  770. if ! postfix_checks; then
  771. log_msg "Postfix hit error limit"
  772. echo postfix-mailcow > /tmp/com_pipe
  773. fi
  774. done
  775. ) &
  776. PID=$!
  777. echo "Spawned postfix_checks with PID ${PID}"
  778. BACKGROUND_TASKS+=(${PID})
  779. (
  780. while true; do
  781. if ! dovecot_checks; then
  782. log_msg "Dovecot hit error limit"
  783. echo dovecot-mailcow > /tmp/com_pipe
  784. fi
  785. done
  786. ) &
  787. PID=$!
  788. echo "Spawned dovecot_checks with PID ${PID}"
  789. BACKGROUND_TASKS+=(${PID})
  790. (
  791. while true; do
  792. if ! dovecot_repl_checks; then
  793. log_msg "Dovecot hit error limit"
  794. echo dovecot_repl_checks > /tmp/com_pipe
  795. fi
  796. done
  797. ) &
  798. PID=$!
  799. echo "Spawned dovecot_repl_checks with PID ${PID}"
  800. BACKGROUND_TASKS+=(${PID})
  801. (
  802. while true; do
  803. if ! rspamd_checks; then
  804. log_msg "Rspamd hit error limit"
  805. echo rspamd-mailcow > /tmp/com_pipe
  806. fi
  807. done
  808. ) &
  809. PID=$!
  810. echo "Spawned rspamd_checks with PID ${PID}"
  811. BACKGROUND_TASKS+=(${PID})
  812. (
  813. while true; do
  814. if ! ratelimit_checks; then
  815. log_msg "Ratelimit hit error limit"
  816. echo ratelimit > /tmp/com_pipe
  817. fi
  818. done
  819. ) &
  820. PID=$!
  821. echo "Spawned ratelimit_checks with PID ${PID}"
  822. BACKGROUND_TASKS+=(${PID})
  823. (
  824. while true; do
  825. if ! fail2ban_checks; then
  826. log_msg "Fail2ban hit error limit"
  827. echo fail2ban > /tmp/com_pipe
  828. fi
  829. done
  830. ) &
  831. PID=$!
  832. echo "Spawned fail2ban_checks with PID ${PID}"
  833. BACKGROUND_TASKS+=(${PID})
  834. (
  835. while true; do
  836. if ! olefy_checks; then
  837. log_msg "Olefy hit error limit"
  838. echo olefy-mailcow > /tmp/com_pipe
  839. fi
  840. done
  841. ) &
  842. PID=$!
  843. echo "Spawned olefy_checks with PID ${PID}"
  844. BACKGROUND_TASKS+=(${PID})
  845. (
  846. while true; do
  847. if ! acme_checks; then
  848. log_msg "ACME client hit error limit"
  849. echo acme-mailcow > /tmp/com_pipe
  850. fi
  851. done
  852. ) &
  853. PID=$!
  854. echo "Spawned acme_checks with PID ${PID}"
  855. BACKGROUND_TASKS+=(${PID})
  856. (
  857. while true; do
  858. if ! ipv6nat_checks; then
  859. log_msg "IPv6 NAT warning: ipv6nat-mailcow container was not started at least 30s after siblings (not an error)"
  860. echo ipv6nat-mailcow > /tmp/com_pipe
  861. fi
  862. done
  863. ) &
  864. PID=$!
  865. echo "Spawned ipv6nat_checks with PID ${PID}"
  866. BACKGROUND_TASKS+=(${PID})
  867. # Monitor watchdog agents, stop script when agents fails and wait for respawn by Docker (restart:always:n)
  868. (
  869. while true; do
  870. for bg_task in ${BACKGROUND_TASKS[*]}; do
  871. if ! kill -0 ${bg_task} 1>&2; then
  872. log_msg "Worker ${bg_task} died, stopping watchdog and waiting for respawn..."
  873. kill -TERM 1
  874. fi
  875. sleep 10
  876. done
  877. done
  878. ) &
  879. # Monitor dockerapi
  880. (
  881. while true; do
  882. while nc -z dockerapi 443; do
  883. sleep 3
  884. done
  885. log_msg "Cannot find dockerapi-mailcow, waiting to recover..."
  886. kill -STOP ${BACKGROUND_TASKS[*]}
  887. until nc -z dockerapi 443; do
  888. sleep 3
  889. done
  890. kill -CONT ${BACKGROUND_TASKS[*]}
  891. kill -USR1 ${BACKGROUND_TASKS[*]}
  892. done
  893. ) &
  894. # Actions when threshold limit is reached
  895. while true; do
  896. CONTAINER_ID=
  897. HAS_INITDB=
  898. read com_pipe_answer </tmp/com_pipe
  899. if [ -s "/tmp/${com_pipe_answer}" ]; then
  900. cat "/tmp/${com_pipe_answer}"
  901. fi
  902. if [[ ${com_pipe_answer} == "ratelimit" ]]; then
  903. log_msg "At least one ratelimit was applied"
  904. [[ ! -z ${WATCHDOG_NOTIFY_EMAIL} ]] && mail_error "${com_pipe_answer}"
  905. elif [[ ${com_pipe_answer} == "external_checks" ]]; then
  906. log_msg "Your mailcow is an open relay!"
  907. [[ ! -z ${WATCHDOG_NOTIFY_EMAIL} ]] && mail_error "${com_pipe_answer}" "Please stop mailcow now and check your network configuration!"
  908. elif [[ ${com_pipe_answer} == "mysql_repl_checks" ]]; then
  909. log_msg "MySQL replication is not working properly"
  910. [[ ! -z ${WATCHDOG_NOTIFY_EMAIL} ]] && mail_error "${com_pipe_answer}"
  911. elif [[ ${com_pipe_answer} == "dovecot_repl_checks" ]]; then
  912. log_msg "Dovecot replication is not working properly" "Please check doveadm replicator status"
  913. [[ ! -z ${WATCHDOG_NOTIFY_EMAIL} ]] && mail_error "${com_pipe_answer}"
  914. elif [[ ${com_pipe_answer} == "acme-mailcow" ]]; then
  915. log_msg "acme-mailcow did not complete successfully"
  916. [[ ! -z ${WATCHDOG_NOTIFY_EMAIL} ]] && mail_error "${com_pipe_answer}" "Please check acme-mailcow for further information."
  917. elif [[ ${com_pipe_answer} == "fail2ban" ]]; then
  918. F2B_RES=($(timeout 4s ${REDIS_CMDLINE} --raw GET F2B_RES 2> /dev/null))
  919. if [[ ! -z "${F2B_RES}" ]]; then
  920. ${REDIS_CMDLINE} DEL F2B_RES > /dev/null
  921. host=
  922. for host in "${F2B_RES[@]}"; do
  923. log_msg "Banned ${host}"
  924. rm /tmp/fail2ban 2> /dev/null
  925. timeout 2s whois "${host}" > /tmp/fail2ban
  926. [[ ! -z ${WATCHDOG_NOTIFY_EMAIL} ]] && [[ ${WATCHDOG_NOTIFY_BAN} =~ ^([yY][eE][sS]|[yY])+$ ]] && mail_error "${com_pipe_answer}" "IP ban: ${host}"
  927. done
  928. fi
  929. elif [[ ${com_pipe_answer} =~ .+-mailcow ]]; then
  930. kill -STOP ${BACKGROUND_TASKS[*]}
  931. sleep 10
  932. 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")
  933. if [[ ! -z ${CONTAINER_ID} ]]; then
  934. if [[ "${com_pipe_answer}" == "php-fpm-mailcow" ]]; then
  935. 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)
  936. fi
  937. S_RUNNING=$(($(date +%s) - $(curl --silent --insecure https://dockerapi/containers/${CONTAINER_ID}/json | jq .State.StartedAt | xargs -n1 date +%s -d)))
  938. if [ ${S_RUNNING} -lt 360 ]; then
  939. log_msg "Container is running for less than 360 seconds, skipping action..."
  940. elif [[ ! -z ${HAS_INITDB} ]]; then
  941. log_msg "Database is being initialized by php-fpm-mailcow, not restarting but delaying checks for a minute..."
  942. sleep 60
  943. else
  944. log_msg "Sending restart command to ${CONTAINER_ID}..."
  945. curl --silent --insecure -XPOST https://dockerapi/containers/${CONTAINER_ID}/restart
  946. if [[ ${com_pipe_answer} != "ipv6nat-mailcow" ]]; then
  947. [[ ! -z ${WATCHDOG_NOTIFY_EMAIL} ]] && mail_error "${com_pipe_answer}"
  948. fi
  949. log_msg "Wait for restarted container to settle and continue watching..."
  950. sleep 35
  951. fi
  952. fi
  953. kill -CONT ${BACKGROUND_TASKS[*]}
  954. sleep 1
  955. kill -USR1 ${BACKGROUND_TASKS[*]}
  956. fi
  957. done