Use "yes" for true, "" for false. Use $PO_DIR instead of $TMP_DIR/po. Add and test Baron's code for 'Options and values after processing arguments'. Make --help exit 0 unless there were errors.

This commit is contained in:
Daniel Nichter
2012-01-24 12:49:18 -07:00
parent 42a8e39635
commit c2fd3f54c2
5 changed files with 51 additions and 27 deletions

View File

@@ -85,12 +85,20 @@ usage_or_errors() {
echo echo
echo "Command line options:" echo "Command line options:"
echo echo
for opt in $(ls $TMPDIR/po/); do for opt in $(ls "$PO_DIR"); do
local desc=$(cat $TMPDIR/po/$opt | grep '^desc:' | sed -e 's/^desc://') local desc=$(cat $TMPDIR/po/$opt | grep '^desc:' | sed -e 's/^desc://')
echo "--$opt" echo "--$opt"
echo " $desc" echo " $desc"
echo echo
done done
echo "Options and values after processing arguments:"
echo
for opt in $(ls "$PO_DIR"); do
local varname="OPT_$(echo "$opt" | tr a-z- A-Z_)"
local varvalue="${!varname}"
printf -- " --%-30s %s" "$opt" "${varvalue:-(No value)}"
echo
done
return 1 return 1
fi fi
@@ -184,13 +192,13 @@ _parse_pod() {
} }
_eval_po() { _eval_po() {
for opt_spec in $(ls "$PO_DIR"); do local old_ifs="$IFS"
IFS=":"
for opt_spec in "$PO_DIR"/*; do
local opt="" local opt=""
local default_val="" local default_val=""
local neg=0 local neg=0
while read line; do while read key val; do
local key=$(echo $line | cut -d ':' -f 1)
local val=$(echo $line | cut -d ':' -f 2)
case "$key" in case "$key" in
long) long)
opt=$(echo $val | sed 's/-/_/g' | tr [:lower:] [:upper:]) opt=$(echo $val | sed 's/-/_/g' | tr [:lower:] [:upper:])
@@ -210,13 +218,13 @@ _eval_po() {
fi fi
;; ;;
*) *)
echo "Invalid attribute in $PO_DIR/$opt_spec: $line" >&2 echo "Invalid attribute in $opt_spec: $line" >&2
exit 1 exit 1
esac esac
done < "$PO_DIR/$opt_spec" done < "$opt_spec"
if [ -z "$opt" ]; then if [ -z "$opt" ]; then
echo "No long attribute in option spec $PO_DIR/$opt_spec" >&2 echo "No long attribute in option spec $opt_spec" >&2
exit 1 exit 1
fi fi
@@ -229,6 +237,8 @@ _eval_po() {
eval "OPT_${opt}"="$default_val" eval "OPT_${opt}"="$default_val"
done done
IFS="$old_ifs"
} }
_parse_config_files() { _parse_config_files() {
@@ -574,7 +584,7 @@ collect() {
fi fi
fi fi
if [ "$CMD_GDB" -a "$OPT_COLLECT_GDB" = "yes" -a "$mysqld_pid" ]; then if [ "$CMD_GDB" -a "$OPT_COLLECT_GDB" -a "$mysqld_pid" ]; then
$CMD_GDB \ $CMD_GDB \
-ex "set pagination 0" \ -ex "set pagination 0" \
-ex "thread apply all bt" \ -ex "thread apply all bt" \
@@ -613,7 +623,7 @@ collect() {
open_tables >> "$d/$p-opentables1" 2>&1 & open_tables >> "$d/$p-opentables1" 2>&1 &
local tcpdump_pid="" local tcpdump_pid=""
if [ "$CMD_TCPDUMP" -a "$OPT_COLLECT_TCPDUMP" = "yes" ]; then if [ "$CMD_TCPDUMP" -a "$OPT_COLLECT_TCPDUMP" ]; then
local port=$(awk '/^port/{print $2}' "$d/$p-variables") local port=$(awk '/^port/{print $2}' "$d/$p-variables")
if [ "$port" ]; then if [ "$port" ]; then
$CMD_TCPDUMP -i any -s 4096 -w "$d/$p-tcpdump" port ${port} & $CMD_TCPDUMP -i any -s 4096 -w "$d/$p-tcpdump" port ${port} &
@@ -622,12 +632,12 @@ collect() {
fi fi
local have_oprofile="no" local have_oprofile="no"
if [ "$CMD_OPCONTROL" -a "$OPT_COLLECT_OPROFILE" = "yes" ]; then if [ "$CMD_OPCONTROL" -a "$OPT_COLLECT_OPROFILE" ]; then
if $CMD_OPCONTROL --init; then if $CMD_OPCONTROL --init; then
$CMD_OPCONTROL --start --no-vmlinux $CMD_OPCONTROL --start --no-vmlinux
have_oprofile="yes" have_oprofile="yes"
fi fi
elif [ "$CMD_STRACE" -a "$OPT_COLLECT_STRACE" = "yes" ]; then elif [ "$CMD_STRACE" -a "$OPT_COLLECT_STRACE" ]; then
$CMD_STRACE -T -s 0 -f -p $mysqld_pid > "${DEST}/$d-strace" 2>&1 & $CMD_STRACE -T -s 0 -f -p $mysqld_pid > "${DEST}/$d-strace" 2>&1 &
local strace_pid=$! local strace_pid=$!
fi fi
@@ -726,7 +736,7 @@ collect() {
"/path/to/mysqld'" \ "/path/to/mysqld'" \
> "$d/$p-opreport" > "$d/$p-opreport"
fi fi
elif [ "$CMD_STRACE" -a "$OPT_COLLECT_STRACE" = "yes" ]; then elif [ "$CMD_STRACE" -a "$OPT_COLLECT_STRACE" ]; then
kill -s 2 $strace_pid kill -s 2 $strace_pid
sleep 1 sleep 1
kill -s 15 $strace_pid kill -s 15 $strace_pid
@@ -931,7 +941,7 @@ sigtrap() {
stalk() { stalk() {
local cycles_true=0 # increment each time check is true, else set to 0 local cycles_true=0 # increment each time check is true, else set to 0
local matched="no" # set to "yes" when check is true local matched="" # set to "yes" when check is true
local last_prefix="" # prefix of last collection local last_prefix="" # prefix of last collection
while oktorun; do while oktorun; do
@@ -944,20 +954,20 @@ stalk() {
if [ -z "$value" ]; then if [ -z "$value" ]; then
# No value. Maybe we failed to connect to MySQL? # No value. Maybe we failed to connect to MySQL?
warn "Detected value is empty; something failed? Trigger exit status: $trg_exit_status" warn "Detected value is empty; something failed? Trigger exit status: $trg_exit_status"
matched="no" matched=""
cycles_true=0 cycles_true=0
elif [ $value -gt $OPT_THRESHOLD ]; then elif [ $value -gt $OPT_THRESHOLD ]; then
matched="yes" matched="yes"
cycles_true=$(($cycles_true + 1)) cycles_true=$(($cycles_true + 1))
else else
matched="no" matched=""
cycles_true=0 cycles_true=0
fi fi
local msg="Check results: $OPT_VARIABLE=$value, matched=$matched, cycles_true=$cycles_true" local msg="Check results: $OPT_VARIABLE=$value, matched=${matched:-no}, cycles_true=$cycles_true"
log "$msg" log "$msg"
if [ "$matched" = "yes" -a $cycles_true -ge $OPT_CYCLES ]; then if [ "$matched" -a $cycles_true -ge $OPT_CYCLES ]; then
# ################################################################## # ##################################################################
# Start collecting, maybe. # Start collecting, maybe.
# ################################################################## # ##################################################################
@@ -1089,7 +1099,8 @@ if [ "$(basename "$0")" = "pt-stalk" ] \
po_status=$? po_status=$?
rm_tmpdir rm_tmpdir
if [ $po_status -ne 0 ]; then if [ $po_status -ne 0 ]; then
exit $po_status [ $OPT_ERRS -gt 0 ] && exit 1
exit 0
fi fi
# Now that we have the cmd line opts, check that we can actually # Now that we have the cmd line opts, check that we can actually
@@ -1097,7 +1108,7 @@ if [ "$(basename "$0")" = "pt-stalk" ] \
[ -n "$(mysql $EXT_ARGV -e 'SELECT 1')" ] \ [ -n "$(mysql $EXT_ARGV -e 'SELECT 1')" ] \
|| die "Cannot connect to MySQL. Check that MySQL is running and that the options after -- are correct." || die "Cannot connect to MySQL. Check that MySQL is running and that the options after -- are correct."
if [ "$OPT_DAEMONIZE" = "yes" ]; then if [ "$OPT_DAEMONIZE" ]; then
# Check access to the --log file. # Check access to the --log file.
( (
set -e set -e

View File

@@ -61,7 +61,7 @@ collect() {
# Getting a GDB stacktrace can be an intensive operation, # Getting a GDB stacktrace can be an intensive operation,
# so do this only if necessary (and possible). # so do this only if necessary (and possible).
if [ "$CMD_GDB" -a "$OPT_COLLECT_GDB" = "yes" -a "$mysqld_pid" ]; then if [ "$CMD_GDB" -a "$OPT_COLLECT_GDB" -a "$mysqld_pid" ]; then
$CMD_GDB \ $CMD_GDB \
-ex "set pagination 0" \ -ex "set pagination 0" \
-ex "thread apply all bt" \ -ex "thread apply all bt" \
@@ -112,7 +112,7 @@ collect() {
# If TCP dumping is specified, start that on the server's port. # If TCP dumping is specified, start that on the server's port.
local tcpdump_pid="" local tcpdump_pid=""
if [ "$CMD_TCPDUMP" -a "$OPT_COLLECT_TCPDUMP" = "yes" ]; then if [ "$CMD_TCPDUMP" -a "$OPT_COLLECT_TCPDUMP" ]; then
local port=$(awk '/^port/{print $2}' "$d/$p-variables") local port=$(awk '/^port/{print $2}' "$d/$p-variables")
if [ "$port" ]; then if [ "$port" ]; then
$CMD_TCPDUMP -i any -s 4096 -w "$d/$p-tcpdump" port ${port} & $CMD_TCPDUMP -i any -s 4096 -w "$d/$p-tcpdump" port ${port} &
@@ -123,12 +123,12 @@ collect() {
# Next, start oprofile gathering data during the whole rest of this process. # Next, start oprofile gathering data during the whole rest of this process.
# The --init should be a no-op if it has already been init-ed. # The --init should be a no-op if it has already been init-ed.
local have_oprofile="no" local have_oprofile="no"
if [ "$CMD_OPCONTROL" -a "$OPT_COLLECT_OPROFILE" = "yes" ]; then if [ "$CMD_OPCONTROL" -a "$OPT_COLLECT_OPROFILE" ]; then
if $CMD_OPCONTROL --init; then if $CMD_OPCONTROL --init; then
$CMD_OPCONTROL --start --no-vmlinux $CMD_OPCONTROL --start --no-vmlinux
have_oprofile="yes" have_oprofile="yes"
fi fi
elif [ "$CMD_STRACE" -a "$OPT_COLLECT_STRACE" = "yes" ]; then elif [ "$CMD_STRACE" -a "$OPT_COLLECT_STRACE" ]; then
# Don't run oprofile and strace at the same time. # Don't run oprofile and strace at the same time.
$CMD_STRACE -T -s 0 -f -p $mysqld_pid > "${DEST}/$d-strace" 2>&1 & $CMD_STRACE -T -s 0 -f -p $mysqld_pid > "${DEST}/$d-strace" 2>&1 &
local strace_pid=$! local strace_pid=$!
@@ -244,7 +244,7 @@ collect() {
"/path/to/mysqld'" \ "/path/to/mysqld'" \
> "$d/$p-opreport" > "$d/$p-opreport"
fi fi
elif [ "$CMD_STRACE" -a "$OPT_COLLECT_STRACE" = "yes" ]; then elif [ "$CMD_STRACE" -a "$OPT_COLLECT_STRACE" ]; then
kill -s 2 $strace_pid kill -s 2 $strace_pid
sleep 1 sleep 1
kill -s 15 $strace_pid kill -s 15 $strace_pid

View File

@@ -91,12 +91,20 @@ usage_or_errors() {
echo echo
echo "Command line options:" echo "Command line options:"
echo echo
for opt in $(ls $TMPDIR/po/); do for opt in $(ls "$PO_DIR"); do
local desc=$(cat $TMPDIR/po/$opt | grep '^desc:' | sed -e 's/^desc://') local desc=$(cat $TMPDIR/po/$opt | grep '^desc:' | sed -e 's/^desc://')
echo "--$opt" echo "--$opt"
echo " $desc" echo " $desc"
echo echo
done done
echo "Options and values after processing arguments:"
echo
for opt in $(ls "$PO_DIR"); do
local varname="OPT_$(echo "$opt" | tr a-z- A-Z_)"
local varvalue="${!varname}"
printf -- " --%-30s %s" "$opt" "${varvalue:-(No value)}"
echo
done
return 1 return 1
fi fi

View File

@@ -5,6 +5,7 @@ TESTS=19
TMPFILE="$TEST_TMPDIR/parse-opts-output" TMPFILE="$TEST_TMPDIR/parse-opts-output"
TMPDIR="$TEST_TMPDIR" TMPDIR="$TEST_TMPDIR"
PATH="$PATH:$PERCONA_TOOLKIT_SANDBOX/bin" PATH="$PATH:$PERCONA_TOOLKIT_SANDBOX/bin"
TOOL="pt-stalk"
mkdir "$TMPDIR/collect" 2>/dev/null mkdir "$TMPDIR/collect" 2>/dev/null

View File

@@ -1,6 +1,6 @@
#!/usr/bin/env bash #!/usr/bin/env bash
TESTS=63 TESTS=64
TMPFILE="$TEST_TMPDIR/parse-opts-output" TMPFILE="$TEST_TMPDIR/parse-opts-output"
TOOL="pt-stalk" TOOL="pt-stalk"
@@ -105,6 +105,10 @@ cmd_ok \
"grep -q \"For more information, 'man pt-stalk' or 'perldoc\" $TMPFILE" \ "grep -q \"For more information, 'man pt-stalk' or 'perldoc\" $TMPFILE" \
"--help" "--help"
cmd_ok \
"grep -q '\-\-string-opt[ ]*(No value)' $TMPFILE" \
"Options and values after processing arguments"
# Don't interpolate. # Don't interpolate.
parse_options "$T_LIB_DIR/samples/bash/po003.sh" --help parse_options "$T_LIB_DIR/samples/bash/po003.sh" --help
usage_or_errors "$T_LIB_DIR/samples/bash/po003.sh" >$TMPFILE 2>&1 usage_or_errors "$T_LIB_DIR/samples/bash/po003.sh" >$TMPFILE 2>&1