layout: post
title: keepalived—nginxcheck
date: 2018-03-13
tags: [“Cluster services”,”软件服务”]


  1. function init(){
  2. LOCKFILE=/tmp/nginxCheck.lock
  3. [ -f $LOCKFILE ] && echo -e "\033[31m nginxCheck is running will exit \033[0m"&&exit '' touch $LOCKFILE
  4. /sbin/ifconfig bond0 &>/dev/null
  5. BOND0=$?
  6. /sbin/ifconfig em1 &>/dev/null
  7. ETH0=$?
  8. if [ $BOND0 -eq 0 ];then
  9. ETH=bond0;
  10. elif [ $ETH0 -eq 0 ];then
  11. ETH=em1
  12. else
  13. ETH=em2;
  14. fi
  15. USER=`whoami`
  16. IP=$(/sbin/ifconfig $ETH ' grep -i "inet addr" ' awk '{print $2}' ' sed 's/addr://')
  17. VIP=$(grep label /etc/keepalived/keepalived.conf ' tr '/' ' ' ' awk '{print $1}')
  18. NGPID=$(ps -ef ' grep "nginx: master" ' grep -v grep ' awk '{print $2}')
  19. if [[ ! -n "$NGPID" ]];then
  20. echo -e "\033[31m nginx master not running \033[0m\n"
  21. if [ $USER == root ];then
  22. echo -e "\033[31m user is root try start nginx \033[0m\n"
  23. killall nginx
  24. sleep 3
  25. /app/tengine-2.2.1/sbin/nginx
  26. START_STATUS=$?
  27. if [ $START_STATUS == 0 ];then
  28. echo -e "\033[32m nginx is restart sucess \033[0m\n"
  29. echo "nginx restart `date`" >> /tmp/nginxcheck.log
  30. else
  31. echo -e "\033[31m nginx is restart fail please chechk \033[0m \n"
  32. fi
  33. else
  34. echo -e "\033[31m user is not root \033[0m\n"
  35. fi
  36. else
  37. echo -e "\033[32m nginx master is running \033[0m \n"
  38. fi
  39. N=1
  40. M=$(grep "listen" /app/tengine-2.2.1/conf/nginx.conf ' grep -v ^# ' tr ';' ' ' ' awk '{print $2}' ' wc -l)
  41. }
  42. checkVipPort(){
  43. echo -e "\033[33m start check VIP status \033[0m \n"
  44. ifconfig'grep $VIP >> /dev/null
  45. VIP_STATUS=$?
  46. if [ $VIP_STATUS == 0 ];then
  47. echo -e "\033[32m $VIP in localhost \033[0m \n"
  48. IS_IN=1
  49. else
  50. echo -e "\033[31m $VIP not in localhost \033[0m \n"
  51. IS_IN=0
  52. fi
  53. echo -e "\033[33m check VIP status end \033[0m \n"
  54. echo -e "\033[33m start check port connect status \033[0m \n"
  55. if [ $IS_IN == 0 ];then
  56. VIP=$IP
  57. fi
  58. for i in $(grep "listen" /app/tengine-2.2.1/conf/nginx.conf ' grep -v ^# ' tr ';' ' ' ' awk '{print $2}')
  59. do
  60. PORT=$i
  61. curl -i -m 1 "http://$VIP:$PORT/lvs.jsp" &>/dev/null
  62. GLOBAL_STAT=$?
  63. if [[ $GLOBAL_STAT -eq 0 ]];then
  64. echo -e "\033[32m $VIP:$PORT is running \033[0m\n"
  65. elif [[ $N -lt $M ]];then
  66. echo -e "\033[31m $VIP:$PORT is stoped \033[0m\n"
  67. let N++
  68. continue
  69. else
  70. if [ $USER == root ];then
  71. echo -e "\033[31m user is root try start nginx \033[0m\n"
  72. killall nginx
  73. sleep 3
  74. /etc/init.d/keepalived restart
  75. /app/tengine-2.2.1/sbin/nginx
  76. START_STATUS=$?
  77. if [ $START_STATUS == 0 ];then
  78. echo -e "\033[32m nginx is restart sucess \033[0m\n"
  79. echo "nginx restart `date`" >> /tmp/nginxcheck.log
  80. else
  81. echo -e "\033[31m nginx is restart fail please chechk \033[0m \n"
  82. fi
  83. else
  84. echo -e "\033[31m user is not root \033[0m\n"
  85. fi
  86. fi
  87. #del lock file
  88. rm -f $LOCKFILE
  89. done
  90. echo -e "\033[33m check port connect status end \033[0m \n"
  91. }
  92. init
  93. checkVipPort
文档更新时间: 2018-12-20 11:19   作者:张尚