From 894c492c6d43a1db6183a5ac5f82629ee7cfc774 Mon Sep 17 00:00:00 2001 From: Daniel Nichter Date: Fri, 13 Jan 2012 10:23:45 -0700 Subject: [PATCH] Update Bash libs. Quote stuff in tool's code. Remove flock. --- bin/pt-stalk | 151 +++++++++++++++++++++++++++------------------------ 1 file changed, 79 insertions(+), 72 deletions(-) diff --git a/bin/pt-stalk b/bin/pt-stalk index fc93ba23..1b446954 100755 --- a/bin/pt-stalk +++ b/bin/pt-stalk @@ -22,16 +22,16 @@ EXIT_STATUS=0 log() { TS=$(date +%F-%T | tr :- _); - echo "$TS $1" + echo "$TS $*" } warn() { - log "$1" >&2 - EXIT_STATUS=$((EXIT_STATUS | 1)) + log "$*" >&2 + EXIT_STATUS=1 } die() { - warn "$1" + warn "$*" exit 1 } @@ -58,19 +58,19 @@ OPT_VERSION="no" # If --version was specified OPT_HELP="no" # If --help was specified usage() { - local file=$1 + local file="$1" - local usage=$(grep '^Usage: ' $file) + local usage=$(grep '^Usage: ' "$file") echo $usage >&2 echo >&2 echo "For more information, 'man $TOOL' or 'perldoc $file'." >&2 } usage_or_errors() { - local file=$1 + local file="$1" if [ "$OPT_VERSION" = "yes" ]; then - local version=$(grep '^pt-[^ ]\+ [0-9]' $file) + local version=$(grep '^pt-[^ ]\+ [0-9]' "$file") echo "$version" return 1 fi @@ -91,7 +91,7 @@ usage_or_errors() { if [ $OPT_ERRS -gt 0 ]; then echo >&2 - usage $file + usage "$file" return 1 fi @@ -99,14 +99,14 @@ usage_or_errors() { } parse_options() { - local file=$1 + local file="$1" shift - mkdir $TMPDIR/po/ 2>/dev/null - rm -rf $TMPDIR/po/* + mkdir "$TMPDIR/po/" 2>/dev/null + rm -rf "$TMPDIR"/po/* ( export PO_DIR="$TMPDIR/po" - cat $file | perl -ne ' + cat "$file" | perl -ne ' BEGIN { $/ = ""; } next unless $_ =~ m/^=head1 OPTIONS/; while ( defined(my $para = <>) ) { @@ -137,13 +137,13 @@ parse_options() { ' ) - for opt_spec in $(ls $TMPDIR/po/); do + for opt_spec in $(ls "$TMPDIR/po/"); do local opt="" local default_val="" local neg=0 while read line; do - local key=`echo $line | cut -d ':' -f 1` - local val=`echo $line | cut -d ':' -f 2` + local key=$(echo $line | cut -d ':' -f 1) + local val=$(echo $line | cut -d ':' -f 2) case "$key" in long) opt=$(echo $val | sed 's/-/_/g' | tr [:lower:] [:upper:]) @@ -166,7 +166,7 @@ parse_options() { echo "Invalid attribute in $TMPDIR/po/$opt_spec: $line" >&2 exit 1 esac - done < $TMPDIR/po/$opt_spec + done < "$TMPDIR/po/$opt_spec" if [ -z "$opt" ]; then echo "No long attribute in option spec $TMPDIR/po/$opt_spec" >&2 @@ -216,7 +216,7 @@ parse_options() { if [ -f "$TMPDIR/po/$opt" ]; then spec="$TMPDIR/po/$opt" else - spec=$(grep "^short form:-$opt\$" $TMPDIR/po/* | cut -d ':' -f 1) + spec=$(grep "^short form:-$opt\$" "$TMPDIR"/po/* | cut -d ':' -f 1) if [ -z "$spec" ]; then OPT_ERRS=$(($OPT_ERRS + 1)) echo "Unknown option: $real_opt" >&2 @@ -224,7 +224,7 @@ parse_options() { fi fi - required_arg=$(cat $spec | grep '^type:' | cut -d':' -f2) + local required_arg=$(cat $spec | awk -F: '/^type:/{print $2}') if [ -n "$required_arg" ]; then if [ $# -eq 0 ]; then OPT_ERRS=$(($OPT_ERRS + 1)) @@ -306,7 +306,7 @@ rm_tmpdir() { set -u _seq() { - local i=$1 + local i="$1" awk "BEGIN { for(i=1; i<=$i; i++) print i; }" } @@ -327,22 +327,22 @@ _seq() { set -u disk_space() { - local filesystem=${1:-"$PWD"} - df -P -k $filesystem + local filesystem="${1:-$PWD}" + df -P -k "$filesystem" } check_disk_space() { - local file=$1 - local mb=${2:-"0"} - local pc=${3:-"0"} - local mb_margin=${4:-"0"} + local file="$1" + local mb="${2:-0}" + local pc="${3:-0}" + local mb_margin="${4:-0}" local kb=$(($mb * 1024)) local kb_margin=$(($mb_margin * 1024)) - local kb_used=$(cat $file | awk '/^\//{print $3}'); - local kb_free=$(cat $file | awk '/^\//{print $4}'); - local pc_used=$(cat $file | awk '/^\//{print $5}' | sed -e 's/%//g'); + local kb_used=$(cat "$file" | awk '/^\//{print $3}'); + local kb_free=$(cat "$file" | awk '/^\//{print $4}'); + local pc_used=$(cat "$file" | awk '/^\//{print $5}' | sed -e 's/%//g'); if [ "$kb_margin" -gt "0" ]; then local kb_total=$(($kb_used + $kb_free)) @@ -379,12 +379,12 @@ check_disk_space() { set -u make_pid_file() { - local file=$1 - local pid=$2 + local file="$1" + local pid="$2" if [ -f "$file" ]; then - local old_pid=$(cat $file) + local old_pid=$(cat "$file") if [ -z "$old_pid" ]; then die "PID file $file already exists but it is empty" else @@ -398,13 +398,13 @@ make_pid_file() { fi fi - echo "$pid" > $file + echo "$pid" > "$file" } remove_pid_file() { - local file=$1 + local file="$1" if [ -f "$file" ]; then - rm $file + rm "$file" fi } @@ -437,8 +437,8 @@ CMD_TCPDUMP=${CMD_TCPDUMP:-"tcpdump"} CMD_VMSTAT=${CMD_VMSTAT:-"vmstat"} collect() { - local d=$1 # directory to save results in - local p=$2 # prefix for each result file + local d="$1" # directory to save results in + local p="$2" # prefix for each result file local mysqld_pid=$(pidof -s mysqld); if [ -z "$mysqld_pid" ]; then @@ -448,7 +448,7 @@ collect() { mysqld_pid=$(ps -eaf | grep 'mysql[d]' | grep -v mysqld_safe | awk '{print $2}' | head -n1); fi - if [ -x "$CMD_PMAP" -a "$mysqld_pid" ]; then + if [ "$mysqld_pid" ]; then if $CMD_PMAP --help 2>&1 | grep -- -x >/dev/null 2>&1 ; then $CMD_PMAP -x $mysqld_pid > "$d/$p-pmap" else @@ -532,7 +532,7 @@ collect() { local have_lock_waits_table=0 $CMD_MYSQL $EXT_ARGV -e "SHOW TABLES FROM INFORMATION_SCHEMA" \ - | grep -qi "INNODB_LOCK_WAITS" + | grep -i "INNODB_LOCK_WAITS" >/dev/null 2>&1 if [ $? -eq 0 ]; then have_lock_waits_table=1 fi @@ -658,7 +658,7 @@ lock_waits() { # Global variables # ########################################################################### EXIT_REASON="" -TOOL=`basename $0` +TOOL=$(basename $0) OKTORUN=1 ITER=1 @@ -667,11 +667,11 @@ ITER=1 # ########################################################################### grep_processlist() { - local file=$1 - local col=$2 - local pat=${3:-""} - local gt=${4:-0} - local quiet=${5:-0} + local file="$1" + local col="$2" + local pat="${3:-""}" + local gt="${4:-0}" + local quiet="${5:-0}" awk " BEGIN { @@ -720,12 +720,14 @@ set_trg_func() { } trg_status() { - local var=$1 - mysqladmin $EXT_ARGV extended-status | grep "$OPT_VARIABLE " | awk '{print $4}' + local var="$1" + mysqladmin $EXT_ARGV extended-status \ + | grep "$OPT_VARIABLE " \ + | awk '{print $4}' } trg_processlist() { - local var=$1 + local var="$1" local tmpfile="$TMPDIR/processlist" mysqladmin $EXT_ARGV processlist > $tmpfile-1 grep_processlist $tmpfile-1 $var $OPT_MATCH 0 0 > $tmpfile-2 @@ -754,8 +756,8 @@ oktorun() { } sleep_ok() { - local seconds=$1 - local msg=${2:-""} + local seconds="$1" + local msg="${2:-""}" if oktorun; then if [ -n "$msg" ]; then log "$msg" @@ -765,11 +767,18 @@ sleep_ok() { } purge_samples() { + local dir="$1" + local retention_time="$2" + # Delete collect files which more than --retention-time days old. - find "$OPT_DEST" -type f -mtime +$OPT_RETENTION_TIME -exec rm -f '{}' \; - if [ -d "/var/lib/oprofile/samples" ]; then - find "/var/lib/oprofile/samples" -type d -name 'pt_collect_*' \ - -depth -mtime +$OPT_RETENTION_TIME -exec rm -f '{}' \; + find "$dir" -type f -mtime +$retention_time -exec rm -f '{}' \; + + local oprofile_dir="/var/lib/oprofile/samples" + if [ -d "$oprofile_dir" ]; then + # "pt_collect_" here needs to match $CMD_OPCONTROL --save=pt_collect_$p + # in collect(). TODO: fix this + find "$oprofile_dir" -type d -name 'pt_collect_*' \ + -depth -mtime +$retention_time -exec rm -f '{}' \; fi } @@ -815,20 +824,20 @@ stalk() { # ################################################################## # Start collecting, maybe. # ################################################################## - local prefix=${OPT_PREFIX:-"$(date +%F-%T | tr :- _)"} + local prefix="${OPT_PREFIX:-$(date +%F-%T | tr :- _)}" log "Collect triggered" # Check if we'll have enough disk space to collect. Disk space # is also checked every interval while collecting. local margin="20" # default 20M margin, unless: if [ -n "$last_prefix" ]; then - margin=$(du -mc $OPT_DEST/$last_prefix-* | tail -n 1 | awk '{print $1'}) + margin=$(du -mc "$OPT_DEST"/"$last_prefix"-* | tail -n 1 | awk '{print $1'}) fi - disk_space $OPT_DEST > $OPT_DEST/$prefix-disk-space - check_disk_space \ - $OPT_DEST/$prefix-disk-space \ - "$OPT_DISK_BYTE_LIMIT" \ - "$OPT_DISK_PCT_LIMIT" \ + disk_space "$OPT_DEST" > "$OPT_DEST/$prefix-disk-space" + check_disk_space \ + "$OPT_DEST/$prefix-disk-space" \ + "$OPT_DISK_BYTE_LIMIT" \ + "$OPT_DISK_PCT_LIMIT" \ "$margin" # real used MB + margin MB if [ $? -eq 0 ]; then # There should be enough disk space, so collect. @@ -847,10 +856,8 @@ stalk() { # while its collecting (hopefully --sleep is longer than # --run-time). ( - flock 200 - collect $OPT_DEST $prefix - ) 200>/tmp/percona-toolkit-collect-lockfile \ - >> "$OPT_DEST/$prefix-output" 2>&1 & + collect "$OPT_DEST" "$prefix" + ) >> "$OPT_DEST/$prefix-output" 2>&1 & else # There will not be enough disk space, so do not collect. warn "Collect canceled because there will not be enough disk space after collecting another $margin MB" @@ -867,7 +874,7 @@ stalk() { fi # Purge old collect file between checks. - purge_samples + purge_samples "$OPT_DEST" "$OPT_RETENTION_TIME" done } @@ -927,8 +934,8 @@ if [ "$(basename "$0")" = "pt-stalk" ] \ # Parse command line options. We must do this first so we can # see if --daemonize was specified. mk_tmpdir - parse_options $0 "$@" - usage_or_errors $0 + parse_options "$0" "$@" + usage_or_errors "$0" po_status=$? rm_tmpdir if [ $po_status -ne 0 ]; then @@ -947,17 +954,17 @@ if [ "$(basename "$0")" = "pt-stalk" ] \ # the PID in the PID file to check or kill the child # process. So we'll need to update the PID file with # the child's PID. - make_pid_file $OPT_PID $$ + make_pid_file "$OPT_PID" $$ - main "$@" >$OPT_LOG 2>&1 & + main "$@" >"$OPT_LOG" 2>&1 & # Update PID file with the child's PID. # The child PID is $BASHPID but that special var is only # in Bash 4+, so we can't rely on it. Consequently, we # use $! to get the PID of the child we just forked. - echo "$!" > $OPT_PID + echo "$!" > "$OPT_PID" else - make_pid_file $OPT_PID $$ + make_pid_file "$OPT_PID" $$ main "$@" fi fi