keepalive筆記之三:keepalived通知腳本進階示例

下面的腳本能夠接受選項,其中:linux

-s, --service SERVICE,...:指定服務腳本名稱,當狀態切換時可自動啓動、重啓或關閉此服務;bash

-a, --address VIP: 指定相關虛擬路由器的VIP地址;spa

-m, --mode {mm|mb}:指定虛擬路由的模型,mm表示主主,mb表示主備;它們表示相對於同一種服務而方,其VIP的工做類型;rest

-n, --notify {master|backup|fault}:指定通知的類型,即vrrp角色切換的目標角色;code

-h, --help:獲取腳本的使用幫助;blog

#!/bin/bash
# Author: MageEdu <linuxedu@foxmail.com>
# description: An example of notify script
# Usage: notify.sh -m|--mode {mm|mb} -s|--service SERVICE1,... -a|--address VIP  -n|--notify {master|backup|falut} -h|--help 
 
#contact='linuxedu@foxmail.com'
helpflag=0
serviceflag=0
modeflag=0
addressflag=0
notifyflag=0
 
contact='root@localhost'
 
Usage() {
  echo "Usage: notify.sh [-m|--mode {mm|mb}] [-s|--service SERVICE1,...] <-a|--address VIP>  <-n|--notify {master|backup|falut}>" 
  echo "Usage: notify.sh -h|--help"
}
 
ParseOptions() {
  local I=1;
  if [ $# -gt 0 ]; then
    while [ $I -le $# ]; do
      case $1 in
  -s|--service)
[ $# -lt 2 ] && return 3
         serviceflag=1
         services=(`echo $2|awk -F"," '{for(i=1;i<=NF;i++) print $i}'`)
shift 2 ;;
  -h|--help)
         helpflag=1
return 0
        shift
;;
  -a|--address)
[ $# -lt 2 ] && return 3
    addressflag=1
vip=$2
shift 2
;;
  -m|--mode)
[ $# -lt 2 ] && return 3
mode=$2
shift 2
;;
  -n|--notify)
[ $# -lt 2 ] && return 3
notifyflag=1
notify=$2
shift 2
;;
  *)
echo "Wrong options..."
Usage
return 7
;;
       esac
    done
    return 0
  fi
}
 
#workspace=$(dirname $0)
 
RestartService() {
  if [ ${#@} -gt 0 ]; then
    for I in $@; do
      if [ -x /etc/rc.d/init.d/$I ]; then
        /etc/rc.d/init.d/$I restart
      else
        echo "$I is not a valid service..."
      fi
    done
  fi
}
 
StopService() {
  if [ ${#@} -gt 0 ]; then
    for I in $@; do
      if [ -x /etc/rc.d/init.d/$I ]; then
        /etc/rc.d/init.d/$I stop
      else
        echo "$I is not a valid service..."
      fi
    done
  fi
}
 
 
Notify() {
    mailsubject="`hostname` to be $1: $vip floating"
    mailbody="`date '+%F %H:%M:%S'`, vrrp transition, `hostname` changed to be $1."
    echo $mailbody | mail -s "$mailsubject" $contact
}
 
 
# Main Function
ParseOptions $@
[ $? -ne 0 ] && Usage && exit 5
 
[ $helpflag -eq 1 ] && Usage && exit 0
 
if [ $addressflag -ne 1 -o $notifyflag -ne 1 ]; then
  Usage
  exit 2
fi
 
mode=${mode:-mb}
 
case $notify in
'master')
  if [ $serviceflag -eq 1 ]; then
      RestartService ${services[*]}
  fi
  Notify master
  ;;
'backup')
  if [ $serviceflag -eq 1 ]; then
    if [ "$mode" == 'mb' ]; then
      StopService ${services[*]}
    else
      RestartService ${services[*]}
    fi
  fi
  Notify backup
  ;;
'fault')
  Notify fault
  ;;
*)
  Usage
  exit 4
  ;;
esac

在keepalived.conf配置文件中,其調用方法以下所示:ip

notify_master "/etc/keepalived/notify.sh -n master -a 172.16.100.1"  
notify_backup "/etc/keepalived/notify.sh -n backup -a 172.16.100.1"  
notify_fault "/etc/keepalived/notify.sh -n fault -a 172.16.100.1"  
相關文章
相關標籤/搜索