watchdog.sh 40 KB

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