check_log.sh 6.5 KB

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