check_log.sh 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  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. #
  57. # Paths to commands used in this script. These
  58. # may have to be modified to match your system setup.
  59. PROGNAME=`/bin/basename $0`
  60. PROGPATH=`echo $0 | sed -e 's,[\\/][^\\/][^\\/]*$,,'`
  61. REVISION="@NP_VERSION@"
  62. PATH="@TRUSTED_PATH@"
  63. export PATH
  64. . $PROGPATH/utils.sh
  65. print_usage() {
  66. echo "Usage: $PROGNAME -F logfile -O oldlog -q query"
  67. echo "Usage: $PROGNAME --help"
  68. echo "Usage: $PROGNAME --version"
  69. }
  70. print_help() {
  71. print_revision $PROGNAME $REVISION
  72. echo ""
  73. print_usage
  74. echo ""
  75. echo "Log file pattern detector plugin for Nagios"
  76. echo ""
  77. support
  78. }
  79. # Make sure the correct number of command line
  80. # arguments have been supplied
  81. if [ $# -lt 1 ]; then
  82. print_usage
  83. exit $STATE_UNKNOWN
  84. fi
  85. # Grab the command line arguments
  86. #logfile=$1
  87. #oldlog=$2
  88. #query=$3
  89. exitstatus=$STATE_WARNING #default
  90. while test -n "$1"; do
  91. case "$1" in
  92. --help)
  93. print_help
  94. exit $STATE_OK
  95. ;;
  96. -h)
  97. print_help
  98. exit $STATE_OK
  99. ;;
  100. --version)
  101. print_revision $PROGNAME $REVISION
  102. exit $STATE_OK
  103. ;;
  104. -V)
  105. print_revision $PROGNAME $REVISION
  106. exit $STATE_OK
  107. ;;
  108. --filename)
  109. logfile=$2
  110. shift
  111. ;;
  112. -F)
  113. logfile=$2
  114. shift
  115. ;;
  116. --oldlog)
  117. oldlog=$2
  118. shift
  119. ;;
  120. -O)
  121. oldlog=$2
  122. shift
  123. ;;
  124. --query)
  125. query=$2
  126. shift
  127. ;;
  128. -q)
  129. query=$2
  130. shift
  131. ;;
  132. -x)
  133. exitstatus=$2
  134. shift
  135. ;;
  136. --exitstatus)
  137. exitstatus=$2
  138. shift
  139. ;;
  140. *)
  141. echo "Unknown argument: $1"
  142. print_usage
  143. exit $STATE_UNKNOWN
  144. ;;
  145. esac
  146. shift
  147. done
  148. # If the source log file doesn't exist, exit
  149. if [ ! -e $logfile ]; then
  150. echo "Log check error: Log file $logfile does not exist!"
  151. exit $STATE_UNKNOWN
  152. elif [ ! -r $logfile ] ; then
  153. echo "Log check error: Log file $logfile is not readable!"
  154. exit $STATE_UNKNOWN
  155. fi
  156. # If the old log file doesn't exist, this must be the first time
  157. # we're running this test, so copy the original log file over to
  158. # the old diff file and exit
  159. if [ ! -e $oldlog ]; then
  160. cat $logfile > $oldlog
  161. echo "Log check data initialized..."
  162. exit $STATE_OK
  163. fi
  164. # The old log file exists, so compare it to the original log now
  165. # The temporary file that the script should use while
  166. # processing the log file.
  167. if [ -x /bin/mktemp ]; then
  168. tempdiff=`/bin/mktemp /tmp/check_log.XXXXXXXXXX`
  169. else
  170. tempdiff=`/bin/date '+%H%M%S'`
  171. tempdiff="/tmp/check_log.${tempdiff}"
  172. touch $tempdiff
  173. chmod 600 $tempdiff
  174. fi
  175. diff $logfile $oldlog | grep -v "^>" > $tempdiff
  176. # Count the number of matching log entries we have
  177. count=`grep -c "$query" $tempdiff`
  178. # Get the last matching entry in the diff file
  179. lastentry=`grep "$query" $tempdiff | tail -1`
  180. rm -f $tempdiff
  181. cat $logfile > $oldlog
  182. if [ "$count" = "0" ]; then # no matches, exit with no error
  183. echo "Log check ok - 0 pattern matches found"
  184. exitstatus=$STATE_OK
  185. else # Print total matche count and the last entry we found
  186. echo "($count) $lastentry"
  187. exitstatus=$STATE_CRITICAL
  188. fi
  189. exit $exitstatus