diff --git a/bin/check_drupal_log b/bin/check_drupal_log index 5dfa529..6c23f6b 100755 --- a/bin/check_drupal_log +++ b/bin/check_drupal_log @@ -26,6 +26,8 @@ EXIT_OK=0 #EXIT_ERR=2 EXIT_UNKNOWN=3 +# Need special behavior when reading from stdin +READING_STDIN= ################################################################################ @@ -62,6 +64,7 @@ print_version() { # @return integer 0 print_usage() { printf "Usage: %s -f \n" "${INFO_NAME}" + printf "OR %s -f -\n" "${INFO_NAME}" printf "OR %s --help\n" "${INFO_NAME}" printf "OR %s --version\n\n" "${INFO_NAME}" return 0 @@ -78,7 +81,8 @@ print_help() { # Show help and details printf "Nagios plugin that will parse the logfile created by 'check_drupal'.\n\n" - printf " -f The full path to logfile created by 'check_drupal'\n\n" + printf " -f The full path to logfile created by 'check_drupal'\n" + printf " Specify '-' to read from standard input\n\n" printf " --help Show this help\n" printf " --version Show version information.\n" @@ -138,28 +142,60 @@ if [ -z "$LOGFILE" ]; then exit $EXIT_UNKNOWN fi -# Check if logfile exists -if [ ! -f "$LOGFILE" ]; then - echo "[UNKNOWN] Logfile does not exist: ${LOGFILE}." - exit $EXIT_UNKNOWN -fi - -# Check if logfile is readable -if [ ! -r "$LOGFILE" ]; then - echo "[UNKNOWN] Logfile is not readable: ${LOGFILE}." - exit $EXIT_UNKNOWN +# Check if logfile is stdin +if [ "$LOGFILE" = "-" ]; then + READING_STDIN=1 + + # Generate random tmpfile + if ! command -v mktemp >/dev/null 2>&1; then + _rand="$(LC_ALL=C tr -dc 'a-z0-9' < /dev/urandom | head -c 32)" + LOGFILE="/tmp/.drushrc.${_rand}" + touch "${LOGFILE}" + _ret=$? + else + LOGFILE="$(mktemp)" + _ret=$? + fi + + # Quit on error immediately + if [ ${_ret} -ne 0 ]; then + echo "[UNKNOWN] Can't create temporary file" + return $EXIT_UNKNOWN + fi + + cat > "$LOGFILE" +else + # Check if logfile exists + if [ ! -f "$LOGFILE" ]; then + echo "[UNKNOWN] Logfile does not exist: ${LOGFILE}." + exit $EXIT_UNKNOWN + fi + + # Check if logfile is readable + if [ ! -r "$LOGFILE" ]; then + echo "[UNKNOWN] Logfile is not readable: ${LOGFILE}." + exit $EXIT_UNKNOWN + fi fi # Check if last line contains error code NAGIOS_EXIT="$(tail -n1 "$LOGFILE")" if ! isint "${NAGIOS_EXIT}" > /dev/null 2>&1 ; then + if [ -n "$READING_STDIN" ]; then + rm -f "$LOGFILE" + fi echo "[UNKNOWN] Logfile seems invalid: Does not contain exit code on last line." exit $EXIT_UNKNOWN fi # Check if exit code is within bounds if [ "$NAGIOS_EXIT" != "0" ] && [ "$NAGIOS_EXIT" != "1" ] && [ "$NAGIOS_EXIT" != "2" ] && [ "$NAGIOS_EXIT" != "3" ]; then - echo "[UNKNOWN] Exit code not within bounds in $LOGFILE" + if [ -n "$READING_STDIN" ]; then + rm -f "$LOGFILE" + echo "[UNKNOWN] Exit code not within bounds on stdin" + else + echo "[UNKNOWN] Exit code not within bounds in $LOGFILE" + fi exit $EXIT_UNKNOWN fi @@ -170,6 +206,10 @@ NUM_LINES="$((NUM_LINES-1))" # Last line is exit code OUTPUT="$(head -n${NUM_LINES} "$LOGFILE")" +if [ -n "$READING_STDIN" ]; then + rm -f "$LOGFILE" +fi + echo "$OUTPUT" exit "$NAGIOS_EXIT"