diff options
author | lgmu <80966566+lgmu@users.noreply.github.com> | 2023-01-19 10:33:25 (GMT) |
---|---|---|
committer | Sven Nierlein <sven@nierlein.org> | 2023-01-20 08:11:26 (GMT) |
commit | db1f87c39e0ff0d76d13babfcbb332c4cc3ad0fe (patch) | |
tree | 07af50a233d55c38a6a22ffa460de159270ebf17 | |
parent | f4930aee28ccb664691809e7dcc9198eb81429c5 (diff) | |
download | monitoring-plugins-db1f87c39e0ff0d76d13babfcbb332c4cc3ad0fe.tar.gz |
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
-rwxr-xr-x | plugins-scripts/check_log.sh | 91 |
1 files changed, 31 insertions, 60 deletions
diff --git a/plugins-scripts/check_log.sh b/plugins-scripts/check_log.sh index fdb5741..1ea70b5 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 | |||
99 | fi | 100 | fi |
100 | 101 | ||
101 | # Grab the command line arguments | 102 | # Grab the command line arguments |
102 | |||
103 | #logfile=$1 | ||
104 | #oldlog=$2 | ||
105 | #query=$3 | ||
106 | exitstatus=$STATE_WARNING #default | 103 | exitstatus=$STATE_WARNING #default |
107 | while test -n "$1"; do | 104 | while 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" | ||
119 | exit "$STATE_OK" | ||
120 | ;; | ||
121 | -V) | ||
122 | print_revision "$PROGNAME" "$REVISION" | 111 | print_revision "$PROGNAME" "$REVISION" |
123 | exit "$STATE_OK" | 112 | exit "$STATE_OK" |
124 | ;; | 113 | ;; |
125 | --filename) | 114 | -F | --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 | ||
143 | shift 2 | ||
144 | ;; | ||
145 | -q) | ||
146 | query=$2 | 123 | query=$2 |
147 | shift 2 | 124 | shift 2 |
148 | ;; | 125 | ;; |
149 | -x) | 126 | --exclude) |
150 | exitstatus=$2 | 127 | exclude=$2 |
151 | shift 2 | 128 | shift 2 |
152 | ;; | 129 | ;; |
153 | --exitstatus) | 130 | -x | --exitstatus) |
154 | exitstatus=$2 | 131 | exitstatus=$2 |
155 | shift 2 | 132 | shift 2 |
156 | ;; | 133 | ;; |
157 | --extended-regex) | 134 | -e | --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 | ;; |
@@ -213,8 +178,8 @@ elif [ ! -r "$logfile" ] ; then | |||
213 | fi | 178 | fi |
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 |
215 | if [ -z "$oldlog" ]; then | 180 | if [ -z "$oldlog" ]; then |
216 | echo "Oldlog parameter is needed" | 181 | echo "Oldlog parameter is needed" |
217 | exit $STATE_UNKNOWN | 182 | exit $STATE_UNKNOWN |
218 | fi | 183 | fi |
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 | ||
247 | if [ $ALL ]; then | 212 | if [ $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 | ||
254 | else | 222 | else |
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 | ||
260 | fi | 231 | fi |
261 | 232 | ||
262 | rm -f "$tempdiff" | 233 | rm -f "$tempdiff" |