summaryrefslogtreecommitdiffstats
path: root/plugins-scripts/check_log.sh
diff options
context:
space:
mode:
Diffstat (limited to 'plugins-scripts/check_log.sh')
-rwxr-xr-xplugins-scripts/check_log.sh149
1 files changed, 106 insertions, 43 deletions
diff --git a/plugins-scripts/check_log.sh b/plugins-scripts/check_log.sh
index d28c8d0a..fdb57416 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#
@@ -44,6 +43,10 @@
44# check the same <log_file> for pattern matches. This is necessary 43# check the same <log_file> for pattern matches. This is necessary
45# because of the way the script operates. 44# because of the way the script operates.
46# 45#
46# 4. This plugin does NOT have an understanding of logrotation or similar
47# mechanisms. Therefore bad timing could lead to missing events
48#
49#
47# Examples: 50# Examples:
48# 51#
49# Check for login failures in the syslog... 52# Check for login failures in the syslog...
@@ -60,20 +63,25 @@
60 63
61PATH="@TRUSTED_PATH@" 64PATH="@TRUSTED_PATH@"
62export PATH 65export PATH
63PROGNAME=`basename $0` 66PROGNAME=$(basename "$0")
64PROGPATH=`echo $0 | sed -e 's,[\\/][^\\/][^\\/]*$,,'` 67PROGPATH=$(echo "$0" | sed -e 's,[\\/][^\\/][^\\/]*$,,')
65REVISION="@NP_VERSION@" 68REVISION="@NP_VERSION@"
66 69
67. $PROGPATH/utils.sh 70. "$PROGPATH"/utils.sh
68 71
69print_usage() { 72print_usage() {
70 echo "Usage: $PROGNAME -F logfile -O oldlog -q query" 73 echo "Usage: $PROGNAME -F logfile -O oldlog -q query"
71 echo "Usage: $PROGNAME --help" 74 echo "Usage: $PROGNAME --help"
72 echo "Usage: $PROGNAME --version" 75 echo "Usage: $PROGNAME --version"
76 echo ""
77 echo "Other parameters:"
78 echo " -a|--all : Print all matching lines"
79 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)"
73} 81}
74 82
75print_help() { 83print_help() {
76 print_revision $PROGNAME $REVISION 84 print_revision "$PROGNAME" "$REVISION"
77 echo "" 85 echo ""
78 print_usage 86 print_usage
79 echo "" 87 echo ""
@@ -87,7 +95,7 @@ print_help() {
87 95
88if [ $# -lt 1 ]; then 96if [ $# -lt 1 ]; then
89 print_usage 97 print_usage
90 exit $STATE_UNKNOWN 98 exit "$STATE_UNKNOWN"
91fi 99fi
92 100
93# Grab the command line arguments 101# Grab the command line arguments
@@ -100,79 +108,123 @@ while test -n "$1"; do
100 case "$1" in 108 case "$1" in
101 --help) 109 --help)
102 print_help 110 print_help
103 exit $STATE_OK 111 exit "$STATE_OK"
104 ;; 112 ;;
105 -h) 113 -h)
106 print_help 114 print_help
107 exit $STATE_OK 115 exit "$STATE_OK"
108 ;; 116 ;;
109 --version) 117 --version)
110 print_revision $PROGNAME $REVISION 118 print_revision "$PROGNAME" "$REVISION"
111 exit $STATE_OK 119 exit "$STATE_OK"
112 ;; 120 ;;
113 -V) 121 -V)
114 print_revision $PROGNAME $REVISION 122 print_revision "$PROGNAME" "$REVISION"
115 exit $STATE_OK 123 exit "$STATE_OK"
116 ;; 124 ;;
117 --filename) 125 --filename)
118 logfile=$2 126 logfile=$2
119 shift 127 shift 2
120 ;; 128 ;;
121 -F) 129 -F)
122 logfile=$2 130 logfile=$2
123 shift 131 shift 2
124 ;; 132 ;;
125 --oldlog) 133 --oldlog)
126 oldlog=$2 134 oldlog=$2
127 shift 135 shift 2
128 ;; 136 ;;
129 -O) 137 -O)
130 oldlog=$2 138 oldlog=$2
131 shift 139 shift 2
132 ;; 140 ;;
133 --query) 141 --query)
134 query=$2 142 query=$2
135 shift 143 shift 2
136 ;; 144 ;;
137 -q) 145 -q)
138 query=$2 146 query=$2
139 shift 147 shift 2
140 ;; 148 ;;
141 -x) 149 -x)
142 exitstatus=$2 150 exitstatus=$2
143 shift 151 shift 2
144 ;; 152 ;;
145 --exitstatus) 153 --exitstatus)
146 exitstatus=$2 154 exitstatus=$2
155 shift 2
156 ;;
157 --extended-regex)
158 ERE=1
159 shift
160 ;;
161 -e)
162 ERE=1
163 shift
164 ;;
165 --perl-regex)
166 PRE=1
167 shift
168 ;;
169 -p)
170 PRE=1
171 shift
172 ;;
173 --all)
174 ALL=1
175 shift
176 ;;
177 -a)
178 ALL=1
147 shift 179 shift
148 ;; 180 ;;
149 *) 181 *)
150 echo "Unknown argument: $1" 182 echo "Unknown argument: $1"
151 print_usage 183 print_usage
152 exit $STATE_UNKNOWN 184 exit "$STATE_UNKNOWN"
153 ;; 185 ;;
154 esac 186 esac
155 shift
156done 187done
157 188
189# Parameter sanity check
190if [ $ERE ] && [ $PRE ] ; then
191 echo "Can not use extended and perl regex at the same time"
192 exit "$STATE_UNKNOWN"
193fi
194
195GREP="grep"
196
197if [ $ERE ]; then
198 GREP="grep -E"
199fi
200
201if [ $PRE ]; then
202 GREP="grep -P"
203fi
204
158# If the source log file doesn't exist, exit 205# If the source log file doesn't exist, exit
159 206
160if [ ! -e $logfile ]; then 207if [ ! -e "$logfile" ]; then
161 echo "Log check error: Log file $logfile does not exist!" 208 echo "Log check error: Log file $logfile does not exist!"
162 exit $STATE_UNKNOWN 209 exit "$STATE_UNKNOWN"
163elif [ ! -r $logfile ] ; then 210elif [ ! -r "$logfile" ] ; then
164 echo "Log check error: Log file $logfile is not readable!" 211 echo "Log check error: Log file $logfile is not readable!"
165 exit $STATE_UNKNOWN 212 exit "$STATE_UNKNOWN"
213fi
214# If no oldlog was given this can not work properly, abort then
215if [ -z "$oldlog" ]; then
216 echo "Oldlog parameter is needed"
217 exit $STATE_UNKNOWN
166fi 218fi
167 219
168# If the old log file doesn't exist, this must be the first time 220# 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 221# we're running this test, so copy the original log file over to
170# the old diff file and exit 222# the old diff file and exit
171 223
172if [ ! -e $oldlog ]; then 224if [ ! -e "$oldlog" ]; then
173 cat $logfile > $oldlog 225 cat "$logfile" > "$oldlog"
174 echo "Log check data initialized..." 226 echo "Log check data initialized..."
175 exit $STATE_OK 227 exit "$STATE_OK"
176fi 228fi
177 229
178# The old log file exists, so compare it to the original log now 230# The old log file exists, so compare it to the original log now
@@ -180,31 +232,42 @@ fi
180# The temporary file that the script should use while 232# The temporary file that the script should use while
181# processing the log file. 233# processing the log file.
182if [ -x /bin/mktemp ]; then 234if [ -x /bin/mktemp ]; then
183 tempdiff=`/bin/mktemp /tmp/check_log.XXXXXXXXXX` 235
236 tempdiff=$(/bin/mktemp /tmp/check_log.XXXXXXXXXX)
184else 237else
185 tempdiff=`/bin/date '+%H%M%S'` 238 tempdiff=$(/bin/date '+%H%M%S')
186 tempdiff="/tmp/check_log.${tempdiff}" 239 tempdiff="/tmp/check_log.${tempdiff}"
187 touch $tempdiff 240 touch "$tempdiff"
188 chmod 600 $tempdiff 241 chmod 600 "$tempdiff"
189fi 242fi
190 243
191diff $logfile $oldlog | grep -v "^>" > $tempdiff 244diff "$logfile" "$oldlog" | grep -v "^>" > "$tempdiff"
245
192 246
193# Count the number of matching log entries we have 247if [ $ALL ]; then
194count=`grep -c "$query" $tempdiff` 248 # Get the last matching entry in the diff file
249 entry=$($GREP "$query" "$tempdiff")
195 250
196# Get the last matching entry in the diff file 251 # Count the number of matching log entries we have
197lastentry=`grep "$query" $tempdiff | tail -1` 252 count=$(echo "$entry" | wc -l)
253
254else
255 # Count the number of matching log entries we have
256 count=$($GREP -c "$query" "$tempdiff")
257
258 # Get the last matching entry in the diff file
259 entry=$($GREP "$query" "$tempdiff" | tail -1)
260fi
198 261
199rm -f $tempdiff 262rm -f "$tempdiff"
200cat $logfile > $oldlog 263cat "$logfile" > "$oldlog"
201 264
202if [ "$count" = "0" ]; then # no matches, exit with no error 265if [ "$count" = "0" ]; then # no matches, exit with no error
203 echo "Log check ok - 0 pattern matches found" 266 echo "Log check ok - 0 pattern matches found"
204 exitstatus=$STATE_OK 267 exitstatus=$STATE_OK
205else # Print total matche count and the last entry we found 268else # Print total match count and the last entry we found
206 echo "($count) $lastentry" 269 echo "($count) $entry"
207 exitstatus=$STATE_CRITICAL 270 exitstatus=$STATE_CRITICAL
208fi 271fi
209 272
210exit $exitstatus 273exit "$exitstatus"