~percona-toolkit-dev/percona-toolkit/pt-stalk-iter-1-bug-1070434

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
#!/bin/sh

# This program is part of Percona Toolkit: http://www.percona.com/software/
# See "COPYRIGHT, LICENSE, AND WARRANTY" at the end of this file for legal
# notices and disclaimers.

usage() {
   if [ "${OPT_ERR}" ]; then
      echo "${OPT_ERR}" >&2
   fi
   echo "Usage: pt-mext [OPTIONS] -- COMMAND" >&2
   echo "For more information, 'man pt-mext' or 'perldoc $0'" >&2
   exit 1
}

if [ -z "$1" ]; then
   usage;
fi

# ###########################################################################
# tmpdir package
# This package is a copy without comments from the original.  The original
# with comments and its test file can be found in the Bazaar repository at,
#   lib/bash/tmpdir.sh
#   t/lib/bash/tmpdir.sh
# See https://launchpad.net/percona-toolkit for more information.
# ###########################################################################


set -u

PT_TMPDIR=""

mk_tmpdir() {
   local dir="${1:-""}"

   if [ -n "$dir" ]; then
      if [ ! -d "$dir" ]; then
         mkdir "$dir" || die "Cannot make tmpdir $dir"
      fi
      PT_TMPDIR="$dir"
   else
      local tool="${0##*/}"
      local pid="$$"
      PT_TMPDIR=`mktemp -d -t "${tool}.${pid}.XXXXXX"` \
         || die "Cannot make secure tmpdir"
   fi
}

rm_tmpdir() {
   if [ -n "$PT_TMPDIR" ] && [ -d "$PT_TMPDIR" ]; then
      rm -rf "$PT_TMPDIR"
   fi
   PT_TMPDIR=""
}

# ###########################################################################
# End tmpdir package
# ###########################################################################

mk_tmpdir

FILE="$PT_TMPDIR/mext_temp_file";
NUM=0;
REL=0;

# Command-line parsing.
args=`getopt -u -n mext r "$@"`;
if [ "$?" = "1" ]; then
   usage;
fi
set -- $args
for o; do
   case "$o" in
      -r)   REL="1"; shift;;
      --)   shift;   break;;
   esac
done

if [ -z "$1" ]; then
   usage;
fi

# Split the output on empty lines and put each into a different file; eliminate
# lines that don't have "real" content.
$@ | grep -v '+' | grep -v Variable_name | sed 's/|//g' \
   | while read line; do
   if [ "$line" = "" ]; then
      NUM=`expr $NUM + 1`;
      echo "" > "$FILE$NUM"
   fi
   echo "$line" >> "$FILE$NUM"
done

# Count how many files there are and prepare to format the output
SPEC="%-33s %13d"
AWKS=""
NUM=`ls "$FILE"* | wc -l`;
# The last file will be empty...
NUM=`expr $NUM - 3`;

# Join each file with the next file, joining on the first field. Build a printf
# spec and awk spec at the same time.
for i in `seq 0 $NUM`; do
   NEXTFILE=`expr $i + 1`;

   # Sort each file and eliminate empty lines, so 'join' doesn't complain.
   sort "$FILE$i" | grep . > "$FILE$i.tmp"
   mv "$FILE$i.tmp" "$FILE$i"
   sort "$FILE${NEXTFILE}" | grep . > "$FILE${NEXTFILE}.tmp"
   mv "$FILE${NEXTFILE}.tmp" "$FILE${NEXTFILE}"

   # Join the files together.  This gets slow O(n^2) as we add more files, but
   # this really shouldn't be performance critical.
   join "$FILE$i" "$FILE${NEXTFILE}" | grep . > "$FILE"

   # Find the max length of the [numeric only] values in the file so we know how
   # wide to make the columns
   MAXLEN=`awk '{print $2}' "$FILE${NEXTFILE}" | grep -v '[^0-9]' | awk '{print length($1)}' | sort -rn | head -n1`
   mv "$FILE" "$FILE${NEXTFILE}"
   SPEC="$SPEC %${MAXLEN}d";
   if [ "$REL" = "1" ]; then
      AWKS="$AWKS, \$`expr $i + 3` - \$`expr $i + 2`";
   else
      AWKS="$AWKS, \$`expr $i + 3`";
   fi
done

# Print output
AWKCMD="printf(\"$SPEC\n\", \$1, \$2$AWKS);";
awk "{$AWKCMD}" "$FILE`expr $NUM + 1`"

