healthcheck.sh 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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. # Simple Netcat Check to connect to common webports
  44. function check_netcat() {
  45. declare -a domains=("mailcow.email" "github.com" "hub.docker.com")
  46. declare -a ports=("80" "443")
  47. for domain in "${domains[@]}" ; do
  48. for port in "${ports[@]}" ; do
  49. nc -z -w 2 $domain $port
  50. if [ $? -ne 0 ]; then
  51. log_to_file "Healthcheck: Could not reach $domain on Port $port... Gave up!"
  52. log_to_file "Please check your internet connection or firewall rules to fix this error."
  53. return 1
  54. fi
  55. done
  56. done
  57. log_to_file "Healthcheck: Netcat Checks WORKING properly!"
  58. return 0
  59. }
  60. if [[ ${SKIP_UNBOUND_HEALTHCHECK} == "y" ]]; then
  61. log_to_file "Healthcheck: ALL CHECKS WERE SKIPPED! Unbound is healthy!"
  62. exit 0
  63. fi
  64. # run checks, if check is not returning 0 (return value if check is ok), healthcheck will exit with 1 (marked in docker as unhealthy)
  65. check_ping
  66. if [ $? -ne 0 ]; then
  67. exit 1
  68. fi
  69. check_dns
  70. if [ $? -ne 0 ]; then
  71. exit 1
  72. fi
  73. check_netcat
  74. if [ $? -ne 0 ]; then
  75. exit 1
  76. fi
  77. log_to_file "Healthcheck: ALL CHECKS WERE SUCCESSFUL! Unbound is healthy!"
  78. exit 0