2
0

watchdog.sh 36 KB

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