From f6382f03d8d55198c7368bb79f2aa2731e01343d Mon Sep 17 00:00:00 2001 From: Brian Fraser Date: Tue, 3 Jan 2012 14:54:27 -0300 Subject: [PATCH] pt-sift: Use mktemp through the tmpdir package. --- bin/pt-sift | 67 ++++++++++++++++++++++++++++++++++++++++++++--------- 1 file changed, 56 insertions(+), 11 deletions(-) diff --git a/bin/pt-sift b/bin/pt-sift index 21d723bf..8282ce4f 100755 --- a/bin/pt-sift +++ b/bin/pt-sift @@ -13,6 +13,47 @@ usage() { exit 1 } +# ########################################################################### +# 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. +# ########################################################################### + +# pt-sift isn't ready for this yet. +#set -u + +TMPDIR="" + +mk_tmpdir() { + local dir=${1:-""} + + if [ -n "$dir" ]; then + if [ ! -d "$dir" ]; then + mkdir $dir || die "Cannot make tmpdir $dir" + fi + TMPDIR="$dir" + else + local tool=`basename $0` + local pid="$$" + TMPDIR=`mktemp -d /tmp/${tool}.${pid}.XXXXX` \ + || die "Cannot make secure tmpdir" + fi +} + +rm_tmpdir() { + if [ -n "$TMPDIR" ] && [ -d "$TMPDIR" ]; then + rm -rf $TMPDIR + fi + TMPDIR="" +} + +# ########################################################################### +# End tmpdir package +# ########################################################################### + # Show current help and settings print_help() { cat <<-HELP @@ -89,19 +130,22 @@ main() { fi done + # Make a secure tmpdir. + mk_tmpdir + # 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 > /tmp/pt-sift.prefixes + ls "${BASEDIR}" | grep -- '-df$' | cut -d- -f1 | sort > $TMPDIR/pt-sift.prefixes if [ -z "${PREFIX}" ]; then - if [ "$(grep -c . /tmp/pt-sift.prefixes)" = "1" ]; then + if [ "$(grep -c . $TMPDIR/pt-sift.prefixes)" = "1" ]; then # If there is only one sample, we use it as the prefix. - PREFIX="$(cat /tmp/pt-sift.prefixes)" + PREFIX="$(cat $TMPDIR/pt-sift.prefixes)" fi fi if [ -z "${PREFIX}" ]; then echo i=0 - cat /tmp/pt-sift.prefixes | while read line; do + cat $TMPDIR/pt-sift.prefixes | while read line; do i=$(($i + 1)) echo -n " $line" if [ "${i}" = "3" ]; then @@ -111,14 +155,14 @@ main() { done # We might have ended mid-line or we might have printed a newline; print a # newline if required to end the list of timestamp prefixes. - awk 'BEGIN { i = 0 } { i++ } END { if ( i % 3 != 0 ) { print "" } }' /tmp/pt-sift.prefixes + awk 'BEGIN { i = 0 } { i++ } END { if ( i % 3 != 0 ) { print "" } }' $TMPDIR/pt-sift.prefixes echo - while [ -z "${PREFIX}" -o "$(grep -c "${PREFIX}" /tmp/pt-sift.prefixes)" -ne 1 ]; do - DEFAULT="$(tail -1 /tmp/pt-sift.prefixes)" + while [ -z "${PREFIX}" -o "$(grep -c "${PREFIX}" $TMPDIR/pt-sift.prefixes)" -ne 1 ]; do + DEFAULT="$(tail -1 $TMPDIR/pt-sift.prefixes)" read -e -p "Select a timestamp from the list [${DEFAULT}] " ARG ARG="${ARG:-${DEFAULT}}" - if [ "$(grep -c "${ARG}" /tmp/pt-sift.prefixes)" -eq 1 ]; then - PREFIX="$(grep "${ARG}" /tmp/pt-sift.prefixes)" + if [ "$(grep -c "${ARG}" $TMPDIR/pt-sift.prefixes)" -eq 1 ]; then + PREFIX="$(grep "${ARG}" $TMPDIR/pt-sift.prefixes)" fi done fi @@ -130,7 +174,7 @@ main() { if [ "${ACTION}" != "INVALID" ]; then # Print the current host, timestamp and action. Figure out if we're at # the first or last sample, to make it easy to navigate. - PAGE="$(awk "/./{i++} /${PREFIX}/{c=i} END{print c, \"of\", i}" /tmp/pt-sift.prefixes)" + PAGE="$(awk "/./{i++} /${PREFIX}/{c=i} END{print c, \"of\", i}" $TMPDIR/pt-sift.prefixes)" HOST="$(cat "${BASEDIR}/${PREFIX}-hostname" 2>/dev/null)" echo -e "======== ${HOST:-unknown} at \033[34m${PREFIX} \033[31m${ACTION}\033[0m (${PAGE}) ========" fi @@ -438,7 +482,7 @@ main() { if ( printed == 0 ) { print \"${PREFIX}\"; } - }" /tmp/pt-sift.prefixes)" + }" $TMPDIR/pt-sift.prefixes)" ;; 1) ACTION="DEFAULT" @@ -475,6 +519,7 @@ main() { esac done + rm_tmpdir } # Execute the program if it was not included from another file. This makes it