diff options
author | RincewindsHat <12514511+RincewindsHat@users.noreply.github.com> | 2021-11-28 13:30:22 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-11-28 13:30:22 (GMT) |
commit | d752c891f47e54cfdb07389d73272cbab9137e01 (patch) | |
tree | a244cf9f011470156db180276021045a5fa544e7 | |
parent | 36415f0c3ecbed76f3e7c161885704f964f2e385 (diff) | |
parent | 10db89344adc7dad1d1ec4ac00e4fadc6b79fa72 (diff) | |
download | monitoring-plugins-d752c891f47e54cfdb07389d73272cbab9137e01.tar.gz |
Merge pull request #1692 from RincewindsHat/modernize_check_log
Modernize check log
-rwxr-xr-x | plugins-scripts/check_log.sh | 136 |
1 files changed, 94 insertions, 42 deletions
diff --git a/plugins-scripts/check_log.sh b/plugins-scripts/check_log.sh index d28c8d0..382bd72 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 | # |
@@ -60,20 +59,25 @@ | |||
60 | 59 | ||
61 | PATH="@TRUSTED_PATH@" | 60 | PATH="@TRUSTED_PATH@" |
62 | export PATH | 61 | export PATH |
63 | PROGNAME=`basename $0` | 62 | PROGNAME=$(basename "$0") |
64 | PROGPATH=`echo $0 | sed -e 's,[\\/][^\\/][^\\/]*$,,'` | 63 | PROGPATH=$(echo "$0" | sed -e 's,[\\/][^\\/][^\\/]*$,,') |
65 | REVISION="@NP_VERSION@" | 64 | REVISION="@NP_VERSION@" |
66 | 65 | ||
67 | . $PROGPATH/utils.sh | 66 | . "$PROGPATH"/utils.sh |
68 | 67 | ||
69 | print_usage() { | 68 | 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 | ||
75 | print_help() { | 79 | print_help() { |
76 | print_revision $PROGNAME $REVISION | 80 | print_revision "$PROGNAME" "$REVISION" |
77 | echo "" | 81 | echo "" |
78 | print_usage | 82 | print_usage |
79 | echo "" | 83 | echo "" |
@@ -87,7 +91,7 @@ print_help() { | |||
87 | 91 | ||
88 | if [ $# -lt 1 ]; then | 92 | if [ $# -lt 1 ]; then |
89 | print_usage | 93 | print_usage |
90 | exit $STATE_UNKNOWN | 94 | exit "$STATE_UNKNOWN" |
91 | fi | 95 | fi |
92 | 96 | ||
93 | # Grab the command line arguments | 97 | # Grab the command line arguments |
@@ -100,79 +104,118 @@ while test -n "$1"; do | |||
100 | case "$1" in | 104 | case "$1" in |
101 | --help) | 105 | --help) |
102 | print_help | 106 | print_help |
103 | exit $STATE_OK | 107 | exit "$STATE_OK" |
104 | ;; | 108 | ;; |
105 | -h) | 109 | -h) |
106 | print_help | 110 | print_help |
107 | exit $STATE_OK | 111 | exit "$STATE_OK" |
108 | ;; | 112 | ;; |
109 | --version) | 113 | --version) |
110 | print_revision $PROGNAME $REVISION | 114 | print_revision "$PROGNAME" "$REVISION" |
111 | exit $STATE_OK | 115 | exit "$STATE_OK" |
112 | ;; | 116 | ;; |
113 | -V) | 117 | -V) |
114 | print_revision $PROGNAME $REVISION | 118 | print_revision "$PROGNAME" "$REVISION" |
115 | exit $STATE_OK | 119 | exit "$STATE_OK" |
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 | *) |
150 | echo "Unknown argument: $1" | 178 | echo "Unknown argument: $1" |
151 | print_usage | 179 | print_usage |
152 | exit $STATE_UNKNOWN | 180 | exit "$STATE_UNKNOWN" |
153 | ;; | 181 | ;; |
154 | esac | 182 | esac |
155 | shift | ||
156 | done | 183 | done |
157 | 184 | ||
185 | # Parameter sanity check | ||
186 | if [ $ERE ] && [ $PRE ] ; then | ||
187 | echo "Can not use extended and perl regex at the same time" | ||
188 | exit "$STATE_UNKNOWN" | ||
189 | fi | ||
190 | |||
191 | GREP="grep" | ||
192 | |||
193 | if [ $ERE ]; then | ||
194 | GREP="grep -E" | ||
195 | fi | ||
196 | |||
197 | if [ $PRE ]; then | ||
198 | GREP="grep -P" | ||
199 | fi | ||
200 | |||
158 | # If the source log file doesn't exist, exit | 201 | # If the source log file doesn't exist, exit |
159 | 202 | ||
160 | if [ ! -e $logfile ]; then | 203 | if [ ! -e "$logfile" ]; then |
161 | echo "Log check error: Log file $logfile does not exist!" | 204 | echo "Log check error: Log file $logfile does not exist!" |
162 | exit $STATE_UNKNOWN | 205 | exit "$STATE_UNKNOWN" |
163 | elif [ ! -r $logfile ] ; then | 206 | elif [ ! -r "$logfile" ] ; then |
164 | echo "Log check error: Log file $logfile is not readable!" | 207 | echo "Log check error: Log file $logfile is not readable!" |
165 | exit $STATE_UNKNOWN | 208 | exit "$STATE_UNKNOWN" |
166 | fi | 209 | fi |
167 | 210 | ||
168 | # If the old log file doesn't exist, this must be the first time | 211 | # If the old log file doesn't exist, this must be the first time |
169 | # we're running this test, so copy the original log file over to | 212 | # we're running this test, so copy the original log file over to |
170 | # the old diff file and exit | 213 | # the old diff file and exit |
171 | 214 | ||
172 | if [ ! -e $oldlog ]; then | 215 | if [ ! -e "$oldlog" ]; then |
173 | cat $logfile > $oldlog | 216 | cat "$logfile" > "$oldlog" |
174 | echo "Log check data initialized..." | 217 | echo "Log check data initialized..." |
175 | exit $STATE_OK | 218 | exit "$STATE_OK" |
176 | fi | 219 | fi |
177 | 220 | ||
178 | # The old log file exists, so compare it to the original log now | 221 | # The old log file exists, so compare it to the original log now |
@@ -180,31 +223,40 @@ 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. |
182 | if [ -x /bin/mktemp ]; then | 225 | if [ -x /bin/mktemp ]; then |
183 | tempdiff=`/bin/mktemp /tmp/check_log.XXXXXXXXXX` | 226 | tempdiff=$(/bin/mktemp /tmp/check_log.XXXXXXXXXX) |
184 | else | 227 | else |
185 | tempdiff=`/bin/date '+%H%M%S'` | 228 | tempdiff=$(/bin/date '+%H%M%S') |
186 | tempdiff="/tmp/check_log.${tempdiff}" | 229 | tempdiff="/tmp/check_log.${tempdiff}" |
187 | touch $tempdiff | 230 | touch "$tempdiff" |
188 | chmod 600 $tempdiff | 231 | chmod 600 "$tempdiff" |
189 | fi | 232 | fi |
190 | 233 | ||
191 | diff $logfile $oldlog | grep -v "^>" > $tempdiff | 234 | diff "$logfile" "$oldlog" | grep -v "^>" > "$tempdiff" |
235 | |||
236 | if [ $ALL ]; then | ||
237 | # Get the last matching entry in the diff file | ||
238 | entry=$($GREP "$query" "$tempdiff") | ||
192 | 239 | ||
193 | # Count the number of matching log entries we have | 240 | # Count the number of matching log entries we have |
194 | count=`grep -c "$query" $tempdiff` | 241 | count=$(echo "$entry" | wc -l) |
195 | 242 | ||
196 | # Get the last matching entry in the diff file | 243 | else |
197 | lastentry=`grep "$query" $tempdiff | tail -1` | 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) | ||
249 | fi | ||
198 | 250 | ||
199 | rm -f $tempdiff | 251 | rm -f "$tempdiff" |
200 | cat $logfile > $oldlog | 252 | cat "$logfile" > "$oldlog" |
201 | 253 | ||
202 | if [ "$count" = "0" ]; then # no matches, exit with no error | 254 | if [ "$count" = "0" ]; then # no matches, exit with no error |
203 | echo "Log check ok - 0 pattern matches found" | 255 | echo "Log check ok - 0 pattern matches found" |
204 | exitstatus=$STATE_OK | 256 | exitstatus=$STATE_OK |
205 | else # Print total matche count and the last entry we found | 257 | else # Print total matche count and the last entry we found |
206 | echo "($count) $lastentry" | 258 | echo "($count) $entry" |
207 | exitstatus=$STATE_CRITICAL | 259 | exitstatus=$STATE_CRITICAL |
208 | fi | 260 | fi |
209 | 261 | ||
210 | exit $exitstatus | 262 | exit "$exitstatus" |