Files
percona-toolkit/bin/pt-mext

210 lines
5.8 KiB
Bash
Executable File

#!/usr/bin/env bash
# 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() {
echo "Usage: $0 [-r] -- commandline"
echo " -r Relative: subtract each column from the previous column."
echo " -- Option separator; after this, all arguments are passed"
echo " to the program whose output mext will columnize."
echo "mext columnizes repeated output from a program like mysqladmin extended:"
echo " mext -r -- mysqladmin ext -i10 -c3"
echo "You can also work with data from a file:"
echo " mext -r -- cat mysqladmin-output.txt"
exit 1
}
if [ -z "$1" ]; then
usage;
fi
FILE=/tmp/mext_temp_file;
NUM=0;
REL=0;
rm -f $FILE*;
# 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.
rm -f $FILE*;
# ############################################################################
# Documentation
# ############################################################################
:<<'DOCUMENTATION'
=pod
=head1 NAME
pt-mext - Aggregate and summarize mysqladmin extended output.
=head1 SYNOPSIS
Usage: pt-mext [OPTION...]
=head1 DESCRIPTION
pt-mext aggregates and summarizes mysqladmin extended output.
=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 ENVIRONMENT
The environment variable C<PTDEBUG> enables verbose debugging output to STDERR.
To enable debugging and capture all output to a file, run the tool like:
PTDEBUG=1 pt-mext ... > FILE 2>&1
Be careful: debugging output is voluminous and can generate several megabytes
of output.
=head1 SYSTEM REQUIREMENTS
You need Bash.
=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 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 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
Percona Toolkit v1.0.0 released 2011-08-01
=cut
DOCUMENTATION