master_ip_failover.sh脚本是用perl编写的,可以在mha-manager源码包中可以找到,下面给出的结合keepalived进行自动切换的脚本:

[root@mha scripts]# cat master_ip_failover.sh
#!/usr/bin/env perl
#  Copyright (C) 2011 DeNA Co.,Ltd.
#
#  This program is free software; you can redistribute it and/or modify
#  it under the terms of the GNU General Public License as published by
#  the Free Software Foundation; either version 2 of the License, or
#  (at your option) any later version.
#
#  This program is distributed in the hope that it will be useful,
#  but WITHOUT ANY WARRANTY; without even the implied warranty of
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#  GNU General Public License for more details.
#
#  You should have received a copy of the GNU General Public License
#   along with this program; if not, write to the Free Software
#  Foundation, Inc.,
#  51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
## Note: This is a sample script and is not complete. Modify the script based on your environment.
#!/usr/bin/env perl
use strict;
use warnings FATAL => 'all';
use Getopt::Long;
use MHA::DBHelper;
my (
    $command,          $ssh_user,        $orig_master_host, $orig_master_ip,
    $orig_master_port, $new_master_host, $new_master_ip,    $new_master_port
);
my $vip = '192.168.56.188';
my $ssh_start_vip = "/etc/init.d/keepalived start";
my $ssh_stop_vip = "/etc/init.d/keepalived stop";
GetOptions(
    'command=s'          => \$command,
    'ssh_user=s'         => \$ssh_user,
    'orig_master_host=s' => \$orig_master_host,
    'orig_master_ip=s'   => \$orig_master_ip,
    'orig_master_port=i' => \$orig_master_port,
    'new_master_host=s'  => \$new_master_host,
    'new_master_ip=s'    => \$new_master_ip,
    'new_master_port=i'  => \$new_master_port,
);
exit &main();
sub main {
    print "\n\nIN SCRIPT TEST====$ssh_stop_vip==$ssh_start_vip===\n\n";
    if ( $command eq "stop" || $command eq "stopssh" ) {
        my $exit_code = 1;
        eval {
            print "Disabling the VIP on old master: $orig_master_host \n";
            &stop_vip();
            $exit_code = 0;
        };
        if ($@) {
            warn "Got Error: $@\n";
            exit $exit_code;
        }
        exit $exit_code;
    }
    elsif ( $command eq "start" ) {
        my $exit_code = 10;
        eval {
            print "Enabling the VIP - $vip on the new master - $new_master_host \n";
            &start_vip();
            $exit_code = 0;
        };
        if ($@) {
            warn $@;
            exit $exit_code;
        }
        exit $exit_code;
    }
    elsif ( $command eq "status" ) {
        print "Checking the Status of the script.. OK \n";
        #`ssh $ssh_user\@cluster1 \" $ssh_start_vip \"`;
        exit 0;
    }
    else {
        &usage();
        exit 1;
    }
}
# A simple system call that enable the VIP on the new master
sub start_vip() {
    `ssh $ssh_user\@$new_master_host \" $ssh_start_vip \"`;
}
# A simple system call that disable the VIP on the old_master
sub stop_vip() {
my $ssh_user = "root"; 
    `ssh $ssh_user\@$orig_master_host \" $ssh_stop_vip \"`;
}
sub usage {
    print
    "Usage: master_ip_failover --command=start|stop|stopssh|status --orig_master_host=host --orig_master_ip=ip --orig_master_port=port --new_master_host=host --new_master_ip=ip --new_master_port=port\n";
}

最新文章

  1. nginx部分命令
  2. log4net一些配置说明
  3. null、空对象和undefined
  4. Apache禁止目录访问的方法
  5. linux命令----网络地址
  6. POJ C++程序设计 编程题#1 编程作业—运算符重载
  7. SQL嵌套查寻初识,以及SOME ANY EXISTS的基础常识
  8. hdu1429之BFS
  9. 浙江大学PAT考试1009~1012(1010上帝是冠军。。)
  10. 1.2为什么需要public static void main(String[] args)这个方法
  11. 自动化服务部署(二):Linux下安装jenkins
  12. J2EE进阶(十五)MyEclipse反向工程实现从数据库反向生成实体类之Hibernate方式
  13. UE4 创建自己的角色
  14. vm12 和14密钥
  15. HotSpot 虚拟机垃圾回收算法实现
  16. git操作中出现Unlink of file '......' failed. Should I try again?
  17. 用 Lua 控制 MIDI 合成器来播放自定义格式乐谱
  18. MySql 安装与使用图文教程
  19. springmvc中Controller方法的返回值
  20. tomcat+ngnix单机搭建集群及端口占用问题

热门文章

  1. linux运维、架构之路-linux文件权限
  2. php array_chunk()函数 语法
  3. CKEDITOR Copying images from word
  4. 数据库的特性与隔离级别和spring事务的传播机制和隔离级别
  5. js点击获取—通过JS获取图片的相对坐标位置
  6. Jenkins报错:该Jenkins实例似乎已离线
  7. Uva 12563 Jin Ge Jin Qu hao(01背包)
  8. 【CDN+】 Kylin 的初步认识与理解
  9. Spring 注解配置 WebApplicationContext
  10. CentOS 7.0 开端口