zoukankan      html  css  js  c++  java
  • keepalive笔记之三:keepalived通知脚本进阶示例

    下面的脚本可以接受选项,其中:

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

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

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

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

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

    #!/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配置文件中,其调用方法如下所示:

    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"  
  • 相关阅读:
    ASP.NET服务器控件开发(4)复合控件
    C#特性对象集合初始化器
    C#特性匿名类型与隐式类型局部变量
    在Handler中使用Session
    使用 UDPClient 生成聊天客户端
    当下10大最热门的网站开发技术
    C#特性扩展方法
    50个非常有用的PHP工具
    c# 调用.bat文件
    c# 特性/属性(Attribute) 以及使用反射查看自定义特性
  • 原文地址:https://www.cnblogs.com/djoker/p/6396685.html
Copyright © 2011-2022 走看看