Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
64 changes: 52 additions & 12 deletions bin/check_drupal_log
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ EXIT_OK=0
#EXIT_ERR=2
EXIT_UNKNOWN=3

# Need special behavior when reading from stdin
READING_STDIN=


################################################################################
Expand Down Expand Up @@ -62,6 +64,7 @@ print_version() {
# @return integer 0
print_usage() {
printf "Usage: %s -f <logfile>\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
Expand All @@ -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 <logfile> The full path to logfile created by 'check_drupal'\n\n"
printf " -f <logfile> 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"
Expand Down Expand Up @@ -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

Expand All @@ -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"

Expand Down