aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-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