bind dns 宕机检测 故障切换shell脚本

www.centos.bz解析有多个A记录,下面是实现故障切换的脚本:
通过检测网站的返回状态码来确定服务器的健康状况,如果不返回或返回的状态非200,则开始记录一次故障,连续三次故障后开始删除此域名的故障ip A记录,如果之后的检测发现服务器已经恢复,则重新添加此ip的A记录。

  1. #!/bin/bash
  2. #===============================================================================
  3. #Description: this script is to automactic update dns record when website is down.
  4. #Author     : www.centos.bz
  5. #文件说明:
  6. # /tmp/domain_list.txt                       需要监控的域名列表,每行一个域名
  7. # /tmp/${domain}_online_ip.txt               记录在线的服务器ip,需要提前写入IP,每行一个IP
  8. # /tmp/${domain}_down_ip.txt                 记录有故障的服务器ip
  9. # /tmp/curl.txt                              记录curl获取的http状态码
  10. # /tmp/${domain}_${server_ip}_cur_time.txt    记录服务器出现故障的次数
  11. #===============================================================================
  12.  
  13. #设置一些必要的变量
  14. keyname=rndc-key
  15. keysecret=gAnBYq6xSv7FKTZFmzAD0Q==
  16.  
  17. #用来检测本机网络是否正常
  18. function network_detect(){
  19. ping -c1 8.8.8.8 >/dev/null 2>&1 && echo connect || exit 1
  20. }
  21.  
  22. #用来删除DNS记录
  23. function del_record(){
  24. /usr/local/bind/bin/nsupdate <<EOF
  25. key $keyname $keysecret
  26. update delete $domain A $1
  27. send
  28. quit
  29. EOF
  30. }
  31.  
  32. #用来增加DNS记录
  33. function add_record(){
  34. /usr/local/bind/bin/nsupdate <<EOF
  35. key $keyname $keysecret
  36. update add $domain 3600 A $1
  37. send
  38. quit
  39. EOF
  40. }
  41.  
  42. #用来检测在线ip列表健康状态
  43. function online_detect(){
  44. if [  -s /tmp/${domain}_online_ip.txt ] ;then
  45. for server_ip in `cat /tmp/${domain}_online_ip.txt` ;
  46. do
  47. curl -I -l -H "Host:$domain"  $server_ip -o "/tmp/curl.txt" >/dev/null 2>&1
  48. ###判断状态码是否为200
  49. if [ -s /tmp/curl.txt ] && grep '200 OK' /tmp/curl.txt >/dev/null 2>&1;then
  50. echo "OK"
  51. ###清空故障次数
  52. rm -f /tmp/${domain}_${server_ip}_cur_time.txt
  53. ###状态码非200时
  54. else
  55. ###开始计算故障次数
  56. cur_time=0
  57. [ -s /tmp/${domain}_${server_ip}_cur_time.txt  ] && cur_time=`cat /tmp/${domain}_${server_ip}_cur_time.txt `
  58. cur_time=`expr $cur_time + 1`
  59.  
  60. ###当故障次数大于等于3时
  61. if [ $cur_time -gt 3 ];then
  62. ###删除故障ip记录
  63. del_record $server_ip
  64. ###从在线ip列表中删除故障ip
  65. sed -i "/$server_ip/d" /tmp/${domain}_online_ip.txt
  66. ###记录故障ip到文件
  67. echo $server_ip >> /tmp/${domain}_down_ip.txt
  68. ###删除记录此ip的故障文件
  69. rm -f /tmp/${domain}_${server_ip}_cur_time.txt
  70.  
  71. else
  72. ###记录故障次数
  73. echo $cur_time > /tmp/${domain}_${server_ip}_cur_time.txt
  74. fi
  75. fi
  76. rm -f /tmp/curl.txt
  77.  
  78. done
  79. fi
  80. }
  81.  
  82. #用来检测故障ip列表健康状态
  83. function down_detect(){
  84. if [ -s /tmp/${domain}_down_ip.txt ];then
  85. for server_ip in `cat /tmp/${domain}_down_ip.txt` ;
  86. do
  87. curl -I -l -H "Host:$domain"  $server_ip -o "/tmp/curl.txt" >/dev/null 2>&1
  88. if [ -s /tmp/curl.txt ] && grep '200 OK' /tmp/curl.txt >/dev/null 2>&1;then
  89. ###添加A记录
  90. add_record $server_ip
  91. ###从${domain}_down_ip.txt删除故障ip
  92. sed -i "/$server_ip/d" /tmp/${domain}_down_ip.txt
  93. ###重新添加此ip到${domain}_online_ip.txt
  94. echo $server_ip >> /tmp/${domain}_online_ip.txt
  95. fi
  96. rm -f /tmp/curl.txt
  97. done
  98. fi
  99. }
  100. network_detect
  101. if [ -s /tmp/domain_list.txt ];then
  102. for domain in `cat /tmp/domain_list.txt` ;
  103. do
  104. online_detect
  105. down_detect
  106. done
  107. else
  108. echo "/tmp/domain_list.txt not found!"
  109. exit 1
  110. fi

要正常使用以上脚本,需要注意以下事项:
1、 /tmp/domain_list.txt 填写需要监控的域名,/tmp/${domain}_online_ip.txt填写对应域名的所有A记录。
2、根据bind设置修改脚本中的三个变量

  1. domain=www.centos.bz
  2. keyname=rndc-key
  3. keysecret=gAnBYq6xSv7FKTZFmzAD0Q==

3、在named.conf文件中的zone添加如下代码:

  1. allow-update {key rndc-key;};

rndc-key修改为自己的。

标签:DNSShell 发布于:2019-11-23 14:35:00