提示信息

master_ip_failover脚本

峰子 已关注
  1. #!/usr/bin/env perl
  2. use strict;
  3. use warnings FATAL => 'all';
  4. use Getopt::Long;
  5. my (
  6. $command, $ssh_user, $orig_master_host, $orig_master_ip,
  7. $orig_master_port, $new_master_host, $new_master_ip, $new_master_port
  8. );
  9. my $vip = '192.168.80.100/24';
  10. my $key = '0';
  11. my $ssh_start_vip = "/sbin/ifconfig eth0:$key $vip";
  12. my $ssh_stop_vip = "/sbin/ifconfig eth0:$key down";
  13. GetOptions(
  14. 'command=s' => \$command,
  15. 'ssh_user=s' => \$ssh_user,
  16. 'orig_master_host=s' => \$orig_master_host,
  17. 'orig_master_ip=s' => \$orig_master_ip,
  18. 'orig_master_port=i' => \$orig_master_port,
  19. 'new_master_host=s' => \$new_master_host,
  20. 'new_master_ip=s' => \$new_master_ip,
  21. 'new_master_port=i' => \$new_master_port,
  22. );
  23. exit &main();
  24. sub main {
  25. print "\n\nIN SCRIPT TEST====$ssh_stop_vip==$ssh_start_vip===\n\n";
  26. if ( $command eq "stop" || $command eq "stopssh" ) {
  27. my $exit_code = 1;
  28. eval {
  29. print "Disabling the VIP on old master: $orig_master_host \n";
  30. &stop_vip();
  31. $exit_code = 0;
  32. };
  33. if ($@) {
  34. warn "Got Error: $@\n";
  35. exit $exit_code;
  36. }
  37. exit $exit_code;
  38. }
  39. elsif ( $command eq "start" ) {
  40. my $exit_code = 10;
  41. eval {
  42. print "Enabling the VIP - $vip on the new master - $new_master_host \n";
  43. &start_vip();
  44. $exit_code = 0;
  45. };
  46. if ($@) {
  47. warn $@;
  48. exit $exit_code;
  49. }
  50. exit $exit_code;
  51. }
  52. elsif ( $command eq "status" ) {
  53. print "Checking the Status of the script.. OK \n";
  54. exit 0;
  55. }
  56. else {
  57. &usage();
  58. exit 1;
  59. }
  60. }
  61. sub start_vip() {
  62. `ssh $ssh_user\@$new_master_host \" $ssh_start_vip \"`;
  63. }
  64. sub stop_vip() {
  65. return 0 unless ($ssh_user);
  66. `ssh $ssh_user\@$orig_master_host \" $ssh_stop_vip \"`;
  67. }
  68. sub usage {
  69. print
  70. "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";
  71. }
    遇到难题? "AI大模型GPT4.0、GPT" 是你的私人解答专家! 点击按钮去提问......
峰子 关注 已关注

最近一次登录:2019-10-10 14:28:58   

暂时还没有签名,请关注我或评论我的文章
×
免费图表工具,画流程图、架构图