黑帽联盟

标题: keepalived通知脚本 [打印本页]

作者: yun    时间: 2017-6-21 20:50
标题: keepalived通知脚本
下面的脚本可以接受选项,其中:

-s, --service SERVICE,...:指定服务脚本名称,当状态切换时可自动启动、重启或关闭此服务;

-a, --address VIP: 指定相关虚拟路由器的VIP地址;

-m, --mode {mm|mb}:指定虚拟路由的模型,mm表示主主,mb表示主备;它们表示相对于同一种服务而方,其VIP的工作类型;

-n, --notify {master|backup|fault}:指定通知的类型,即vrrp角色切换的目标角色;

-h, --help:获取脚本的使用帮助;

  1. #!/bin/bash
  2. # description: An example of notify script
  3. # Usage: notify.sh -m|--mode {mm|mb} -s|--service SERVICE1,... -a|--address VIP  -n|--notify {master|backup|falut} -h|--help

  4. helpflag=0
  5. serviceflag=0
  6. modeflag=0
  7. addressflag=0
  8. notifyflag=0

  9. contact='root@localhost'

  10. Usage() {
  11.   echo "Usage: notify.sh [-m|--mode {mm|mb}] [-s|--service SERVICE1,...] <-a|--address VIP>  <-n|--notify {master|backup|falut}>"
  12.   echo "Usage: notify.sh -h|--help"
  13. }

  14. ParseOptions() {
  15.   local I=1;
  16.   if [ $# -gt 0 ]; then
  17.     while [ $I -le $# ]; do
  18.       case $1 in
  19.   -s|--service)
  20. [ $# -lt 2 ] && return 3
  21.          serviceflag=1
  22.          services=(`echo $2|awk -F"," '{for(i=1;i<=NF;i++) print $i}'`)
  23. shift 2 ;;
  24.   -h|--help)
  25.          helpflag=1
  26. return 0
  27.         shift
  28. ;;
  29.   -a|--address)
  30. [ $# -lt 2 ] && return 3
  31.     addressflag=1
  32. vip=$2
  33. shift 2
  34. ;;
  35.   -m|--mode)
  36. [ $# -lt 2 ] && return 3
  37. mode=$2
  38. shift 2
  39. ;;
  40.   -n|--notify)
  41. [ $# -lt 2 ] && return 3
  42. notifyflag=1
  43. notify=$2
  44. shift 2
  45. ;;
  46.   *)
  47. echo "Wrong options..."
  48. Usage
  49. return 7
  50. ;;
  51.        esac
  52.     done
  53.     return 0
  54.   fi
  55. }

  56. #workspace=$(dirname $0)

  57. RestartService() {
  58.   if [ ${#@} -gt 0 ]; then
  59.     for I in $@; do
  60.       if [ -x /etc/rc.d/init.d/$I ]; then
  61.         /etc/rc.d/init.d/$I restart
  62.       else
  63.         echo "$I is not a valid service..."
  64.       fi
  65.     done
  66.   fi
  67. }

  68. StopService() {
  69.   if [ ${#@} -gt 0 ]; then
  70.     for I in $@; do
  71.       if [ -x /etc/rc.d/init.d/$I ]; then
  72.         /etc/rc.d/init.d/$I stop
  73.       else
  74.         echo "$I is not a valid service..."
  75.       fi
  76.     done
  77.   fi
  78. }


  79. Notify() {
  80.     mailsubject="`hostname` to be $1: $vip floating"
  81.     mailbody="`date '+%F %H:%M:%S'`, vrrp transition, `hostname` changed to be $1."
  82.     echo $mailbody | mail -s "$mailsubject" $contact
  83. }


  84. # Main Function
  85. ParseOptions $@
  86. [ $? -ne 0 ] && Usage && exit 5

  87. [ $helpflag -eq 1 ] && Usage && exit 0

  88. if [ $addressflag -ne 1 -o $notifyflag -ne 1 ]; then
  89.   Usage
  90.   exit 2
  91. fi

  92. mode=${mode:-mb}

  93. case $notify in
  94. 'master')
  95.   if [ $serviceflag -eq 1 ]; then
  96.       RestartService ${services[*]}
  97.   fi
  98.   Notify master
  99.   ;;
  100. 'backup')
  101.   if [ $serviceflag -eq 1 ]; then
  102.     if [ "$mode" == 'mb' ]; then
  103.       StopService ${services[*]}
  104.     else
  105.       RestartService ${services[*]}
  106.     fi
  107.   fi
  108.   Notify backup
  109.   ;;
  110. 'fault')
  111.   Notify fault
  112.   ;;
  113. *)
  114.   Usage
  115.   exit 4
  116.   ;;
  117. esac
复制代码

在keepalived.conf配置文件中,其调用方法如下所示:
  1. notify_master "/etc/keepalived/notify.sh -n master -a 172.16.100.1"  
  2. notify_backup "/etc/keepalived/notify.sh -n backup -a 172.16.100.1"  
  3. notify_fault "/etc/keepalived/notify.sh -n fault -a 172.16.100.1"  
复制代码







欢迎光临 黑帽联盟 (https://bbs.cnblackhat.com/) Powered by Discuz! X2.5