watchdog.sh 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332
  1. #!/bin/bash
  2. trap "exit" INT TERM
  3. trap "kill 0" EXIT
  4. PARENT_PID=$$
  5. # Prepare
  6. BACKGROUND_TASKS=()
  7. # Checks pipe their corresponding container name in this pipe
  8. if [[ ! -p /tmp/com_pipe ]]; then
  9. mkfifo /tmp/com_pipe
  10. fi
  11. # Common functions
  12. progress() {
  13. SERVICE=${1}
  14. TOTAL=${2}
  15. CURRENT=${3}
  16. DIFF=${4}
  17. [[ -z ${DIFF} ]] && DIFF=0
  18. [[ -z ${TOTAL} || -z ${CURRENT} ]] && return
  19. [[ ${CURRENT} -gt ${TOTAL} ]] && return
  20. [[ ${CURRENT} -lt 0 ]] && CURRENT=0
  21. PERCENT=$(( 200 * ${CURRENT} / ${TOTAL} % 2 + 100 * ${CURRENT} / ${TOTAL} ))
  22. echo -ne "$(date) - ${SERVICE} health level: \e[7m${PERCENT}%\e[0m (${CURRENT}/${TOTAL}), health trend: "
  23. [[ ${DIFF} =~ ^-[1-9] ]] && echo -en '[\e[41m \e[0m] ' || echo -en '[\e[42m \e[0m] '
  24. echo "(${DIFF})"
  25. }
  26. get_container_ip() {
  27. # ${1} is container
  28. CONTAINER_ID=
  29. CONTAINER_IP=
  30. LOOP_C=1
  31. until [[ ${CONTAINER_IP} =~ ^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}$ ]] || [[ ${LOOP_C} -gt 5 ]]; do
  32. sleep 1
  33. CONTAINER_ID=$(curl --silent --unix-socket /var/run/docker.sock http/containers/json?all=1 | jq -rc "map(select(.Names[] | contains (\"${1}\"))) | .[] .Id")
  34. CONTAINER_IP=$(curl --silent --unix-socket /var/run/docker.sock http/containers/${CONTAINER_ID}/json | jq -r '.NetworkSettings.Networks[].IPAddress')
  35. LOOP_C=$((LOOP_C + 1))
  36. done
  37. [[ ${LOOP_C} -gt 5 ]] && echo 240.0.0.0 || echo ${CONTAINER_IP}
  38. }
  39. # Check functions
  40. nginx_checks() {
  41. err_count=0
  42. diff_c=0
  43. THRESHOLD=16
  44. # Reduce error count by 2 after restarting an unhealthy container
  45. trap "[ ${err_count} -gt 1 ] && err_count=$(( ${err_count} - 2 ))" USR1
  46. while [ ${err_count} -lt ${THRESHOLD} ]; do
  47. host_ip=$(get_container_ip nginx-mailcow)
  48. err_c_cur=${err_count}
  49. /usr/lib/nagios/plugins/check_ping -4 -H ${host_ip} -w 2000,10% -c 4000,100% -p2 1>&2; err_count=$(( ${err_count} + $? ))
  50. /usr/lib/nagios/plugins/check_http -4 -H ${host_ip} -u / -p 8081 1>&2; err_count=$(( ${err_count} + $? ))
  51. [ ${err_c_cur} -eq ${err_count} ] && [ ! $((${err_count} - 1)) -lt 0 ] && err_count=$((${err_count} - 1)) diff_c=1
  52. [ ${err_c_cur} -ne ${err_count} ] && diff_c=$(( ${err_c_cur} - ${err_count} ))
  53. progress "Nginx" ${THRESHOLD} $(( ${THRESHOLD} - ${err_count} )) ${diff_c}
  54. diff_c=0
  55. sleep $(( ( RANDOM % 30 ) + 10 ))
  56. done
  57. return 1
  58. }
  59. mysql_checks() {
  60. err_count=0
  61. diff_c=0
  62. THRESHOLD=12
  63. # Reduce error count by 2 after restarting an unhealthy container
  64. trap "[ ${err_count} -gt 1 ] && err_count=$(( ${err_count} - 2 ))" USR1
  65. while [ ${err_count} -lt ${THRESHOLD} ]; do
  66. host_ip=$(get_container_ip mysql-mailcow)
  67. err_c_cur=${err_count}
  68. /usr/lib/nagios/plugins/check_mysql -H ${host_ip} -P 3306 -u ${DBUSER} -p ${DBPASS} -d ${DBNAME} 1>&2; err_count=$(( ${err_count} + $? ))
  69. /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} + $? ))
  70. [ ${err_c_cur} -eq ${err_count} ] && [ ! $((${err_count} - 1)) -lt 0 ] && err_count=$((${err_count} - 1)) diff_c=1
  71. [ ${err_c_cur} -ne ${err_count} ] && diff_c=$(( ${err_c_cur} - ${err_count} ))
  72. progress "MySQL/MariaDB" ${THRESHOLD} $(( ${THRESHOLD} - ${err_count} )) ${diff_c}
  73. diff_c=0
  74. sleep $(( ( RANDOM % 30 ) + 10 ))
  75. done
  76. return 1
  77. }
  78. sogo_checks() {
  79. err_count=0
  80. diff_c=0
  81. THRESHOLD=20
  82. # Reduce error count by 2 after restarting an unhealthy container
  83. trap "[ ${err_count} -gt 1 ] && err_count=$(( ${err_count} - 2 ))" USR1
  84. while [ ${err_count} -lt ${THRESHOLD} ]; do
  85. host_ip=$(get_container_ip sogo-mailcow)
  86. err_c_cur=${err_count}
  87. /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} + $? ))
  88. /usr/lib/nagios/plugins/check_http -4 -H ${host_ip} -u /SOGo.index/ -p 20000 -R "SOGo\sGroupware" 1>&2; err_count=$(( ${err_count} + $? ))
  89. [ ${err_c_cur} -eq ${err_count} ] && [ ! $((${err_count} - 1)) -lt 0 ] && err_count=$((${err_count} - 1)) diff_c=1
  90. [ ${err_c_cur} -ne ${err_count} ] && diff_c=$(( ${err_c_cur} - ${err_count} ))
  91. progress "SOGo" ${THRESHOLD} $(( ${THRESHOLD} - ${err_count} )) ${diff_c}
  92. diff_c=0
  93. sleep $(( ( RANDOM % 30 ) + 10 ))
  94. done
  95. return 1
  96. }
  97. postfix_checks() {
  98. err_count=0
  99. diff_c=0
  100. THRESHOLD=16
  101. # Reduce error count by 2 after restarting an unhealthy container
  102. trap "[ ${err_count} -gt 1 ] && err_count=$(( ${err_count} - 2 ))" USR1
  103. while [ ${err_count} -lt ${THRESHOLD} ]; do
  104. host_ip=$(get_container_ip postfix-mailcow)
  105. err_c_cur=${err_count}
  106. /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} + $? ))
  107. /usr/lib/nagios/plugins/check_smtp -4 -H ${host_ip} -p 589 -S 1>&2; err_count=$(( ${err_count} + $? ))
  108. [ ${err_c_cur} -eq ${err_count} ] && [ ! $((${err_count} - 1)) -lt 0 ] && err_count=$((${err_count} - 1)) diff_c=1
  109. [ ${err_c_cur} -ne ${err_count} ] && diff_c=$(( ${err_c_cur} - ${err_count} ))
  110. progress "Postfix" ${THRESHOLD} $(( ${THRESHOLD} - ${err_count} )) ${diff_c}
  111. diff_c=0
  112. sleep $(( ( RANDOM % 30 ) + 10 ))
  113. done
  114. return 1
  115. }
  116. dovecot_checks() {
  117. err_count=0
  118. diff_c=0
  119. THRESHOLD=24
  120. # Reduce error count by 2 after restarting an unhealthy container
  121. trap "[ ${err_count} -gt 1 ] && err_count=$(( ${err_count} - 2 ))" USR1
  122. while [ ${err_count} -lt ${THRESHOLD} ]; do
  123. host_ip=$(get_container_ip dovecot-mailcow)
  124. err_c_cur=${err_count}
  125. /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} + $? ))
  126. /usr/lib/nagios/plugins/check_imap -4 -H ${host_ip} -p 993 -S -e "OK " 1>&2; err_count=$(( ${err_count} + $? ))
  127. /usr/lib/nagios/plugins/check_imap -4 -H ${host_ip} -p 143 -e "OK " 1>&2; err_count=$(( ${err_count} + $? ))
  128. /usr/lib/nagios/plugins/check_tcp -4 -H ${host_ip} -p 10001 -e "VERSION" 1>&2; err_count=$(( ${err_count} + $? ))
  129. /usr/lib/nagios/plugins/check_tcp -4 -H ${host_ip} -p 4190 -e "Dovecot ready" 1>&2; err_count=$(( ${err_count} + $? ))
  130. [ ${err_c_cur} -eq ${err_count} ] && [ ! $((${err_count} - 1)) -lt 0 ] && err_count=$((${err_count} - 1)) diff_c=1
  131. [ ${err_c_cur} -ne ${err_count} ] && diff_c=$(( ${err_c_cur} - ${err_count} ))
  132. progress "Dovecot" ${THRESHOLD} $(( ${THRESHOLD} - ${err_count} )) ${diff_c}
  133. diff_c=0
  134. sleep $(( ( RANDOM % 30 ) + 10 ))
  135. done
  136. return 1
  137. }
  138. phpfpm_checks() {
  139. err_count=0
  140. diff_c=0
  141. THRESHOLD=10
  142. # Reduce error count by 2 after restarting an unhealthy container
  143. trap "[ ${err_count} -gt 1 ] && err_count=$(( ${err_count} - 2 ))" USR1
  144. while [ ${err_count} -lt ${THRESHOLD} ]; do
  145. host_ip=$(get_container_ip php-fpm-mailcow)
  146. err_c_cur=${err_count}
  147. cgi-fcgi -bind -connect ${host_ip}:9000 | grep PHP 1>&2; err_count=$(( ${err_count} + ($? * 2)))
  148. /usr/lib/nagios/plugins/check_ping -4 -H ${host_ip} -w 2000,10% -c 4000,100% -p2 1>&2; err_count=$(( ${err_count} + $? ))
  149. [ ${err_c_cur} -eq ${err_count} ] && [ ! $((${err_count} - 1)) -lt 0 ] && err_count=$((${err_count} - 1)) diff_c=1
  150. [ ${err_c_cur} -ne ${err_count} ] && diff_c=$(( ${err_c_cur} - ${err_count} ))
  151. progress "PHP-FPM" ${THRESHOLD} $(( ${THRESHOLD} - ${err_count} )) ${diff_c}
  152. diff_c=0
  153. sleep $(( ( RANDOM % 30 ) + 10 ))
  154. done
  155. return 1
  156. }
  157. rspamd_checks() {
  158. err_count=0
  159. diff_c=0
  160. THRESHOLD=10
  161. # Reduce error count by 2 after restarting an unhealthy container
  162. trap "[ ${err_count} -gt 1 ] && err_count=$(( ${err_count} - 2 ))" USR1
  163. while [ ${err_count} -lt ${THRESHOLD} ]; do
  164. host_ip=$(get_container_ip rspamd-mailcow)
  165. err_c_cur=${err_count}
  166. SCORE=$(curl --silent ${host_ip}:11333/scan -d '
  167. To: null@localhost
  168. From: watchdog@localhost
  169. Empty
  170. ' | jq -rc .required_score)
  171. if [[ ${SCORE} != "9999" ]]; then
  172. echo "Rspamd settings check failed" 1>&2
  173. err_count=$(( ${err_count} + 1))
  174. else
  175. echo "Rspamd settings check succeeded" 1>&2
  176. fi
  177. /usr/lib/nagios/plugins/check_ping -4 -H ${host_ip} -w 2000,10% -c 4000,100% -p2 1>&2; err_count=$(( ${err_count} + $? ))
  178. [ ${err_c_cur} -eq ${err_count} ] && [ ! $((${err_count} - 1)) -lt 0 ] && err_count=$((${err_count} - 1)) diff_c=1
  179. [ ${err_c_cur} -ne ${err_count} ] && diff_c=$(( ${err_c_cur} - ${err_count} ))
  180. progress "Rspamd" ${THRESHOLD} $(( ${THRESHOLD} - ${err_count} )) ${diff_c}
  181. diff_c=0
  182. sleep $(( ( RANDOM % 30 ) + 10 ))
  183. done
  184. return 1
  185. }
  186. dns_checks() {
  187. err_count=0
  188. diff_c=0
  189. THRESHOLD=28
  190. # Reduce error count by 2 after restarting an unhealthy container
  191. trap "[ ${err_count} -gt 1 ] && err_count=$(( ${err_count} - 2 ))" USR1
  192. while [ ${err_count} -lt ${THRESHOLD} ]; do
  193. host_ip=$(get_container_ip unbound-mailcow)
  194. err_c_cur=${err_count}
  195. /usr/lib/nagios/plugins/check_dns -H google.com 1>&2; err_count=$(( ${err_count} + ($? * 2)))
  196. /usr/lib/nagios/plugins/check_dns -s ${host_ip} -H google.com 1>&2; err_count=$(( ${err_count} + ($? * 2)))
  197. dig +dnssec org. @${host_ip} | grep -E 'flags:.+ad' 1>&2; err_count=$(( ${err_count} + ($? * 2)))
  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 "Unbound" ${THRESHOLD} $(( ${THRESHOLD} - ${err_count} )) ${diff_c}
  201. diff_c=0
  202. sleep $(( ( RANDOM % 30 ) + 10 ))
  203. done
  204. return 1
  205. }
  206. # Create watchdog agents
  207. (
  208. while true; do
  209. if ! nginx_checks; then
  210. echo -e "\e[31m$(date) - Nginx hit error limit\e[0m"
  211. echo nginx-mailcow > /tmp/com_pipe
  212. fi
  213. done
  214. ) &
  215. BACKGROUND_TASKS+=($!)
  216. (
  217. while true; do
  218. if ! mysql_checks; then
  219. echo -e "\e[31m$(date) - MySQL hit error limit\e[0m"
  220. echo mysql-mailcow > /tmp/com_pipe
  221. fi
  222. done
  223. ) &
  224. BACKGROUND_TASKS+=($!)
  225. (
  226. while true; do
  227. if ! phpfpm_checks; then
  228. echo -e "\e[31m$(date) - PHP-FPM hit error limit\e[0m"
  229. echo php-fpm-mailcow > /tmp/com_pipe
  230. fi
  231. done
  232. ) &
  233. BACKGROUND_TASKS+=($!)
  234. (
  235. while true; do
  236. if ! sogo_checks; then
  237. echo -e "\e[31m$(date) - SOGo hit error limit\e[0m"
  238. echo sogo-mailcow > /tmp/com_pipe
  239. fi
  240. done
  241. ) &
  242. BACKGROUND_TASKS+=($!)
  243. (
  244. while true; do
  245. if ! postfix_checks; then
  246. echo -e "\e[31m$(date) - Postfix hit error limit\e[0m"
  247. echo postfix-mailcow > /tmp/com_pipe
  248. fi
  249. done
  250. ) &
  251. BACKGROUND_TASKS+=($!)
  252. (
  253. while true; do
  254. if ! dovecot_checks; then
  255. echo -e "\e[31m$(date) - Dovecot hit error limit\e[0m"
  256. echo dovecot-mailcow > /tmp/com_pipe
  257. fi
  258. done
  259. ) &
  260. BACKGROUND_TASKS+=($!)
  261. (
  262. while true; do
  263. if ! dns_checks; then
  264. echo -e "\e[31m$(date) - Unbound hit error limit\e[0m"
  265. #echo unbound-mailcow > /tmp/com_pipe
  266. fi
  267. done
  268. ) &
  269. BACKGROUND_TASKS+=($!)
  270. (
  271. while true; do
  272. if ! rspamd_checks; then
  273. echo -e "\e[31m$(date) - Rspamd hit error limit\e[0m"
  274. echo rspamd-mailcow > /tmp/com_pipe
  275. fi
  276. done
  277. ) &
  278. BACKGROUND_TASKS+=($!)
  279. # Monitor watchdog agents, stop script when agents fails and wait for respawn by Docker (restart:always:n)
  280. (
  281. while true; do
  282. for bg_task in ${BACKGROUND_TASKS[*]}; do
  283. if ! kill -0 ${bg_task} 21>&2; then
  284. echo "Worker ${bg_task} died, stopping watchdog and waiting for respawn..."
  285. kill -TERM ${PARENT_PID}
  286. fi
  287. sleep 1
  288. done
  289. done
  290. ) &
  291. # Restart container when threshold limit reached
  292. while true; do
  293. CONTAINER_ID=
  294. read com_pipe_answer </tmp/com_pipe
  295. if [[ ${com_pipe_answer} =~ .+-mailcow ]]; then
  296. kill -STOP ${BACKGROUND_TASKS[*]}
  297. sleep 3
  298. CONTAINER_ID=$(curl --silent --unix-socket /var/run/docker.sock http/containers/json?all=1 | jq -rc "map(select(.Names[] | contains (\"${com_pipe_answer}\"))) | .[] .Id")
  299. if [[ ! -z ${CONTAINER_ID} ]]; then
  300. echo "Sending restart command to ${CONTAINER_ID}..."
  301. curl --silent --unix-socket /var/run/docker.sock -XPOST http/containers/${CONTAINER_ID}/restart
  302. fi
  303. echo "Wait for restarted container to settle and continue watching..."
  304. sleep 30s
  305. kill -CONT ${BACKGROUND_TASKS[*]}
  306. kill -USR1 ${BACKGROUND_TASKS[*]}
  307. fi
  308. done