aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTom Ryder <tom@sanctum.geek.nz>2015-03-18 09:46:51 +1300
committerTom Ryder <tom@sanctum.geek.nz>2015-03-18 09:46:51 +1300
commit13c15f7058ac10e1de6dda084695cb424cc26452 (patch)
treebb298c6d6215209cf47891e32215497904945453
parent2dd6caaa9b9b40b6f7ce690753d669ff918b1981 (diff)
downloadnscaw-13c15f7058ac10e1de6dda084695cb424cc26452.tar.gz
Include running time in output of check
-rwxr-xr-xnscaw27
1 files changed, 18 insertions, 9 deletions
diff --git a/nscaw b/nscaw
index dc35637..591b171 100755
--- a/nscaw
+++ b/nscaw
@@ -49,27 +49,36 @@ shift 2
# it can be overridden with the value of NSCAW_HOSTNAME if defined
hostname=${NSCAW_HOSTNAME:-$(hostname -s)}
-# Attempt to run command
-"$@"
+# Attempt to run command within a time wrapper
+# <http://mywiki.wooledge.org/BashFAQ/032>
+exec 3>&1 4>&2
+time=$( { time "$@" 1>&3 2>&4 ; } 2>&1) ; ret=$?
+exec 3>&- 4>&-
+
+# Munge the time to make it palatable as a single line
+time=${time#$'\n'} # Strip leading newline
+time=${time//$'\n'/, } # Change all newlines to comma-space
+time=${time//$'\t'/ } # Change all tabs to spaces
# Decide return code and message based on command exit value
-case $? in
+case $ret in
0)
code=0 # OK
- message=$(printf '%s: Command "%s" ran successfully' \
- "$self" "$*")
+ message=$(printf '%s: `%s` ran with no errors as user %s: %s' \
+ "$self" "$*" "$USER" "$time")
;;
127)
code=3 # UNKNOWN
- message=$(printf '%s: Command "%s" could not be found' \
- "$self" "$*")
+ message=$(printf '%s: `%s` could not be found as user %s with PATH=%s' \
+ "$self" "$1" "$USER" "$PATH")
;;
*)
code=2 # CRITICAL
- message=$(printf '%s: Command "%s" ran with errors' \
- "$self" "$*")
+ message=$(printf '%s: `%s` ran with errors as user %s: %s' \
+ "$self" "$*" "$USER" "$time")
;;
esac
+printf '%s\n' "$message"
# Format the passive check and pipe it into send_nsca; note that we ignore the
# stdout of send_nsca as it's just a diagnostic message