From 916572c1aecc37ebfea474f952df61d15d2f60b8 Mon Sep 17 00:00:00 2001 From: andrew bezella Date: Wed, 19 Oct 2022 21:33:18 +0200 Subject: Fixing nullmailer regex attached is a patch that updates the format expected in the nullmailer mailq output. the regex is a little more flexible and less specific than the previous version. --- plugins-scripts/check_mailq.pl | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'plugins-scripts') diff --git a/plugins-scripts/check_mailq.pl b/plugins-scripts/check_mailq.pl index 4c72332a..27073d3c 100755 --- a/plugins-scripts/check_mailq.pl +++ b/plugins-scripts/check_mailq.pl @@ -537,9 +537,9 @@ elsif ( $mailq eq "nullmailer" ) { } while () { - #2006-06-22 16:00:00 282 bytes + #2022-08-25 01:30:40 502 bytes from - if (/^[1-9][0-9]*-[01][0-9]-[0-3][0-9]\s[0-2][0-9]\:[0-5][0-9]\:[0-5][0-9]\s+[0-9]+\sbytes/) { + if (/^\d{4}-\d{2}-\d{2}\s+\d{2}\:\d{2}\:\d{2}\s+\d+\sbytes/) { $msg_q++ ; } } -- cgit v1.2.3-74-g34f1 From db1f87c39e0ff0d76d13babfcbb332c4cc3ad0fe Mon Sep 17 00:00:00 2001 From: lgmu <80966566+lgmu@users.noreply.github.com> Date: Thu, 19 Jan 2023 11:33:25 +0100 Subject: 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(-) (limited to 'plugins-scripts') diff --git a/plugins-scripts/check_log.sh b/plugins-scripts/check_log.sh index fdb57416..1ea70b56 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" -- cgit v1.2.3-74-g34f1 From b153a8c499802c2fdba199e84f5f7426ff4c0748 Mon Sep 17 00:00:00 2001 From: lgmu <80966566+lgmu@users.noreply.github.com> Date: Thu, 19 Jan 2023 11:37:45 +0100 Subject: Fix indents --- plugins-scripts/check_log.sh | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) (limited to 'plugins-scripts') diff --git a/plugins-scripts/check_log.sh b/plugins-scripts/check_log.sh index 1ea70b56..c623a8d6 100755 --- a/plugins-scripts/check_log.sh +++ b/plugins-scripts/check_log.sh @@ -153,18 +153,18 @@ done # Parameter sanity check if [ $ERE ] && [ $PRE ] ; then - echo "Can not use extended and perl regex at the same time" - exit "$STATE_UNKNOWN" + echo "Can not use extended and perl regex at the same time" + exit "$STATE_UNKNOWN" fi GREP="grep" if [ $ERE ]; then - GREP="grep -E" + GREP="grep -E" fi if [ $PRE ]; then - GREP="grep -P" + GREP="grep -P" fi # If the source log file doesn't exist, exit -- cgit v1.2.3-74-g34f1 From c410ad38798fde8cc278a3f1522a9571dbdb7fae Mon Sep 17 00:00:00 2001 From: lorenzg Date: Fri, 20 Jan 2023 08:52:38 +0100 Subject: add tests for check_log --- plugins-scripts/t/check_log.t | 82 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 82 insertions(+) create mode 100644 plugins-scripts/t/check_log.t (limited to 'plugins-scripts') diff --git a/plugins-scripts/t/check_log.t b/plugins-scripts/t/check_log.t new file mode 100644 index 00000000..b66e0fd8 --- /dev/null +++ b/plugins-scripts/t/check_log.t @@ -0,0 +1,82 @@ +#!/usr/bin/perl -w -I .. +# +# check_log tests +# +# + +use strict; +use Test::More; +use NPTest; + +my $tests = 18; +plan tests => $tests; + +my $firstTimeOutput ='/^Log check data initialized/'; +my $okOutput = '/^Log check ok - 0 pattern matches found/'; +my $criticalOutput = '/^\(\d+\) < /'; +my $multilineOutput = '/\(3\) <.*\n.*\n.*$/'; +my $unknownOutput = '/^Usage: /'; +my $unknownArgOutput = '/^Unknown argument: /'; +my $bothRegexOutput = '/^Can not use extended and perl regex/'; + +my $result; +my $temp_file = "/tmp/check_log.tmp"; +my $oldlog = "/tmp/oldlog.tmp"; + +open(FH, '>', $temp_file) or die $!; +close(FH); + +$result = NPTest->testCmd("./check_log"); +cmp_ok( $result->return_code, '==', 3, "Missing parameters" ); +like ( $result->output, $unknownOutput, "Output for unknown correct" ); + +$result = NPTest->testCmd("./check_log -f"); +cmp_ok( $result->return_code, '==', 3, "Wrong parameters" ); +like ( $result->output, $unknownArgOutput, "Output for unknown correct" ); + +$result = NPTest->testCmd("./check_log -F ".$temp_file." -O ".$oldlog." -q 'Simple match' -e -p"); +cmp_ok( $result->return_code, '==', 3, "Both regex parameters" ); +like ( $result->output, $bothRegexOutput, "Output for unknown correct" ); + +$result = NPTest->testCmd("./check_log -F ".$temp_file." -O ".$oldlog." -q 'Simple match'"); +cmp_ok( $result->return_code, '==', 0, "First time executing" ); +like ( $result->output, $firstTimeOutput, "Output for first time executing correct" ); + +open(FH, '>>', $temp_file) or die $!; +print FH "This is some text, that should not match\n"; +close(FH); + +$result = NPTest->testCmd("./check_log -F ".$temp_file." -O ".$oldlog." -q 'No match'"); +cmp_ok( $result->return_code, '==', 0, "No match" ); +like ( $result->output, $okOutput, "Output for no match correct" ); + +open(FH, '>>', $temp_file) or die $!; +print FH "This text should match\n"; +close(FH); + +$result = NPTest->testCmd("./check_log -F ".$temp_file." -O ".$oldlog." -q 'should match'"); +cmp_ok( $result->return_code, '==', 2, "Pattern match" ); +like ( $result->output, $criticalOutput, "Output for match correct" ); + +open(FH, '>>', $temp_file) or die $!; +print FH "This text should not match, because it is excluded\n"; +close(FH); + +$result = NPTest->testCmd("./check_log -F ".$temp_file." -O ".$oldlog." -q 'match' --exclude 'because'"); +cmp_ok( $result->return_code, '==', 0, "Exclude a pattern" ); +like ( $result->output, $okOutput, "Output for no match correct" ); + +open(FH, '>>', $temp_file) or die $!; +print FH "Trying\nwith\nmultiline\nignore me\n"; +close(FH); + +$result = NPTest->testCmd("./check_log -F ".$temp_file." -O ".$oldlog." -q 'Trying\\|with\\|multiline\\|ignore' --exclude 'me' --all"); +cmp_ok( $result->return_code, '==', 2, "Multiline pattern match with --all" ); +like ( $result->output, $multilineOutput, "Output for multiline match correct" ); + +$result = NPTest->testCmd("./check_log -F ".$temp_file." -O ".$oldlog." -q 'match' -a"); +cmp_ok( $result->return_code, '==', 0, "Non matching --all" ); +like ( $result->output, $okOutput, "Output for no match correct" ); + +unlink($oldlog); +unlink($temp_file); -- cgit v1.2.3-74-g34f1