healthcheck.sh 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. #!/bin/bash
  2. STATUS_FILE="/tmp/healthcheck_status"
  3. RUNS=0
  4. # Declare log function for logfile to stdout
  5. function log_to_stdout() {
  6. echo "$(date +"%Y-%m-%d %H:%M:%S"): $1"
  7. }
  8. # General Ping function to check general pingability
  9. function check_ping() {
  10. declare -a ipstoping=("1.1.1.1" "8.8.8.8" "9.9.9.9")
  11. local fail_tolerance=1
  12. local failures=0
  13. for ip in "${ipstoping[@]}" ; do
  14. success=false
  15. for ((i=1; i<=3; i++)); do
  16. ping -q -c 3 -w 5 "$ip" > /dev/null
  17. if [ $? -eq 0 ]; then
  18. success=true
  19. break
  20. else
  21. log_to_stdout "Healthcheck: Failed to ping $ip on attempt $i. Trying again..."
  22. fi
  23. done
  24. if [ "$success" = false ]; then
  25. log_to_stdout "Healthcheck: Couldn't ping $ip after 3 attempts. Marking this IP as failed."
  26. ((failures++))
  27. fi
  28. done
  29. if [ $failures -gt $fail_tolerance ]; then
  30. log_to_stdout "Healthcheck: Too many ping failures ($fail_tolerance failures allowed, you got $failures failures), marking Healthcheck as unhealthy..."
  31. return 1
  32. fi
  33. return 0
  34. }
  35. # General DNS Resolve Check against Unbound Resolver himself
  36. function check_dns() {
  37. declare -a domains=("fuzzy.mailcow.email" "github.com" "hub.docker.com")
  38. local fail_tolerance=1
  39. local failures=0
  40. for domain in "${domains[@]}" ; do
  41. success=false
  42. for ((i=1; i<=3; i++)); do
  43. dig_output=$(dig +short +timeout=2 +tries=1 "$domain" @127.0.0.1 2>/dev/null)
  44. dig_rc=$?
  45. if [ $dig_rc -ne 0 ] || [ -z "$dig_output" ]; then
  46. log_to_stdout "Healthcheck: DNS Resolution Failed on attempt $i for $domain! Trying again..."
  47. else
  48. success=true
  49. break
  50. fi
  51. done
  52. if [ "$success" = false ]; then
  53. log_to_stdout "Healthcheck: DNS Resolution not possible after 3 attempts for $domain... Gave up!"
  54. ((failures++))
  55. fi
  56. done
  57. if [ $failures -gt $fail_tolerance ]; then
  58. log_to_stdout "Healthcheck: Too many DNS failures ($fail_tolerance failures allowed, you got $failures failures), marking Healthcheck as unhealthy..."
  59. return 1
  60. fi
  61. return 0
  62. }
  63. while true; do
  64. if [[ ${SKIP_UNBOUND_HEALTHCHECK} == "y" ]]; then
  65. log_to_stdout "Healthcheck: ALL CHECKS WERE SKIPPED! Unbound is healthy!"
  66. echo "0" > $STATUS_FILE
  67. sleep 365d
  68. fi
  69. # run checks, if check is not returning 0 (return value if check is ok), healthcheck will exit with 1 (marked in docker as unhealthy)
  70. check_ping
  71. PING_STATUS=$?
  72. check_dns
  73. DNS_STATUS=$?
  74. if [ $PING_STATUS -ne 0 ] || [ $DNS_STATUS -ne 0 ]; then
  75. echo "1" > $STATUS_FILE
  76. else
  77. echo "0" > $STATUS_FILE
  78. fi
  79. sleep 30
  80. done