watchdog.sh 40 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128
  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} -a ${REDISPASS} --no-auth-warning"
  37. else
  38. REDIS_CMDLINE="redis-cli -h redis -p 6379 -a ${REDISPASS} --no-auth-warning"
  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=$(mariadb --skip-ssl -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 "AUTH ${REDISPASS}\nPING\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 -a ${REDISPASS} --no-auth-warning -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 -a ${REDISPASS} --no-auth-warning 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 -a ${REDISPASS} --no-auth-warning 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 -a ${REDISPASS} --no-auth-warning 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 -a ${REDISPASS} --no-auth-warning 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 -a ${REDISPASS} --no-auth-warning 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 -a ${REDISPASS} --no-auth-warning 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. if [[ "${SKIP_OLEFY}" =~ ^([nN][oO]|[nN])+$ ]]; then
  936. (
  937. while true; do
  938. if ! olefy_checks; then
  939. log_msg "Olefy hit error limit"
  940. echo olefy-mailcow > /tmp/com_pipe
  941. fi
  942. done
  943. ) &
  944. PID=$!
  945. echo "Spawned olefy_checks with PID ${PID}"
  946. BACKGROUND_TASKS+=(${PID})
  947. fi
  948. (
  949. while true; do
  950. if ! acme_checks; then
  951. log_msg "ACME client hit error limit"
  952. echo acme-mailcow > /tmp/com_pipe
  953. fi
  954. done
  955. ) &
  956. PID=$!
  957. echo "Spawned acme_checks with PID ${PID}"
  958. BACKGROUND_TASKS+=(${PID})
  959. # Monitor watchdog agents, stop script when agents fails and wait for respawn by Docker (restart:always:n)
  960. (
  961. while true; do
  962. for bg_task in ${BACKGROUND_TASKS[*]}; do
  963. if ! kill -0 ${bg_task} 1>&2; then
  964. log_msg "Worker ${bg_task} died, stopping watchdog and waiting for respawn..."
  965. kill -TERM 1
  966. fi
  967. sleep 10
  968. done
  969. done
  970. ) &
  971. # Monitor dockerapi
  972. (
  973. while true; do
  974. while nc -z dockerapi 443; do
  975. sleep 3
  976. done
  977. log_msg "Cannot find dockerapi-mailcow, waiting to recover..."
  978. kill -STOP ${BACKGROUND_TASKS[*]}
  979. until nc -z dockerapi 443; do
  980. sleep 3
  981. done
  982. kill -CONT ${BACKGROUND_TASKS[*]}
  983. kill -USR1 ${BACKGROUND_TASKS[*]}
  984. done
  985. ) &
  986. # Actions when threshold limit is reached
  987. while true; do
  988. CONTAINER_ID=
  989. HAS_INITDB=
  990. read com_pipe_answer </tmp/com_pipe
  991. if [ -s "/tmp/${com_pipe_answer}" ]; then
  992. cat "/tmp/${com_pipe_answer}"
  993. fi
  994. if [[ ${com_pipe_answer} == "ratelimit" ]]; then
  995. log_msg "At least one ratelimit was applied"
  996. notify_error "${com_pipe_answer}"
  997. elif [[ ${com_pipe_answer} == "mail_queue_status" ]]; then
  998. log_msg "Mail queue status is critical"
  999. notify_error "${com_pipe_answer}"
  1000. elif [[ ${com_pipe_answer} == "external_checks" ]]; then
  1001. log_msg "Your mailcow is an open relay!"
  1002. # Define $2 to override message text, else print service was restarted at ...
  1003. notify_error "${com_pipe_answer}" "Please stop mailcow now and check your network configuration!"
  1004. elif [[ ${com_pipe_answer} == "mysql_repl_checks" ]]; then
  1005. log_msg "MySQL replication is not working properly"
  1006. # Define $2 to override message text, else print service was restarted at ...
  1007. # Once mail per 10 minutes
  1008. notify_error "${com_pipe_answer}" "Please check the SQL replication status" 600
  1009. elif [[ ${com_pipe_answer} == "dovecot_repl_checks" ]]; then
  1010. log_msg "Dovecot replication is not working properly"
  1011. # Define $2 to override message text, else print service was restarted at ...
  1012. # Once mail per 10 minutes
  1013. notify_error "${com_pipe_answer}" "Please check the Dovecot replicator status" 600
  1014. elif [[ ${com_pipe_answer} == "certcheck" ]]; then
  1015. log_msg "Certificates are about to expire"
  1016. # Define $2 to override message text, else print service was restarted at ...
  1017. # Only mail once a day
  1018. notify_error "${com_pipe_answer}" "Please renew your certificate" 86400
  1019. elif [[ ${com_pipe_answer} == "acme-mailcow" ]]; then
  1020. log_msg "acme-mailcow did not complete successfully"
  1021. # Define $2 to override message text, else print service was restarted at ...
  1022. notify_error "${com_pipe_answer}" "Please check acme-mailcow for further information."
  1023. elif [[ ${com_pipe_answer} == "fail2ban" ]]; then
  1024. F2B_RES=($(timeout 4s ${REDIS_CMDLINE} --raw GET F2B_RES 2> /dev/null))
  1025. if [[ ! -z "${F2B_RES}" ]]; then
  1026. ${REDIS_CMDLINE} DEL F2B_RES > /dev/null
  1027. host=
  1028. for host in "${F2B_RES[@]}"; do
  1029. log_msg "Banned ${host}"
  1030. rm /tmp/fail2ban 2> /dev/null
  1031. timeout 2s whois "${host}" > /tmp/fail2ban
  1032. [[ ${WATCHDOG_NOTIFY_BAN} =~ ^([yY][eE][sS]|[yY])+$ ]] && notify_error "${com_pipe_answer}" "IP ban: ${host}"
  1033. done
  1034. fi
  1035. elif [[ ${com_pipe_answer} =~ .+-mailcow ]]; then
  1036. kill -STOP ${BACKGROUND_TASKS[*]}
  1037. sleep 10
  1038. 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")
  1039. if [[ ! -z ${CONTAINER_ID} ]]; then
  1040. if [[ "${com_pipe_answer}" == "php-fpm-mailcow" ]]; then
  1041. 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)
  1042. fi
  1043. 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)))
  1044. if [ ${S_RUNNING} -lt 360 ]; then
  1045. log_msg "Container is running for less than 360 seconds, skipping action..."
  1046. elif [[ ! -z ${HAS_INITDB} ]]; then
  1047. log_msg "Database is being initialized by php-fpm-mailcow, not restarting but delaying checks for a minute..."
  1048. sleep 60
  1049. else
  1050. log_msg "Sending restart command to ${CONTAINER_ID}..."
  1051. curl --silent --insecure -XPOST https://dockerapi.${COMPOSE_PROJECT_NAME}_mailcow-network/containers/${CONTAINER_ID}/restart
  1052. notify_error "${com_pipe_answer}"
  1053. log_msg "Wait for restarted container to settle and continue watching..."
  1054. sleep 35
  1055. fi
  1056. fi
  1057. kill -CONT ${BACKGROUND_TASKS[*]}
  1058. sleep 1
  1059. kill -USR1 ${BACKGROUND_TASKS[*]}
  1060. fi
  1061. done