watchdog.sh 14 KB

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