summaryrefslogtreecommitdiffstats
path: root/plugins-scripts/check_log.sh
diff options
context:
space:
mode:
authorrincewind <rincewind@vulgrim.de>2021-07-02 16:38:12 (GMT)
committerrincewind <rincewind@vulgrim.de>2021-07-02 16:38:12 (GMT)
commit53b77dee91f780b4d78839f881c642189b829e3c (patch)
tree6ea73d3ef108051a6840fa9841b4bbbac62b37e6 /plugins-scripts/check_log.sh
parentc2aa1a5fa2dd96c7186704901d33721a63b9cd03 (diff)
downloadmonitoring-plugins-53b77dee91f780b4d78839f881c642189b829e3c.tar.gz
Add -a option to print all matching lines and -p and -e options for perl and extended RE
Diffstat (limited to 'plugins-scripts/check_log.sh')
-rwxr-xr-xplugins-scripts/check_log.sh51
1 files changed, 37 insertions, 14 deletions
diff --git a/plugins-scripts/check_log.sh b/plugins-scripts/check_log.sh
index 6e9fbca..8af07b5 100755
--- a/plugins-scripts/check_log.sh
+++ b/plugins-scripts/check_log.sh
@@ -69,6 +69,11 @@ print_usage() {
69 echo "Usage: $PROGNAME -F logfile -O oldlog -q query" 69 echo "Usage: $PROGNAME -F logfile -O oldlog -q query"
70 echo "Usage: $PROGNAME --help" 70 echo "Usage: $PROGNAME --help"
71 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)"
72} 77}
73 78
74print_help() { 79print_help() {
@@ -115,35 +120,35 @@ while test -n "$1"; do
115 ;; 120 ;;
116 --filename) 121 --filename)
117 logfile=$2 122 logfile=$2
118 shift 123 shift 2
119 ;; 124 ;;
120 -F) 125 -F)
121 logfile=$2 126 logfile=$2
122 shift 127 shift 2
123 ;; 128 ;;
124 --oldlog) 129 --oldlog)
125 oldlog=$2 130 oldlog=$2
126 shift 131 shift 2
127 ;; 132 ;;
128 -O) 133 -O)
129 oldlog=$2 134 oldlog=$2
130 shift 135 shift 2
131 ;; 136 ;;
132 --query) 137 --query)
133 query=$2 138 query=$2
134 shift 139 shift 2
135 ;; 140 ;;
136 -q) 141 -q)
137 query=$2 142 query=$2
138 shift 143 shift 2
139 ;; 144 ;;
140 -x) 145 -x)
141 exitstatus=$2 146 exitstatus=$2
142 shift 147 shift 2
143 ;; 148 ;;
144 --exitstatus) 149 --exitstatus)
145 exitstatus=$2 150 exitstatus=$2
146 shift 151 shift 2
147 ;; 152 ;;
148 --extended-regex) 153 --extended-regex)
149 ERE=1 154 ERE=1
@@ -161,13 +166,20 @@ while test -n "$1"; do
161 PRE=1 166 PRE=1
162 shift 167 shift
163 ;; 168 ;;
169 --all)
170 ALL=1
171 shift
172 ;;
173 -a)
174 ALL=1
175 shift
176 ;;
164 *) 177 *)
165 echo "Unknown argument: $1" 178 echo "Unknown argument: $1"
166 print_usage 179 print_usage
167 exit "$STATE_UNKNOWN" 180 exit "$STATE_UNKNOWN"
168 ;; 181 ;;
169 esac 182 esac
170 shift
171done 183done
172 184
173# Parameter sanity check 185# Parameter sanity check
@@ -176,6 +188,8 @@ if [ $ERE ] && [ $PRE ] ; then
176 exit "$STATE_UNKNOWN" 188 exit "$STATE_UNKNOWN"
177fi 189fi
178 190
191GREP="grep"
192
179if [ $ERE ]; then 193if [ $ERE ]; then
180 GREP="grep -E" 194 GREP="grep -E"
181fi 195fi
@@ -219,11 +233,20 @@ fi
219 233
220diff "$logfile" "$oldlog" | grep -v "^>" > "$tempdiff" 234diff "$logfile" "$oldlog" | grep -v "^>" > "$tempdiff"
221 235
222# Count the number of matching log entries we have 236if [ $ALL ]; then
223count=$($GREP -c "$query" "$tempdiff") 237 # Get the last matching entry in the diff file
238 entry=$($GREP "$query" "$tempdiff")
224 239
225# Get the last matching entry in the diff file 240 # Count the number of matching log entries we have
226lastentry=$($GREP "$query" "$tempdiff" | tail -1) 241 count=$(echo "$entry" | wc -l)
242
243else
244 # Count the number of matching log entries we have
245 count=$($GREP -c "$query" "$tempdiff")
246
247 # Get the last matching entry in the diff file
248 entry=$($GREP "$query" "$tempdiff" | tail -1)
249fi
227 250
228rm -f "$tempdiff" 251rm -f "$tempdiff"
229cat "$logfile" > "$oldlog" 252cat "$logfile" > "$oldlog"
@@ -232,7 +255,7 @@ if [ "$count" = "0" ]; then # no matches, exit with no error
232 echo "Log check ok - 0 pattern matches found" 255 echo "Log check ok - 0 pattern matches found"
233 exitstatus=$STATE_OK 256 exitstatus=$STATE_OK
234else # Print total matche count and the last entry we found 257else # Print total matche count and the last entry we found
235 echo "($count) $lastentry" 258 echo "($count) $entry"
236 exitstatus=$STATE_CRITICAL 259 exitstatus=$STATE_CRITICAL
237fi 260fi
238 261