watchdog.sh 40 KB

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