healthcheck.sh 2.4 KB

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