summaryrefslogtreecommitdiffstats
path: root/plugins-scripts
diff options
context:
space:
mode:
authorLorenz <12514511+RincewindsHat@users.noreply.github.com>2023-03-27 12:34:19 +0200
committerGitHub <noreply@github.com>2023-03-27 12:34:19 +0200
commitbbc97d57f7f9e0a49fea78c02861c444fe95d46a (patch)
treec8956bbbe9b73e37d261735699a82cccaacea0db /plugins-scripts
parent917dba07fded8ef04967183a4c0443b5cce2db82 (diff)
parent482aa6c7fe86d7a899e4888d14149f1ccd5401fd (diff)
downloadmonitoring-plugins-bbc97d57f7f9e0a49fea78c02861c444fe95d46a.tar.gz
Merge branch 'master' into dev/check_ssh-patches
Diffstat (limited to 'plugins-scripts')
-rwxr-xr-xplugins-scripts/check_log.sh99
-rwxr-xr-xplugins-scripts/check_mailq.pl40
-rw-r--r--plugins-scripts/t/check_log.t82
3 files changed, 149 insertions, 72 deletions
diff --git a/plugins-scripts/check_log.sh b/plugins-scripts/check_log.sh
index fdb57416..c623a8d6 100755
--- a/plugins-scripts/check_log.sh
+++ b/plugins-scripts/check_log.sh
@@ -18,7 +18,7 @@
18# On the first run of the plugin, it will return an OK state with a message 18# On the first run of the plugin, it will return an OK state with a message
19# of "Log check data initialized". On successive runs, it will return an OK 19# of "Log check data initialized". On successive runs, it will return an OK
20# state if *no* pattern matches have been found in the *difference* between the 20# state if *no* pattern matches have been found in the *difference* between the
21# log file and the older copy of the log file. If the plugin detects any 21# log file and the older copy of the log file. If the plugin detects any
22# pattern matches in the log diff, it will return a CRITICAL state and print 22# pattern matches in the log diff, it will return a CRITICAL state and print
23# out a message is the following format: "(x) last_match", where "x" is the 23# out a message is the following format: "(x) last_match", where "x" is the
24# total number of pattern matches found in the file and "last_match" is the 24# total number of pattern matches found in the file and "last_match" is the
@@ -76,6 +76,7 @@ print_usage() {
76 echo "" 76 echo ""
77 echo "Other parameters:" 77 echo "Other parameters:"
78 echo " -a|--all : Print all matching lines" 78 echo " -a|--all : Print all matching lines"
79 echo " --exclude: Exclude a pattern (-p or -e also applies here when used)"
79 echo " -p|--perl-regex : Use perl style regular expressions in the query" 80 echo " -p|--perl-regex : Use perl style regular expressions in the query"
80 echo " -e|--extended-regex : Use extended style regular expressions in the query (not necessary for GNU grep)" 81 echo " -e|--extended-regex : Use extended style regular expressions in the query (not necessary for GNU grep)"
81} 82}
@@ -99,82 +100,46 @@ if [ $# -lt 1 ]; then
99fi 100fi
100 101
101# Grab the command line arguments 102# Grab the command line arguments
102
103#logfile=$1
104#oldlog=$2
105#query=$3
106exitstatus=$STATE_WARNING #default 103exitstatus=$STATE_WARNING #default
107while test -n "$1"; do 104while test -n "$1"; do
108 case "$1" in 105 case "$1" in
109 --help) 106 -h | --help)
110 print_help
111 exit "$STATE_OK"
112 ;;
113 -h)
114 print_help 107 print_help
115 exit "$STATE_OK" 108 exit "$STATE_OK"
116 ;; 109 ;;
117 --version) 110 -V | --version)
118 print_revision "$PROGNAME" "$REVISION" 111 print_revision "$PROGNAME" "$REVISION"
119 exit "$STATE_OK" 112 exit "$STATE_OK"
120 ;; 113 ;;
121 -V) 114 -F | --filename)
122 print_revision "$PROGNAME" "$REVISION"
123 exit "$STATE_OK"
124 ;;
125 --filename)
126 logfile=$2 115 logfile=$2
127 shift 2 116 shift 2
128 ;; 117 ;;
129 -F) 118 -O | --oldlog)
130 logfile=$2
131 shift 2
132 ;;
133 --oldlog)
134 oldlog=$2 119 oldlog=$2
135 shift 2 120 shift 2
136 ;; 121 ;;
137 -O) 122 -q | --query)
138 oldlog=$2
139 shift 2
140 ;;
141 --query)
142 query=$2 123 query=$2
143 shift 2 124 shift 2
144 ;; 125 ;;
145 -q) 126 --exclude)
146 query=$2 127 exclude=$2
147 shift 2 128 shift 2
148 ;; 129 ;;
149 -x) 130 -x | --exitstatus)
150 exitstatus=$2 131 exitstatus=$2
151 shift 2 132 shift 2
152 ;; 133 ;;
153 --exitstatus) 134 -e | --extended-regex)
154 exitstatus=$2
155 shift 2
156 ;;
157 --extended-regex)
158 ERE=1 135 ERE=1
159 shift 136 shift
160 ;; 137 ;;
161 -e) 138 -p | --perl-regex)
162 ERE=1
163 shift
164 ;;
165 --perl-regex)
166 PRE=1
167 shift
168 ;;
169 -p)
170 PRE=1 139 PRE=1
171 shift 140 shift
172 ;; 141 ;;
173 --all) 142 -a | --all)
174 ALL=1
175 shift
176 ;;
177 -a)
178 ALL=1 143 ALL=1
179 shift 144 shift
180 ;; 145 ;;
@@ -188,18 +153,18 @@ done
188 153
189# Parameter sanity check 154# Parameter sanity check
190if [ $ERE ] && [ $PRE ] ; then 155if [ $ERE ] && [ $PRE ] ; then
191 echo "Can not use extended and perl regex at the same time" 156 echo "Can not use extended and perl regex at the same time"
192 exit "$STATE_UNKNOWN" 157 exit "$STATE_UNKNOWN"
193fi 158fi
194 159
195GREP="grep" 160GREP="grep"
196 161
197if [ $ERE ]; then 162if [ $ERE ]; then
198 GREP="grep -E" 163 GREP="grep -E"
199fi 164fi
200 165
201if [ $PRE ]; then 166if [ $PRE ]; then
202 GREP="grep -P" 167 GREP="grep -P"
203fi 168fi
204 169
205# If the source log file doesn't exist, exit 170# If the source log file doesn't exist, exit
@@ -213,8 +178,8 @@ elif [ ! -r "$logfile" ] ; then
213fi 178fi
214# If no oldlog was given this can not work properly, abort then 179# If no oldlog was given this can not work properly, abort then
215if [ -z "$oldlog" ]; then 180if [ -z "$oldlog" ]; then
216 echo "Oldlog parameter is needed" 181 echo "Oldlog parameter is needed"
217 exit $STATE_UNKNOWN 182 exit $STATE_UNKNOWN
218fi 183fi
219 184
220# If the old log file doesn't exist, this must be the first time 185# If the old log file doesn't exist, this must be the first time
@@ -245,18 +210,24 @@ diff "$logfile" "$oldlog" | grep -v "^>" > "$tempdiff"
245 210
246 211
247if [ $ALL ]; then 212if [ $ALL ]; then
248 # Get the last matching entry in the diff file 213 # Get all matching entries in the diff file
249 entry=$($GREP "$query" "$tempdiff") 214 if [ -n "$exclude" ]; then
250 215 entry=$($GREP "$query" "$tempdiff" | $GREP -v "$exclude")
251 # Count the number of matching log entries we have 216 count=$($GREP "$query" "$tempdiff" | $GREP -vc "$exclude")
252 count=$(echo "$entry" | wc -l) 217 else
218 entry=$($GREP "$query" "$tempdiff")
219 count=$($GREP -c "$query" "$tempdiff")
220 fi
253 221
254else 222else
255 # Count the number of matching log entries we have 223 # Get the last matching entry in the diff file
256 count=$($GREP -c "$query" "$tempdiff") 224 if [ -n "$exclude" ]; then
257 225 entry=$($GREP "$query" "$tempdiff" | $GREP -v "$exclude" | tail -1)
258 # Get the last matching entry in the diff file 226 count=$($GREP "$query" "$tempdiff" | $GREP -vc "$exclude")
259 entry=$($GREP "$query" "$tempdiff" | tail -1) 227 else
228 entry=$($GREP "$query" "$tempdiff" | tail -1)
229 count=$($GREP -c "$query" "$tempdiff")
230 fi
260fi 231fi
261 232
262rm -f "$tempdiff" 233rm -f "$tempdiff"
diff --git a/plugins-scripts/check_mailq.pl b/plugins-scripts/check_mailq.pl
index 4c72332a..f02c90fb 100755
--- a/plugins-scripts/check_mailq.pl
+++ b/plugins-scripts/check_mailq.pl
@@ -149,7 +149,26 @@ if ($mailq eq "sendmail") {
149##/var/spool/mqueue/qF/df is empty 149##/var/spool/mqueue/qF/df is empty
150## Total Requests: 1 150## Total Requests: 1
151 151
152 152# separate submission/transport queues, empty
153## MSP Queue status...
154## /var/spool/mqueue-client is empty
155## Total requests: 0
156## MTA Queue status...
157## /var/spool/mqueue is empty
158## Total requests: 0
159# separate submission/transport queues: 1
160## MSP Queue status...
161## /var/spool/mqueue-client (1 request)
162## -----Q-ID----- --Size-- -----Q-Time----- ------------Sender/Recipient-----------
163## oAJEfhdW014123 5 Fri Nov 19 14:41 jwm
164## (Deferred: Connection refused by [127.0.0.1])
165## root
166## Total requests: 1
167## MTA Queue status...
168## /var/spool/mqueue is empty
169## Total requests: 0
170
171 my $this_msg_q = 0;
153 while (<MAILQ>) { 172 while (<MAILQ>) {
154 173
155 # match email addr on queue listing 174 # match email addr on queue listing
@@ -189,13 +208,18 @@ if ($mailq eq "sendmail") {
189 # 208 #
190 # single queue: first line 209 # single queue: first line
191 # multi queue: one for each queue. overwrite on multi queue below 210 # multi queue: one for each queue. overwrite on multi queue below
192 $msg_q = $1 ; 211 $this_msg_q = $1 ;
212 $msg_q += $1 ;
193 } 213 }
194 } elsif (/^\s+Total\sRequests:\s(\d+)$/i) { 214 } elsif (/^\s+Total\sRequests:\s(\d+)$/i) {
195 print "$utils::PATH_TO_MAILQ = $_ \n" if $verbose ; 215 if ($this_msg_q) {
196 # 216 $this_msg_q = 0 ;
197 # multi queue: last line 217 } else {
198 $msg_q = $1 ; 218 print "$utils::PATH_TO_MAILQ = $_ \n" if $verbose ;
219 #
220 # multi queue: last line
221 $msg_q += $1 ;
222 }
199 } 223 }
200 224
201 } 225 }
@@ -537,9 +561,9 @@ elsif ( $mailq eq "nullmailer" ) {
537 } 561 }
538 562
539 while (<MAILQ>) { 563 while (<MAILQ>) {
540 #2006-06-22 16:00:00 282 bytes 564 #2022-08-25 01:30:40 502 bytes from <user@example.com>
541 565
542 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/) { 566 if (/^\d{4}-\d{2}-\d{2}\s+\d{2}\:\d{2}\:\d{2}\s+\d+\sbytes/) {
543 $msg_q++ ; 567 $msg_q++ ;
544 } 568 }
545 } 569 }
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 @@
1#!/usr/bin/perl -w -I ..
2#
3# check_log tests
4#
5#
6
7use strict;
8use Test::More;
9use NPTest;
10
11my $tests = 18;
12plan tests => $tests;
13
14my $firstTimeOutput ='/^Log check data initialized/';
15my $okOutput = '/^Log check ok - 0 pattern matches found/';
16my $criticalOutput = '/^\(\d+\) < /';
17my $multilineOutput = '/\(3\) <.*\n.*\n.*$/';
18my $unknownOutput = '/^Usage: /';
19my $unknownArgOutput = '/^Unknown argument: /';
20my $bothRegexOutput = '/^Can not use extended and perl regex/';
21
22my $result;
23my $temp_file = "/tmp/check_log.tmp";
24my $oldlog = "/tmp/oldlog.tmp";
25
26open(FH, '>', $temp_file) or die $!;
27close(FH);
28
29$result = NPTest->testCmd("./check_log");
30cmp_ok( $result->return_code, '==', 3, "Missing parameters" );
31like ( $result->output, $unknownOutput, "Output for unknown correct" );
32
33$result = NPTest->testCmd("./check_log -f");
34cmp_ok( $result->return_code, '==', 3, "Wrong parameters" );
35like ( $result->output, $unknownArgOutput, "Output for unknown correct" );
36
37$result = NPTest->testCmd("./check_log -F ".$temp_file." -O ".$oldlog." -q 'Simple match' -e -p");
38cmp_ok( $result->return_code, '==', 3, "Both regex parameters" );
39like ( $result->output, $bothRegexOutput, "Output for unknown correct" );
40
41$result = NPTest->testCmd("./check_log -F ".$temp_file." -O ".$oldlog." -q 'Simple match'");
42cmp_ok( $result->return_code, '==', 0, "First time executing" );
43like ( $result->output, $firstTimeOutput, "Output for first time executing correct" );
44
45open(FH, '>>', $temp_file) or die $!;
46print FH "This is some text, that should not match\n";
47close(FH);
48
49$result = NPTest->testCmd("./check_log -F ".$temp_file." -O ".$oldlog." -q 'No match'");
50cmp_ok( $result->return_code, '==', 0, "No match" );
51like ( $result->output, $okOutput, "Output for no match correct" );
52
53open(FH, '>>', $temp_file) or die $!;
54print FH "This text should match\n";
55close(FH);
56
57$result = NPTest->testCmd("./check_log -F ".$temp_file." -O ".$oldlog." -q 'should match'");
58cmp_ok( $result->return_code, '==', 2, "Pattern match" );
59like ( $result->output, $criticalOutput, "Output for match correct" );
60
61open(FH, '>>', $temp_file) or die $!;
62print FH "This text should not match, because it is excluded\n";
63close(FH);
64
65$result = NPTest->testCmd("./check_log -F ".$temp_file." -O ".$oldlog." -q 'match' --exclude 'because'");
66cmp_ok( $result->return_code, '==', 0, "Exclude a pattern" );
67like ( $result->output, $okOutput, "Output for no match correct" );
68
69open(FH, '>>', $temp_file) or die $!;
70print FH "Trying\nwith\nmultiline\nignore me\n";
71close(FH);
72
73$result = NPTest->testCmd("./check_log -F ".$temp_file." -O ".$oldlog." -q 'Trying\\|with\\|multiline\\|ignore' --exclude 'me' --all");
74cmp_ok( $result->return_code, '==', 2, "Multiline pattern match with --all" );
75like ( $result->output, $multilineOutput, "Output for multiline match correct" );
76
77$result = NPTest->testCmd("./check_log -F ".$temp_file." -O ".$oldlog." -q 'match' -a");
78cmp_ok( $result->return_code, '==', 0, "Non matching --all" );
79like ( $result->output, $okOutput, "Output for no match correct" );
80
81unlink($oldlog);
82unlink($temp_file);