腳本node
#!/bin/bash # Author: freeloda # 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 #下面定義了一些變量,經過ParseOptions函數,把腳本的位置變量參數賦值給這些變量。測試 contact='root' helpflag=0 serviceflag=0 modeflag=0 addressflag=0 notifyflag=0 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
執行結果bash
[root@node21 keepalived]# ./notify.sh Usage: notify.sh [-m|--mode {mm|mb}] [-s|--service SERVICE1,...] <-a|--address VIP> <-n|--notify {master|backup|falut}> Usage: notify.sh -h|--help [root@node21 keepalived]# bash -x notify.sh -s httpd,keepalived -a 1.1.1.1 -n master -m mb + contact=root + helpflag=0 + serviceflag=0 + modeflag=0 + addressflag=0 + notifyflag=0 + ParseOptions -s httpd,keepalived -a 1.1.1.1 -n master -m mb + local I=1 + '[' 8 -gt 0 ']' + '[' 1 -le 8 ']' + case $1 in + '[' 8 -lt 2 ']' + serviceflag=1 + services=(`echo $2|awk -F"," '{for(i=1;i<=NF;i++) print $i}'`) ++ echo httpd,keepalived ++ awk -F, '{for(i=1;i<=NF;i++) print $i}' + shift 2 + '[' 1 -le 6 ']' + case $1 in + '[' 6 -lt 2 ']' + addressflag=1 + vip=1.1.1.1 + shift 2 + '[' 1 -le 4 ']' + case $1 in + '[' 4 -lt 2 ']' + notifyflag=1 + notify=master + shift 2 + '[' 1 -le 2 ']' + case $1 in + '[' 2 -lt 2 ']' + mode=mb + shift 2 + '[' 1 -le 0 ']' + return 0 + '[' 0 -ne 0 ']' + '[' 0 -eq 1 ']' + '[' 1 -ne 1 -o 1 -ne 1 ']' + mode=mb + case $notify in + '[' 1 -eq 1 ']' + RestartService httpd keepalived + '[' 2 -gt 0 ']' + for I in '$@' + '[' -x /etc/rc.d/init.d/httpd ']' + /etc/rc.d/init.d/httpd restart 中止 httpd: [肯定] 正在啓動 httpd: [肯定] + for I in '$@' + '[' -x /etc/rc.d/init.d/keepalived ']' + /etc/rc.d/init.d/keepalived restart 中止 keepalived: [肯定] 正在啓動 keepalived: [肯定] + Notify master ++ hostname + mailsubject='node21.xx.com to be master: 1.1.1.1 floating' ++ date '+%F %H:%M:%S' ++ hostname + mailbody='2014-07-24 09:45:18, vrrp transition, node21.xx.com changed to be master.' + echo 2014-07-24 09:45:18, vrrp transition, node21.xx.com changed to be master. + mail -s 'node21.xx.com to be master: 1.1.1.1 floating' root