watchdog.sh 41 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126
  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. THROTTLE=
  78. [[ -z ${1} ]] && return 1
  79. # If exists, body will be the content of "/tmp/${1}", even if ${2} is set
  80. [[ -z ${2} ]] && BODY="Service was restarted on $(date), please check your mailcow installation." || BODY="$(date) - ${2}"
  81. # If exists, mail will be throttled by argument in seconds
  82. [[ ! -z ${3} ]] && THROTTLE=${3}
  83. if [[ ! -z ${THROTTLE} ]]; then
  84. TTL_LEFT="$(${REDIS_CMDLINE} TTL THROTTLE_${1} 2> /dev/null)"
  85. if [[ "${TTL_LEFT}" == "-2" ]]; then
  86. # Delay key not found, setting a delay key now
  87. ${REDIS_CMDLINE} SET THROTTLE_${1} 1 EX ${THROTTLE}
  88. else
  89. log_msg "Not sending notification email now, blocked for ${TTL_LEFT} seconds..."
  90. return 1
  91. fi
  92. fi
  93. WATCHDOG_NOTIFY_EMAIL=$(echo "${WATCHDOG_NOTIFY_EMAIL}" | sed 's/"//;s|"$||')
  94. # Some exceptions for subject and body formats
  95. if [[ ${1} == "fail2ban" ]]; then
  96. SUBJECT="${BODY}"
  97. BODY="Please see netfilter-mailcow for more details and triggered rules."
  98. else
  99. SUBJECT="Watchdog ALERT: ${1}"
  100. fi
  101. IFS=',' read -r -a MAIL_RCPTS <<< "${WATCHDOG_NOTIFY_EMAIL}"
  102. for rcpt in "${MAIL_RCPTS[@]}"; do
  103. RCPT_DOMAIN=
  104. #RCPT_MX=
  105. RCPT_DOMAIN=$(echo ${rcpt} | awk -F @ {'print $NF'})
  106. # Latest smtp-cli looks up mx via dns
  107. #RCPT_MX=$(dig +short ${RCPT_DOMAIN} mx | sort -n | awk '{print $2; exit}')
  108. #if [[ -z ${RCPT_MX} ]]; then
  109. # log_msg "Cannot determine MX for ${rcpt}, skipping email notification..."
  110. # return 1
  111. #fi
  112. [ -f "/tmp/${1}" ] && BODY="/tmp/${1}"
  113. timeout 10s ./smtp-cli --missing-modules-ok \
  114. --charset=UTF-8 \
  115. --subject="${SUBJECT}" \
  116. --body-plain="${BODY}" \
  117. --add-header="X-Priority: 1" \
  118. --to=${rcpt} \
  119. --from="watchdog@${MAILCOW_HOSTNAME}" \
  120. --hello-host=${MAILCOW_HOSTNAME} \
  121. --ipv4
  122. #--server="${RCPT_MX}"
  123. log_msg "Sent notification email to ${rcpt}"
  124. done
  125. }
  126. get_container_ip() {
  127. # ${1} is container
  128. CONTAINER_ID=()
  129. CONTAINER_IPS=()
  130. CONTAINER_IP=
  131. LOOP_C=1
  132. until [[ ${CONTAINER_IP} =~ ^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}$ ]] || [[ ${LOOP_C} -gt 5 ]]; do
  133. if [ ${IP_BY_DOCKER_API} -eq 0 ]; then
  134. CONTAINER_IP=$(dig a "${1}" +short)
  135. else
  136. sleep 0.5
  137. # get long container id for exact match
  138. CONTAINER_ID=($(curl --silent --insecure https://dockerapi/containers/json | jq -r ".[] | {name: .Config.Labels[\"com.docker.compose.service\"], project: .Config.Labels[\"com.docker.compose.project\"], id: .Id}" | jq -rc "select( .name | tostring == \"${1}\") | select( .project | tostring | contains(\"${COMPOSE_PROJECT_NAME,,}\")) | .id"))
  139. # returned id can have multiple elements (if scaled), shuffle for random test
  140. CONTAINER_ID=($(printf "%s\n" "${CONTAINER_ID[@]}" | shuf))
  141. if [[ ! -z ${CONTAINER_ID} ]]; then
  142. for matched_container in "${CONTAINER_ID[@]}"; do
  143. CONTAINER_IPS=($(curl --silent --insecure https://dockerapi/containers/${matched_container}/json | jq -r '.NetworkSettings.Networks[].IPAddress'))
  144. for ip_match in "${CONTAINER_IPS[@]}"; do
  145. # grep will do nothing if one of these vars is empty
  146. [[ -z ${ip_match} ]] && continue
  147. [[ -z ${IPV4_NETWORK} ]] && continue
  148. # only return ips that are part of our network
  149. if ! grep -q ${IPV4_NETWORK} <(echo ${ip_match}); then
  150. continue
  151. else
  152. CONTAINER_IP=${ip_match}
  153. break
  154. fi
  155. done
  156. [[ ! -z ${CONTAINER_IP} ]] && break
  157. done
  158. fi
  159. fi
  160. LOOP_C=$((LOOP_C + 1))
  161. done
  162. [[ ${LOOP_C} -gt 5 ]] && echo 240.0.0.0 || echo ${CONTAINER_IP}
  163. }
  164. # One-time check
  165. if grep -qi "$(echo ${IPV6_NETWORK} | cut -d: -f1-3)" <<< "$(ip a s)"; then
  166. if [[ -z "$(get_ipv6)" ]]; then
  167. 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."
  168. fi
  169. fi
  170. external_checks() {
  171. err_count=0
  172. diff_c=0
  173. THRESHOLD=${EXTERNAL_CHECKS_THRESHOLD}
  174. # Reduce error count by 2 after restarting an unhealthy container
  175. GUID=$(mysql -u${DBUSER} -p${DBPASS} ${DBNAME} -e "SELECT version FROM versions WHERE application = 'GUID'" -BN)
  176. trap "[ ${err_count} -gt 1 ] && err_count=$(( ${err_count} - 2 ))" USR1
  177. while [ ${err_count} -lt ${THRESHOLD} ]; do
  178. err_c_cur=${err_count}
  179. CHECK_REPONSE="$(curl --connect-timeout 3 -m 10 -4 -s https://checks.mailcow.email -X POST -dguid=${GUID} 2> /dev/null)"
  180. if [[ ! -z "${CHECK_REPONSE}" ]] && [[ "$(echo ${CHECK_REPONSE} | jq -r .response)" == "critical" ]]; then
  181. echo ${CHECK_REPONSE} | jq -r .out > /tmp/external_checks
  182. err_count=$(( ${err_count} + 1 ))
  183. fi
  184. CHECK_REPONSE6="$(curl --connect-timeout 3 -m 10 -6 -s https://checks.mailcow.email -X POST -dguid=${GUID} 2> /dev/null)"
  185. if [[ ! -z "${CHECK_REPONSE6}" ]] && [[ "$(echo ${CHECK_REPONSE6} | jq -r .response)" == "critical" ]]; then
  186. echo ${CHECK_REPONSE} | jq -r .out > /tmp/external_checks
  187. err_count=$(( ${err_count} + 1 ))
  188. fi
  189. [ ${err_c_cur} -eq ${err_count} ] && [ ! $((${err_count} - 1)) -lt 0 ] && err_count=$((${err_count} - 1)) diff_c=1
  190. [ ${err_c_cur} -ne ${err_count} ] && diff_c=$(( ${err_c_cur} - ${err_count} ))
  191. progress "External checks" ${THRESHOLD} $(( ${THRESHOLD} - ${err_count} )) ${diff_c}
  192. if [[ $? == 10 ]]; then
  193. diff_c=0
  194. sleep 60
  195. else
  196. diff_c=0
  197. sleep $(( ( RANDOM % 20 ) + 120 ))
  198. fi
  199. done
  200. return 1
  201. }
  202. nginx_checks() {
  203. err_count=0
  204. diff_c=0
  205. THRESHOLD=${NGINX_THRESHOLD}
  206. # Reduce error count by 2 after restarting an unhealthy container
  207. trap "[ ${err_count} -gt 1 ] && err_count=$(( ${err_count} - 2 ))" USR1
  208. while [ ${err_count} -lt ${THRESHOLD} ]; do
  209. touch /tmp/nginx-mailcow; echo "$(tail -50 /tmp/nginx-mailcow)" > /tmp/nginx-mailcow
  210. host_ip=$(get_container_ip nginx-mailcow)
  211. err_c_cur=${err_count}
  212. /usr/lib/nagios/plugins/check_http -4 -H ${host_ip} -u / -p 8081 2>> /tmp/nginx-mailcow 1>&2; err_count=$(( ${err_count} + $? ))
  213. [ ${err_c_cur} -eq ${err_count} ] && [ ! $((${err_count} - 1)) -lt 0 ] && err_count=$((${err_count} - 1)) diff_c=1
  214. [ ${err_c_cur} -ne ${err_count} ] && diff_c=$(( ${err_c_cur} - ${err_count} ))
  215. progress "Nginx" ${THRESHOLD} $(( ${THRESHOLD} - ${err_count} )) ${diff_c}
  216. if [[ $? == 10 ]]; then
  217. diff_c=0
  218. sleep 1
  219. else
  220. diff_c=0
  221. sleep $(( ( RANDOM % 60 ) + 20 ))
  222. fi
  223. done
  224. return 1
  225. }
  226. unbound_checks() {
  227. err_count=0
  228. diff_c=0
  229. THRESHOLD=${UNBOUND_THRESHOLD}
  230. # Reduce error count by 2 after restarting an unhealthy container
  231. trap "[ ${err_count} -gt 1 ] && err_count=$(( ${err_count} - 2 ))" USR1
  232. while [ ${err_count} -lt ${THRESHOLD} ]; do
  233. touch /tmp/unbound-mailcow; echo "$(tail -50 /tmp/unbound-mailcow)" > /tmp/unbound-mailcow
  234. host_ip=$(get_container_ip unbound-mailcow)
  235. err_c_cur=${err_count}
  236. /usr/lib/nagios/plugins/check_dns -s ${host_ip} -H stackoverflow.com 2>> /tmp/unbound-mailcow 1>&2; err_count=$(( ${err_count} + $? ))
  237. DNSSEC=$(dig com +dnssec | egrep 'flags:.+ad')
  238. if [[ -z ${DNSSEC} ]]; then
  239. echo "DNSSEC failure" 2>> /tmp/unbound-mailcow 1>&2
  240. err_count=$(( ${err_count} + 1))
  241. else
  242. echo "DNSSEC check succeeded" 2>> /tmp/unbound-mailcow 1>&2
  243. fi
  244. [ ${err_c_cur} -eq ${err_count} ] && [ ! $((${err_count} - 1)) -lt 0 ] && err_count=$((${err_count} - 1)) diff_c=1
  245. [ ${err_c_cur} -ne ${err_count} ] && diff_c=$(( ${err_c_cur} - ${err_count} ))
  246. progress "Unbound" ${THRESHOLD} $(( ${THRESHOLD} - ${err_count} )) ${diff_c}
  247. if [[ $? == 10 ]]; then
  248. diff_c=0
  249. sleep 1
  250. else
  251. diff_c=0
  252. sleep $(( ( RANDOM % 60 ) + 20 ))
  253. fi
  254. done
  255. return 1
  256. }
  257. redis_checks() {
  258. # A check for the local redis container
  259. err_count=0
  260. diff_c=0
  261. THRESHOLD=${REDIS_THRESHOLD}
  262. # Reduce error count by 2 after restarting an unhealthy container
  263. trap "[ ${err_count} -gt 1 ] && err_count=$(( ${err_count} - 2 ))" USR1
  264. while [ ${err_count} -lt ${THRESHOLD} ]; do
  265. touch /tmp/redis-mailcow; echo "$(tail -50 /tmp/redis-mailcow)" > /tmp/redis-mailcow
  266. host_ip=$(get_container_ip redis-mailcow)
  267. err_c_cur=${err_count}
  268. /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} + $? ))
  269. [ ${err_c_cur} -eq ${err_count} ] && [ ! $((${err_count} - 1)) -lt 0 ] && err_count=$((${err_count} - 1)) diff_c=1
  270. [ ${err_c_cur} -ne ${err_count} ] && diff_c=$(( ${err_c_cur} - ${err_count} ))
  271. progress "Redis" ${THRESHOLD} $(( ${THRESHOLD} - ${err_count} )) ${diff_c}
  272. if [[ $? == 10 ]]; then
  273. diff_c=0
  274. sleep 1
  275. else
  276. diff_c=0
  277. sleep $(( ( RANDOM % 60 ) + 20 ))
  278. fi
  279. done
  280. return 1
  281. }
  282. mysql_checks() {
  283. err_count=0
  284. diff_c=0
  285. THRESHOLD=${MYSQL_THRESHOLD}
  286. # Reduce error count by 2 after restarting an unhealthy container
  287. trap "[ ${err_count} -gt 1 ] && err_count=$(( ${err_count} - 2 ))" USR1
  288. while [ ${err_count} -lt ${THRESHOLD} ]; do
  289. touch /tmp/mysql-mailcow; echo "$(tail -50 /tmp/mysql-mailcow)" > /tmp/mysql-mailcow
  290. err_c_cur=${err_count}
  291. /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} + $? ))
  292. /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} + $? ))
  293. [ ${err_c_cur} -eq ${err_count} ] && [ ! $((${err_count} - 1)) -lt 0 ] && err_count=$((${err_count} - 1)) diff_c=1
  294. [ ${err_c_cur} -ne ${err_count} ] && diff_c=$(( ${err_c_cur} - ${err_count} ))
  295. progress "MySQL/MariaDB" ${THRESHOLD} $(( ${THRESHOLD} - ${err_count} )) ${diff_c}
  296. if [[ $? == 10 ]]; then
  297. diff_c=0
  298. sleep 1
  299. else
  300. diff_c=0
  301. sleep $(( ( RANDOM % 60 ) + 20 ))
  302. fi
  303. done
  304. return 1
  305. }
  306. mysql_repl_checks() {
  307. err_count=0
  308. diff_c=0
  309. THRESHOLD=${MYSQL_REPLICATION_THRESHOLD}
  310. # Reduce error count by 2 after restarting an unhealthy container
  311. trap "[ ${err_count} -gt 1 ] && err_count=$(( ${err_count} - 2 ))" USR1
  312. while [ ${err_count} -lt ${THRESHOLD} ]; do
  313. touch /tmp/mysql_repl_checks; echo "$(tail -50 /tmp/mysql_repl_checks)" > /tmp/mysql_repl_checks
  314. err_c_cur=${err_count}
  315. /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} + $? ))
  316. [ ${err_c_cur} -eq ${err_count} ] && [ ! $((${err_count} - 1)) -lt 0 ] && err_count=$((${err_count} - 1)) diff_c=1
  317. [ ${err_c_cur} -ne ${err_count} ] && diff_c=$(( ${err_c_cur} - ${err_count} ))
  318. progress "MySQL/MariaDB replication" ${THRESHOLD} $(( ${THRESHOLD} - ${err_count} )) ${diff_c}
  319. if [[ $? == 10 ]]; then
  320. diff_c=0
  321. sleep 60
  322. else
  323. diff_c=0
  324. sleep $(( ( RANDOM % 60 ) + 20 ))
  325. fi
  326. done
  327. return 1
  328. }
  329. sogo_checks() {
  330. err_count=0
  331. diff_c=0
  332. THRESHOLD=${SOGO_THRESHOLD}
  333. # Reduce error count by 2 after restarting an unhealthy container
  334. trap "[ ${err_count} -gt 1 ] && err_count=$(( ${err_count} - 2 ))" USR1
  335. while [ ${err_count} -lt ${THRESHOLD} ]; do
  336. touch /tmp/sogo-mailcow; echo "$(tail -50 /tmp/sogo-mailcow)" > /tmp/sogo-mailcow
  337. host_ip=$(get_container_ip sogo-mailcow)
  338. err_c_cur=${err_count}
  339. /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} + $? ))
  340. [ ${err_c_cur} -eq ${err_count} ] && [ ! $((${err_count} - 1)) -lt 0 ] && err_count=$((${err_count} - 1)) diff_c=1
  341. [ ${err_c_cur} -ne ${err_count} ] && diff_c=$(( ${err_c_cur} - ${err_count} ))
  342. progress "SOGo" ${THRESHOLD} $(( ${THRESHOLD} - ${err_count} )) ${diff_c}
  343. if [[ $? == 10 ]]; then
  344. diff_c=0
  345. sleep 1
  346. else
  347. diff_c=0
  348. sleep $(( ( RANDOM % 60 ) + 20 ))
  349. fi
  350. done
  351. return 1
  352. }
  353. postfix_checks() {
  354. err_count=0
  355. diff_c=0
  356. THRESHOLD=${POSTFIX_THRESHOLD}
  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. touch /tmp/postfix-mailcow; echo "$(tail -50 /tmp/postfix-mailcow)" > /tmp/postfix-mailcow
  361. host_ip=$(get_container_ip postfix-mailcow)
  362. err_c_cur=${err_count}
  363. /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} + $? ))
  364. /usr/lib/nagios/plugins/check_smtp -4 -H ${host_ip} -p 589 -S 2>> /tmp/postfix-mailcow 1>&2; err_count=$(( ${err_count} + $? ))
  365. [ ${err_c_cur} -eq ${err_count} ] && [ ! $((${err_count} - 1)) -lt 0 ] && err_count=$((${err_count} - 1)) diff_c=1
  366. [ ${err_c_cur} -ne ${err_count} ] && diff_c=$(( ${err_c_cur} - ${err_count} ))
  367. progress "Postfix" ${THRESHOLD} $(( ${THRESHOLD} - ${err_count} )) ${diff_c}
  368. if [[ $? == 10 ]]; then
  369. diff_c=0
  370. sleep 1
  371. else
  372. diff_c=0
  373. sleep $(( ( RANDOM % 60 ) + 20 ))
  374. fi
  375. done
  376. return 1
  377. }
  378. clamd_checks() {
  379. err_count=0
  380. diff_c=0
  381. THRESHOLD=${CLAMD_THRESHOLD}
  382. # Reduce error count by 2 after restarting an unhealthy container
  383. trap "[ ${err_count} -gt 1 ] && err_count=$(( ${err_count} - 2 ))" USR1
  384. while [ ${err_count} -lt ${THRESHOLD} ]; do
  385. touch /tmp/clamd-mailcow; echo "$(tail -50 /tmp/clamd-mailcow)" > /tmp/clamd-mailcow
  386. host_ip=$(get_container_ip clamd-mailcow)
  387. err_c_cur=${err_count}
  388. /usr/lib/nagios/plugins/check_clamd -4 -H ${host_ip} 2>> /tmp/clamd-mailcow 1>&2; err_count=$(( ${err_count} + $? ))
  389. [ ${err_c_cur} -eq ${err_count} ] && [ ! $((${err_count} - 1)) -lt 0 ] && err_count=$((${err_count} - 1)) diff_c=1
  390. [ ${err_c_cur} -ne ${err_count} ] && diff_c=$(( ${err_c_cur} - ${err_count} ))
  391. progress "Clamd" ${THRESHOLD} $(( ${THRESHOLD} - ${err_count} )) ${diff_c}
  392. if [[ $? == 10 ]]; then
  393. diff_c=0
  394. sleep 1
  395. else
  396. diff_c=0
  397. sleep $(( ( RANDOM % 120 ) + 20 ))
  398. fi
  399. done
  400. return 1
  401. }
  402. dovecot_checks() {
  403. err_count=0
  404. diff_c=0
  405. THRESHOLD=${DOVECOT_THRESHOLD}
  406. # Reduce error count by 2 after restarting an unhealthy container
  407. trap "[ ${err_count} -gt 1 ] && err_count=$(( ${err_count} - 2 ))" USR1
  408. while [ ${err_count} -lt ${THRESHOLD} ]; do
  409. touch /tmp/dovecot-mailcow; echo "$(tail -50 /tmp/dovecot-mailcow)" > /tmp/dovecot-mailcow
  410. host_ip=$(get_container_ip dovecot-mailcow)
  411. err_c_cur=${err_count}
  412. /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} + $? ))
  413. /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} + $? ))
  414. /usr/lib/nagios/plugins/check_imap -4 -H ${host_ip} -p 143 -e "OK " 2>> /tmp/dovecot-mailcow 1>&2; err_count=$(( ${err_count} + $? ))
  415. /usr/lib/nagios/plugins/check_tcp -4 -H ${host_ip} -p 10001 -e "VERSION" 2>> /tmp/dovecot-mailcow 1>&2; err_count=$(( ${err_count} + $? ))
  416. /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} + $? ))
  417. [ ${err_c_cur} -eq ${err_count} ] && [ ! $((${err_count} - 1)) -lt 0 ] && err_count=$((${err_count} - 1)) diff_c=1
  418. [ ${err_c_cur} -ne ${err_count} ] && diff_c=$(( ${err_c_cur} - ${err_count} ))
  419. progress "Dovecot" ${THRESHOLD} $(( ${THRESHOLD} - ${err_count} )) ${diff_c}
  420. if [[ $? == 10 ]]; then
  421. diff_c=0
  422. sleep 1
  423. else
  424. diff_c=0
  425. sleep $(( ( RANDOM % 60 ) + 20 ))
  426. fi
  427. done
  428. return 1
  429. }
  430. dovecot_repl_checks() {
  431. err_count=0
  432. diff_c=0
  433. THRESHOLD=${DOVECOT_REPL_THRESHOLD}
  434. D_REPL_STATUS=$(redis-cli -h redis -r GET DOVECOT_REPL_HEALTH)
  435. # Reduce error count by 2 after restarting an unhealthy container
  436. trap "[ ${err_count} -gt 1 ] && err_count=$(( ${err_count} - 2 ))" USR1
  437. while [ ${err_count} -lt ${THRESHOLD} ]; do
  438. err_c_cur=${err_count}
  439. D_REPL_STATUS=$(redis-cli --raw -h redis GET DOVECOT_REPL_HEALTH)
  440. if [[ "${D_REPL_STATUS}" != "1" ]]; then
  441. err_count=$(( ${err_count} + 1 ))
  442. fi
  443. [ ${err_c_cur} -eq ${err_count} ] && [ ! $((${err_count} - 1)) -lt 0 ] && err_count=$((${err_count} - 1)) diff_c=1
  444. [ ${err_c_cur} -ne ${err_count} ] && diff_c=$(( ${err_c_cur} - ${err_count} ))
  445. progress "Dovecot replication" ${THRESHOLD} $(( ${THRESHOLD} - ${err_count} )) ${diff_c}
  446. if [[ $? == 10 ]]; then
  447. diff_c=0
  448. sleep 60
  449. else
  450. diff_c=0
  451. sleep $(( ( RANDOM % 60 ) + 20 ))
  452. fi
  453. done
  454. return 1
  455. }
  456. cert_checks() {
  457. err_count=0
  458. diff_c=0
  459. THRESHOLD=7
  460. # Reduce error count by 2 after restarting an unhealthy container
  461. trap "[ ${err_count} -gt 1 ] && err_count=$(( ${err_count} - 2 ))" USR1
  462. while [ ${err_count} -lt ${THRESHOLD} ]; do
  463. touch /tmp/certcheck; echo "$(tail -50 /tmp/certcheck)" > /tmp/certcheck
  464. host_ip_postfix=$(get_container_ip postfix)
  465. host_ip_dovecot=$(get_container_ip dovecot)
  466. err_c_cur=${err_count}
  467. /usr/lib/nagios/plugins/check_smtp -H ${host_ip_postfix} -p 589 -4 -S -D 7 2>> /tmp/certcheck 1>&2; err_count=$(( ${err_count} + $? ))
  468. /usr/lib/nagios/plugins/check_imap -H ${host_ip_dovecot} -p 993 -4 -S -D 7 2>> /tmp/certcheck 1>&2; err_count=$(( ${err_count} + $? ))
  469. [ ${err_c_cur} -eq ${err_count} ] && [ ! $((${err_count} - 1)) -lt 0 ] && err_count=$((${err_count} - 1)) diff_c=1
  470. [ ${err_c_cur} -ne ${err_count} ] && diff_c=$(( ${err_c_cur} - ${err_count} ))
  471. progress "Primary certificate expiry check" ${THRESHOLD} $(( ${THRESHOLD} - ${err_count} )) ${diff_c}
  472. # Always sleep 5 minutes, mail notifications are limited
  473. sleep 300
  474. done
  475. return 1
  476. }
  477. phpfpm_checks() {
  478. err_count=0
  479. diff_c=0
  480. THRESHOLD=${PHPFPM_THRESHOLD}
  481. # Reduce error count by 2 after restarting an unhealthy container
  482. trap "[ ${err_count} -gt 1 ] && err_count=$(( ${err_count} - 2 ))" USR1
  483. while [ ${err_count} -lt ${THRESHOLD} ]; do
  484. touch /tmp/php-fpm-mailcow; echo "$(tail -50 /tmp/php-fpm-mailcow)" > /tmp/php-fpm-mailcow
  485. host_ip=$(get_container_ip php-fpm-mailcow)
  486. err_c_cur=${err_count}
  487. /usr/lib/nagios/plugins/check_tcp -H ${host_ip} -p 9001 2>> /tmp/php-fpm-mailcow 1>&2; err_count=$(( ${err_count} + $? ))
  488. /usr/lib/nagios/plugins/check_tcp -H ${host_ip} -p 9002 2>> /tmp/php-fpm-mailcow 1>&2; err_count=$(( ${err_count} + $? ))
  489. [ ${err_c_cur} -eq ${err_count} ] && [ ! $((${err_count} - 1)) -lt 0 ] && err_count=$((${err_count} - 1)) diff_c=1
  490. [ ${err_c_cur} -ne ${err_count} ] && diff_c=$(( ${err_c_cur} - ${err_count} ))
  491. progress "PHP-FPM" ${THRESHOLD} $(( ${THRESHOLD} - ${err_count} )) ${diff_c}
  492. if [[ $? == 10 ]]; then
  493. diff_c=0
  494. sleep 1
  495. else
  496. diff_c=0
  497. sleep $(( ( RANDOM % 60 ) + 20 ))
  498. fi
  499. done
  500. return 1
  501. }
  502. ratelimit_checks() {
  503. err_count=0
  504. diff_c=0
  505. THRESHOLD=${RATELIMIT_THRESHOLD}
  506. RL_LOG_STATUS=$(redis-cli -h redis LRANGE RL_LOG 0 0 | jq .qid)
  507. # Reduce error count by 2 after restarting an unhealthy container
  508. trap "[ ${err_count} -gt 1 ] && err_count=$(( ${err_count} - 2 ))" USR1
  509. while [ ${err_count} -lt ${THRESHOLD} ]; do
  510. err_c_cur=${err_count}
  511. RL_LOG_STATUS_PREV=${RL_LOG_STATUS}
  512. RL_LOG_STATUS=$(redis-cli -h redis LRANGE RL_LOG 0 0 | jq .qid)
  513. if [[ ${RL_LOG_STATUS_PREV} != ${RL_LOG_STATUS} ]]; then
  514. err_count=$(( ${err_count} + 1 ))
  515. echo 'Last 10 applied ratelimits (may overlap with previous reports).' > /tmp/ratelimit
  516. echo 'Full ratelimit buckets can be emptied by deleting the ratelimit hash from within mailcow UI (see /debug -> Protocols -> Ratelimit):' >> /tmp/ratelimit
  517. echo >> /tmp/ratelimit
  518. redis-cli --raw -h redis LRANGE RL_LOG 0 10 | jq . >> /tmp/ratelimit
  519. fi
  520. [ ${err_c_cur} -eq ${err_count} ] && [ ! $((${err_count} - 1)) -lt 0 ] && err_count=$((${err_count} - 1)) diff_c=1
  521. [ ${err_c_cur} -ne ${err_count} ] && diff_c=$(( ${err_c_cur} - ${err_count} ))
  522. progress "Ratelimit" ${THRESHOLD} $(( ${THRESHOLD} - ${err_count} )) ${diff_c}
  523. if [[ $? == 10 ]]; then
  524. diff_c=0
  525. sleep 1
  526. else
  527. diff_c=0
  528. sleep $(( ( RANDOM % 60 ) + 20 ))
  529. fi
  530. done
  531. return 1
  532. }
  533. mailq_checks() {
  534. err_count=0
  535. diff_c=0
  536. THRESHOLD=${MAILQ_THRESHOLD}
  537. # Reduce error count by 2 after restarting an unhealthy container
  538. trap "[ ${err_count} -gt 1 ] && err_count=$(( ${err_count} - 2 ))" USR1
  539. while [ ${err_count} -lt ${THRESHOLD} ]; do
  540. touch /tmp/mail_queue_status; echo "$(tail -50 /tmp/mail_queue_status)" > /tmp/mail_queue_status
  541. MAILQ_LOG_STATUS=$(find /var/spool/postfix/deferred -type f | wc -l)
  542. echo "Mail queue contains ${MAILQ_LOG_STATUS} items (critical limit is ${MAILQ_CRIT}) at $(date)" >> /tmp/mail_queue_status
  543. err_c_cur=${err_count}
  544. if [ ${MAILQ_LOG_STATUS} -ge ${MAILQ_CRIT} ]; then
  545. err_count=$(( ${err_count} + 1 ))
  546. echo "Mail queue contains ${MAILQ_LOG_STATUS} items (critical limit is ${MAILQ_CRIT}) at $(date)" >> /tmp/mail_queue_status
  547. fi
  548. [ ${err_c_cur} -eq ${err_count} ] && [ ! $((${err_count} - 1)) -lt 0 ] && err_count=$((${err_count} - 1)) diff_c=1
  549. [ ${err_c_cur} -ne ${err_count} ] && diff_c=$(( ${err_c_cur} - ${err_count} ))
  550. progress "Mail queue" ${THRESHOLD} $(( ${THRESHOLD} - ${err_count} )) ${diff_c}
  551. if [[ $? == 10 ]]; then
  552. diff_c=0
  553. sleep 60
  554. else
  555. diff_c=0
  556. sleep $(( ( RANDOM % 60 ) + 20 ))
  557. fi
  558. done
  559. return 1
  560. }
  561. fail2ban_checks() {
  562. err_count=0
  563. diff_c=0
  564. THRESHOLD=${FAIL2BAN_THRESHOLD}
  565. F2B_LOG_STATUS=($(${REDIS_CMDLINE} --raw HKEYS F2B_ACTIVE_BANS))
  566. F2B_RES=
  567. # Reduce error count by 2 after restarting an unhealthy container
  568. trap "[ ${err_count} -gt 1 ] && err_count=$(( ${err_count} - 2 ))" USR1
  569. while [ ${err_count} -lt ${THRESHOLD} ]; do
  570. err_c_cur=${err_count}
  571. F2B_LOG_STATUS_PREV=(${F2B_LOG_STATUS[@]})
  572. F2B_LOG_STATUS=($(${REDIS_CMDLINE} --raw HKEYS F2B_ACTIVE_BANS))
  573. array_diff F2B_RES F2B_LOG_STATUS F2B_LOG_STATUS_PREV
  574. if [[ ! -z "${F2B_RES}" ]]; then
  575. err_count=$(( ${err_count} + 1 ))
  576. echo -n "${F2B_RES[@]}" | tr -cd "[a-fA-F0-9.:/] " | timeout 3s ${REDIS_CMDLINE} -x SET F2B_RES > /dev/null
  577. if [ $? -ne 0 ]; then
  578. ${REDIS_CMDLINE} -x DEL F2B_RES
  579. fi
  580. fi
  581. [ ${err_c_cur} -eq ${err_count} ] && [ ! $((${err_count} - 1)) -lt 0 ] && err_count=$((${err_count} - 1)) diff_c=1
  582. [ ${err_c_cur} -ne ${err_count} ] && diff_c=$(( ${err_c_cur} - ${err_count} ))
  583. progress "Fail2ban" ${THRESHOLD} $(( ${THRESHOLD} - ${err_count} )) ${diff_c}
  584. if [[ $? == 10 ]]; then
  585. diff_c=0
  586. sleep 1
  587. else
  588. diff_c=0
  589. sleep $(( ( RANDOM % 60 ) + 20 ))
  590. fi
  591. done
  592. return 1
  593. }
  594. acme_checks() {
  595. err_count=0
  596. diff_c=0
  597. THRESHOLD=${ACME_THRESHOLD}
  598. ACME_LOG_STATUS=$(redis-cli -h redis GET ACME_FAIL_TIME)
  599. if [[ -z "${ACME_LOG_STATUS}" ]]; then
  600. ${REDIS_CMDLINE} SET ACME_FAIL_TIME 0
  601. ACME_LOG_STATUS=0
  602. fi
  603. # Reduce error count by 2 after restarting an unhealthy container
  604. trap "[ ${err_count} -gt 1 ] && err_count=$(( ${err_count} - 2 ))" USR1
  605. while [ ${err_count} -lt ${THRESHOLD} ]; do
  606. err_c_cur=${err_count}
  607. ACME_LOG_STATUS_PREV=${ACME_LOG_STATUS}
  608. ACME_LC=0
  609. until [[ ! -z ${ACME_LOG_STATUS} ]] || [ ${ACME_LC} -ge 3 ]; do
  610. ACME_LOG_STATUS=$(redis-cli -h redis GET ACME_FAIL_TIME 2> /dev/null)
  611. sleep 3
  612. ACME_LC=$((ACME_LC+1))
  613. done
  614. if [[ ${ACME_LOG_STATUS_PREV} != ${ACME_LOG_STATUS} ]]; then
  615. err_count=$(( ${err_count} + 1 ))
  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 "ACME" ${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. ipv6nat_checks() {
  631. err_count=0
  632. diff_c=0
  633. THRESHOLD=${IPV6NAT_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. err_c_cur=${err_count}
  638. CONTAINERS=$(curl --silent --insecure https://dockerapi/containers/json)
  639. IPV6NAT_CONTAINER_ID=$(echo ${CONTAINERS} | jq -r ".[] | {name: .Config.Labels[\"com.docker.compose.service\"], project: .Config.Labels[\"com.docker.compose.project\"], id: .Id}" | jq -rc "select( .name | tostring | contains(\"ipv6nat-mailcow\")) | select( .project | tostring | contains(\"${COMPOSE_PROJECT_NAME,,}\")) | .id")
  640. if [[ ! -z ${IPV6NAT_CONTAINER_ID} ]]; then
  641. LATEST_STARTED="$(echo ${CONTAINERS} | jq -r ".[] | {name: .Config.Labels[\"com.docker.compose.service\"], project: .Config.Labels[\"com.docker.compose.project\"], StartedAt: .State.StartedAt}" | jq -rc "select( .project | tostring | contains(\"${COMPOSE_PROJECT_NAME,,}\")) | select( .name | tostring | contains(\"ipv6nat-mailcow\") | not)" | jq -rc .StartedAt | xargs -n1 date +%s -d | sort | tail -n1)"
  642. LATEST_IPV6NAT="$(echo ${CONTAINERS} | jq -r ".[] | {name: .Config.Labels[\"com.docker.compose.service\"], project: .Config.Labels[\"com.docker.compose.project\"], StartedAt: .State.StartedAt}" | jq -rc "select( .project | tostring | contains(\"${COMPOSE_PROJECT_NAME,,}\")) | select( .name | tostring | contains(\"ipv6nat-mailcow\"))" | jq -rc .StartedAt | xargs -n1 date +%s -d | sort | tail -n1)"
  643. DIFFERENCE_START_TIME=$(expr ${LATEST_IPV6NAT} - ${LATEST_STARTED} 2>/dev/null)
  644. if [[ "${DIFFERENCE_START_TIME}" -lt 30 ]]; then
  645. err_count=$(( ${err_count} + 1 ))
  646. fi
  647. fi
  648. [ ${err_c_cur} -eq ${err_count} ] && [ ! $((${err_count} - 1)) -lt 0 ] && err_count=$((${err_count} - 1)) diff_c=1
  649. [ ${err_c_cur} -ne ${err_count} ] && diff_c=$(( ${err_c_cur} - ${err_count} ))
  650. progress "IPv6 NAT" ${THRESHOLD} $(( ${THRESHOLD} - ${err_count} )) ${diff_c}
  651. if [[ $? == 10 ]]; then
  652. diff_c=0
  653. sleep 30
  654. else
  655. diff_c=0
  656. sleep 300
  657. fi
  658. done
  659. return 1
  660. }
  661. rspamd_checks() {
  662. err_count=0
  663. diff_c=0
  664. THRESHOLD=${RSPAMD_THRESHOLD}
  665. # Reduce error count by 2 after restarting an unhealthy container
  666. trap "[ ${err_count} -gt 1 ] && err_count=$(( ${err_count} - 2 ))" USR1
  667. while [ ${err_count} -lt ${THRESHOLD} ]; do
  668. touch /tmp/rspamd-mailcow; echo "$(tail -50 /tmp/rspamd-mailcow)" > /tmp/rspamd-mailcow
  669. host_ip=$(get_container_ip rspamd-mailcow)
  670. err_c_cur=${err_count}
  671. SCORE=$(echo 'To: null@localhost
  672. From: watchdog@localhost
  673. Empty
  674. ' | usr/bin/curl --max-time 10 -s --data-binary @- --unix-socket /var/lib/rspamd/rspamd.sock http://rspamd/scan | jq -rc .default.required_score)
  675. if [[ ${SCORE} != "9999" ]]; then
  676. echo "Rspamd settings check failed, score returned: ${SCORE}" 2>> /tmp/rspamd-mailcow 1>&2
  677. err_count=$(( ${err_count} + 1))
  678. else
  679. echo "Rspamd settings check succeeded, score returned: ${SCORE}" 2>> /tmp/rspamd-mailcow 1>&2
  680. fi
  681. # A dirty hack until a PING PONG event is implemented to worker proxy
  682. # We expect an empty response, not a timeout
  683. if [ "$(curl -s --max-time 10 ${host_ip}:9900 2> /dev/null ; echo $?)" == "28" ]; then
  684. echo "Milter check failed" 2>> /tmp/rspamd-mailcow 1>&2; err_count=$(( ${err_count} + 1 ));
  685. else
  686. echo "Milter check succeeded" 2>> /tmp/rspamd-mailcow 1>&2
  687. fi
  688. [ ${err_c_cur} -eq ${err_count} ] && [ ! $((${err_count} - 1)) -lt 0 ] && err_count=$((${err_count} - 1)) diff_c=1
  689. [ ${err_c_cur} -ne ${err_count} ] && diff_c=$(( ${err_c_cur} - ${err_count} ))
  690. progress "Rspamd" ${THRESHOLD} $(( ${THRESHOLD} - ${err_count} )) ${diff_c}
  691. if [[ $? == 10 ]]; then
  692. diff_c=0
  693. sleep 1
  694. else
  695. diff_c=0
  696. sleep $(( ( RANDOM % 60 ) + 20 ))
  697. fi
  698. done
  699. return 1
  700. }
  701. olefy_checks() {
  702. err_count=0
  703. diff_c=0
  704. THRESHOLD=${OLEFY_THRESHOLD}
  705. # Reduce error count by 2 after restarting an unhealthy container
  706. trap "[ ${err_count} -gt 1 ] && err_count=$(( ${err_count} - 2 ))" USR1
  707. while [ ${err_count} -lt ${THRESHOLD} ]; do
  708. touch /tmp/olefy-mailcow; echo "$(tail -50 /tmp/olefy-mailcow)" > /tmp/olefy-mailcow
  709. host_ip=$(get_container_ip olefy-mailcow)
  710. err_c_cur=${err_count}
  711. /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} + $? ))
  712. [ ${err_c_cur} -eq ${err_count} ] && [ ! $((${err_count} - 1)) -lt 0 ] && err_count=$((${err_count} - 1)) diff_c=1
  713. [ ${err_c_cur} -ne ${err_count} ] && diff_c=$(( ${err_c_cur} - ${err_count} ))
  714. progress "Olefy" ${THRESHOLD} $(( ${THRESHOLD} - ${err_count} )) ${diff_c}
  715. if [[ $? == 10 ]]; then
  716. diff_c=0
  717. sleep 1
  718. else
  719. diff_c=0
  720. sleep $(( ( RANDOM % 60 ) + 20 ))
  721. fi
  722. done
  723. return 1
  724. }
  725. # Notify about start
  726. if [[ ! -z ${WATCHDOG_NOTIFY_EMAIL} ]]; then
  727. mail_error "watchdog-mailcow" "Watchdog started monitoring mailcow."
  728. fi
  729. # Create watchdog agents
  730. (
  731. while true; do
  732. if ! nginx_checks; then
  733. log_msg "Nginx hit error limit"
  734. echo nginx-mailcow > /tmp/com_pipe
  735. fi
  736. done
  737. ) &
  738. PID=$!
  739. echo "Spawned nginx_checks with PID ${PID}"
  740. BACKGROUND_TASKS+=(${PID})
  741. if [[ ${WATCHDOG_EXTERNAL_CHECKS} =~ ^([yY][eE][sS]|[yY])+$ ]]; then
  742. (
  743. while true; do
  744. if ! external_checks; then
  745. log_msg "External checks hit error limit"
  746. echo external_checks > /tmp/com_pipe
  747. fi
  748. done
  749. ) &
  750. PID=$!
  751. echo "Spawned external_checks with PID ${PID}"
  752. BACKGROUND_TASKS+=(${PID})
  753. fi
  754. if [[ ${WATCHDOG_MYSQL_REPLICATION_CHECKS} =~ ^([yY][eE][sS]|[yY])+$ ]]; then
  755. (
  756. while true; do
  757. if ! mysql_repl_checks; then
  758. log_msg "MySQL replication check hit error limit"
  759. echo mysql_repl_checks > /tmp/com_pipe
  760. fi
  761. done
  762. ) &
  763. PID=$!
  764. echo "Spawned mysql_repl_checks with PID ${PID}"
  765. BACKGROUND_TASKS+=(${PID})
  766. fi
  767. (
  768. while true; do
  769. if ! mysql_checks; then
  770. log_msg "MySQL hit error limit"
  771. echo mysql-mailcow > /tmp/com_pipe
  772. fi
  773. done
  774. ) &
  775. PID=$!
  776. echo "Spawned mysql_checks with PID ${PID}"
  777. BACKGROUND_TASKS+=(${PID})
  778. (
  779. while true; do
  780. if ! redis_checks; then
  781. log_msg "Local Redis hit error limit"
  782. echo redis-mailcow > /tmp/com_pipe
  783. fi
  784. done
  785. ) &
  786. PID=$!
  787. echo "Spawned redis_checks with PID ${PID}"
  788. BACKGROUND_TASKS+=(${PID})
  789. (
  790. while true; do
  791. if ! phpfpm_checks; then
  792. log_msg "PHP-FPM hit error limit"
  793. echo php-fpm-mailcow > /tmp/com_pipe
  794. fi
  795. done
  796. ) &
  797. PID=$!
  798. echo "Spawned phpfpm_checks with PID ${PID}"
  799. BACKGROUND_TASKS+=(${PID})
  800. if [[ "${SKIP_SOGO}" =~ ^([nN][oO]|[nN])+$ ]]; then
  801. (
  802. while true; do
  803. if ! sogo_checks; then
  804. log_msg "SOGo hit error limit"
  805. echo sogo-mailcow > /tmp/com_pipe
  806. fi
  807. done
  808. ) &
  809. PID=$!
  810. echo "Spawned sogo_checks with PID ${PID}"
  811. BACKGROUND_TASKS+=(${PID})
  812. fi
  813. if [ ${CHECK_UNBOUND} -eq 1 ]; then
  814. (
  815. while true; do
  816. if ! unbound_checks; then
  817. log_msg "Unbound hit error limit"
  818. echo unbound-mailcow > /tmp/com_pipe
  819. fi
  820. done
  821. ) &
  822. PID=$!
  823. echo "Spawned unbound_checks with PID ${PID}"
  824. BACKGROUND_TASKS+=(${PID})
  825. fi
  826. if [[ "${SKIP_CLAMD}" =~ ^([nN][oO]|[nN])+$ ]]; then
  827. (
  828. while true; do
  829. if ! clamd_checks; then
  830. log_msg "Clamd hit error limit"
  831. echo clamd-mailcow > /tmp/com_pipe
  832. fi
  833. done
  834. ) &
  835. PID=$!
  836. echo "Spawned clamd_checks with PID ${PID}"
  837. BACKGROUND_TASKS+=(${PID})
  838. fi
  839. (
  840. while true; do
  841. if ! postfix_checks; then
  842. log_msg "Postfix hit error limit"
  843. echo postfix-mailcow > /tmp/com_pipe
  844. fi
  845. done
  846. ) &
  847. PID=$!
  848. echo "Spawned postfix_checks with PID ${PID}"
  849. BACKGROUND_TASKS+=(${PID})
  850. (
  851. while true; do
  852. if ! mailq_checks; then
  853. log_msg "Mail queue hit error limit"
  854. echo mail_queue_status > /tmp/com_pipe
  855. fi
  856. done
  857. ) &
  858. PID=$!
  859. echo "Spawned mailq_checks with PID ${PID}"
  860. BACKGROUND_TASKS+=(${PID})
  861. (
  862. while true; do
  863. if ! dovecot_checks; then
  864. log_msg "Dovecot hit error limit"
  865. echo dovecot-mailcow > /tmp/com_pipe
  866. fi
  867. done
  868. ) &
  869. PID=$!
  870. echo "Spawned dovecot_checks with PID ${PID}"
  871. BACKGROUND_TASKS+=(${PID})
  872. (
  873. while true; do
  874. if ! dovecot_repl_checks; then
  875. log_msg "Dovecot hit error limit"
  876. echo dovecot_repl_checks > /tmp/com_pipe
  877. fi
  878. done
  879. ) &
  880. PID=$!
  881. echo "Spawned dovecot_repl_checks with PID ${PID}"
  882. BACKGROUND_TASKS+=(${PID})
  883. (
  884. while true; do
  885. if ! rspamd_checks; then
  886. log_msg "Rspamd hit error limit"
  887. echo rspamd-mailcow > /tmp/com_pipe
  888. fi
  889. done
  890. ) &
  891. PID=$!
  892. echo "Spawned rspamd_checks with PID ${PID}"
  893. BACKGROUND_TASKS+=(${PID})
  894. (
  895. while true; do
  896. if ! ratelimit_checks; then
  897. log_msg "Ratelimit hit error limit"
  898. echo ratelimit > /tmp/com_pipe
  899. fi
  900. done
  901. ) &
  902. PID=$!
  903. echo "Spawned ratelimit_checks with PID ${PID}"
  904. BACKGROUND_TASKS+=(${PID})
  905. (
  906. while true; do
  907. if ! fail2ban_checks; then
  908. log_msg "Fail2ban hit error limit"
  909. echo fail2ban > /tmp/com_pipe
  910. fi
  911. done
  912. ) &
  913. PID=$!
  914. echo "Spawned fail2ban_checks with PID ${PID}"
  915. BACKGROUND_TASKS+=(${PID})
  916. (
  917. while true; do
  918. if ! cert_checks; then
  919. log_msg "Cert check hit error limit"
  920. echo certcheck > /tmp/com_pipe
  921. fi
  922. done
  923. ) &
  924. PID=$!
  925. echo "Spawned cert_checks with PID ${PID}"
  926. BACKGROUND_TASKS+=(${PID})
  927. (
  928. while true; do
  929. if ! olefy_checks; then
  930. log_msg "Olefy hit error limit"
  931. echo olefy-mailcow > /tmp/com_pipe
  932. fi
  933. done
  934. ) &
  935. PID=$!
  936. echo "Spawned olefy_checks with PID ${PID}"
  937. BACKGROUND_TASKS+=(${PID})
  938. (
  939. while true; do
  940. if ! acme_checks; then
  941. log_msg "ACME client hit error limit"
  942. echo acme-mailcow > /tmp/com_pipe
  943. fi
  944. done
  945. ) &
  946. PID=$!
  947. echo "Spawned acme_checks with PID ${PID}"
  948. BACKGROUND_TASKS+=(${PID})
  949. (
  950. while true; do
  951. if ! ipv6nat_checks; then
  952. log_msg "IPv6 NAT warning: ipv6nat-mailcow container was not started at least 30s after siblings (not an error)"
  953. echo ipv6nat-mailcow > /tmp/com_pipe
  954. fi
  955. done
  956. ) &
  957. PID=$!
  958. echo "Spawned ipv6nat_checks with PID ${PID}"
  959. BACKGROUND_TASKS+=(${PID})
  960. # Monitor watchdog agents, stop script when agents fails and wait for respawn by Docker (restart:always:n)
  961. (
  962. while true; do
  963. for bg_task in ${BACKGROUND_TASKS[*]}; do
  964. if ! kill -0 ${bg_task} 1>&2; then
  965. log_msg "Worker ${bg_task} died, stopping watchdog and waiting for respawn..."
  966. kill -TERM 1
  967. fi
  968. sleep 10
  969. done
  970. done
  971. ) &
  972. # Monitor dockerapi
  973. (
  974. while true; do
  975. while nc -z dockerapi 443; do
  976. sleep 3
  977. done
  978. log_msg "Cannot find dockerapi-mailcow, waiting to recover..."
  979. kill -STOP ${BACKGROUND_TASKS[*]}
  980. until nc -z dockerapi 443; do
  981. sleep 3
  982. done
  983. kill -CONT ${BACKGROUND_TASKS[*]}
  984. kill -USR1 ${BACKGROUND_TASKS[*]}
  985. done
  986. ) &
  987. # Actions when threshold limit is reached
  988. while true; do
  989. CONTAINER_ID=
  990. HAS_INITDB=
  991. read com_pipe_answer </tmp/com_pipe
  992. if [ -s "/tmp/${com_pipe_answer}" ]; then
  993. cat "/tmp/${com_pipe_answer}"
  994. fi
  995. if [[ ${com_pipe_answer} == "ratelimit" ]]; then
  996. log_msg "At least one ratelimit was applied"
  997. [[ ! -z ${WATCHDOG_NOTIFY_EMAIL} ]] && mail_error "${com_pipe_answer}"
  998. elif [[ ${com_pipe_answer} == "mail_queue_status" ]]; then
  999. log_msg "Mail queue status is critical"
  1000. [[ ! -z ${WATCHDOG_NOTIFY_EMAIL} ]] && mail_error "${com_pipe_answer}"
  1001. elif [[ ${com_pipe_answer} == "external_checks" ]]; then
  1002. log_msg "Your mailcow is an open relay!"
  1003. # Define $2 to override message text, else print service was restarted at ...
  1004. [[ ! -z ${WATCHDOG_NOTIFY_EMAIL} ]] && mail_error "${com_pipe_answer}" "Please stop mailcow now and check your network configuration!"
  1005. elif [[ ${com_pipe_answer} == "mysql_repl_checks" ]]; then
  1006. log_msg "MySQL replication is not working properly"
  1007. # Define $2 to override message text, else print service was restarted at ...
  1008. # Once mail per 10 minutes
  1009. [[ ! -z ${WATCHDOG_NOTIFY_EMAIL} ]] && mail_error "${com_pipe_answer}" "Please check the SQL replication status" 600
  1010. elif [[ ${com_pipe_answer} == "dovecot_repl_checks" ]]; then
  1011. log_msg "Dovecot replication is not working properly"
  1012. # Define $2 to override message text, else print service was restarted at ...
  1013. # Once mail per 10 minutes
  1014. [[ ! -z ${WATCHDOG_NOTIFY_EMAIL} ]] && mail_error "${com_pipe_answer}" "Please check the Dovecot replicator status" 600
  1015. elif [[ ${com_pipe_answer} == "certcheck" ]]; then
  1016. log_msg "Certificates are about to expire"
  1017. # Define $2 to override message text, else print service was restarted at ...
  1018. # Only mail once a day
  1019. [[ ! -z ${WATCHDOG_NOTIFY_EMAIL} ]] && mail_error "${com_pipe_answer}" "Please renew your certificate" 86400
  1020. elif [[ ${com_pipe_answer} == "acme-mailcow" ]]; then
  1021. log_msg "acme-mailcow did not complete successfully"
  1022. # Define $2 to override message text, else print service was restarted at ...
  1023. [[ ! -z ${WATCHDOG_NOTIFY_EMAIL} ]] && mail_error "${com_pipe_answer}" "Please check acme-mailcow for further information."
  1024. elif [[ ${com_pipe_answer} == "fail2ban" ]]; then
  1025. F2B_RES=($(timeout 4s ${REDIS_CMDLINE} --raw GET F2B_RES 2> /dev/null))
  1026. if [[ ! -z "${F2B_RES}" ]]; then
  1027. ${REDIS_CMDLINE} DEL F2B_RES > /dev/null
  1028. host=
  1029. for host in "${F2B_RES[@]}"; do
  1030. log_msg "Banned ${host}"
  1031. rm /tmp/fail2ban 2> /dev/null
  1032. timeout 2s whois "${host}" > /tmp/fail2ban
  1033. [[ ! -z ${WATCHDOG_NOTIFY_EMAIL} ]] && [[ ${WATCHDOG_NOTIFY_BAN} =~ ^([yY][eE][sS]|[yY])+$ ]] && mail_error "${com_pipe_answer}" "IP ban: ${host}"
  1034. done
  1035. fi
  1036. elif [[ ${com_pipe_answer} =~ .+-mailcow ]]; then
  1037. kill -STOP ${BACKGROUND_TASKS[*]}
  1038. sleep 10
  1039. CONTAINER_ID=$(curl --silent --insecure https://dockerapi/containers/json | jq -r ".[] | {name: .Config.Labels[\"com.docker.compose.service\"], project: .Config.Labels[\"com.docker.compose.project\"], id: .Id}" | jq -rc "select( .name | tostring | contains(\"${com_pipe_answer}\")) | select( .project | tostring | contains(\"${COMPOSE_PROJECT_NAME,,}\")) | .id")
  1040. if [[ ! -z ${CONTAINER_ID} ]]; then
  1041. if [[ "${com_pipe_answer}" == "php-fpm-mailcow" ]]; then
  1042. 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)
  1043. fi
  1044. S_RUNNING=$(($(date +%s) - $(curl --silent --insecure https://dockerapi/containers/${CONTAINER_ID}/json | jq .State.StartedAt | xargs -n1 date +%s -d)))
  1045. if [ ${S_RUNNING} -lt 360 ]; then
  1046. log_msg "Container is running for less than 360 seconds, skipping action..."
  1047. elif [[ ! -z ${HAS_INITDB} ]]; then
  1048. log_msg "Database is being initialized by php-fpm-mailcow, not restarting but delaying checks for a minute..."
  1049. sleep 60
  1050. else
  1051. log_msg "Sending restart command to ${CONTAINER_ID}..."
  1052. curl --silent --insecure -XPOST https://dockerapi/containers/${CONTAINER_ID}/restart
  1053. if [[ ${com_pipe_answer} != "ipv6nat-mailcow" ]]; then
  1054. [[ ! -z ${WATCHDOG_NOTIFY_EMAIL} ]] && mail_error "${com_pipe_answer}"
  1055. fi
  1056. log_msg "Wait for restarted container to settle and continue watching..."
  1057. sleep 35
  1058. fi
  1059. fi
  1060. kill -CONT ${BACKGROUND_TASKS[*]}
  1061. sleep 1
  1062. kill -USR1 ${BACKGROUND_TASKS[*]}
  1063. fi
  1064. done