watchdog.sh 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841
  1. #!/bin/bash
  2. trap "exit" INT TERM
  3. trap "kill 0" EXIT
  4. # Prepare
  5. BACKGROUND_TASKS=()
  6. echo "Waiting for containers to settle..."
  7. sleep 10
  8. if [[ "${USE_WATCHDOG}" =~ ^([nN][oO]|[nN])+$ ]]; then
  9. echo -e "$(date) - USE_WATCHDOG=n, skipping watchdog..."
  10. sleep 365d
  11. exec $(readlink -f "$0")
  12. fi
  13. # Checks pipe their corresponding container name in this pipe
  14. if [[ ! -p /tmp/com_pipe ]]; then
  15. mkfifo /tmp/com_pipe
  16. fi
  17. # Wait for containers
  18. while ! mysqladmin status --socket=/var/run/mysqld/mysqld.sock -u${DBUSER} -p${DBPASS} --silent; do
  19. echo "Waiting for SQL..."
  20. sleep 2
  21. done
  22. until [[ $(redis-cli -h redis-mailcow PING) == "PONG" ]]; do
  23. echo "Waiting for Redis..."
  24. sleep 2
  25. done
  26. redis-cli -h redis-mailcow DEL F2B_RES > /dev/null
  27. # Common functions
  28. array_diff() {
  29. # https://stackoverflow.com/questions/2312762, Alex Offshore
  30. eval local ARR1=\(\"\${$2[@]}\"\)
  31. eval local ARR2=\(\"\${$3[@]}\"\)
  32. local IFS=$'\n'
  33. mapfile -t $1 < <(comm -23 <(echo "${ARR1[*]}" | sort) <(echo "${ARR2[*]}" | sort))
  34. }
  35. progress() {
  36. SERVICE=${1}
  37. TOTAL=${2}
  38. CURRENT=${3}
  39. DIFF=${4}
  40. [[ -z ${DIFF} ]] && DIFF=0
  41. [[ -z ${TOTAL} || -z ${CURRENT} ]] && return
  42. [[ ${CURRENT} -gt ${TOTAL} ]] && return
  43. [[ ${CURRENT} -lt 0 ]] && CURRENT=0
  44. PERCENT=$(( 200 * ${CURRENT} / ${TOTAL} % 2 + 100 * ${CURRENT} / ${TOTAL} ))
  45. redis-cli -h redis LPUSH WATCHDOG_LOG "{\"time\":\"$(date +%s)\",\"service\":\"${SERVICE}\",\"lvl\":\"${PERCENT}\",\"hpnow\":\"${CURRENT}\",\"hptotal\":\"${TOTAL}\",\"hpdiff\":\"${DIFF}\"}" > /dev/null
  46. log_msg "${SERVICE} health level: ${PERCENT}% (${CURRENT}/${TOTAL}), health trend: ${DIFF}" no_redis
  47. # Return 10 to indicate a dead service
  48. [ ${CURRENT} -le 0 ] && return 10
  49. }
  50. log_msg() {
  51. if [[ ${2} != "no_redis" ]]; then
  52. redis-cli -h redis LPUSH WATCHDOG_LOG "{\"time\":\"$(date +%s)\",\"message\":\"$(printf '%s' "${1}" | \
  53. tr '\r\n%&;$"_[]{}-' ' ')\"}" > /dev/null
  54. fi
  55. echo $(date) $(printf '%s\n' "${1}")
  56. }
  57. function mail_error() {
  58. [[ -z ${1} ]] && return 1
  59. [[ -z ${2} ]] && BODY="Service was restarted on $(date), please check your mailcow installation." || BODY="$(date) - ${2}"
  60. WATCHDOG_NOTIFY_EMAIL=$(echo "${WATCHDOG_NOTIFY_EMAIL}" | sed 's/"//;s|"$||')
  61. # Some exceptions for subject and body formats
  62. if [[ ${1} == "fail2ban" ]]; then
  63. SUBJECT="${BODY}"
  64. BODY="Please see netfilter-mailcow for more details and triggered rules."
  65. else
  66. SUBJECT="Watchdog ALERT: ${1}"
  67. fi
  68. IFS=',' read -r -a MAIL_RCPTS <<< "${WATCHDOG_NOTIFY_EMAIL}"
  69. for rcpt in "${MAIL_RCPTS[@]}"; do
  70. RCPT_DOMAIN=
  71. RCPT_MX=
  72. RCPT_DOMAIN=$(echo ${rcpt} | awk -F @ {'print $NF'})
  73. RCPT_MX=$(dig +short ${RCPT_DOMAIN} mx | sort -n | awk '{print $2; exit}')
  74. if [[ -z ${RCPT_MX} ]]; then
  75. log_msg "Cannot determine MX for ${rcpt}, skipping email notification..."
  76. return 1
  77. fi
  78. [ -f "/tmp/${1}" ] && BODY="/tmp/${1}"
  79. timeout 10s ./smtp-cli --missing-modules-ok \
  80. --charset=UTF-8 \
  81. --subject="${SUBJECT}" \
  82. --body-plain="${BODY}" \
  83. --to=${rcpt} \
  84. --from="watchdog@${MAILCOW_HOSTNAME}" \
  85. --server="${RCPT_MX}" \
  86. --hello-host=${MAILCOW_HOSTNAME}
  87. log_msg "Sent notification email to ${rcpt}"
  88. done
  89. }
  90. get_container_ip() {
  91. # ${1} is container
  92. CONTAINER_ID=()
  93. CONTAINER_IPS=()
  94. CONTAINER_IP=
  95. LOOP_C=1
  96. until [[ ${CONTAINER_IP} =~ ^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}$ ]] || [[ ${LOOP_C} -gt 5 ]]; do
  97. if [ ${IP_BY_DOCKER_API} -eq 0 ]; then
  98. CONTAINER_IP=$(dig a "${1}" +short)
  99. else
  100. sleep 0.5
  101. # get long container id for exact match
  102. CONTAINER_ID=($(curl --silent --insecure https://dockerapi/containers/json | jq -r ".[] | {name: .Config.Labels[\"com.docker.compose.service\"], id: .Id}" | jq -rc "select( .name | tostring == \"${1}\") | .id"))
  103. # returned id can have multiple elements (if scaled), shuffle for random test
  104. CONTAINER_ID=($(printf "%s\n" "${CONTAINER_ID[@]}" | shuf))
  105. if [[ ! -z ${CONTAINER_ID} ]]; then
  106. for matched_container in "${CONTAINER_ID[@]}"; do
  107. CONTAINER_IPS=($(curl --silent --insecure https://dockerapi/containers/${matched_container}/json | jq -r '.NetworkSettings.Networks[].IPAddress'))
  108. for ip_match in "${CONTAINER_IPS[@]}"; do
  109. # grep will do nothing if one of these vars is empty
  110. [[ -z ${ip_match} ]] && continue
  111. [[ -z ${IPV4_NETWORK} ]] && continue
  112. # only return ips that are part of our network
  113. if ! grep -q ${IPV4_NETWORK} <(echo ${ip_match}); then
  114. continue
  115. else
  116. CONTAINER_IP=${ip_match}
  117. break
  118. fi
  119. done
  120. [[ ! -z ${CONTAINER_IP} ]] && break
  121. done
  122. fi
  123. fi
  124. LOOP_C=$((LOOP_C + 1))
  125. done
  126. [[ ${LOOP_C} -gt 5 ]] && echo 240.0.0.0 || echo ${CONTAINER_IP}
  127. }
  128. nginx_checks() {
  129. err_count=0
  130. diff_c=0
  131. THRESHOLD=5
  132. # Reduce error count by 2 after restarting an unhealthy container
  133. trap "[ ${err_count} -gt 1 ] && err_count=$(( ${err_count} - 2 ))" USR1
  134. while [ ${err_count} -lt ${THRESHOLD} ]; do
  135. touch /tmp/nginx-mailcow; echo "$(tail -50 /tmp/nginx-mailcow)" > /tmp/nginx-mailcow
  136. host_ip=$(get_container_ip nginx-mailcow)
  137. err_c_cur=${err_count}
  138. /usr/lib/nagios/plugins/check_http -4 -H ${host_ip} -u / -p 8081 2>> /tmp/nginx-mailcow 1>&2; err_count=$(( ${err_count} + $? ))
  139. [ ${err_c_cur} -eq ${err_count} ] && [ ! $((${err_count} - 1)) -lt 0 ] && err_count=$((${err_count} - 1)) diff_c=1
  140. [ ${err_c_cur} -ne ${err_count} ] && diff_c=$(( ${err_c_cur} - ${err_count} ))
  141. progress "Nginx" ${THRESHOLD} $(( ${THRESHOLD} - ${err_count} )) ${diff_c}
  142. if [[ $? == 10 ]]; then
  143. diff_c=0
  144. sleep 1
  145. else
  146. diff_c=0
  147. sleep $(( ( RANDOM % 60 ) + 20 ))
  148. fi
  149. done
  150. return 1
  151. }
  152. unbound_checks() {
  153. err_count=0
  154. diff_c=0
  155. THRESHOLD=5
  156. # Reduce error count by 2 after restarting an unhealthy container
  157. trap "[ ${err_count} -gt 1 ] && err_count=$(( ${err_count} - 2 ))" USR1
  158. while [ ${err_count} -lt ${THRESHOLD} ]; do
  159. touch /tmp/unbound-mailcow; echo "$(tail -50 /tmp/unbound-mailcow)" > /tmp/unbound-mailcow
  160. host_ip=$(get_container_ip unbound-mailcow)
  161. err_c_cur=${err_count}
  162. /usr/lib/nagios/plugins/check_dns -s ${host_ip} -H stackoverflow.com 2>> /tmp/unbound-mailcow 1>&2; err_count=$(( ${err_count} + $? ))
  163. DNSSEC=$(dig com +dnssec | egrep 'flags:.+ad')
  164. if [[ -z ${DNSSEC} ]]; then
  165. echo "DNSSEC failure" 2>> /tmp/unbound-mailcow 1>&2
  166. err_count=$(( ${err_count} + 1))
  167. else
  168. echo "DNSSEC check succeeded" 2>> /tmp/unbound-mailcow 1>&2
  169. fi
  170. [ ${err_c_cur} -eq ${err_count} ] && [ ! $((${err_count} - 1)) -lt 0 ] && err_count=$((${err_count} - 1)) diff_c=1
  171. [ ${err_c_cur} -ne ${err_count} ] && diff_c=$(( ${err_c_cur} - ${err_count} ))
  172. progress "Unbound" ${THRESHOLD} $(( ${THRESHOLD} - ${err_count} )) ${diff_c}
  173. if [[ $? == 10 ]]; then
  174. diff_c=0
  175. sleep 1
  176. else
  177. diff_c=0
  178. sleep $(( ( RANDOM % 60 ) + 20 ))
  179. fi
  180. done
  181. return 1
  182. }
  183. redis_checks() {
  184. err_count=0
  185. diff_c=0
  186. THRESHOLD=5
  187. # Reduce error count by 2 after restarting an unhealthy container
  188. trap "[ ${err_count} -gt 1 ] && err_count=$(( ${err_count} - 2 ))" USR1
  189. while [ ${err_count} -lt ${THRESHOLD} ]; do
  190. touch /tmp/redis-mailcow; echo "$(tail -50 /tmp/redis-mailcow)" > /tmp/redis-mailcow
  191. host_ip=$(get_container_ip redis-mailcow)
  192. err_c_cur=${err_count}
  193. /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} + $? ))
  194. [ ${err_c_cur} -eq ${err_count} ] && [ ! $((${err_count} - 1)) -lt 0 ] && err_count=$((${err_count} - 1)) diff_c=1
  195. [ ${err_c_cur} -ne ${err_count} ] && diff_c=$(( ${err_c_cur} - ${err_count} ))
  196. progress "Redis" ${THRESHOLD} $(( ${THRESHOLD} - ${err_count} )) ${diff_c}
  197. if [[ $? == 10 ]]; then
  198. diff_c=0
  199. sleep 1
  200. else
  201. diff_c=0
  202. sleep $(( ( RANDOM % 60 ) + 20 ))
  203. fi
  204. done
  205. return 1
  206. }
  207. mysql_checks() {
  208. err_count=0
  209. diff_c=0
  210. THRESHOLD=5
  211. # Reduce error count by 2 after restarting an unhealthy container
  212. trap "[ ${err_count} -gt 1 ] && err_count=$(( ${err_count} - 2 ))" USR1
  213. while [ ${err_count} -lt ${THRESHOLD} ]; do
  214. touch /tmp/mysql-mailcow; echo "$(tail -50 /tmp/mysql-mailcow)" > /tmp/mysql-mailcow
  215. host_ip=$(get_container_ip mysql-mailcow)
  216. err_c_cur=${err_count}
  217. /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} + $? ))
  218. /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} + $? ))
  219. [ ${err_c_cur} -eq ${err_count} ] && [ ! $((${err_count} - 1)) -lt 0 ] && err_count=$((${err_count} - 1)) diff_c=1
  220. [ ${err_c_cur} -ne ${err_count} ] && diff_c=$(( ${err_c_cur} - ${err_count} ))
  221. progress "MySQL/MariaDB" ${THRESHOLD} $(( ${THRESHOLD} - ${err_count} )) ${diff_c}
  222. if [[ $? == 10 ]]; then
  223. diff_c=0
  224. sleep 1
  225. else
  226. diff_c=0
  227. sleep $(( ( RANDOM % 60 ) + 20 ))
  228. fi
  229. done
  230. return 1
  231. }
  232. sogo_checks() {
  233. err_count=0
  234. diff_c=0
  235. THRESHOLD=5
  236. # Reduce error count by 2 after restarting an unhealthy container
  237. trap "[ ${err_count} -gt 1 ] && err_count=$(( ${err_count} - 2 ))" USR1
  238. while [ ${err_count} -lt ${THRESHOLD} ]; do
  239. touch /tmp/sogo-mailcow; echo "$(tail -50 /tmp/sogo-mailcow)" > /tmp/sogo-mailcow
  240. host_ip=$(get_container_ip sogo-mailcow)
  241. err_c_cur=${err_count}
  242. /usr/lib/nagios/plugins/check_http -4 -H ${host_ip} -u /SOGo.index/ -p 20000 -R "SOGo\.MainUI" 2>> /tmp/sogo-mailcow 1>&2; err_count=$(( ${err_count} + $? ))
  243. [ ${err_c_cur} -eq ${err_count} ] && [ ! $((${err_count} - 1)) -lt 0 ] && err_count=$((${err_count} - 1)) diff_c=1
  244. [ ${err_c_cur} -ne ${err_count} ] && diff_c=$(( ${err_c_cur} - ${err_count} ))
  245. progress "SOGo" ${THRESHOLD} $(( ${THRESHOLD} - ${err_count} )) ${diff_c}
  246. if [[ $? == 10 ]]; then
  247. diff_c=0
  248. sleep 1
  249. else
  250. diff_c=0
  251. sleep $(( ( RANDOM % 60 ) + 20 ))
  252. fi
  253. done
  254. return 1
  255. }
  256. postfix_checks() {
  257. err_count=0
  258. diff_c=0
  259. THRESHOLD=8
  260. # Reduce error count by 2 after restarting an unhealthy container
  261. trap "[ ${err_count} -gt 1 ] && err_count=$(( ${err_count} - 2 ))" USR1
  262. while [ ${err_count} -lt ${THRESHOLD} ]; do
  263. touch /tmp/postfix-mailcow; echo "$(tail -50 /tmp/postfix-mailcow)" > /tmp/postfix-mailcow
  264. host_ip=$(get_container_ip postfix-mailcow)
  265. err_c_cur=${err_count}
  266. /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} + $? ))
  267. /usr/lib/nagios/plugins/check_smtp -4 -H ${host_ip} -p 589 -S 2>> /tmp/postfix-mailcow 1>&2; err_count=$(( ${err_count} + $? ))
  268. [ ${err_c_cur} -eq ${err_count} ] && [ ! $((${err_count} - 1)) -lt 0 ] && err_count=$((${err_count} - 1)) diff_c=1
  269. [ ${err_c_cur} -ne ${err_count} ] && diff_c=$(( ${err_c_cur} - ${err_count} ))
  270. progress "Postfix" ${THRESHOLD} $(( ${THRESHOLD} - ${err_count} )) ${diff_c}
  271. if [[ $? == 10 ]]; then
  272. diff_c=0
  273. sleep 1
  274. else
  275. diff_c=0
  276. sleep $(( ( RANDOM % 60 ) + 20 ))
  277. fi
  278. done
  279. return 1
  280. }
  281. clamd_checks() {
  282. err_count=0
  283. diff_c=0
  284. THRESHOLD=15
  285. # Reduce error count by 2 after restarting an unhealthy container
  286. trap "[ ${err_count} -gt 1 ] && err_count=$(( ${err_count} - 2 ))" USR1
  287. while [ ${err_count} -lt ${THRESHOLD} ]; do
  288. touch /tmp/clamd-mailcow; echo "$(tail -50 /tmp/clamd-mailcow)" > /tmp/clamd-mailcow
  289. host_ip=$(get_container_ip clamd-mailcow)
  290. err_c_cur=${err_count}
  291. /usr/lib/nagios/plugins/check_clamd -4 -H ${host_ip} 2>> /tmp/clamd-mailcow 1>&2; err_count=$(( ${err_count} + $? ))
  292. [ ${err_c_cur} -eq ${err_count} ] && [ ! $((${err_count} - 1)) -lt 0 ] && err_count=$((${err_count} - 1)) diff_c=1
  293. [ ${err_c_cur} -ne ${err_count} ] && diff_c=$(( ${err_c_cur} - ${err_count} ))
  294. progress "Clamd" ${THRESHOLD} $(( ${THRESHOLD} - ${err_count} )) ${diff_c}
  295. if [[ $? == 10 ]]; then
  296. diff_c=0
  297. sleep 1
  298. else
  299. diff_c=0
  300. sleep $(( ( RANDOM % 120 ) + 20 ))
  301. fi
  302. done
  303. return 1
  304. }
  305. dovecot_checks() {
  306. err_count=0
  307. diff_c=0
  308. THRESHOLD=12
  309. # Reduce error count by 2 after restarting an unhealthy container
  310. trap "[ ${err_count} -gt 1 ] && err_count=$(( ${err_count} - 2 ))" USR1
  311. while [ ${err_count} -lt ${THRESHOLD} ]; do
  312. touch /tmp/dovecot-mailcow; echo "$(tail -50 /tmp/dovecot-mailcow)" > /tmp/dovecot-mailcow
  313. host_ip=$(get_container_ip dovecot-mailcow)
  314. err_c_cur=${err_count}
  315. /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} + $? ))
  316. /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} + $? ))
  317. /usr/lib/nagios/plugins/check_imap -4 -H ${host_ip} -p 143 -e "OK " 2>> /tmp/dovecot-mailcow 1>&2; err_count=$(( ${err_count} + $? ))
  318. /usr/lib/nagios/plugins/check_tcp -4 -H ${host_ip} -p 10001 -e "VERSION" 2>> /tmp/dovecot-mailcow 1>&2; err_count=$(( ${err_count} + $? ))
  319. /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} + $? ))
  320. [ ${err_c_cur} -eq ${err_count} ] && [ ! $((${err_count} - 1)) -lt 0 ] && err_count=$((${err_count} - 1)) diff_c=1
  321. [ ${err_c_cur} -ne ${err_count} ] && diff_c=$(( ${err_c_cur} - ${err_count} ))
  322. progress "Dovecot" ${THRESHOLD} $(( ${THRESHOLD} - ${err_count} )) ${diff_c}
  323. if [[ $? == 10 ]]; then
  324. diff_c=0
  325. sleep 1
  326. else
  327. diff_c=0
  328. sleep $(( ( RANDOM % 60 ) + 20 ))
  329. fi
  330. done
  331. return 1
  332. }
  333. phpfpm_checks() {
  334. err_count=0
  335. diff_c=0
  336. THRESHOLD=5
  337. # Reduce error count by 2 after restarting an unhealthy container
  338. trap "[ ${err_count} -gt 1 ] && err_count=$(( ${err_count} - 2 ))" USR1
  339. while [ ${err_count} -lt ${THRESHOLD} ]; do
  340. touch /tmp/php-fpm-mailcow; echo "$(tail -50 /tmp/php-fpm-mailcow)" > /tmp/php-fpm-mailcow
  341. host_ip=$(get_container_ip php-fpm-mailcow)
  342. err_c_cur=${err_count}
  343. /usr/lib/nagios/plugins/check_tcp -H ${host_ip} -p 9001 2>> /tmp/php-fpm-mailcow 1>&2; err_count=$(( ${err_count} + $? ))
  344. /usr/lib/nagios/plugins/check_tcp -H ${host_ip} -p 9002 2>> /tmp/php-fpm-mailcow 1>&2; err_count=$(( ${err_count} + $? ))
  345. [ ${err_c_cur} -eq ${err_count} ] && [ ! $((${err_count} - 1)) -lt 0 ] && err_count=$((${err_count} - 1)) diff_c=1
  346. [ ${err_c_cur} -ne ${err_count} ] && diff_c=$(( ${err_c_cur} - ${err_count} ))
  347. progress "PHP-FPM" ${THRESHOLD} $(( ${THRESHOLD} - ${err_count} )) ${diff_c}
  348. if [[ $? == 10 ]]; then
  349. diff_c=0
  350. sleep 1
  351. else
  352. diff_c=0
  353. sleep $(( ( RANDOM % 60 ) + 20 ))
  354. fi
  355. done
  356. return 1
  357. }
  358. ratelimit_checks() {
  359. err_count=0
  360. diff_c=0
  361. THRESHOLD=1
  362. RL_LOG_STATUS=$(redis-cli -h redis LRANGE RL_LOG 0 0 | jq .qid)
  363. # Reduce error count by 2 after restarting an unhealthy container
  364. trap "[ ${err_count} -gt 1 ] && err_count=$(( ${err_count} - 2 ))" USR1
  365. while [ ${err_count} -lt ${THRESHOLD} ]; do
  366. err_c_cur=${err_count}
  367. RL_LOG_STATUS_PREV=${RL_LOG_STATUS}
  368. RL_LOG_STATUS=$(redis-cli -h redis LRANGE RL_LOG 0 0 | jq .qid)
  369. if [[ ${RL_LOG_STATUS_PREV} != ${RL_LOG_STATUS} ]]; then
  370. err_count=$(( ${err_count} + 1 ))
  371. fi
  372. [ ${err_c_cur} -eq ${err_count} ] && [ ! $((${err_count} - 1)) -lt 0 ] && err_count=$((${err_count} - 1)) diff_c=1
  373. [ ${err_c_cur} -ne ${err_count} ] && diff_c=$(( ${err_c_cur} - ${err_count} ))
  374. progress "Ratelimit" ${THRESHOLD} $(( ${THRESHOLD} - ${err_count} )) ${diff_c}
  375. if [[ $? == 10 ]]; then
  376. diff_c=0
  377. sleep 1
  378. else
  379. diff_c=0
  380. sleep $(( ( RANDOM % 60 ) + 20 ))
  381. fi
  382. done
  383. return 1
  384. }
  385. fail2ban_checks() {
  386. err_count=0
  387. diff_c=0
  388. THRESHOLD=1
  389. F2B_LOG_STATUS=($(redis-cli -h redis-mailcow --raw HKEYS F2B_ACTIVE_BANS))
  390. F2B_RES=
  391. # Reduce error count by 2 after restarting an unhealthy container
  392. trap "[ ${err_count} -gt 1 ] && err_count=$(( ${err_count} - 2 ))" USR1
  393. while [ ${err_count} -lt ${THRESHOLD} ]; do
  394. err_c_cur=${err_count}
  395. F2B_LOG_STATUS_PREV=(${F2B_LOG_STATUS[@]})
  396. F2B_LOG_STATUS=($(redis-cli -h redis-mailcow --raw HKEYS F2B_ACTIVE_BANS))
  397. array_diff F2B_RES F2B_LOG_STATUS F2B_LOG_STATUS_PREV
  398. if [[ ! -z "${F2B_RES}" ]]; then
  399. err_count=$(( ${err_count} + 1 ))
  400. echo -n "${F2B_RES[@]}" | tr -cd "[a-fA-F0-9.:/] " | timeout 3s redis-cli -x -h redis-mailcow SET F2B_RES > /dev/null
  401. if [ $? -ne 0 ]; then
  402. redis-cli -x -h redis-mailcow DEL F2B_RES
  403. fi
  404. fi
  405. [ ${err_c_cur} -eq ${err_count} ] && [ ! $((${err_count} - 1)) -lt 0 ] && err_count=$((${err_count} - 1)) diff_c=1
  406. [ ${err_c_cur} -ne ${err_count} ] && diff_c=$(( ${err_c_cur} - ${err_count} ))
  407. progress "Fail2ban" ${THRESHOLD} $(( ${THRESHOLD} - ${err_count} )) ${diff_c}
  408. if [[ $? == 10 ]]; then
  409. diff_c=0
  410. sleep 1
  411. else
  412. diff_c=0
  413. sleep $(( ( RANDOM % 60 ) + 20 ))
  414. fi
  415. done
  416. return 1
  417. }
  418. acme_checks() {
  419. err_count=0
  420. diff_c=0
  421. THRESHOLD=1
  422. ACME_LOG_STATUS=$(redis-cli -h redis GET ACME_FAIL_TIME)
  423. if [[ -z "${ACME_LOG_STATUS}" ]]; then
  424. redis-cli -h redis SET ACME_FAIL_TIME 0
  425. ACME_LOG_STATUS=0
  426. fi
  427. # Reduce error count by 2 after restarting an unhealthy container
  428. trap "[ ${err_count} -gt 1 ] && err_count=$(( ${err_count} - 2 ))" USR1
  429. while [ ${err_count} -lt ${THRESHOLD} ]; do
  430. err_c_cur=${err_count}
  431. ACME_LOG_STATUS_PREV=${ACME_LOG_STATUS}
  432. ACME_LOG_STATUS=$(redis-cli -h redis GET ACME_FAIL_TIME)
  433. if [[ ${ACME_LOG_STATUS_PREV} != ${ACME_LOG_STATUS} ]]; then
  434. err_count=$(( ${err_count} + 1 ))
  435. fi
  436. [ ${err_c_cur} -eq ${err_count} ] && [ ! $((${err_count} - 1)) -lt 0 ] && err_count=$((${err_count} - 1)) diff_c=1
  437. [ ${err_c_cur} -ne ${err_count} ] && diff_c=$(( ${err_c_cur} - ${err_count} ))
  438. progress "ACME" ${THRESHOLD} $(( ${THRESHOLD} - ${err_count} )) ${diff_c}
  439. if [[ $? == 10 ]]; then
  440. diff_c=0
  441. sleep 1
  442. else
  443. diff_c=0
  444. sleep $(( ( RANDOM % 60 ) + 20 ))
  445. fi
  446. done
  447. return 1
  448. }
  449. ipv6nat_checks() {
  450. err_count=0
  451. diff_c=0
  452. THRESHOLD=1
  453. # Reduce error count by 2 after restarting an unhealthy container
  454. trap "[ ${err_count} -gt 1 ] && err_count=$(( ${err_count} - 2 ))" USR1
  455. while [ ${err_count} -lt ${THRESHOLD} ]; do
  456. err_c_cur=${err_count}
  457. CONTAINERS=$(curl --silent --insecure https://dockerapi/containers/json)
  458. IPV6NAT_CONTAINER_ID=$(echo ${CONTAINERS} | jq -r ".[] | {name: .Config.Labels[\"com.docker.compose.service\"], id: .Id}" | jq -rc "select( .name | tostring | contains(\"ipv6nat-mailcow\")) | .id")
  459. if [[ ! -z ${IPV6NAT_CONTAINER_ID} ]]; then
  460. LATEST_STARTED="$(echo ${CONTAINERS} | jq -r ".[] | {name: .Config.Labels[\"com.docker.compose.service\"], StartedAt: .State.StartedAt}" | jq -rc "select( .name | tostring | contains(\"ipv6nat-mailcow\") | not)" | jq -rc .StartedAt | xargs -n1 date +%s -d | sort | tail -n1)"
  461. LATEST_IPV6NAT="$(echo ${CONTAINERS} | jq -r ".[] | {name: .Config.Labels[\"com.docker.compose.service\"], StartedAt: .State.StartedAt}" | jq -rc "select( .name | tostring | contains(\"ipv6nat-mailcow\"))" | jq -rc .StartedAt | xargs -n1 date +%s -d | sort | tail -n1)"
  462. DIFFERENCE_START_TIME=$(expr ${LATEST_IPV6NAT} - ${LATEST_STARTED} 2>/dev/null)
  463. if [[ "${DIFFERENCE_START_TIME}" -lt 30 ]]; then
  464. err_count=$(( ${err_count} + 1 ))
  465. fi
  466. fi
  467. [ ${err_c_cur} -eq ${err_count} ] && [ ! $((${err_count} - 1)) -lt 0 ] && err_count=$((${err_count} - 1)) diff_c=1
  468. [ ${err_c_cur} -ne ${err_count} ] && diff_c=$(( ${err_c_cur} - ${err_count} ))
  469. progress "IPv6 NAT" ${THRESHOLD} $(( ${THRESHOLD} - ${err_count} )) ${diff_c}
  470. if [[ $? == 10 ]]; then
  471. diff_c=0
  472. sleep 30
  473. else
  474. diff_c=0
  475. sleep 300
  476. fi
  477. done
  478. return 1
  479. }
  480. rspamd_checks() {
  481. err_count=0
  482. diff_c=0
  483. THRESHOLD=5
  484. # Reduce error count by 2 after restarting an unhealthy container
  485. trap "[ ${err_count} -gt 1 ] && err_count=$(( ${err_count} - 2 ))" USR1
  486. while [ ${err_count} -lt ${THRESHOLD} ]; do
  487. touch /tmp/rspamd-mailcow; echo "$(tail -50 /tmp/rspamd-mailcow)" > /tmp/rspamd-mailcow
  488. host_ip=$(get_container_ip rspamd-mailcow)
  489. err_c_cur=${err_count}
  490. SCORE=$(echo 'To: null@localhost
  491. From: watchdog@localhost
  492. Empty
  493. ' | usr/bin/curl -s --data-binary @- --unix-socket /var/lib/rspamd/rspamd.sock http://rspamd/scan | jq -rc .required_score)
  494. if [[ ${SCORE} != "9999" ]]; then
  495. echo "Rspamd settings check failed" 2>> /tmp/rspamd-mailcow 1>&2
  496. err_count=$(( ${err_count} + 1))
  497. else
  498. echo "Rspamd settings check succeeded" 2>> /tmp/rspamd-mailcow 1>&2
  499. fi
  500. [ ${err_c_cur} -eq ${err_count} ] && [ ! $((${err_count} - 1)) -lt 0 ] && err_count=$((${err_count} - 1)) diff_c=1
  501. [ ${err_c_cur} -ne ${err_count} ] && diff_c=$(( ${err_c_cur} - ${err_count} ))
  502. progress "Rspamd" ${THRESHOLD} $(( ${THRESHOLD} - ${err_count} )) ${diff_c}
  503. if [[ $? == 10 ]]; then
  504. diff_c=0
  505. sleep 1
  506. else
  507. diff_c=0
  508. sleep $(( ( RANDOM % 60 ) + 20 ))
  509. fi
  510. done
  511. return 1
  512. }
  513. olefy_checks() {
  514. err_count=0
  515. diff_c=0
  516. THRESHOLD=5
  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/olefy-mailcow; echo "$(tail -50 /tmp/olefy-mailcow)" > /tmp/olefy-mailcow
  521. host_ip=$(get_container_ip olefy-mailcow)
  522. err_c_cur=${err_count}
  523. /usr/lib/nagios/plugins/check_tcp -4 -H ${host_ip} -p 10055 2>> /tmp/olefy-mailcow 1>&2; err_count=$(( ${err_count} + $? ))
  524. [ ${err_c_cur} -eq ${err_count} ] && [ ! $((${err_count} - 1)) -lt 0 ] && err_count=$((${err_count} - 1)) diff_c=1
  525. [ ${err_c_cur} -ne ${err_count} ] && diff_c=$(( ${err_c_cur} - ${err_count} ))
  526. progress "Olefy" ${THRESHOLD} $(( ${THRESHOLD} - ${err_count} )) ${diff_c}
  527. if [[ $? == 10 ]]; then
  528. diff_c=0
  529. sleep 1
  530. else
  531. diff_c=0
  532. sleep $(( ( RANDOM % 60 ) + 20 ))
  533. fi
  534. done
  535. return 1
  536. }
  537. # Notify about start
  538. if [[ ! -z ${WATCHDOG_NOTIFY_EMAIL} ]]; then
  539. mail_error "watchdog-mailcow" "Watchdog started monitoring mailcow."
  540. fi
  541. # Create watchdog agents
  542. (
  543. while true; do
  544. if ! nginx_checks; then
  545. log_msg "Nginx hit error limit"
  546. echo nginx-mailcow > /tmp/com_pipe
  547. fi
  548. done
  549. ) &
  550. PID=$!
  551. echo "Spawned nginx_checks with PID ${PID}"
  552. BACKGROUND_TASKS+=(${PID})
  553. (
  554. while true; do
  555. if ! mysql_checks; then
  556. log_msg "MySQL hit error limit"
  557. echo mysql-mailcow > /tmp/com_pipe
  558. fi
  559. done
  560. ) &
  561. PID=$!
  562. echo "Spawned mysql_checks with PID ${PID}"
  563. BACKGROUND_TASKS+=(${PID})
  564. (
  565. while true; do
  566. if ! redis_checks; then
  567. log_msg "Redis hit error limit"
  568. echo redis-mailcow > /tmp/com_pipe
  569. fi
  570. done
  571. ) &
  572. PID=$!
  573. echo "Spawned redis_checks with PID ${PID}"
  574. BACKGROUND_TASKS+=(${PID})
  575. (
  576. while true; do
  577. if ! phpfpm_checks; then
  578. log_msg "PHP-FPM hit error limit"
  579. echo php-fpm-mailcow > /tmp/com_pipe
  580. fi
  581. done
  582. ) &
  583. PID=$!
  584. echo "Spawned phpfpm_checks with PID ${PID}"
  585. BACKGROUND_TASKS+=(${PID})
  586. (
  587. while true; do
  588. if ! sogo_checks; then
  589. log_msg "SOGo hit error limit"
  590. echo sogo-mailcow > /tmp/com_pipe
  591. fi
  592. done
  593. ) &
  594. PID=$!
  595. echo "Spawned sogo_checks with PID ${PID}"
  596. BACKGROUND_TASKS+=(${PID})
  597. if [ ${CHECK_UNBOUND} -eq 1 ]; then
  598. (
  599. while true; do
  600. if ! unbound_checks; then
  601. log_msg "Unbound hit error limit"
  602. echo unbound-mailcow > /tmp/com_pipe
  603. fi
  604. done
  605. ) &
  606. PID=$!
  607. echo "Spawned unbound_checks with PID ${PID}"
  608. BACKGROUND_TASKS+=(${PID})
  609. fi
  610. if [[ "${SKIP_CLAMD}" =~ ^([nN][oO]|[nN])+$ ]]; then
  611. (
  612. while true; do
  613. if ! clamd_checks; then
  614. log_msg "Clamd hit error limit"
  615. echo clamd-mailcow > /tmp/com_pipe
  616. fi
  617. done
  618. ) &
  619. PID=$!
  620. echo "Spawned clamd_checks with PID ${PID}"
  621. BACKGROUND_TASKS+=(${PID})
  622. fi
  623. (
  624. while true; do
  625. if ! postfix_checks; then
  626. log_msg "Postfix hit error limit"
  627. echo postfix-mailcow > /tmp/com_pipe
  628. fi
  629. done
  630. ) &
  631. PID=$!
  632. echo "Spawned postfix_checks with PID ${PID}"
  633. BACKGROUND_TASKS+=(${PID})
  634. (
  635. while true; do
  636. if ! dovecot_checks; then
  637. log_msg "Dovecot hit error limit"
  638. echo dovecot-mailcow > /tmp/com_pipe
  639. fi
  640. done
  641. ) &
  642. PID=$!
  643. echo "Spawned dovecot_checks with PID ${PID}"
  644. BACKGROUND_TASKS+=(${PID})
  645. (
  646. while true; do
  647. if ! rspamd_checks; then
  648. log_msg "Rspamd hit error limit"
  649. echo rspamd-mailcow > /tmp/com_pipe
  650. fi
  651. done
  652. ) &
  653. PID=$!
  654. echo "Spawned rspamd_checks with PID ${PID}"
  655. BACKGROUND_TASKS+=(${PID})
  656. (
  657. while true; do
  658. if ! ratelimit_checks; then
  659. log_msg "Ratelimit hit error limit"
  660. echo ratelimit > /tmp/com_pipe
  661. fi
  662. done
  663. ) &
  664. PID=$!
  665. echo "Spawned ratelimit_checks with PID ${PID}"
  666. BACKGROUND_TASKS+=(${PID})
  667. (
  668. while true; do
  669. if ! fail2ban_checks; then
  670. log_msg "Fail2ban hit error limit"
  671. echo fail2ban > /tmp/com_pipe
  672. fi
  673. done
  674. ) &
  675. PID=$!
  676. echo "Spawned fail2ban_checks with PID ${PID}"
  677. BACKGROUND_TASKS+=(${PID})
  678. #(
  679. #while true; do
  680. # if ! olefy_checks; then
  681. # log_msg "Olefy hit error limit"
  682. # echo olefy-mailcow > /tmp/com_pipe
  683. # fi
  684. #done
  685. #) &
  686. #PID=$!
  687. #echo "Spawned olefy_checks with PID ${PID}"
  688. #BACKGROUND_TASKS+=(${PID})
  689. (
  690. while true; do
  691. if ! acme_checks; then
  692. log_msg "ACME client hit error limit"
  693. echo acme-mailcow > /tmp/com_pipe
  694. fi
  695. done
  696. ) &
  697. PID=$!
  698. echo "Spawned acme_checks with PID ${PID}"
  699. BACKGROUND_TASKS+=(${PID})
  700. (
  701. while true; do
  702. if ! ipv6nat_checks; then
  703. log_msg "IPv6 NAT warning: ipv6nat-mailcow container was not started at least 30s after siblings (not an error)"
  704. echo ipv6nat-mailcow > /tmp/com_pipe
  705. fi
  706. done
  707. ) &
  708. PID=$!
  709. echo "Spawned ipv6nat_checks with PID ${PID}"
  710. BACKGROUND_TASKS+=(${PID})
  711. # Monitor watchdog agents, stop script when agents fails and wait for respawn by Docker (restart:always:n)
  712. (
  713. while true; do
  714. for bg_task in ${BACKGROUND_TASKS[*]}; do
  715. if ! kill -0 ${bg_task} 1>&2; then
  716. log_msg "Worker ${bg_task} died, stopping watchdog and waiting for respawn..."
  717. kill -TERM 1
  718. fi
  719. sleep 10
  720. done
  721. done
  722. ) &
  723. # Monitor dockerapi
  724. (
  725. while true; do
  726. while nc -z dockerapi 443; do
  727. sleep 3
  728. done
  729. log_msg "Cannot find dockerapi-mailcow, waiting to recover..."
  730. kill -STOP ${BACKGROUND_TASKS[*]}
  731. until nc -z dockerapi 443; do
  732. sleep 3
  733. done
  734. kill -CONT ${BACKGROUND_TASKS[*]}
  735. kill -USR1 ${BACKGROUND_TASKS[*]}
  736. done
  737. ) &
  738. # Actions when threshold limit is reached
  739. while true; do
  740. CONTAINER_ID=
  741. HAS_INITDB=
  742. read com_pipe_answer </tmp/com_pipe
  743. if [ -s "/tmp/${com_pipe_answer}" ]; then
  744. cat "/tmp/${com_pipe_answer}"
  745. fi
  746. if [[ ${com_pipe_answer} == "ratelimit" ]]; then
  747. log_msg "At least one ratelimit was applied"
  748. [[ ! -z ${WATCHDOG_NOTIFY_EMAIL} ]] && mail_error "${com_pipe_answer}" "Please see mailcow UI logs for further information."
  749. elif [[ ${com_pipe_answer} == "acme-mailcow" ]]; then
  750. log_msg "acme-mailcow did not complete successfully"
  751. [[ ! -z ${WATCHDOG_NOTIFY_EMAIL} ]] && mail_error "${com_pipe_answer}" "Please check acme-mailcow for further information."
  752. elif [[ ${com_pipe_answer} == "fail2ban" ]]; then
  753. F2B_RES=($(timeout 4s redis-cli -h redis-mailcow --raw GET F2B_RES 2> /dev/null))
  754. if [[ ! -z "${F2B_RES}" ]]; then
  755. redis-cli -h redis-mailcow DEL F2B_RES > /dev/null
  756. host=
  757. for host in "${F2B_RES[@]}"; do
  758. log_msg "Banned ${host}"
  759. rm /tmp/fail2ban 2> /dev/null
  760. timeout 2s whois "${host}" > /tmp/fail2ban
  761. [[ ! -z ${WATCHDOG_NOTIFY_EMAIL} ]] && [[ ${WATCHDOG_NOTIFY_BAN} =~ ^([yY][eE][sS]|[yY])+$ ]] && mail_error "${com_pipe_answer}" "IP ban: ${host}"
  762. done
  763. fi
  764. elif [[ ${com_pipe_answer} =~ .+-mailcow ]]; then
  765. kill -STOP ${BACKGROUND_TASKS[*]}
  766. sleep 10
  767. CONTAINER_ID=$(curl --silent --insecure https://dockerapi/containers/json | jq -r ".[] | {name: .Config.Labels[\"com.docker.compose.service\"], id: .Id}" | jq -rc "select( .name | tostring | contains(\"${com_pipe_answer}\")) | .id")
  768. if [[ ! -z ${CONTAINER_ID} ]]; then
  769. if [[ "${com_pipe_answer}" == "php-fpm-mailcow" ]]; then
  770. 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)
  771. fi
  772. S_RUNNING=$(($(date +%s) - $(curl --silent --insecure https://dockerapi/containers/${CONTAINER_ID}/json | jq .State.StartedAt | xargs -n1 date +%s -d)))
  773. if [ ${S_RUNNING} -lt 360 ]; then
  774. log_msg "Container is running for less than 360 seconds, skipping action..."
  775. elif [[ ! -z ${HAS_INITDB} ]]; then
  776. log_msg "Database is being initialized by php-fpm-mailcow, not restarting but delaying checks for a minute..."
  777. sleep 60
  778. else
  779. log_msg "Sending restart command to ${CONTAINER_ID}..."
  780. curl --silent --insecure -XPOST https://dockerapi/containers/${CONTAINER_ID}/restart
  781. if [[ ${com_pipe_answer} != "ipv6nat-mailcow" ]]; then
  782. [[ ! -z ${WATCHDOG_NOTIFY_EMAIL} ]] && mail_error "${com_pipe_answer}"
  783. fi
  784. log_msg "Wait for restarted container to settle and continue watching..."
  785. sleep 35
  786. fi
  787. fi
  788. kill -CONT ${BACKGROUND_TASKS[*]}
  789. sleep 1
  790. kill -USR1 ${BACKGROUND_TASKS[*]}
  791. fi
  792. done