# Remove all temporary files and the tmp dir.
rm_tmpdir

exit 0

# ############################################################################
# Documentation
# ############################################################################
:<<'DOCUMENTATION'
=pod

=head1 NAME

pt-mext - Look at many samples of MySQL C<SHOW GLOBAL STATUS> side-by-side.

=head1 SYNOPSIS

Usage: pt-mext [OPTIONS] -- COMMAND

pt-mext columnizes repeated output from a program like mysqladmin extended.

Get output from C<mysqladmin>:

   pt-mext -r -- mysqladmin ext -i10 -c3"

Get output from a file:

   pt-mext -r -- cat mysqladmin-output.txt

=head1 RISKS

The following section is included to inform users about the potential risks,
whether known or unknown, of using this tool.  The two main categories of risks
are those created by the nature of the tool (e.g. read-only tools vs. read-write
tools) and those created by bugs.

pt-mext is a read-only tool.  It should be very low-risk.

At the time of this release, we know of no bugs that could cause serious harm
to users.

The authoritative source for updated information is always the online issue
tracking system.  Issues that affect this tool will be marked as such.  You can
see a list of such issues at the following URL:
L<http://www.percona.com/bugs/pt-mext>.

See also L<"BUGS"> for more information on filing bugs and getting help.

=head1 DESCRIPTION

pt-mext executes the C<COMMAND> you specify, and reads through the result one
line at a time.  It places each line into a temporary file.  When it finds a
blank line, it assumes that a new sample of SHOW GLOBAL STATUS is starting,
and it creates a new temporary file.  At the end of this process, it has a
number of temporary files.  It joins the temporary files together side-by-side
and prints the result.  If the L<"-r"> option is given, it first subtracts
each sample from the one after it before printing results.

=head1 OPTIONS

=over

=item -r

Relative: subtract each column from the previous column.

=back

=head1 ENVIRONMENT

This tool does not use any environment variables.

=head1 SYSTEM REQUIREMENTS

This tool requires the Bourne shell (F</bin/sh>) and the seq program.

=head1 BUGS

For a list of known bugs, see L<http://www.percona.com/bugs/pt-mext>.

Please report bugs at L<https://bugs.launchpad.net/percona-toolkit>.
Include the following information in your bug report:

=over

=item * Complete command-line used to run the tool

=item * Tool L<"--version">

=item * MySQL version of all servers involved

=item * Output from the tool including STDERR

=item * Input files (log/dump/config files, etc.)

=back

If possible, include debugging output by running the tool with C<PTDEBUG>;
see L<"ENVIRONMENT">.

=head1 DOWNLOADING

Visit L<http://www.percona.com/software/percona-toolkit/> to download the
latest release of Percona Toolkit.  Or, get the latest release from the
command line:

   wget percona.com/get/percona-toolkit.tar.gz

   wget percona.com/get/percona-toolkit.rpm

   wget percona.com/get/percona-toolkit.deb

You can also get individual tools from the latest release:

   wget percona.com/get/TOOL

Replace C<TOOL> with the name of any tool.

=head1 AUTHORS

Baron Schwartz

=head1 ABOUT PERCONA TOOLKIT

This tool is part of Percona Toolkit, a collection of advanced command-line
tools developed by Percona for MySQL support and consulting.  Percona Toolkit
was forked from two projects in June, 2011: Maatkit and Aspersa.  Those
projects were created by Baron Schwartz and developed primarily by him and
Daniel Nichter, both of whom are employed by Percona.  Visit
L<http://www.percona.com/software/> for more software developed by Percona.

=head1 COPYRIGHT, LICENSE, AND WARRANTY

This program is copyright 2010 Baron Schwartz, 2011-2012 Percona Inc.
Feedback and improvements are welcome.

THIS PROGRAM IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR IMPLIED
WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.

This program is free software; you can redistribute it and/or modify it under
the terms of the GNU General Public License as published by the Free Software
Foundation, version 2; OR the Perl Artistic License.  On UNIX and similar
systems, you can issue `man perlgpl' or `man perlartistic' to read these
licenses.

You should have received a copy of the GNU General Public License along with
this program; if not, write to the Free Software Foundation, Inc., 59 Temple
Place, Suite 330, Boston, MA  02111-1307  USA.

=head1 VERSION

pt-mext 2.1.5

=cut

DOCUMENTATION