Code

check_host: Allocate a large-enough buffer for the host table.
[nagiosplug.git] / plugins-scripts / check_log.sh
1 #!/bin/sh
2 #
3 # Log file pattern detector plugin for Nagios
4 # Written by Ethan Galstad (nagios@nagios.org)
5 # Last Modified: 07-31-1999
6 #
7 # Usage: ./check_log <log_file> <old_log_file> <pattern>
8 #
9 # Description:
10 #
11 # This plugin will scan a log file (specified by the <log_file> option)
12 # for a specific pattern (specified by the <pattern> option).  Successive
13 # calls to the plugin script will only report *new* pattern matches in the
14 # log file, since an copy of the log file from the previous run is saved
15 # to <old_log_file>.
16 #
17 # Output:
18 #
19 # On the first run of the plugin, it will return an OK state with a message
20 # of "Log check data initialized".  On successive runs, it will return an OK
21 # state if *no* pattern matches have been found in the *difference* between the
22 # log file and the older copy of the log file.  If the plugin detects any 
23 # pattern matches in the log diff, it will return a CRITICAL state and print
24 # out a message is the following format: "(x) last_match", where "x" is the
25 # total number of pattern matches found in the file and "last_match" is the
26 # last entry in the log file which matches the pattern.
27 #
28 # Notes:
29 #
30 # If you use this plugin make sure to keep the following in mind:
31 #
32 #    1.  The "max_attempts" value for the service should be 1, as this
33 #        will prevent Nagios from retrying the service check (the
34 #        next time the check is run it will not produce the same results).
35 #
36 #    2.  The "notify_recovery" value for the service should be 0, so that
37 #        Nagios does not notify you of "recoveries" for the check.  Since
38 #        pattern matches in the log file will only be reported once and not
39 #        the next time, there will always be "recoveries" for the service, even
40 #        though recoveries really don't apply to this type of check.
41 #
42 #    3.  You *must* supply a different <old_file_log> for each service that
43 #        you define to use this plugin script - even if the different services
44 #        check the same <log_file> for pattern matches.  This is necessary
45 #        because of the way the script operates.
46 #
47 # Examples:
48 #
49 # Check for login failures in the syslog...
50 #
51 #   check_log /var/log/messages ./check_log.badlogins.old "LOGIN FAILURE"
52 #
53 # Check for port scan alerts generated by Psionic's PortSentry software...
54 #
55 #   check_log /var/log/message ./check_log.portscan.old "attackalert"
56 #
58 # Paths to commands used in this script.  These
59 # may have to be modified to match your system setup.
60 # TV: removed PATH restriction. Need to think more about what this means overall
61 #PATH=""
63 ECHO="/bin/echo"
64 GREP="/bin/egrep"
65 DIFF="/bin/diff"
66 TAIL="/bin/tail"
67 CAT="/bin/cat"
68 RM="/bin/rm"
69 CHMOD="/bin/chmod"
70 TOUCH="/bin/touch"
72 PROGNAME=`/bin/basename $0`
73 PROGPATH=`echo $0 | sed -e 's,[\\/][^\\/][^\\/]*$,,'`
74 REVISION="@NP_VERSION@"
76 . $PROGPATH/utils.sh
78 print_usage() {
79     echo "Usage: $PROGNAME -F logfile -O oldlog -q query"
80     echo "Usage: $PROGNAME --help"
81     echo "Usage: $PROGNAME --version"
82 }
84 print_help() {
85     print_revision $PROGNAME $REVISION
86     echo ""
87     print_usage
88     echo ""
89     echo "Log file pattern detector plugin for Nagios"
90     echo ""
91     support
92 }
94 # Make sure the correct number of command line
95 # arguments have been supplied
97 if [ $# -lt 1 ]; then
98     print_usage
99     exit $STATE_UNKNOWN
100 fi
102 # Grab the command line arguments
104 #logfile=$1
105 #oldlog=$2
106 #query=$3
107 exitstatus=$STATE_WARNING #default
108 while test -n "$1"; do
109     case "$1" in
110         --help)
111             print_help
112             exit $STATE_OK
113             ;;
114         -h)
115             print_help
116             exit $STATE_OK
117             ;;
118         --version)
119             print_revision $PROGNAME $REVISION
120             exit $STATE_OK
121             ;;
122         -V)
123             print_revision $PROGNAME $REVISION
124             exit $STATE_OK
125             ;;
126         --filename)
127             logfile=$2
128             shift
129             ;;
130         -F)
131             logfile=$2
132             shift
133             ;;
134         --oldlog)
135             oldlog=$2
136             shift
137             ;;
138         -O)
139             oldlog=$2
140             shift
141             ;;
142         --query)
143             query=$2
144             shift
145             ;;
146         -q)
147             query=$2
148             shift
149             ;;
150         -x)
151             exitstatus=$2
152             shift
153             ;;
154         --exitstatus)
155             exitstatus=$2
156             shift
157             ;;
158         *)
159             echo "Unknown argument: $1"
160             print_usage
161             exit $STATE_UNKNOWN
162             ;;
163     esac
164     shift
165 done
167 # If the source log file doesn't exist, exit
169 if [ ! -e $logfile ]; then
170     $ECHO "Log check error: Log file $logfile does not exist!\n"
171     exit $STATE_UNKNOWN
172 elif [ ! -r $logfile ] ; then
173     $ECHO "Log check error: Log file $logfile is not readable!\n"
174     exit $STATE_UNKNOWN
175 fi
177 # If the old log file doesn't exist, this must be the first time
178 # we're running this test, so copy the original log file over to
179 # the old diff file and exit
181 if [ ! -e $oldlog ]; then
182     $CAT $logfile > $oldlog
183     $ECHO "Log check data initialized...\n"
184     exit $STATE_OK
185 fi
187 # The old log file exists, so compare it to the original log now
189 # The temporary file that the script should use while
190 # processing the log file.
191 if [ -x /bin/mktemp ]; then
192     tempdiff=`/bin/mktemp /tmp/check_log.XXXXXXXXXX`
193 else
194     tempdiff=`/bin/date '+%H%M%S'`
195     tempdiff="/tmp/check_log.${tempdiff}"
196     $TOUCH $tempdiff
197     $CHMOD 600 $tempdiff
198 fi
200 $DIFF $logfile $oldlog | $GREP -v "^>" > $tempdiff
202 # Count the number of matching log entries we have
203 count=`$GREP -c "$query" $tempdiff`
205 # Get the last matching entry in the diff file
206 lastentry=`$GREP "$query" $tempdiff | $TAIL -1`
208 $RM -f $tempdiff
209 $CAT $logfile > $oldlog
211 if [ "$count" = "0" ]; then # no matches, exit with no error
212     $ECHO "Log check ok - 0 pattern matches found\n"
213     exitstatus=$STATE_OK
214 else # Print total matche count and the last entry we found
215     $ECHO "($count) $lastentry"
216     exitstatus=$STATE_CRITICAL
217 fi
219 exit $exitstatus