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