From 7be99c5140c40b9fc45b2a8e051d1f7e1598ddbf Mon Sep 17 00:00:00 2001 From: Daniel Nichter Date: Thu, 2 Feb 2012 12:04:28 -0700 Subject: [PATCH] Add _which() to alt_cmds.sh and update pt-stalk. --- bin/pt-stalk | 33 +++++++++++++++++++-------------- lib/bash/alt_cmds.sh | 6 ++++++ lib/bash/collect.sh | 32 ++++++++++++++++++-------------- 3 files changed, 43 insertions(+), 28 deletions(-) diff --git a/bin/pt-stalk b/bin/pt-stalk index c7a3168b..a7ded134 100755 --- a/bin/pt-stalk +++ b/bin/pt-stalk @@ -486,6 +486,10 @@ _lsof() { fi } +_which() { + [ -x /usr/bin/which ] && /usr/bin/which "$1" 2>/dev/null | awk '{print $1}' +} + # ########################################################################### # End alt_cmds package # ########################################################################### @@ -605,20 +609,21 @@ remove_pid_file() { # ########################################################################### + set -u -CMD_GDB="$(which gdb)" -CMD_IOSTAT="$(which iostat)" -CMD_MPSTAT="$(which mpstat)" -CMD_MYSQL="$(which mysql)" -CMD_MYSQLADMIN="$(which mysqladmin)" -CMD_OPCONTROL="$(which opcontrol)" -CMD_OPREPORT="$(which opreport)" -CMD_PMAP="$(which pmap)" -CMD_STRACE="$(which strace)" -CMD_SYSCTL="$(which sysctl)" -CMD_TCPDUMP="$(which tcpdump)" -CMD_VMSTAT="$(which vmstat)" +CMD_GDB="$(_which gdb)" +CMD_IOSTAT="$(_which iostat)" +CMD_MPSTAT="$(_which mpstat)" +CMD_MYSQL="$(_which mysql)" +CMD_MYSQLADMIN="$(_which mysqladmin)" +CMD_OPCONTROL="$(_which opcontrol)" +CMD_OPREPORT="$(_which opreport)" +CMD_PMAP="$(_which pmap)" +CMD_STRACE="$(_which strace)" +CMD_SYSCTL="$(_which sysctl)" +CMD_TCPDUMP="$(_which tcpdump)" +CMD_VMSTAT="$(_which vmstat)" [ -z "$CMD_SYSCTL" -a -x "/sbin/sysctl" ] && CMD_SYSCTL="/sbin/sysctl" @@ -626,7 +631,7 @@ collect() { local d="$1" # directory to save results in local p="$2" # prefix for each result file - local mysqld_pid=$(_pidof mysqld | awk '{print $1; exit;}') + local mysqld_pid=$(_pidof mysqld | head -n1) if [ "$CMD_PMAP" -a "$mysqld_pid" ]; then if $CMD_PMAP --help 2>&1 | grep -- -x >/dev/null 2>&1 ; then @@ -787,7 +792,7 @@ collect() { $CMD_OPCONTROL --save=pt_collect_$p - local mysqld_path=$(which mysqld); + local mysqld_path=$(_which mysqld); if [ "$mysqld_path" -a -f "$mysqld_path" ]; then $CMD_OPREPORT \ --demangle=smart \ diff --git a/lib/bash/alt_cmds.sh b/lib/bash/alt_cmds.sh index e0d96e64..723d75b8 100644 --- a/lib/bash/alt_cmds.sh +++ b/lib/bash/alt_cmds.sh @@ -43,6 +43,12 @@ _lsof() { fi } +_which() { + # which on CentOS is aliased to a cmd that prints extra stuff. + # Also, if the cmd isn't found, a msg is printed to stderr. + [ -x /usr/bin/which ] && /usr/bin/which "$1" 2>/dev/null | awk '{print $1}' +} + # ########################################################################### # End alt_cmds package # ########################################################################### diff --git a/lib/bash/collect.sh b/lib/bash/collect.sh index ed303bcb..ee1b5965 100644 --- a/lib/bash/collect.sh +++ b/lib/bash/collect.sh @@ -21,21 +21,25 @@ # Package: collect # collect collects system information. +# XXX +# THIS LIB REQUIRES log_warn_die.sh, safeguards.sh, and alt_cmds.sh! +# XXX + set -u # Global variables. -CMD_GDB="$(which gdb)" -CMD_IOSTAT="$(which iostat)" -CMD_MPSTAT="$(which mpstat)" -CMD_MYSQL="$(which mysql)" -CMD_MYSQLADMIN="$(which mysqladmin)" -CMD_OPCONTROL="$(which opcontrol)" -CMD_OPREPORT="$(which opreport)" -CMD_PMAP="$(which pmap)" -CMD_STRACE="$(which strace)" -CMD_SYSCTL="$(which sysctl)" -CMD_TCPDUMP="$(which tcpdump)" -CMD_VMSTAT="$(which vmstat)" +CMD_GDB="$(_which gdb)" +CMD_IOSTAT="$(_which iostat)" +CMD_MPSTAT="$(_which mpstat)" +CMD_MYSQL="$(_which mysql)" +CMD_MYSQLADMIN="$(_which mysqladmin)" +CMD_OPCONTROL="$(_which opcontrol)" +CMD_OPREPORT="$(_which opreport)" +CMD_PMAP="$(_which pmap)" +CMD_STRACE="$(_which strace)" +CMD_SYSCTL="$(_which sysctl)" +CMD_TCPDUMP="$(_which tcpdump)" +CMD_VMSTAT="$(_which vmstat)" # Try to find command manually. [ -z "$CMD_SYSCTL" -a -x "/sbin/sysctl" ] && CMD_SYSCTL="/sbin/sysctl" @@ -45,7 +49,7 @@ collect() { local p="$2" # prefix for each result file # Get pidof mysqld. - local mysqld_pid=$(_pidof mysqld | awk '{print $1; exit;}') + local mysqld_pid=$(_pidof mysqld | head -n1) # Get memory allocation info before anything else. if [ "$CMD_PMAP" -a "$mysqld_pid" ]; then @@ -243,7 +247,7 @@ collect() { # Attempt to generate a report; if this fails, then just tell the user # how to generate the report. - local mysqld_path=$(which mysqld); + local mysqld_path=$(_which mysqld); if [ "$mysqld_path" -a -f "$mysqld_path" ]; then $CMD_OPREPORT \ --demangle=smart \