pt-slave-find added --resolve-address option

This commit is contained in:
Frank Cizmich
2015-03-19 17:44:39 -03:00
parent 16a7ffa6f9
commit 29cbec8b75
2 changed files with 52 additions and 6 deletions

View File

@@ -3778,6 +3778,7 @@ sub main {
node => $root, node => $root,
print_node => $print_node, print_node => $print_node,
MasterSlave => $ms, MasterSlave => $ms,
resolve_address => $o->get('resolve-address'),
); );
return 0; return 0;
@@ -3823,7 +3824,7 @@ sub print_slaves {
sub print_node_hostname { sub print_node_hostname {
my ( %args ) = @_; my ( %args ) = @_;
my ($ms, $node, $level) = @args{qw(MasterSlave node level)}; my ($ms, $node, $level, $resolve_address) = @args{qw(MasterSlave node level resolve_address)};
die "I need a node" unless $node; die "I need a node" unless $node;
$level ||= 0; $level ||= 0;
@@ -3833,15 +3834,16 @@ sub print_node_hostname {
my $prefix = $level ? (' ' x (($level-1)*3) . '+- ') : ''; my $prefix = $level ? (' ' x (($level-1)*3) . '+- ') : '';
PTDEBUG && _d('level', $level, 'host', $host); PTDEBUG && _d('level', $level, 'host', $host);
print "$prefix$host\n";
print_host($prefix, $host, $resolve_address);
return; return;
} }
sub print_node_summary { sub print_node_summary {
my ( %args ) = @_; my ( %args ) = @_;
my ($ms, $node, $level) my ($ms, $node, $level, $resolve_address)
= @args{qw(MasterSlave node level)}; = @args{qw(MasterSlave node level resolve_address)};
die "I need a node" unless $node; die "I need a node" unless $node;
$level ||= 0; $level ||= 0;
@@ -3851,7 +3853,7 @@ sub print_node_summary {
PTDEBUG && _d('level', $level, 'host', $host); PTDEBUG && _d('level', $level, 'host', $host);
print "$prefix$host\n"; print_host($prefix, $host, $resolve_address);
my $dbh = $node->{dbh}; my $dbh = $node->{dbh};
if ( !$dbh ) { if ( !$dbh ) {
@@ -3923,6 +3925,28 @@ sub print_node_summary {
return; return;
} }
sub print_host {
my ($prefix, $host, $resolve_address) = @_;
my $hostname;
# resolve address to hostname if user requested it
if($resolve_address) {
use Socket;
my $without_port = $host;
$without_port =~ s/:\d*$//; # strip port from ip address
my $packed = inet_aton($without_port);
if ($packed) {
$hostname = gethostbyaddr($packed, AF_INET);
}
}
if ($hostname) {
print "$prefix$host ($hostname)\n";
}
else {
print "$prefix$host\n";
}
}
sub _d { sub _d {
my ($package, undef, $line) = caller 0; my ($package, undef, $line) = caller 0;
@_ = map { (my $temp = $_) =~ s/\n/\n# /g; $temp; } @_ = map { (my $temp = $_) =~ s/\n/\n# /g; $temp; }
@@ -4155,6 +4179,16 @@ about each slave, like:
=back =back
=item --resolve-address
Resolve ip-address to hostname. Report will print both IP and hostname.
Example:
10.10.7.14 (dbase1.sample.net)
Might delay runtime a few seconds.
=item --set-vars =item --set-vars
type: Array type: Array

View File

@@ -43,7 +43,7 @@ elsif ( !$slave2_dbh ) {
plan skip_all => 'Cannot connect to second sandbox slave'; plan skip_all => 'Cannot connect to second sandbox slave';
} }
else { else {
plan tests => 9; plan tests => 10;
} }
my @args = ('h=127.0.0.1,P=12345,u=msandbox,p=msandbox'); my @args = ('h=127.0.0.1,P=12345,u=msandbox,p=msandbox');
@@ -67,6 +67,17 @@ my $expected = <<EOF;
EOF EOF
is($output, $expected, 'Master with slave and slave of slave'); is($output, $expected, 'Master with slave and slave of slave');
###############################################################################
# Test --resolve-hostname option (we don't know the hostname of the test
# machine so we settle for any non null string)
###############################################################################
$output = `$trunk/bin/pt-slave-find -h 127.0.0.1 -P 12345 -u msandbox -p msandbox --report-format hostname --resolve-address`;
like (
$output,
qr/127\.0\.0\.1:12345\s+\(\w+\)/s,
"--resolve-address option"
) or diag($output);
# ############################################################################# # #############################################################################
# Until MasterSlave::find_slave_hosts() is improved to overcome the problems # Until MasterSlave::find_slave_hosts() is improved to overcome the problems
# with SHOW SLAVE HOSTS, this test won't work. # with SHOW SLAVE HOSTS, this test won't work.
@@ -146,6 +157,7 @@ ok(
"Summary report format", "Summary report format",
); );
# ############################################################################# # #############################################################################
# Done. # Done.
# ############################################################################# # #############################################################################