From be88190131f07aa5dbcd4d43461a4925554bae39 Mon Sep 17 00:00:00 2001 From: arstercz Date: Fri, 23 Jul 2021 14:14:55 +0800 Subject: [PATCH] fix ssh check when master is down --- bin/master_ip_failover | 4 ++++ bin/master_ip_online_change | 5 +++++ lib/MHA/Extra.pm | 22 ++++++++++++++++++++++ 3 files changed, 31 insertions(+) diff --git a/bin/master_ip_failover b/bin/master_ip_failover index ad6c3b4..a300538 100755 --- a/bin/master_ip_failover +++ b/bin/master_ip_failover @@ -400,6 +400,10 @@ sub main { option => $ssh_options, ); + if ( ! $iphelper->ssh_is_alive() ) { + print current_time_us() . "$orig_master_ssh_ip is not alive\n"; + exit 0; + } if ( $iphelper->check_node_vip( $cfg->{vip} ) ) { print current_time_us() . " vip $cfg->{vip} is configured\n"; exit 0; diff --git a/bin/master_ip_online_change b/bin/master_ip_online_change index 2a4c4ee..93af67a 100755 --- a/bin/master_ip_online_change +++ b/bin/master_ip_online_change @@ -573,6 +573,11 @@ sub main { option => $ssh_options, ); + if ( ! $iphelper->ssh_is_alive() ) { + print current_time_us() . "$orig_master_ssh_ip is not alive\n"; + exit 0; + } + if ( $iphelper->check_node_vip( $cfg->{vip} ) ) { print current_time_us() . " vip $cfg->{vip} is configured\n"; exit 0; diff --git a/lib/MHA/Extra.pm b/lib/MHA/Extra.pm index 5469b47..eac4f8d 100755 --- a/lib/MHA/Extra.pm +++ b/lib/MHA/Extra.pm @@ -395,6 +395,7 @@ package MHA::Extra::IpHelper; use strict; use warnings; +use IO::Socket::INET; use Carp; sub new { @@ -451,6 +452,27 @@ sub _safe_qx { } } +sub ssh_is_alive { + my $self = shift; + my $t = 0; + my $n = 2; + + while($n && $n--) { + my $socket = IO::Socket::INET->new( + PeerAddr => $self->{host}, + PeerPort => $self->{port}, + Proto => 'tcp', + Timeout => 2 + ) || $t++; + } + if ($t <= 1) { + return 1; + } + else { + return 0; + } +} + sub ssh_cmd { my ( $self, $cmd ) = @_; $self->{user} ||= 'root';