ip_swich_chk_N.sh 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. #!/bin/bash
  2. #Whritted: dufs
  3. #E-mail: fengshan.du@zznode.com
  4. #Date: 2016-08-25 11:00
  5. #ip switch check
  6. SH_NAME=`basename $0`
  7. SH_HOME=$HOME/zsjk
  8. SH_Log=$SH_HOME/log/${0%\.sh*}.log
  9. function outlog {
  10. echo "`date '+%Y-%m-%d %T'` : " "$*" >> $SH_Log
  11. logSize=`ls -lrt $SH_Log|awk '{print $5}'`
  12. if [ $logSize -gt 10240000 ]
  13. then
  14. cp -rp $SH_Log $SH_Log.`date '+%Y-%m-%d'`
  15. gzip $SH_Log.`date '+%Y-%m-%d'`
  16. cat /dev/null>$SH_Log
  17. fi
  18. }
  19. outlog =============================================================
  20. outlog start check
  21. msgTemp=$SH_HOME/conf/sendmsg_temp.sql
  22. if [ ! -f $msgTemp ]
  23. then
  24. outlog can not find $msgTemp, please check !!!
  25. exit 0
  26. fi
  27. ip_list=$SH_HOME/conf/ip_swich_chk.conf
  28. if [ ! -f $ip_list ]
  29. then
  30. outlog can not find $ip_list, please check !!!
  31. exit 0
  32. fi
  33. cd $SH_HOME/bin
  34. for line in `cat $ip_list`
  35. do
  36. sharppos=$(echo $line|awk '{print index($1,"#")}')
  37. if [ $sharppos = 1 ]
  38. then
  39. #echo remarked line,ignore
  40. continue
  41. fi
  42. outlog -------------------------------------------------------------
  43. Local_IP=`echo $line | awk -F"," '{print $1}'`
  44. Virtu_IP=`echo $line | awk -F"," '{print $2}'`
  45. chk_user=`echo $line | awk -F"," '{print $3}'`
  46. ssh_port=`echo $line | awk -F"," '{print $4}'`
  47. outlog Local_IP $Local_IP
  48. #outlog read config: Virtu_IP $Virtu_IP
  49. #outlog read config: chk_user $chk_user
  50. #outlog read config: ssh_port $ssh_port
  51. ssh -t -p $ssh_port $chk_user@$Local_IP "/sbin/ip add" >$SH_HOME/data/ipadd.$Local_IP
  52. rowNum=`cat $SH_HOME/data/ipadd.$Local_IP|wc -l`
  53. if [ $rowNum -eq 0 ]
  54. then
  55. outlog can not get '/sbin/ip add' from $Local_IP, please check !!!
  56. cat /dev/null>$SH_HOME/data/Last_ipadd.$Local_IP
  57. curT=`date '+%Y-%m-%d %T'`
  58. msg="【浮动IP切换】$curT获取$Local_IP的IP地址信息失败,请检查。"
  59. sed -e "s/__MSG__/$msg/g" \
  60. -e "s/__SHELL__/$SH_NAME/g" $msgTemp > $SH_HOME/bin/ip_swich.$Local_IP.sql
  61. outlog `./sendMsg.sh ip_swich.$Local_IP.sql`
  62. rm $SH_HOME/bin/ip_swich.$Local_IP.sql
  63. continue
  64. else
  65. Vip=`cat $SH_HOME/data/ipadd.$Local_IP|grep "scope global secondary"|awk '{print $2}'|awk -F"/" '{print $1}'`
  66. #outlog local ip : $Local_IP
  67. outlog this flow ip : $Vip
  68. #get last flow ip
  69. if [ -f $SH_HOME/data/Last_ipadd.$Local_IP ]
  70. then
  71. Vip_last=`cat $SH_HOME/data/Last_ipadd.$Local_IP`
  72. else
  73. Vip_last=""
  74. fi
  75. outlog last flow ip : $Vip_last
  76. Vip_chg=0
  77. Vip_run=0
  78. if [ "$Vip_last" != "$Vip" ]
  79. then
  80. Vip_chg=1
  81. fi
  82. if [ "$Virtu_IP" = "$Vip" ]
  83. then
  84. Vip_run=1
  85. fi
  86. if [ $Vip_chg -eq 0 -a $Vip_run -eq 0 ]
  87. then
  88. outlog Virtu_IP $Virtu_IP not running on server $Local_IP.
  89. fi
  90. if [ $Vip_chg -eq 1 -a $Vip_run -eq 0 ]
  91. then
  92. outlog switched !!!
  93. outlog Virtu_IP $Vip_last switch to other server from server $Local_IP.
  94. curT=`date '+%Y-%m-%d %T'`
  95. msg="【浮动IP切换】$curT监测到浮动IP $Vip_last从$Local_IP切换到了其它服务器,请检查。"
  96. oulog send message $msg
  97. #echo $msg > $SH_HOME/bin/ip_swich.$Local_IP.txt
  98. #outlog `sendMsg.sh ip_swich.$Local_IP tnms2 tn15ms! 10.101.129.8 /home/tnms2/applications/bin/zsjk/data/`
  99. sed -e "s/__MSG__/$msg/g" \
  100. -e "s/__SHELL__/$SH_NAME/g" $msgTemp > $SH_HOME/bin/ip_swich.$Local_IP.sql
  101. outlog `./sendMsg.sh ip_swich.$Local_IP.sql`
  102. rm $SH_HOME/bin/ip_swich.$Local_IP.sql
  103. fi
  104. if [ $Vip_chg -eq 0 -a $Vip_run -eq 1 ]
  105. then
  106. outlog Virtu_IP $Vip runing on server $Local_IP.
  107. fi
  108. if [ $Vip_chg -eq 1 -a $Vip_run -eq 1 ]
  109. then
  110. outlog switched !!!
  111. outlog flow ip $Vip switch from other server to server $Local_IP.
  112. curT=`date '+%Y-%m-%d %T'`
  113. msg="【浮动IP切换】$curT监测到浮动IP $Vip切换到了服务器$Local_IP,请检查。"
  114. outlog send message $msg
  115. #echo $msg > $SH_HOME/bin/ip_swich.$Local_IP.txt
  116. #outlog `./sendMsg.sh ip_swich.$Local_IP tnms2 tn15ms! 10.101.129.8 /home/tnms2/applications/bin/zsjk/data/`
  117. sed -e "s/__MSG__/$msg/g" \
  118. -e "s/__SHELL__/$SH_NAME/g" $msgTemp > $SH_HOME/bin/ip_swich.$Local_IP.sql
  119. outlog `./sendMsg.sh ip_swich.$Local_IP.sql`
  120. rm $SH_HOME/bin/ip_swich.$Local_IP.sql
  121. fi
  122. echo $Vip >$SH_HOME/data/Last_ipadd.$Local_IP
  123. fi
  124. outlog -------------------------------------------------------------
  125. done
  126. outlog =============================================================
  127. exit 0