From 3abcf83af41af9e56382b8e93a48d8d3ce1f18e8 Mon Sep 17 00:00:00 2001 From: rincewind Date: Fri, 2 Jul 2021 10:37:35 +0200 Subject: Remove modified note, since this is a git repository --- plugins-scripts/check_log.sh | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/plugins-scripts/check_log.sh b/plugins-scripts/check_log.sh index d28c8d0a..4330235e 100755 --- a/plugins-scripts/check_log.sh +++ b/plugins-scripts/check_log.sh @@ -1,8 +1,7 @@ #!/bin/sh # # Log file pattern detector plugin for monitoring -# Written by Ethan Galstad (nagios@nagios.org) -# Last Modified: 07-31-1999 +# Written originally by Ethan Galstad (nagios@nagios.org) # # Usage: ./check_log # -- cgit v1.2.3-74-g34f1 From 14e1d7f6af409c28dab4732357a2b355a1ada85a Mon Sep 17 00:00:00 2001 From: rincewind Date: Fri, 2 Jul 2021 17:39:47 +0200 Subject: Apply shellcheck --- plugins-scripts/check_log.sh | 58 ++++++++++++++++++++++---------------------- 1 file changed, 29 insertions(+), 29 deletions(-) diff --git a/plugins-scripts/check_log.sh b/plugins-scripts/check_log.sh index 4330235e..8a79704a 100755 --- a/plugins-scripts/check_log.sh +++ b/plugins-scripts/check_log.sh @@ -59,11 +59,11 @@ PATH="@TRUSTED_PATH@" export PATH -PROGNAME=`basename $0` -PROGPATH=`echo $0 | sed -e 's,[\\/][^\\/][^\\/]*$,,'` +PROGNAME=$(basename "$0") +PROGPATH=$(echo "$0" | sed -e 's,[\\/][^\\/][^\\/]*$,,') REVISION="@NP_VERSION@" -. $PROGPATH/utils.sh +. "$PROGPATH"/utils.sh print_usage() { echo "Usage: $PROGNAME -F logfile -O oldlog -q query" @@ -72,7 +72,7 @@ print_usage() { } print_help() { - print_revision $PROGNAME $REVISION + print_revision "$PROGNAME" $REVISION echo "" print_usage echo "" @@ -86,7 +86,7 @@ print_help() { if [ $# -lt 1 ]; then print_usage - exit $STATE_UNKNOWN + exit "$STATE_UNKNOWN" fi # Grab the command line arguments @@ -99,19 +99,19 @@ while test -n "$1"; do case "$1" in --help) print_help - exit $STATE_OK + exit "$STATE_OK" ;; -h) print_help - exit $STATE_OK + exit "$STATE_OK" ;; --version) - print_revision $PROGNAME $REVISION - exit $STATE_OK + print_revision "$PROGNAME" $REVISION + exit "$STATE_OK" ;; -V) - print_revision $PROGNAME $REVISION - exit $STATE_OK + print_revision "$PROGNAME" $REVISION + exit "$STATE_OK" ;; --filename) logfile=$2 @@ -148,7 +148,7 @@ while test -n "$1"; do *) echo "Unknown argument: $1" print_usage - exit $STATE_UNKNOWN + exit "$STATE_UNKNOWN" ;; esac shift @@ -156,22 +156,22 @@ done # If the source log file doesn't exist, exit -if [ ! -e $logfile ]; then +if [ ! -e "$logfile" ]; then echo "Log check error: Log file $logfile does not exist!" - exit $STATE_UNKNOWN -elif [ ! -r $logfile ] ; then + exit "$STATE_UNKNOWN" +elif [ ! -r "$logfile" ] ; then echo "Log check error: Log file $logfile is not readable!" - exit $STATE_UNKNOWN + exit "$STATE_UNKNOWN" fi # If the old log file doesn't exist, this must be the first time # we're running this test, so copy the original log file over to # the old diff file and exit -if [ ! -e $oldlog ]; then - cat $logfile > $oldlog +if [ ! -e "$oldlog" ]; then + cat "$logfile" > "$oldlog" echo "Log check data initialized..." - exit $STATE_OK + exit "$STATE_OK" fi # The old log file exists, so compare it to the original log now @@ -179,24 +179,24 @@ fi # The temporary file that the script should use while # processing the log file. if [ -x /bin/mktemp ]; then - tempdiff=`/bin/mktemp /tmp/check_log.XXXXXXXXXX` + tempdiff=$(/bin/mktemp /tmp/check_log.XXXXXXXXXX) else - tempdiff=`/bin/date '+%H%M%S'` + tempdiff=$(/bin/date '+%H%M%S') tempdiff="/tmp/check_log.${tempdiff}" - touch $tempdiff - chmod 600 $tempdiff + touch "$tempdiff" + chmod 600 "$tempdiff" fi -diff $logfile $oldlog | grep -v "^>" > $tempdiff +diff "$logfile" "$oldlog" | grep -v "^>" > "$tempdiff" # Count the number of matching log entries we have -count=`grep -c "$query" $tempdiff` +count=$(grep -c "$query" "$tempdiff") # Get the last matching entry in the diff file -lastentry=`grep "$query" $tempdiff | tail -1` +lastentry=$(grep "$query" "$tempdiff" | tail -1) -rm -f $tempdiff -cat $logfile > $oldlog +rm -f "$tempdiff" +cat "$logfile" > "$oldlog" if [ "$count" = "0" ]; then # no matches, exit with no error echo "Log check ok - 0 pattern matches found" @@ -206,4 +206,4 @@ else # Print total matche count and the last entry we found exitstatus=$STATE_CRITICAL fi -exit $exitstatus +exit "$exitstatus" -- cgit v1.2.3-74-g34f1 From c2aa1a5fa2dd96c7186704901d33721a63b9cd03 Mon Sep 17 00:00:00 2001 From: rincewind Date: Fri, 2 Jul 2021 18:01:47 +0200 Subject: Add extended and perl regex --- plugins-scripts/check_log.sh | 34 ++++++++++++++++++++++++++++++++-- 1 file changed, 32 insertions(+), 2 deletions(-) diff --git a/plugins-scripts/check_log.sh b/plugins-scripts/check_log.sh index 8a79704a..6e9fbca5 100755 --- a/plugins-scripts/check_log.sh +++ b/plugins-scripts/check_log.sh @@ -145,6 +145,22 @@ while test -n "$1"; do exitstatus=$2 shift ;; + --extended-regex) + ERE=1 + shift + ;; + -e) + ERE=1 + shift + ;; + --perl-regex) + PRE=1 + shift + ;; + -p) + PRE=1 + shift + ;; *) echo "Unknown argument: $1" print_usage @@ -154,6 +170,20 @@ while test -n "$1"; do shift done +# Parameter sanity check +if [ $ERE ] && [ $PRE ] ; then + echo "Can not use extended and perl regex at the same time" + exit "$STATE_UNKNOWN" +fi + +if [ $ERE ]; then + GREP="grep -E" +fi + +if [ $PRE ]; then + GREP="grep -P" +fi + # If the source log file doesn't exist, exit if [ ! -e "$logfile" ]; then @@ -190,10 +220,10 @@ fi diff "$logfile" "$oldlog" | grep -v "^>" > "$tempdiff" # Count the number of matching log entries we have -count=$(grep -c "$query" "$tempdiff") +count=$($GREP -c "$query" "$tempdiff") # Get the last matching entry in the diff file -lastentry=$(grep "$query" "$tempdiff" | tail -1) +lastentry=$($GREP "$query" "$tempdiff" | tail -1) rm -f "$tempdiff" cat "$logfile" > "$oldlog" -- cgit v1.2.3-74-g34f1 From 53b77dee91f780b4d78839f881c642189b829e3c Mon Sep 17 00:00:00 2001 From: rincewind Date: Fri, 2 Jul 2021 18:38:12 +0200 Subject: Add -a option to print all matching lines and -p and -e options for perl and extended RE --- plugins-scripts/check_log.sh | 51 ++++++++++++++++++++++++++++++++------------ 1 file changed, 37 insertions(+), 14 deletions(-) diff --git a/plugins-scripts/check_log.sh b/plugins-scripts/check_log.sh index 6e9fbca5..8af07b5e 100755 --- a/plugins-scripts/check_log.sh +++ b/plugins-scripts/check_log.sh @@ -69,6 +69,11 @@ print_usage() { echo "Usage: $PROGNAME -F logfile -O oldlog -q query" echo "Usage: $PROGNAME --help" echo "Usage: $PROGNAME --version" + echo "" + echo "Other parameters:" + echo " -a|--all : Print all matching lines" + echo " -p|--perl-regex : Use perl style regular expressions in the query" + echo " -e|--extended-regex : Use extended style regular expressions in the query (not necessary for GNU grep)" } print_help() { @@ -115,35 +120,35 @@ while test -n "$1"; do ;; --filename) logfile=$2 - shift + shift 2 ;; -F) logfile=$2 - shift + shift 2 ;; --oldlog) oldlog=$2 - shift + shift 2 ;; -O) oldlog=$2 - shift + shift 2 ;; --query) query=$2 - shift + shift 2 ;; -q) query=$2 - shift + shift 2 ;; -x) exitstatus=$2 - shift + shift 2 ;; --exitstatus) exitstatus=$2 - shift + shift 2 ;; --extended-regex) ERE=1 @@ -161,13 +166,20 @@ while test -n "$1"; do PRE=1 shift ;; + --all) + ALL=1 + shift + ;; + -a) + ALL=1 + shift + ;; *) echo "Unknown argument: $1" print_usage exit "$STATE_UNKNOWN" ;; esac - shift done # Parameter sanity check @@ -176,6 +188,8 @@ if [ $ERE ] && [ $PRE ] ; then exit "$STATE_UNKNOWN" fi +GREP="grep" + if [ $ERE ]; then GREP="grep -E" fi @@ -219,11 +233,20 @@ fi diff "$logfile" "$oldlog" | grep -v "^>" > "$tempdiff" -# Count the number of matching log entries we have -count=$($GREP -c "$query" "$tempdiff") +if [ $ALL ]; then + # Get the last matching entry in the diff file + entry=$($GREP "$query" "$tempdiff") -# Get the last matching entry in the diff file -lastentry=$($GREP "$query" "$tempdiff" | tail -1) + # Count the number of matching log entries we have + count=$(echo "$entry" | wc -l) + +else + # Count the number of matching log entries we have + count=$($GREP -c "$query" "$tempdiff") + + # Get the last matching entry in the diff file + entry=$($GREP "$query" "$tempdiff" | tail -1) +fi rm -f "$tempdiff" cat "$logfile" > "$oldlog" @@ -232,7 +255,7 @@ if [ "$count" = "0" ]; then # no matches, exit with no error echo "Log check ok - 0 pattern matches found" exitstatus=$STATE_OK else # Print total matche count and the last entry we found - echo "($count) $lastentry" + echo "($count) $entry" exitstatus=$STATE_CRITICAL fi -- cgit v1.2.3-74-g34f1 From 10db89344adc7dad1d1ec4ac00e4fadc6b79fa72 Mon Sep 17 00:00:00 2001 From: rincewind Date: Mon, 5 Jul 2021 09:56:19 +0200 Subject: Add quoting for the remaining variables --- plugins-scripts/check_log.sh | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/plugins-scripts/check_log.sh b/plugins-scripts/check_log.sh index 8af07b5e..382bd72c 100755 --- a/plugins-scripts/check_log.sh +++ b/plugins-scripts/check_log.sh @@ -77,7 +77,7 @@ print_usage() { } print_help() { - print_revision "$PROGNAME" $REVISION + print_revision "$PROGNAME" "$REVISION" echo "" print_usage echo "" @@ -111,11 +111,11 @@ while test -n "$1"; do exit "$STATE_OK" ;; --version) - print_revision "$PROGNAME" $REVISION + print_revision "$PROGNAME" "$REVISION" exit "$STATE_OK" ;; -V) - print_revision "$PROGNAME" $REVISION + print_revision "$PROGNAME" "$REVISION" exit "$STATE_OK" ;; --filename) -- cgit v1.2.3-74-g34f1