From 87423810292052cf2e0488809ee74b8487134bde Mon Sep 17 00:00:00 2001 From: Daniel Nichter Date: Mon, 23 Jul 2012 16:16:00 -0600 Subject: [PATCH] Clean up pt-sift: capture signals, verify and simplify arg processing, improve error messages and docs, exit if there are no pt-stalk files. --- bin/pt-sift | 82 +++++++++++++++++++++++++++++++++++++++-------------- 1 file changed, 61 insertions(+), 21 deletions(-) diff --git a/bin/pt-sift b/bin/pt-sift index 1df1e6fb..7d90efc4 100755 --- a/bin/pt-sift +++ b/bin/pt-sift @@ -1,14 +1,12 @@ #!/usr/bin/env bash -TOOL="pt-sift" - # 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 + echo "Error: $OPT_ERR" >&2 fi echo "Usage: pt-sift FILE|PREFIX|DIRECTORY" >&2 echo "For more information, 'man pt-sift' or 'perldoc $0'." >&2 @@ -56,6 +54,24 @@ rm_tmpdir() { # 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 print_help() { cat <<-HELP @@ -75,23 +91,34 @@ print_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() { + trap sigtrap SIGHUP SIGINT SIGTERM # 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 # 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". - ARG="${1:-.}" - if [ -d "${ARG}" ]; then - BASEDIR="$(echo "${ARG}" | sed -e 's!/$!!')" - PREFIX="" - elif [ -f "${ARG}" -o -f "${ARG}-df" -o -f "${ARG}df" ]; then - BASEDIR="$(dirname "${ARG}")" - PREFIX="$(echo "${ARG}" | sed -e 's/-[a-z1]*$//' -e 's!^.*/!!')" - else - echo "Error: ${ARG} doesn't look like a directory, file, or file prefix" - exit 1 + if [ $# -gt 1 ]; then + OPT_ERR="Specify only one PREFIX or DIR" + usage + fi + + if [ $# -eq 1 ]; then + if [ -d "$1" ]; then + BASEDIR="$1" + PREFIX="" + 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 # 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 # 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 [ "$(grep -c . $PT_TMPDIR/pt-sift.prefixes)" = "1" ]; then # 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 i=$(($i + 1)) echo -n " $line" - if [ "${i}" = "3" ]; then + if [ $i -eq 3 ]; then echo i=0 fi @@ -552,15 +587,20 @@ fi =head1 NAME -pt-sift - Browses files created by pt-collect. +pt-sift - Browses files created by pt-stalk. =head1 SYNOPSIS Usage: pt-sift FILE|PREFIX|DIRECTORY -pt-sift browses the files created by L. If you specify a -FILE or PREFIX, it browses only files with that prefix. If you specify a -DIRECTORY, then it browses all files within that directory. +pt-sift browses files created by L. If no options are given, +the tool browses all pt-stalk files in the current working 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 @@ -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, and then makes a list of the unique timestamp prefixes of all the files in -the directory, as written by the L tool. If the user specified +the directory, as written by the L tool. If the user specified 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 a selection. Thereafter, it displays a summary of the selected sample, and