healthcheck.sh 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. #!/bin/bash
  2. # Skip Unbound (DNS Resolver) Healthchecks (NOT Recommended!)
  3. if [[ "${SKIP_UNBOUND_HEALTHCHECK}" =~ ^([yY][eE][sS]|[yY])+$ ]]; then
  4. SKIP_UNBOUND_HEALTHCHECK=y
  5. fi
  6. # Declare log function for logfile inside container
  7. function log_to_file() {
  8. echo "$(date +"%Y-%m-%d %H:%M:%S"): $1" > /var/log/healthcheck.log
  9. }
  10. # General Ping function to check general pingability
  11. function check_ping() {
  12. declare -a ipstoping=("1.1.1.1" "8.8.8.8" "9.9.9.9")
  13. for ip in "${ipstoping[@]}" ; do
  14. ping -q -c 3 -w 5 "$ip"
  15. if [ $? -ne 0 ]; then
  16. log_to_file "Healthcheck: Couldn't ping $ip for 5 seconds... Gave up!"
  17. log_to_file "Please check your internet connection or firewall rules to fix this error, because a simple ping test should always go through from the unbound container!"
  18. return 1
  19. fi
  20. done
  21. log_to_file "Healthcheck: Ping Checks WORKING properly!"
  22. return 0
  23. }
  24. # General DNS Resolve Check against Unbound Resolver himself
  25. function check_dns() {
  26. declare -a domains=("mailcow.email" "github.com" "hub.docker.com")
  27. for domain in "${domains[@]}" ; do
  28. for ((i=1; i<=3; i++)); do
  29. dig +short +timeout=2 +tries=1 "$domain" @127.0.0.1 > /dev/null
  30. if [ $? -ne 0 ]; then
  31. log_to_file "Healthcheck: DNS Resolution Failed on $i attempt! Trying again..."
  32. if [ $i -eq 3 ]; then
  33. log_to_file "Healthcheck: DNS Resolution not possible after $i attempts... Gave up!"
  34. log_to_file "Maybe check your outbound firewall, as it needs to resolve DNS over TCP AND UDP!"
  35. return 1
  36. fi
  37. fi
  38. done
  39. done
  40. log_to_file "Healthcheck: DNS Resolver WORKING properly!"
  41. return 0
  42. }
  43. if [[ ${SKIP_UNBOUND_HEALTHCHECK} == "y" ]]; then
  44. log_to_file "Healthcheck: ALL CHECKS WERE SKIPPED! Unbound is healthy!"
  45. exit 0
  46. fi
  47. # run checks, if check is not returning 0 (return value if check is ok), healthcheck will exit with 1 (marked in docker as unhealthy)
  48. check_ping
  49. if [ $? -ne 0 ]; then
  50. exit 1
  51. fi
  52. check_dns
  53. if [ $? -ne 0 ]; then
  54. exit 1
  55. fi
  56. log_to_file "Healthcheck: ALL CHECKS WERE SUCCESSFUL! Unbound is healthy!"
  57. exit 0