summaryrefslogtreecommitdiffstats
path: root/plugins-scripts/check_log.sh
diff options
context:
space:
mode:
authorRincewindsHat <12514511+RincewindsHat@users.noreply.github.com>2021-12-02 16:42:05 +0100
committerGitHub <noreply@github.com>2021-12-02 16:42:05 +0100
commit911e44045d7291f5ede22739fd176ef55dd3de4a (patch)
treecf36b95a4a964b03d6ecf75770ced2cb3a2ac3a9 /plugins-scripts/check_log.sh
parent8294af907bd8482a86df749f562b7ec09e3faeed (diff)
parented7cdf82a42f16532801ea4f118870ce9a130fcf (diff)
downloadmonitoring-plugins-911e44045d7291f5ede22739fd176ef55dd3de4a.tar.gz
Merge branch 'master' into fix/shellcheck
Diffstat (limited to 'plugins-scripts/check_log.sh')
-rwxr-xr-xplugins-scripts/check_log.sh88
1 files changed, 71 insertions, 17 deletions
diff --git a/plugins-scripts/check_log.sh b/plugins-scripts/check_log.sh
index 8f76b1b5..61131236 100755
--- a/plugins-scripts/check_log.sh
+++ b/plugins-scripts/check_log.sh
@@ -1,8 +1,7 @@
1#!/bin/sh 1#!/bin/sh
2# 2#
3# Log file pattern detector plugin for monitoring 3# Log file pattern detector plugin for monitoring
4# Written by Ethan Galstad (nagios@nagios.org) 4# Written originally by Ethan Galstad (nagios@nagios.org)
5# Last Modified: 07-31-1999
6# 5#
7# Usage: ./check_log <log_file> <old_log_file> <pattern> 6# Usage: ./check_log <log_file> <old_log_file> <pattern>
8# 7#
@@ -70,6 +69,11 @@ print_usage() {
70 echo "Usage: $PROGNAME -F logfile -O oldlog -q query" 69 echo "Usage: $PROGNAME -F logfile -O oldlog -q query"
71 echo "Usage: $PROGNAME --help" 70 echo "Usage: $PROGNAME --help"
72 echo "Usage: $PROGNAME --version" 71 echo "Usage: $PROGNAME --version"
72 echo ""
73 echo "Other parameters:"
74 echo " -a|--all : Print all matching lines"
75 echo " -p|--perl-regex : Use perl style regular expressions in the query"
76 echo " -e|--extended-regex : Use extended style regular expressions in the query (not necessary for GNU grep)"
73} 77}
74 78
75print_help() { 79print_help() {
@@ -116,34 +120,58 @@ while test -n "$1"; do
116 ;; 120 ;;
117 --filename) 121 --filename)
118 logfile=$2 122 logfile=$2
119 shift 123 shift 2
120 ;; 124 ;;
121 -F) 125 -F)
122 logfile=$2 126 logfile=$2
123 shift 127 shift 2
124 ;; 128 ;;
125 --oldlog) 129 --oldlog)
126 oldlog=$2 130 oldlog=$2
127 shift 131 shift 2
128 ;; 132 ;;
129 -O) 133 -O)
130 oldlog=$2 134 oldlog=$2
131 shift 135 shift 2
132 ;; 136 ;;
133 --query) 137 --query)
134 query=$2 138 query=$2
135 shift 139 shift 2
136 ;; 140 ;;
137 -q) 141 -q)
138 query=$2 142 query=$2
139 shift 143 shift 2
140 ;; 144 ;;
141 -x) 145 -x)
142 exitstatus=$2 146 exitstatus=$2
143 shift 147 shift 2
144 ;; 148 ;;
145 --exitstatus) 149 --exitstatus)
146 exitstatus=$2 150 exitstatus=$2
151 shift 2
152 ;;
153 --extended-regex)
154 ERE=1
155 shift
156 ;;
157 -e)
158 ERE=1
159 shift
160 ;;
161 --perl-regex)
162 PRE=1
163 shift
164 ;;
165 -p)
166 PRE=1
167 shift
168 ;;
169 --all)
170 ALL=1
171 shift
172 ;;
173 -a)
174 ALL=1
147 shift 175 shift
148 ;; 176 ;;
149 *) 177 *)
@@ -152,9 +180,24 @@ while test -n "$1"; do
152 exit "$STATE_UNKNOWN" 180 exit "$STATE_UNKNOWN"
153 ;; 181 ;;
154 esac 182 esac
155 shift
156done 183done
157 184
185# Parameter sanity check
186if [ $ERE ] && [ $PRE ] ; then
187 echo "Can not use extended and perl regex at the same time"
188 exit "$STATE_UNKNOWN"
189fi
190
191GREP="grep"
192
193if [ $ERE ]; then
194 GREP="grep -E"
195fi
196
197if [ $PRE ]; then
198 GREP="grep -P"
199fi
200
158# If the source log file doesn't exist, exit 201# If the source log file doesn't exist, exit
159 202
160if [ ! -e "$logfile" ]; then 203if [ ! -e "$logfile" ]; then
@@ -180,9 +223,10 @@ fi
180# The temporary file that the script should use while 223# The temporary file that the script should use while
181# processing the log file. 224# processing the log file.
182if [ -x /bin/mktemp ]; then 225if [ -x /bin/mktemp ]; then
183 tempdiff=$(/bin/mktemp /tmp/check_log.XXXXXXXXXX) 226
227 tempdiff=$(/bin/mktemp /tmp/check_log.XXXXXXXXXX)
184else 228else
185 tempdiff=$(/bin/date '+%H%M%S') 229 tempdiff=$(/bin/date '+%H%M%S')
186 tempdiff="/tmp/check_log.${tempdiff}" 230 tempdiff="/tmp/check_log.${tempdiff}"
187 touch "$tempdiff" 231 touch "$tempdiff"
188 chmod 600 "$tempdiff" 232 chmod 600 "$tempdiff"
@@ -190,11 +234,21 @@ fi
190 234
191diff "$logfile" "$oldlog" | grep -v "^>" > "$tempdiff" 235diff "$logfile" "$oldlog" | grep -v "^>" > "$tempdiff"
192 236
193# Count the number of matching log entries we have
194count=$(grep -c "$query" "$tempdiff")
195 237
196# Get the last matching entry in the diff file 238if [ $ALL ]; then
197lastentry=$(grep "$query" "$tempdiff" | tail -1) 239 # Get the last matching entry in the diff file
240 entry=$($GREP "$query" "$tempdiff")
241
242 # Count the number of matching log entries we have
243 count=$(echo "$entry" | wc -l)
244
245else
246 # Count the number of matching log entries we have
247 count=$($GREP -c "$query" "$tempdiff")
248
249 # Get the last matching entry in the diff file
250 entry=$($GREP "$query" "$tempdiff" | tail -1)
251fi
198 252
199rm -f "$tempdiff" 253rm -f "$tempdiff"
200cat "$logfile" > "$oldlog" 254cat "$logfile" > "$oldlog"
@@ -203,7 +257,7 @@ if [ "$count" = "0" ]; then # no matches, exit with no error
203 echo "Log check ok - 0 pattern matches found" 257 echo "Log check ok - 0 pattern matches found"
204 exitstatus=$STATE_OK 258 exitstatus=$STATE_OK
205else # Print total matche count and the last entry we found 259else # Print total matche count and the last entry we found
206 echo "($count) $lastentry" 260 echo "($count) $entry"
207 exitstatus=$STATE_CRITICAL 261 exitstatus=$STATE_CRITICAL
208fi 262fi
209 263