watchdog.sh 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397
  1. #!/bin/bash
  2. trap "exit" INT TERM
  3. trap "kill 0" EXIT
  4. # Prepare
  5. BACKGROUND_TASKS=()
  6. if [[ "${USE_WATCHDOG}" =~ ^([nN][oO]|[nN])+$ ]]; then
  7. echo -e "$(date) - USE_WATCHDOG=n, skipping watchdog..."
  8. sleep 365d
  9. exec $(readlink -f "$0")
  10. fi
  11. # Checks pipe their corresponding container name in this pipe
  12. if [[ ! -p /tmp/com_pipe ]]; then
  13. mkfifo /tmp/com_pipe
  14. fi
  15. # Common functions
  16. progress() {
  17. SERVICE=${1}
  18. TOTAL=${2}
  19. CURRENT=${3}
  20. DIFF=${4}
  21. [[ -z ${DIFF} ]] && DIFF=0
  22. [[ -z ${TOTAL} || -z ${CURRENT} ]] && return
  23. [[ ${CURRENT} -gt ${TOTAL} ]] && return
  24. [[ ${CURRENT} -lt 0 ]] && CURRENT=0
  25. PERCENT=$(( 200 * ${CURRENT} / ${TOTAL} % 2 + 100 * ${CURRENT} / ${TOTAL} ))
  26. echo -ne "$(date) - ${SERVICE} health level: \e[7m${PERCENT}%\e[0m (${CURRENT}/${TOTAL}), health trend: "
  27. [[ ${DIFF} =~ ^-[1-9] ]] && echo -en '[\e[41m \e[0m] ' || echo -en '[\e[42m \e[0m] '
  28. echo "(${DIFF})"
  29. }
  30. log_to_redis() {
  31. redis-cli -h redis LPUSH WATCHDOG_LOG "{\"time\":\"$(date +%s)\",\"message\":\"$(printf '%s' "${1}")\"}"
  32. }
  33. function mail_error() {
  34. [[ -z ${1} ]] && return 1
  35. [[ -z ${2} ]] && return 2
  36. RCPT_DOMAIN=$(echo ${1} | awk -F @ {'print $NF'})
  37. RCPT_MX=$(dig +short ${RCPT_DOMAIN} mx | sort -n | awk '{print $2; exit}')
  38. if [[ -z ${RCPT_MX} ]]; then
  39. log_to_redis "Cannot determine MX for ${1}, skipping email notification..."
  40. echo "Cannot determine MX for ${1}"
  41. return 1
  42. fi
  43. ./smtp-cli --missing-modules-ok \
  44. --subject="Watchdog: ${2} service hit the error rate limit" \
  45. --body-plain="Service was restarted, please check your mailcow installation." \
  46. --to=${1} \
  47. --from="watchdog@${MAILCOW_HOSTNAME}" \
  48. --server="${RCPT_MX}" \
  49. --hello-host=${MAILCOW_HOSTNAME}
  50. }
  51. get_container_ip() {
  52. # ${1} is container
  53. CONTAINER_ID=
  54. CONTAINER_IP=
  55. LOOP_C=1
  56. until [[ ${CONTAINER_IP} =~ ^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}$ ]] || [[ ${LOOP_C} -gt 5 ]]; do
  57. sleep 1
  58. CONTAINER_ID=$(curl --silent http://dockerapi:8080/containers/json | jq -r ".[] | {name: .Config.Labels[\"com.docker.compose.service\"], id: .Id}" | jq -rc "select( .name | tostring | contains(\"${1}\")) | .id")
  59. if [[ ! -z ${CONTAINER_ID} ]]; then
  60. CONTAINER_IP=$(curl --silent http://dockerapi:8080/containers/${CONTAINER_ID}/json | jq -r '.NetworkSettings.Networks[].IPAddress')
  61. fi
  62. LOOP_C=$((LOOP_C + 1))
  63. done
  64. [[ ${LOOP_C} -gt 5 ]] && echo 240.0.0.0 || echo ${CONTAINER_IP}
  65. }
  66. # Check functions
  67. nginx_checks() {
  68. err_count=0
  69. diff_c=0
  70. THRESHOLD=16
  71. # Reduce error count by 2 after restarting an unhealthy container
  72. trap "[ ${err_count} -gt 1 ] && err_count=$(( ${err_count} - 2 ))" USR1
  73. while [ ${err_count} -lt ${THRESHOLD} ]; do
  74. host_ip=$(get_container_ip nginx-mailcow)
  75. err_c_cur=${err_count}
  76. /usr/lib/nagios/plugins/check_ping -4 -H ${host_ip} -w 2000,10% -c 4000,100% -p2 1>&2; err_count=$(( ${err_count} + $? ))
  77. /usr/lib/nagios/plugins/check_http -4 -H ${host_ip} -u / -p 8081 1>&2; err_count=$(( ${err_count} + $? ))
  78. [ ${err_c_cur} -eq ${err_count} ] && [ ! $((${err_count} - 1)) -lt 0 ] && err_count=$((${err_count} - 1)) diff_c=1
  79. [ ${err_c_cur} -ne ${err_count} ] && diff_c=$(( ${err_c_cur} - ${err_count} ))
  80. progress "Nginx" ${THRESHOLD} $(( ${THRESHOLD} - ${err_count} )) ${diff_c}
  81. diff_c=0
  82. sleep $(( ( RANDOM % 30 ) + 10 ))
  83. done
  84. return 1
  85. }
  86. mysql_checks() {
  87. err_count=0
  88. diff_c=0
  89. THRESHOLD=12
  90. # Reduce error count by 2 after restarting an unhealthy container
  91. trap "[ ${err_count} -gt 1 ] && err_count=$(( ${err_count} - 2 ))" USR1
  92. while [ ${err_count} -lt ${THRESHOLD} ]; do
  93. host_ip=$(get_container_ip mysql-mailcow)
  94. err_c_cur=${err_count}
  95. /usr/lib/nagios/plugins/check_mysql -H ${host_ip} -P 3306 -u ${DBUSER} -p ${DBPASS} -d ${DBNAME} 1>&2; err_count=$(( ${err_count} + $? ))
  96. /usr/lib/nagios/plugins/check_mysql_query -H ${host_ip} -P 3306 -u ${DBUSER} -p ${DBPASS} -d ${DBNAME} -q "SELECT COUNT(*) FROM information_schema.tables" 1>&2; err_count=$(( ${err_count} + $? ))
  97. [ ${err_c_cur} -eq ${err_count} ] && [ ! $((${err_count} - 1)) -lt 0 ] && err_count=$((${err_count} - 1)) diff_c=1
  98. [ ${err_c_cur} -ne ${err_count} ] && diff_c=$(( ${err_c_cur} - ${err_count} ))
  99. progress "MySQL/MariaDB" ${THRESHOLD} $(( ${THRESHOLD} - ${err_count} )) ${diff_c}
  100. diff_c=0
  101. sleep $(( ( RANDOM % 30 ) + 10 ))
  102. done
  103. return 1
  104. }
  105. sogo_checks() {
  106. err_count=0
  107. diff_c=0
  108. THRESHOLD=20
  109. # Reduce error count by 2 after restarting an unhealthy container
  110. trap "[ ${err_count} -gt 1 ] && err_count=$(( ${err_count} - 2 ))" USR1
  111. while [ ${err_count} -lt ${THRESHOLD} ]; do
  112. host_ip=$(get_container_ip sogo-mailcow)
  113. err_c_cur=${err_count}
  114. /usr/lib/nagios/plugins/check_http -4 -H ${host_ip} -u /WebServerResources/css/theme-default.css -p 9192 -R md-default-theme 1>&2; err_count=$(( ${err_count} + $? ))
  115. /usr/lib/nagios/plugins/check_http -4 -H ${host_ip} -u /SOGo.index/ -p 20000 -R "SOGo\sGroupware" 1>&2; err_count=$(( ${err_count} + $? ))
  116. [ ${err_c_cur} -eq ${err_count} ] && [ ! $((${err_count} - 1)) -lt 0 ] && err_count=$((${err_count} - 1)) diff_c=1
  117. [ ${err_c_cur} -ne ${err_count} ] && diff_c=$(( ${err_c_cur} - ${err_count} ))
  118. progress "SOGo" ${THRESHOLD} $(( ${THRESHOLD} - ${err_count} )) ${diff_c}
  119. diff_c=0
  120. sleep $(( ( RANDOM % 30 ) + 10 ))
  121. done
  122. return 1
  123. }
  124. postfix_checks() {
  125. err_count=0
  126. diff_c=0
  127. THRESHOLD=16
  128. # Reduce error count by 2 after restarting an unhealthy container
  129. trap "[ ${err_count} -gt 1 ] && err_count=$(( ${err_count} - 2 ))" USR1
  130. while [ ${err_count} -lt ${THRESHOLD} ]; do
  131. host_ip=$(get_container_ip postfix-mailcow)
  132. err_c_cur=${err_count}
  133. /usr/lib/nagios/plugins/check_smtp -4 -H ${host_ip} -p 589 -f watchdog -C "RCPT TO:null@localhost" -C DATA -C . -R 250 1>&2; err_count=$(( ${err_count} + $? ))
  134. /usr/lib/nagios/plugins/check_smtp -4 -H ${host_ip} -p 589 -S 1>&2; err_count=$(( ${err_count} + $? ))
  135. [ ${err_c_cur} -eq ${err_count} ] && [ ! $((${err_count} - 1)) -lt 0 ] && err_count=$((${err_count} - 1)) diff_c=1
  136. [ ${err_c_cur} -ne ${err_count} ] && diff_c=$(( ${err_c_cur} - ${err_count} ))
  137. progress "Postfix" ${THRESHOLD} $(( ${THRESHOLD} - ${err_count} )) ${diff_c}
  138. diff_c=0
  139. sleep $(( ( RANDOM % 30 ) + 10 ))
  140. done
  141. return 1
  142. }
  143. dovecot_checks() {
  144. err_count=0
  145. diff_c=0
  146. THRESHOLD=24
  147. # Reduce error count by 2 after restarting an unhealthy container
  148. trap "[ ${err_count} -gt 1 ] && err_count=$(( ${err_count} - 2 ))" USR1
  149. while [ ${err_count} -lt ${THRESHOLD} ]; do
  150. host_ip=$(get_container_ip dovecot-mailcow)
  151. err_c_cur=${err_count}
  152. /usr/lib/nagios/plugins/check_smtp -4 -H ${host_ip} -p 24 -f "watchdog" -C "RCPT TO:<watchdog@invalid>" -L -R "User doesn't exist" 1>&2; err_count=$(( ${err_count} + $? ))
  153. /usr/lib/nagios/plugins/check_imap -4 -H ${host_ip} -p 993 -S -e "OK " 1>&2; err_count=$(( ${err_count} + $? ))
  154. /usr/lib/nagios/plugins/check_imap -4 -H ${host_ip} -p 143 -e "OK " 1>&2; err_count=$(( ${err_count} + $? ))
  155. /usr/lib/nagios/plugins/check_tcp -4 -H ${host_ip} -p 10001 -e "VERSION" 1>&2; err_count=$(( ${err_count} + $? ))
  156. /usr/lib/nagios/plugins/check_tcp -4 -H ${host_ip} -p 4190 -e "Dovecot ready" 1>&2; err_count=$(( ${err_count} + $? ))
  157. [ ${err_c_cur} -eq ${err_count} ] && [ ! $((${err_count} - 1)) -lt 0 ] && err_count=$((${err_count} - 1)) diff_c=1
  158. [ ${err_c_cur} -ne ${err_count} ] && diff_c=$(( ${err_c_cur} - ${err_count} ))
  159. progress "Dovecot" ${THRESHOLD} $(( ${THRESHOLD} - ${err_count} )) ${diff_c}
  160. diff_c=0
  161. sleep $(( ( RANDOM % 30 ) + 10 ))
  162. done
  163. return 1
  164. }
  165. phpfpm_checks() {
  166. err_count=0
  167. diff_c=0
  168. THRESHOLD=10
  169. # Reduce error count by 2 after restarting an unhealthy container
  170. trap "[ ${err_count} -gt 1 ] && err_count=$(( ${err_count} - 2 ))" USR1
  171. while [ ${err_count} -lt ${THRESHOLD} ]; do
  172. host_ip=$(get_container_ip php-fpm-mailcow)
  173. err_c_cur=${err_count}
  174. cgi-fcgi -bind -connect ${host_ip}:9000 | grep "Content-type" 1>&2; err_count=$(( ${err_count} + ($? * 2)))
  175. /usr/lib/nagios/plugins/check_ping -4 -H ${host_ip} -w 2000,10% -c 4000,100% -p2 1>&2; err_count=$(( ${err_count} + $? ))
  176. [ ${err_c_cur} -eq ${err_count} ] && [ ! $((${err_count} - 1)) -lt 0 ] && err_count=$((${err_count} - 1)) diff_c=1
  177. [ ${err_c_cur} -ne ${err_count} ] && diff_c=$(( ${err_c_cur} - ${err_count} ))
  178. progress "PHP-FPM" ${THRESHOLD} $(( ${THRESHOLD} - ${err_count} )) ${diff_c}
  179. diff_c=0
  180. sleep $(( ( RANDOM % 30 ) + 10 ))
  181. done
  182. return 1
  183. }
  184. rspamd_checks() {
  185. err_count=0
  186. diff_c=0
  187. THRESHOLD=10
  188. # Reduce error count by 2 after restarting an unhealthy container
  189. trap "[ ${err_count} -gt 1 ] && err_count=$(( ${err_count} - 2 ))" USR1
  190. while [ ${err_count} -lt ${THRESHOLD} ]; do
  191. host_ip=$(get_container_ip rspamd-mailcow)
  192. err_c_cur=${err_count}
  193. SCORE=$(curl --silent ${host_ip}:11333/scan -d '
  194. To: null@localhost
  195. From: watchdog@localhost
  196. Empty
  197. ' | jq -rc .required_score)
  198. if [[ ${SCORE} != "9999" ]]; then
  199. echo "Rspamd settings check failed" 1>&2
  200. err_count=$(( ${err_count} + 1))
  201. else
  202. echo "Rspamd settings check succeeded" 1>&2
  203. fi
  204. /usr/lib/nagios/plugins/check_ping -4 -H ${host_ip} -w 2000,10% -c 4000,100% -p2 1>&2; err_count=$(( ${err_count} + $? ))
  205. [ ${err_c_cur} -eq ${err_count} ] && [ ! $((${err_count} - 1)) -lt 0 ] && err_count=$((${err_count} - 1)) diff_c=1
  206. [ ${err_c_cur} -ne ${err_count} ] && diff_c=$(( ${err_c_cur} - ${err_count} ))
  207. progress "Rspamd" ${THRESHOLD} $(( ${THRESHOLD} - ${err_count} )) ${diff_c}
  208. diff_c=0
  209. sleep $(( ( RANDOM % 30 ) + 10 ))
  210. done
  211. return 1
  212. }
  213. dns_checks() {
  214. err_count=0
  215. diff_c=0
  216. THRESHOLD=28
  217. # Reduce error count by 2 after restarting an unhealthy container
  218. trap "[ ${err_count} -gt 1 ] && err_count=$(( ${err_count} - 2 ))" USR1
  219. while [ ${err_count} -lt ${THRESHOLD} ]; do
  220. host_ip=$(get_container_ip unbound-mailcow)
  221. err_c_cur=${err_count}
  222. /usr/lib/nagios/plugins/check_dns -H google.com 1>&2; err_count=$(( ${err_count} + ($? * 2)))
  223. /usr/lib/nagios/plugins/check_dns -s ${host_ip} -H google.com 1>&2; err_count=$(( ${err_count} + ($? * 2)))
  224. dig +dnssec org. @${host_ip} | grep -E 'flags:.+ad' 1>&2; err_count=$(( ${err_count} + ($? * 2)))
  225. [ ${err_c_cur} -eq ${err_count} ] && [ ! $((${err_count} - 1)) -lt 0 ] && err_count=$((${err_count} - 1)) diff_c=1
  226. [ ${err_c_cur} -ne ${err_count} ] && diff_c=$(( ${err_c_cur} - ${err_count} ))
  227. progress "Unbound" ${THRESHOLD} $(( ${THRESHOLD} - ${err_count} )) ${diff_c}
  228. diff_c=0
  229. sleep $(( ( RANDOM % 30 ) + 10 ))
  230. done
  231. return 1
  232. }
  233. # Create watchdog agents
  234. (
  235. while true; do
  236. if ! nginx_checks; then
  237. log_to_redis "Nginx hit error limit"
  238. [[ ! -z ${WATCHDOG_NOTIFY_EMAIL} ]] && mail_error "${WATCHDOG_NOTIFY_EMAIL}" "nginx-mailcow"
  239. echo -e "\e[31m$(date) - Nginx hit error limit\e[0m"
  240. echo nginx-mailcow > /tmp/com_pipe
  241. fi
  242. done
  243. ) &
  244. BACKGROUND_TASKS+=($!)
  245. (
  246. while true; do
  247. if ! mysql_checks; then
  248. log_to_redis "MySQL hit error limit"
  249. [[ ! -z ${WATCHDOG_NOTIFY_EMAIL} ]] && mail_error "${WATCHDOG_NOTIFY_EMAIL}" "mysql-mailcow"
  250. echo -e "\e[31m$(date) - MySQL hit error limit\e[0m"
  251. echo mysql-mailcow > /tmp/com_pipe
  252. fi
  253. done
  254. ) &
  255. BACKGROUND_TASKS+=($!)
  256. (
  257. while true; do
  258. if ! phpfpm_checks; then
  259. log_to_redis "PHP-FPM hit error limit"
  260. [[ ! -z ${WATCHDOG_NOTIFY_EMAIL} ]] && mail_error "${WATCHDOG_NOTIFY_EMAIL}" "php-fpm-mailcow"
  261. echo -e "\e[31m$(date) - PHP-FPM hit error limit\e[0m"
  262. echo php-fpm-mailcow > /tmp/com_pipe
  263. fi
  264. done
  265. ) &
  266. BACKGROUND_TASKS+=($!)
  267. (
  268. while true; do
  269. if ! sogo_checks; then
  270. log_to_redis "SOGo hit error limit"
  271. [[ ! -z ${WATCHDOG_NOTIFY_EMAIL} ]] && mail_error "${WATCHDOG_NOTIFY_EMAIL}" "sogo-mailcow"
  272. echo -e "\e[31m$(date) - SOGo hit error limit\e[0m"
  273. echo sogo-mailcow > /tmp/com_pipe
  274. fi
  275. done
  276. ) &
  277. BACKGROUND_TASKS+=($!)
  278. (
  279. while true; do
  280. if ! postfix_checks; then
  281. log_to_redis "Postfix hit error limit"
  282. [[ ! -z ${WATCHDOG_NOTIFY_EMAIL} ]] && mail_error "${WATCHDOG_NOTIFY_EMAIL}" "postfix-mailcow"
  283. echo -e "\e[31m$(date) - Postfix hit error limit\e[0m"
  284. echo postfix-mailcow > /tmp/com_pipe
  285. fi
  286. done
  287. ) &
  288. BACKGROUND_TASKS+=($!)
  289. (
  290. while true; do
  291. if ! dovecot_checks; then
  292. log_to_redis "Dovecot hit error limit"
  293. [[ ! -z ${WATCHDOG_NOTIFY_EMAIL} ]] && mail_error "${WATCHDOG_NOTIFY_EMAIL}" "dovecot-mailcow"
  294. echo -e "\e[31m$(date) - Dovecot hit error limit\e[0m"
  295. echo dovecot-mailcow > /tmp/com_pipe
  296. fi
  297. done
  298. ) &
  299. BACKGROUND_TASKS+=($!)
  300. (
  301. while true; do
  302. if ! dns_checks; then
  303. log_to_redis "Unbound hit error limit"
  304. [[ ! -z ${WATCHDOG_NOTIFY_EMAIL} ]] && mail_error "${WATCHDOG_NOTIFY_EMAIL}" "unbound-mailcow"
  305. echo -e "\e[31m$(date) - Unbound hit error limit\e[0m"
  306. #echo unbound-mailcow > /tmp/com_pipe
  307. fi
  308. done
  309. ) &
  310. BACKGROUND_TASKS+=($!)
  311. (
  312. while true; do
  313. if ! rspamd_checks; then
  314. log_to_redis "Rspamd hit error limit"
  315. [[ ! -z ${WATCHDOG_NOTIFY_EMAIL} ]] && mail_error "${WATCHDOG_NOTIFY_EMAIL}" "rspamd-mailcow"
  316. echo -e "\e[31m$(date) - Rspamd hit error limit\e[0m"
  317. echo rspamd-mailcow > /tmp/com_pipe
  318. fi
  319. done
  320. ) &
  321. BACKGROUND_TASKS+=($!)
  322. # Monitor watchdog agents, stop script when agents fails and wait for respawn by Docker (restart:always:n)
  323. (
  324. while true; do
  325. for bg_task in ${BACKGROUND_TASKS[*]}; do
  326. if ! kill -0 ${bg_task} 1>&2; then
  327. echo "Worker ${bg_task} died, stopping watchdog and waiting for respawn..."
  328. log_to_redis "Worker ${bg_task} died, stopping watchdog and waiting for respawn..."
  329. kill -TERM 1
  330. fi
  331. sleep 10
  332. done
  333. done
  334. ) &
  335. # Monitor dockerapi
  336. (
  337. while true; do
  338. while nc -z dockerapi 8080; do
  339. sleep 3
  340. done
  341. echo "Cannot find dockerapi-mailcow, waiting to recover..."
  342. kill -STOP ${BACKGROUND_TASKS[*]}
  343. until nc -z dockerapi 8080; do
  344. sleep 3
  345. done
  346. kill -CONT ${BACKGROUND_TASKS[*]}
  347. kill -USR1 ${BACKGROUND_TASKS[*]}
  348. done
  349. ) &
  350. # Restart container when threshold limit reached
  351. while true; do
  352. CONTAINER_ID=
  353. read com_pipe_answer </tmp/com_pipe
  354. if [[ ${com_pipe_answer} =~ .+-mailcow ]]; then
  355. kill -STOP ${BACKGROUND_TASKS[*]}
  356. sleep 3
  357. CONTAINER_ID=$(curl --silent http://dockerapi:8080/containers/json | jq -r ".[] | {name: .Config.Labels[\"com.docker.compose.service\"], id: .Id}" | jq -rc "select( .name | tostring | contains(\"${com_pipe_answer}\")) | .id")
  358. if [[ ! -z ${CONTAINER_ID} ]]; then
  359. log_to_redis "Sending restart command to ${CONTAINER_ID}..."
  360. echo "Sending restart command to ${CONTAINER_ID}..."
  361. curl --silent -XPOST http://dockerapi:8080/containers/${CONTAINER_ID}/restart
  362. fi
  363. echo "Wait for restarted container to settle and continue watching..."
  364. sleep 30s
  365. kill -CONT ${BACKGROUND_TASKS[*]}
  366. kill -USR1 ${BACKGROUND_TASKS[*]}
  367. fi
  368. done