Permalink
Browse files

* New Feature: Supporting ssh_host and ssh_port parameters

  • Loading branch information...
1 parent 06dd340 commit 0e0bb1bccfced545dfa45e2747d8d55bf0a7b444 @yoshinorim committed Nov 9, 2011
@@ -26,14 +26,18 @@ use Pod::Usage;
use MHA::ManagerConst;
my @monitoring_servers;
-my ( $help, $version, $ssh_user, $master_host, $master_ip, $master_port );
+my (
+ $help, $version, $ssh_user, $ssh_port,
+ $master_host, $master_ip, $master_port
+);
$| = 1;
GetOptions(
'help' => \$help,
'version' => \$version,
'secondary_host=s' => \@monitoring_servers,
'user=s' => \$ssh_user,
+ 'port=s' => \$ssh_port,
'master_host=s' => \$master_host,
'master_ip=s' => \$master_ip,
'master_port=i' => \$master_port,
@@ -53,6 +57,7 @@ unless ($master_host) {
}
$ssh_user = "root" unless ($ssh_user);
+$ssh_port = 22 unless ($ssh_port);
$master_port = 3306 unless ($master_port);
sub exit_by_signal {
@@ -69,7 +74,7 @@ my $exit_code = 0;
foreach my $monitoring_server (@monitoring_servers) {
my $ssh_user_host = $ssh_user . '@' . $monitoring_server;
my $command =
- "ssh $MHA::ManagerConst::SSH_OPT_CHECK $ssh_user_host \"perl -e "
+"ssh $MHA::ManagerConst::SSH_OPT_CHECK -p $ssh_port $ssh_user_host \"perl -e "
. "\\\"use IO::Socket::INET; my \\\\\\\$sock = IO::Socket::INET->new"
. "(PeerAddr => \\\\\\\"$master_host\\\\\\\", PeerPort=> $master_port, "
. "Proto =>'tcp', Timeout => 4); if(\\\\\\\$sock) { close(\\\\\\\$sock); "
View
@@ -31,7 +31,7 @@ use MHA::NodeUtil;
use MHA::ManagerConst;
my @PARAM_ARRAY =
- qw/ hostname ip port node_label candidate_master no_master ignore_fail skip_init_ssh_check skip_reset_slave user password repl_user repl_password disable_log_bin master_pid_file handle_raw_binlog ssh_user remote_workdir master_binlog_dir log_level manager_workdir manager_log check_repl_delay check_repl_filter latest_priority multi_tier_slave ping_interval ping_type secondary_check_script master_ip_failover_script master_ip_online_change_script shutdown_script report_script init_conf_load_script /;
+ qw/ hostname ip port ssh_host ssh_ip ssh_port node_label candidate_master no_master ignore_fail skip_init_ssh_check skip_reset_slave user password repl_user repl_password disable_log_bin master_pid_file handle_raw_binlog ssh_user remote_workdir master_binlog_dir log_level manager_workdir manager_log check_repl_delay check_repl_filter latest_priority multi_tier_slave ping_interval ping_type secondary_check_script master_ip_failover_script master_ip_online_change_script shutdown_script report_script init_conf_load_script /;
my %PARAM;
for (@PARAM_ARRAY) { $PARAM{$_} = 1; }
@@ -68,6 +68,27 @@ sub parse_server {
$value{port} = $default->{port};
$value{port} = 3306 unless ( $value{port} );
}
+
+ $value{ssh_host} = $param_arg->{ssh_host};
+ if ( !defined( $value{ssh_host} ) ) {
+ $value{ssh_host} = $value{hostname};
+ }
+ if ( $value{hostname}
+ && $value{ssh_host}
+ && $value{ssh_host} eq $value{hostname} )
+ {
+ $value{ssh_ip} = $value{ip};
+ }
+ else {
+ $value{ssh_ip} = MHA::NodeUtil::get_ip( $value{ssh_host} );
+ }
+
+ $value{ssh_port} = $param_arg->{ssh_port};
+ if ( !defined( $value{ssh_port} ) ) {
+ $value{ssh_port} = $default->{ssh_port};
+ $value{ssh_port} = 22 unless ( $value{ssh_port} );
+ }
+
$value{user} = $param_arg->{user};
if ( !defined( $value{user} ) ) {
$value{user} = $default->{user};
@@ -43,6 +43,9 @@ sub new {
hostname => undef,
port => undef,
ssh_user => undef,
+ ssh_host => undef,
+ ssh_ip => undef,
+ ssh_port => undef,
workdir => undef,
status_handler => undef,
secondary_check_script => undef,
@@ -265,35 +268,38 @@ sub ping_select($) {
sub ssh_check {
my $ssh_user = shift;
- my $hostname = shift;
- my $ip = shift;
+ my $ssh_host = shift;
+ my $ssh_ip = shift;
+ my $ssh_port = shift;
my $log = shift;
my $num_secs_to_timeout = shift;
- my $ssh_user_host = $ssh_user . '@' . $ip;
+ my $ssh_user_host = $ssh_user . '@' . $ssh_ip;
my $rc = 1;
my $command = "exit 0";
eval {
if ( my $pid = fork )
{
local $SIG{ALRM} = sub {
kill 9, $pid;
- die "Got timeout on checking SSH connection to $hostname!";
+ die "Got timeout on checking SSH connection to $ssh_host!";
};
alarm $num_secs_to_timeout;
waitpid( $pid, 0 );
alarm 0;
my $exit_code = $? >> 8;
if ( $exit_code == 0 ) {
- $log->info("HealthCheck: SSH to $hostname is reachable.");
+ $log->info("HealthCheck: SSH to $ssh_host is reachable.");
$rc = 0;
}
else {
- $log->warning("HealthCheck: SSH to $hostname is NOT reachable.");
+ $log->warning("HealthCheck: SSH to $ssh_host is NOT reachable.");
$rc = 1;
}
}
elsif ( defined $pid ) {
- exec("ssh $MHA::ManagerConst::SSH_OPT_CHECK $ssh_user_host $command");
+ exec(
+"ssh $MHA::ManagerConst::SSH_OPT_CHECK -p $ssh_port $ssh_user_host $command"
+ );
}
else {
croak "Forking SSH connection process failed!\n";
@@ -401,8 +407,8 @@ sub invoke_ssh_check {
#child ssh check process
exit ssh_check(
- $self->{ssh_user}, $self->{hostname}, $self->{ip},
- $self->{logger}, $self->{interval} * 3
+ $self->{ssh_user}, $self->{ssh_host}, $self->{ssh_ip},
+ $self->{ssh_port}, $self->{logger}, $self->{interval} * 3
);
}
else {
@@ -69,33 +69,39 @@ sub exec_system {
}
}
-sub exec_ssh_check_cmd($$$) {
+sub exec_ssh_check_cmd($$$$) {
my $ssh_host = shift;
+ my $ssh_port = shift;
my $ssh_cmd = shift;
my $log_output = shift;
my $ret;
return exec_system(
- "ssh $MHA::ManagerConst::SSH_OPT_CHECK $ssh_host $ssh_cmd", $log_output );
+ "ssh $MHA::ManagerConst::SSH_OPT_CHECK -p $ssh_port $ssh_host $ssh_cmd",
+ $log_output );
}
-sub exec_ssh_cmd($$$) {
+sub exec_ssh_cmd($$$$) {
my $ssh_host = shift;
+ my $ssh_port = shift;
my $ssh_cmd = shift;
my $log_output = shift;
my $ret;
return exec_system(
- "ssh $MHA::ManagerConst::SSH_OPT_ALIVE $ssh_host $ssh_cmd", $log_output );
+ "ssh $MHA::ManagerConst::SSH_OPT_ALIVE -p $ssh_port $ssh_host $ssh_cmd",
+ $log_output );
}
sub check_node_version {
my $log = shift;
my $ssh_user = shift;
my $ssh_host = shift;
my $ssh_ip = shift;
+ my $ssh_port = shift;
my $ssh_user_host;
my $node_version;
eval {
my $command = "apply_diff_relay_logs --version";
+
if ( $ssh_host || $ssh_ip ) {
if ($ssh_ip) {
$ssh_user_host = $ssh_user . '@' . $ssh_ip;
@@ -104,7 +110,7 @@ sub check_node_version {
$ssh_user_host = $ssh_user . '@' . $ssh_host;
}
$command =
-"ssh $MHA::ManagerConst::SSH_OPT_ALIVE $ssh_user_host \"$command\" 2>&1";
+"ssh $MHA::ManagerConst::SSH_OPT_ALIVE $ssh_user_host -p $ssh_port \"$command\" 2>&1";
}
my $v = `$command`;
@@ -140,9 +146,10 @@ sub check_node_version_nodie {
my $ssh_user = shift;
my $ssh_host = shift;
my $ssh_ip = shift;
+ my $ssh_port = shift;
my $rc = 1;
eval {
- check_node_version( $log, $ssh_user, $ssh_host, $ssh_ip );
+ check_node_version( $log, $ssh_user, $ssh_host, $ssh_ip, $ssh_port );
$rc = 0;
};
if ($@) {
@@ -83,9 +83,10 @@ sub exit_by_signal {
}
sub exec_ssh_child_cmd {
- my ( $ssh_user_host, $ssh_cmd, $logger, $file ) = @_;
+ my ( $ssh_user_host, $ssh_port, $ssh_cmd, $logger, $file ) = @_;
my ( $high, $low ) =
- MHA::ManagerUtil::exec_ssh_cmd( $ssh_user_host, $ssh_cmd, $file );
+ MHA::ManagerUtil::exec_ssh_cmd( $ssh_user_host, $ssh_port, $ssh_cmd,
+ $file );
if ( $logger && $file ) {
$logger->info( "\n" . `cat $file` );
unlink $file;
@@ -320,6 +321,7 @@ sub force_shutdown_internal($) {
else {
$command .= " --command=stop";
}
+ $command .= $dead_master->get_ssh_args_if("orig");
$log->info("Executing master IP deactivatation script:");
$log->info(" $command");
my ( $high, $low ) = MHA::ManagerUtil::exec_system( $command, $g_logfile );
@@ -361,6 +363,7 @@ sub force_shutdown_internal($) {
" --host=$dead_master->{hostname} --ip=$dead_master->{ip} --port=$dead_master->{port} ";
$command .= " --pid_file=$dead_master->{master_pid_file}"
if ( $dead_master->{master_pid_file} );
+ $command .= $dead_master->get_ssh_args_if("shutdown");
$log->info("Executing SHUTDOWN script:");
$log->info(" $command");
my ( $high, $low ) = MHA::ManagerUtil::exec_system( $command, $g_logfile );
@@ -512,21 +515,23 @@ sub save_master_binlog_internal {
if ( $dead_master->{log_level} eq "debug" ) {
$command .= " --debug ";
}
- my $ssh_user_host = $dead_master->{ssh_user} . '@' . $dead_master->{ip};
+ my $ssh_user_host = $dead_master->{ssh_user} . '@' . $dead_master->{ssh_ip};
$log->info(
sprintf(
"Executing command on the dead master %s: %s",
$dead_master->get_hostinfo(), $command
)
);
my ( $high, $low ) =
- MHA::ManagerUtil::exec_ssh_cmd( $ssh_user_host, $command, $g_logfile );
+ MHA::ManagerUtil::exec_ssh_cmd( $ssh_user_host, $dead_master->{ssh_port},
+ $command, $g_logfile );
if ( $high == 0 && $low == 0 ) {
if (
MHA::NodeUtil::file_copy(
0, $_diff_binary_log,
$_diff_binary_log_remote, $dead_master->{ssh_user},
- $dead_master->{ip}, $g_logfile
+ $dead_master->{ssh_ip}, $g_logfile,
+ $dead_master->{ssh_port}
)
)
{
@@ -567,8 +572,9 @@ sub save_master_binlog {
MHA::ManagerUtil::check_node_version(
$log,
$dead_master->{ssh_user},
- $dead_master->{hostname},
- $dead_master->{ip}
+ $dead_master->{ssh_host},
+ $dead_master->{ssh_ip},
+ $dead_master->{ssh_port}
);
my $latest_file =
( $_server_manager->get_latest_slaves() )[0]->{Master_Log_File};
@@ -602,7 +608,8 @@ sub find_slave_with_all_relay_logs {
next if ( $latest_slave->{ssh_ok} == 0 );
}
- my $ssh_user_host = $latest_slave->{ssh_user} . '@' . $latest_slave->{ip};
+ my $ssh_user_host =
+ $latest_slave->{ssh_user} . '@' . $latest_slave->{ssh_ip};
my $command =
"apply_diff_relay_logs --command=find --latest_mlf=$latest_slave->{Master_Log_File} --latest_rmlp=$latest_slave->{Read_Master_Log_Pos} --target_mlf=$oldest_master_log_file --target_rmlp=$oldest_master_log_pos --server_id=$latest_slave->{server_id} --workdir=$latest_slave->{remote_workdir} --timestamp=$_start_datetime --manager_version=$MHA::ManagerConst::VERSION";
if ( $latest_slave->{relay_log_info_type} eq "TABLE" ) {
@@ -621,7 +628,8 @@ sub find_slave_with_all_relay_logs {
);
$log->info("Executing command: $command :");
my ( $high, $low ) =
- MHA::ManagerUtil::exec_ssh_cmd( $ssh_user_host, $command, $g_logfile );
+ MHA::ManagerUtil::exec_ssh_cmd( $ssh_user_host, $latest_slave->{ssh_port},
+ $command, $g_logfile );
if ( $high eq '0' && $low eq '0' ) {
$log->info("OK. $latest_slave->{hostname} has all relay logs.");
return $latest_slave;
@@ -774,7 +782,8 @@ sub send_binlog {
MHA::NodeUtil::file_copy(
1, $_diff_binary_log,
$_diff_binary_log_remote, $target->{ssh_user},
- $target->{ip}, $g_logfile
+ $target->{ssh_ip}, $g_logfile,
+ $target->{ssh_port}
)
)
{
@@ -805,9 +814,10 @@ sub generate_diff_from_readpos {
"Need to get diffs from the latest slave($latest_slave->{hostname}) up to: $latest_slave->{Master_Log_File}:$latest_slave->{Read_Master_Log_Pos} (using the latest slave's relay logs)"
);
- my $ssh_user_host = $latest_slave->{ssh_user} . '@' . $latest_slave->{ip};
+ my $ssh_user_host = $latest_slave->{ssh_user} . '@' . $latest_slave->{ssh_ip};
my ( $high, $low ) =
- MHA::ManagerUtil::exec_ssh_cmd( $ssh_user_host, "exit 0", $g_logfile );
+ MHA::ManagerUtil::exec_ssh_cmd( $ssh_user_host, $latest_slave->{ssh_port},
+ "exit 0", $g_logfile );
if ( $high ne '0' || $low ne '0' ) {
$logger->error("SSH authentication test failed. user=$ssh_user_host");
return ( $high, $low );
@@ -817,7 +827,11 @@ sub generate_diff_from_readpos {
"Connecting to the latest slave host $latest_slave->{hostname}, generating diff relay log files.."
);
my $command =
-"apply_diff_relay_logs --command=generate_and_send --scp_user=$target->{ssh_user} --scp_host=$target->{ip} --latest_mlf=$latest_slave->{Master_Log_File} --latest_rmlp=$latest_slave->{Read_Master_Log_Pos} --target_mlf=$target->{Master_Log_File} --target_rmlp=$target->{Read_Master_Log_Pos} --server_id=$latest_slave->{server_id} --diff_file_readtolatest=$target->{diff_file_readtolatest} --workdir=$latest_slave->{remote_workdir} --timestamp=$_start_datetime --handle_raw_binlog=$target->{handle_raw_binlog} --disable_log_bin=$target->{disable_log_bin} --manager_version=$MHA::ManagerConst::VERSION";
+"apply_diff_relay_logs --command=generate_and_send --scp_user=$target->{ssh_user} --scp_host=$target->{ssh_ip} --latest_mlf=$latest_slave->{Master_Log_File} --latest_rmlp=$latest_slave->{Read_Master_Log_Pos} --target_mlf=$target->{Master_Log_File} --target_rmlp=$target->{Read_Master_Log_Pos} --server_id=$latest_slave->{server_id} --diff_file_readtolatest=$target->{diff_file_readtolatest} --workdir=$latest_slave->{remote_workdir} --timestamp=$_start_datetime --handle_raw_binlog=$target->{handle_raw_binlog} --disable_log_bin=$target->{disable_log_bin} --manager_version=$MHA::ManagerConst::VERSION";
+ if ( $target->{ssh_port} ne 22 ) {
+ $command .= " --scp_port=$target->{ssh_port}";
+ }
+
if ( $latest_slave->{relay_log_info_type} eq "TABLE" ) {
$command .=
" --relay_dir=$latest_slave->{relay_dir} --current_relay_log=$latest_slave->{current_relay_log} ";
@@ -833,8 +847,8 @@ sub generate_diff_from_readpos {
$command .= " --debug ";
}
$logger->info("Executing command: $command");
- return exec_ssh_child_cmd( $ssh_user_host, $command, $logger,
- "$g_workdir/$target->{hostname}_$target->{port}.work" );
+ return exec_ssh_child_cmd( $ssh_user_host, $target->{ssh_port}, $command,
+ $logger, "$g_workdir/$target->{hostname}_$target->{port}.work" );
}
# 0: no need to generate diff
@@ -1028,8 +1042,9 @@ sub gen_diff_from_exec_to_read {
$logger->error("Failed to connect via SSH!");
return 1;
}
- my ( $high, $low ) = exec_ssh_child_cmd( $ssh_user_host, $command, $logger,
- "$g_workdir/$target->{hostname}_$target->{port}.work" );
+ my ( $high, $low ) =
+ exec_ssh_child_cmd( $ssh_user_host, $target->{ssh_port}, $command,
+ $logger, "$g_workdir/$target->{hostname}_$target->{port}.work" );
if ( $high eq '0' && $low eq '0' ) {
return 0;
}
@@ -1108,7 +1123,8 @@ sub apply_diff {
$logger->error("Failed to connect via SSH!");
return ( 1, 0 );
}
- my ( $high, $low ) = exec_ssh_child_cmd( $ssh_user_host, $command, $logger,
+ my ( $high, $low ) =
+ exec_ssh_child_cmd( $ssh_user_host, $target->{ssh_port}, $command, $logger,
"$g_workdir/$target->{hostname}_$target->{port}.work" );
$target->set_default_max_allowed_packet($logger);
@@ -1198,6 +1214,8 @@ sub recover_master($$$) {
if ( $new_master->{master_ip_failover_script} ) {
my $command =
"$new_master->{master_ip_failover_script} --command=start --ssh_user=$new_master->{ssh_user} --orig_master_host=$dead_master->{hostname} --orig_master_ip=$dead_master->{ip} --orig_master_port=$dead_master->{port} --new_master_host=$new_master->{hostname} --new_master_ip=$new_master->{ip} --new_master_port=$new_master->{port}";
+ $command .= $dead_master->get_ssh_args_if("orig");
+ $command .= $new_master->get_ssh_args_if("new");
$log->info("Executing master IP activate script:");
$log->info(" $command");
my ( $high, $low ) = MHA::ManagerUtil::exec_system( $command, $g_logfile );
Oops, something went wrong.

0 comments on commit 0e0bb1b

Please sign in to comment.