Merge lp:~percona-toolkit-dev/percona-toolkit/pt-slave-find-add-resolve-address-option-1418446 into lp:~percona-toolkit-dev/percona-toolkit/release-2.2.14

Proposed by Frank Cizmich
Status: Merged
Approved by: Daniel Nichter
Approved revision: 614
Merged at revision: 621
Proposed branch: lp:~percona-toolkit-dev/percona-toolkit/pt-slave-find-add-resolve-address-option-1418446
Merge into: lp:~percona-toolkit-dev/percona-toolkit/release-2.2.14
Diff against target: 134 lines (+52/-6)
2 files modified
bin/pt-slave-find (+39/-5)
t/pt-slave-find/pt-slave-find.t (+13/-1)
To merge this branch: bzr merge lp:~percona-toolkit-dev/percona-toolkit/pt-slave-find-add-resolve-address-option-1418446
Reviewer Review Type Date Requested Status
Daniel Nichter Approve
Review via email: mp+253576@code.launchpad.net

Description of the change

In response to feature request:

Adds option --resolve-address to pt-slave-find.
This makes the tool resolve the IP address of the server to a hostname and print it out within parenthesis alongside the IP.

Example:

127.0.0.1:12345 (db1.sample.net)
+- 127.0.0.1:12346 (db2.sample.net)
   +- 127.0.0.1:12347 (db3.sample.net)

Off by default

To post a comment you must log in.
Revision history for this message
Daniel Nichter (daniel-nichter) wrote :

Just one spelling suggestion, otherwise code is fine.

review: Approve

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'bin/pt-slave-find'
2--- bin/pt-slave-find 2015-01-23 10:19:56 +0000
3+++ bin/pt-slave-find 2015-03-19 20:58:57 +0000
4@@ -3778,6 +3778,7 @@
5 node => $root,
6 print_node => $print_node,
7 MasterSlave => $ms,
8+ resolve_address => $o->get('resolve-address'),
9 );
10
11 return 0;
12@@ -3823,7 +3824,7 @@
13
14 sub print_node_hostname {
15 my ( %args ) = @_;
16- my ($ms, $node, $level) = @args{qw(MasterSlave node level)};
17+ my ($ms, $node, $level, $resolve_address) = @args{qw(MasterSlave node level resolve_address)};
18 die "I need a node" unless $node;
19 $level ||= 0;
20
21@@ -3833,15 +3834,16 @@
22 my $prefix = $level ? (' ' x (($level-1)*3) . '+- ') : '';
23
24 PTDEBUG && _d('level', $level, 'host', $host);
25- print "$prefix$host\n";
26+
27+ print_host($prefix, $host, $resolve_address);
28
29 return;
30 }
31
32 sub print_node_summary {
33 my ( %args ) = @_;
34- my ($ms, $node, $level)
35- = @args{qw(MasterSlave node level)};
36+ my ($ms, $node, $level, $resolve_address)
37+ = @args{qw(MasterSlave node level resolve_address)};
38 die "I need a node" unless $node;
39 $level ||= 0;
40
41@@ -3851,7 +3853,7 @@
42
43 PTDEBUG && _d('level', $level, 'host', $host);
44
45- print "$prefix$host\n";
46+ print_host($prefix, $host, $resolve_address);
47
48 my $dbh = $node->{dbh};
49 if ( !$dbh ) {
50@@ -3923,6 +3925,28 @@
51 return;
52 }
53
54+sub print_host {
55+ my ($prefix, $host, $resolve_address) = @_;
56+
57+ my $hostname;
58+ # resolve address to hostname if user requested it
59+ if($resolve_address) {
60+ use Socket;
61+ my $without_port = $host;
62+ $without_port =~ s/:\d*$//; # strip port from ip address
63+ my $packed = inet_aton($without_port);
64+ if ($packed) {
65+ $hostname = gethostbyaddr($packed, AF_INET);
66+ }
67+ }
68+ if ($hostname) {
69+ print "$prefix$host ($hostname)\n";
70+ }
71+ else {
72+ print "$prefix$host\n";
73+ }
74+}
75+
76 sub _d {
77 my ($package, undef, $line) = caller 0;
78 @_ = map { (my $temp = $_) =~ s/\n/\n# /g; $temp; }
79@@ -4155,6 +4179,16 @@
80
81 =back
82
83+=item --resolve-address
84+
85+Resolve ip-address to hostname. Report will print both IP and hostname.
86+
87+Example:
88+
89+ 10.10.7.14 (dbase1.sample.net)
90+
91+Might delay runtime a few seconds.
92+
93 =item --set-vars
94
95 type: Array
96
97=== modified file 't/pt-slave-find/pt-slave-find.t'
98--- t/pt-slave-find/pt-slave-find.t 2012-08-14 23:08:06 +0000
99+++ t/pt-slave-find/pt-slave-find.t 2015-03-19 20:58:57 +0000
100@@ -43,7 +43,7 @@
101 plan skip_all => 'Cannot connect to second sandbox slave';
102 }
103 else {
104- plan tests => 9;
105+ plan tests => 10;
106 }
107
108 my @args = ('h=127.0.0.1,P=12345,u=msandbox,p=msandbox');
109@@ -67,6 +67,17 @@
110 EOF
111 is($output, $expected, 'Master with slave and slave of slave');
112
113+###############################################################################
114+# Test --resolve-hostname option (we don't know the hostname of the test
115+# machine so we settle for any non null string)
116+###############################################################################
117+$output = `$trunk/bin/pt-slave-find -h 127.0.0.1 -P 12345 -u msandbox -p msandbox --report-format hostname --resolve-address`;
118+like (
119+ $output,
120+ qr/127\.0\.0\.1:12345\s+\(\w+\)/s,
121+ "--resolve-address option"
122+) or diag($output);
123+
124 # #############################################################################
125 # Until MasterSlave::find_slave_hosts() is improved to overcome the problems
126 # with SHOW SLAVE HOSTS, this test won't work.
127@@ -146,6 +157,7 @@
128 "Summary report format",
129 );
130
131+
132 # #############################################################################
133 # Done.
134 # #############################################################################

Subscribers

People subscribed via source and target branches

to all changes: