Clean up pt-sift: capture signals, verify and simplify arg processing, improve error messages and docs, exit if there are no pt-stalk files.

This commit is contained in:
Daniel Nichter
2012-07-23 16:16:00 -06:00
parent b90a375cb5
commit 8742381029

View File

@@ -1,14 +1,12 @@
#!/usr/bin/env bash #!/usr/bin/env bash
TOOL="pt-sift"
# This program is part of Percona Toolkit: http://www.percona.com/software/ # 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 # See "COPYRIGHT, LICENSE, AND WARRANTY" at the end of this file for legal
# notices and disclaimers. # notices and disclaimers.
usage() { usage() {
if [ "${OPT_ERR}" ]; then if [ "${OPT_ERR}" ]; then
echo "${OPT_ERR}" >&2 echo "Error: $OPT_ERR" >&2
fi fi
echo "Usage: pt-sift FILE|PREFIX|DIRECTORY" >&2 echo "Usage: pt-sift FILE|PREFIX|DIRECTORY" >&2
echo "For more information, 'man pt-sift' or 'perldoc $0'." >&2 echo "For more information, 'man pt-sift' or 'perldoc $0'." >&2
@@ -56,6 +54,24 @@ rm_tmpdir() {
# End tmpdir package # End tmpdir package
# ########################################################################### # ###########################################################################
# ###########################################################################
# Global variables
# ###########################################################################
TOOL="pt-sift"
BASEDIR="$PWD"
PREFIX=""
# ###########################################################################
# Subroutines
# ###########################################################################
sigtrap() {
echo "Caught signal, exiting" >&2
rm_tmpdir
exit 0
}
# Show current help and settings # Show current help and settings
print_help() { print_help() {
cat <<-HELP cat <<-HELP
@@ -75,23 +91,34 @@ print_help() {
HELP HELP
} }
# The main code that runs by default. Arguments are the command-line options. # ###########################################################################
# Main program loop, called below if tool is ran from the command line.
# ###########################################################################
main() { main() {
trap sigtrap SIGHUP SIGINT SIGTERM
# If there's a command-line arg, figure out if it's a file, directory, or # If there's a command-line arg, figure out if it's a file, directory, or
# prefix. The outcome of this block of code should be that BASEDIR is the # prefix. The outcome of this block of code should be that BASEDIR is the
# directory where the files live, without a trailing slash; and PREFIX is # directory where the files live, without a trailing slash; and PREFIX is
# either empty or a timestamp, such as "2011_02_08_16_58_07". # either empty or a timestamp, such as "2011_02_08_16_58_07".
ARG="${1:-.}" if [ $# -gt 1 ]; then
if [ -d "${ARG}" ]; then OPT_ERR="Specify only one PREFIX or DIR"
BASEDIR="$(echo "${ARG}" | sed -e 's!/$!!')" usage
PREFIX="" fi
elif [ -f "${ARG}" -o -f "${ARG}-df" -o -f "${ARG}df" ]; then
BASEDIR="$(dirname "${ARG}")" if [ $# -eq 1 ]; then
PREFIX="$(echo "${ARG}" | sed -e 's/-[a-z1]*$//' -e 's!^.*/!!')" if [ -d "$1" ]; then
else BASEDIR="$1"
echo "Error: ${ARG} doesn't look like a directory, file, or file prefix" PREFIX=""
exit 1 elif [ -f "$1" -o -f "$1-df" -o -f "$1df" ]; then
BASEDIR="$(dirname "$1")"
PREFIX="$(echo "$1" | perl -ne '$_ =~ m/([\d_]+)/; print $1;')"
else
echo "Error: $1 is not a directory, and there are no pt-stalk files in the curent working directory ($BASEDIR) with a $1 prefix." >&2
echo "For more information, 'man pt-sift' or 'perldoc $0'." >&2
exit 1
fi
fi fi
# If the programs we need don't exist, try to get them. # If the programs we need don't exist, try to get them.
@@ -121,7 +148,15 @@ main() {
# We need to generate a list of timestamps, and ask the user to choose one if # We need to generate a list of timestamps, and ask the user to choose one if
# there is no PREFIX yet. NOTE: we rely on the "-df" files here. # there is no PREFIX yet. NOTE: we rely on the "-df" files here.
ls "${BASEDIR}" | grep -- '-df$' | cut -d- -f1 | sort > $PT_TMPDIR/pt-sift.prefixes (
cd "$BASEDIR"
ls *-df | cut -d- -f1 | sort > "$PT_TMPDIR/pt-sift.prefixes"
)
if [ ! -s "$PT_TMPDIR/pt-sift.prefixes" ]; then
echo "Error: There are no pt-stalk files in $BASEDIR" >&2
echo "For more information, 'man pt-sift' or 'perldoc $0'." >&2
exit 1
fi
if [ -z "${PREFIX}" ]; then if [ -z "${PREFIX}" ]; then
if [ "$(grep -c . $PT_TMPDIR/pt-sift.prefixes)" = "1" ]; then if [ "$(grep -c . $PT_TMPDIR/pt-sift.prefixes)" = "1" ]; then
# If there is only one sample, we use it as the prefix. # If there is only one sample, we use it as the prefix.
@@ -134,7 +169,7 @@ main() {
cat $PT_TMPDIR/pt-sift.prefixes | while read line; do cat $PT_TMPDIR/pt-sift.prefixes | while read line; do
i=$(($i + 1)) i=$(($i + 1))
echo -n " $line" echo -n " $line"
if [ "${i}" = "3" ]; then if [ $i -eq 3 ]; then
echo echo
i=0 i=0
fi fi
@@ -552,15 +587,20 @@ fi
=head1 NAME =head1 NAME
pt-sift - Browses files created by pt-collect. pt-sift - Browses files created by pt-stalk.
=head1 SYNOPSIS =head1 SYNOPSIS
Usage: pt-sift FILE|PREFIX|DIRECTORY Usage: pt-sift FILE|PREFIX|DIRECTORY
pt-sift browses the files created by L<pt-collect>. If you specify a pt-sift browses files created by L<pt-stalk>. If no options are given,
FILE or PREFIX, it browses only files with that prefix. If you specify a the tool browses all pt-stalk files in the current working directory.
DIRECTORY, then it browses all files within that directory. Specify a DIRECTORY to browse all files in that directory. Specify
a FILE to browse all files with the same prefix in that file's directory.
Specify a PREFIX to browse all files in the current working directory
with that prefix. Prefixes are timestamps like C<2012_07_23_14_27_11>.
The tool prompts for a prefix if more than is found, else the only available
prefix is used by default.
=head1 RISKS =head1 RISKS
@@ -585,7 +625,7 @@ See also L<"BUGS"> for more information on filing bugs and getting help.
pt-sift downloads other tools that it might need, such as L<pt-diskstats>, pt-sift downloads other tools that it might need, such as L<pt-diskstats>,
and then makes a list of the unique timestamp prefixes of all the files in and then makes a list of the unique timestamp prefixes of all the files in
the directory, as written by the L<pt-collect> tool. If the user specified the directory, as written by the L<pt-stalk> tool. If the user specified
a timestamp on the command line, then it begins with that sample of data; a timestamp on the command line, then it begins with that sample of data;
otherwise it begins by showing a list of the timestamps and prompting for otherwise it begins by showing a list of the timestamps and prompting for
a selection. Thereafter, it displays a summary of the selected sample, and a selection. Thereafter, it displays a summary of the selected sample, and