[monitoring-plugins] Added --exclude, cleanup args, fix -a count bug
Sven Nierlein
git at monitoring-plugins.org
Fri Jan 20 09:20:11 CET 2023
Module: monitoring-plugins
Branch: master
Commit: db1f87c39e0ff0d76d13babfcbb332c4cc3ad0fe
Author: lgmu <80966566+lgmu at users.noreply.github.com>
Committer: Sven Nierlein <sven at nierlein.org>
Date: Thu Jan 19 11:33:25 2023 +0100
URL: https://www.monitoring-plugins.org/repositories/monitoring-plugins/commit/?id=db1f87c
Added --exclude, cleanup args, fix -a count bug
Added --exclude to exclude patterns
Cleaned up duplicated code in the args
Fixed a bug when using --all because the count always returned "1" even when nothing matched
entry=$($GREP "$query" "$tempdiff")
count=$(echo "$entry" | wc -l)
Example:
$ touch testfile
$ TEST123=$(grep 'test' testfile)
$ echo "$TEST123" | wc -l
1
---
plugins-scripts/check_log.sh | 91 +++++++++++++++-----------------------------
1 file changed, 31 insertions(+), 60 deletions(-)
diff --git a/plugins-scripts/check_log.sh b/plugins-scripts/check_log.sh
index fdb5741..1ea70b5 100755
--- a/plugins-scripts/check_log.sh
+++ b/plugins-scripts/check_log.sh
@@ -18,7 +18,7 @@
# On the first run of the plugin, it will return an OK state with a message
# of "Log check data initialized". On successive runs, it will return an OK
# state if *no* pattern matches have been found in the *difference* between the
-# log file and the older copy of the log file. If the plugin detects any
+# log file and the older copy of the log file. If the plugin detects any
# pattern matches in the log diff, it will return a CRITICAL state and print
# out a message is the following format: "(x) last_match", where "x" is the
# total number of pattern matches found in the file and "last_match" is the
@@ -76,6 +76,7 @@ print_usage() {
echo ""
echo "Other parameters:"
echo " -a|--all : Print all matching lines"
+ echo " --exclude: Exclude a pattern (-p or -e also applies here when used)"
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)"
}
@@ -99,82 +100,46 @@ if [ $# -lt 1 ]; then
fi
# Grab the command line arguments
-
-#logfile=$1
-#oldlog=$2
-#query=$3
exitstatus=$STATE_WARNING #default
while test -n "$1"; do
case "$1" in
- --help)
- print_help
- exit "$STATE_OK"
- ;;
- -h)
+ -h | --help)
print_help
exit "$STATE_OK"
;;
- --version)
- print_revision "$PROGNAME" "$REVISION"
- exit "$STATE_OK"
- ;;
- -V)
+ -V | --version)
print_revision "$PROGNAME" "$REVISION"
exit "$STATE_OK"
;;
- --filename)
+ -F | --filename)
logfile=$2
shift 2
;;
- -F)
- logfile=$2
- shift 2
- ;;
- --oldlog)
+ -O | --oldlog)
oldlog=$2
shift 2
;;
- -O)
- oldlog=$2
- shift 2
- ;;
- --query)
- query=$2
- shift 2
- ;;
- -q)
+ -q | --query)
query=$2
shift 2
;;
- -x)
- exitstatus=$2
+ --exclude)
+ exclude=$2
shift 2
;;
- --exitstatus)
+ -x | --exitstatus)
exitstatus=$2
shift 2
;;
- --extended-regex)
+ -e | --extended-regex)
ERE=1
shift
;;
- -e)
- ERE=1
- shift
- ;;
- --perl-regex)
- PRE=1
- shift
- ;;
- -p)
+ -p | --perl-regex)
PRE=1
shift
;;
- --all)
- ALL=1
- shift
- ;;
- -a)
+ -a | --all)
ALL=1
shift
;;
@@ -213,8 +178,8 @@ elif [ ! -r "$logfile" ] ; then
fi
# If no oldlog was given this can not work properly, abort then
if [ -z "$oldlog" ]; then
- echo "Oldlog parameter is needed"
- exit $STATE_UNKNOWN
+ echo "Oldlog parameter is needed"
+ exit $STATE_UNKNOWN
fi
# If the old log file doesn't exist, this must be the first time
@@ -245,18 +210,24 @@ diff "$logfile" "$oldlog" | grep -v "^>" > "$tempdiff"
if [ $ALL ]; then
- # Get the last matching entry in the diff file
- entry=$($GREP "$query" "$tempdiff")
-
- # Count the number of matching log entries we have
- count=$(echo "$entry" | wc -l)
+ # Get all matching entries in the diff file
+ if [ -n "$exclude" ]; then
+ entry=$($GREP "$query" "$tempdiff" | $GREP -v "$exclude")
+ count=$($GREP "$query" "$tempdiff" | $GREP -vc "$exclude")
+ else
+ entry=$($GREP "$query" "$tempdiff")
+ count=$($GREP -c "$query" "$tempdiff")
+ fi
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)
+ # Get the last matching entry in the diff file
+ if [ -n "$exclude" ]; then
+ entry=$($GREP "$query" "$tempdiff" | $GREP -v "$exclude" | tail -1)
+ count=$($GREP "$query" "$tempdiff" | $GREP -vc "$exclude")
+ else
+ entry=$($GREP "$query" "$tempdiff" | tail -1)
+ count=$($GREP -c "$query" "$tempdiff")
+ fi
fi
rm -f "$tempdiff"
More information about the Commits
mailing list