Skip to content

Commit

Permalink
fix ssh check when master is down
Browse files Browse the repository at this point in the history
  • Loading branch information
arstercz committed Jul 23, 2021
1 parent d98f692 commit be88190
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 0 deletions.
4 changes: 4 additions & 0 deletions bin/master_ip_failover
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
5 changes: 5 additions & 0 deletions bin/master_ip_online_change
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
22 changes: 22 additions & 0 deletions lib/MHA/Extra.pm
Original file line number Diff line number Diff line change
Expand Up @@ -395,6 +395,7 @@ package MHA::Extra::IpHelper;
use strict;
use warnings;
use IO::Socket::INET;
use Carp;
sub new {
Expand Down Expand Up @@ -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';
Expand Down

0 comments on commit be88190

Please sign in to comment.