check_log.sh 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  1. #!/bin/sh
  2. #
  3. # Log file pattern detector plugin for Nagios
  4. # Written by Ethan Galstad (nagios@nagios.org)
  5. #
  6. # Usage: ./check_log <log_file> <old_log_file> <pattern>
  7. #
  8. # Description:
  9. #
  10. # This plugin will scan a log file (specified by the <log_file> option)
  11. # for a specific pattern (specified by the <pattern> option). Successive
  12. # calls to the plugin script will only report *new* pattern matches in the
  13. # log file, since an copy of the log file from the previous run is saved
  14. # to <old_log_file>.
  15. #
  16. # Output:
  17. #
  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
  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
  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
  24. # total number of pattern matches found in the file and "last_match" is the
  25. # last entry in the log file which matches the pattern.
  26. #
  27. # Notes:
  28. #
  29. # If you use this plugin make sure to keep the following in mind:
  30. #
  31. # 1. The "max_attempts" value for the service should be 1, as this
  32. # will prevent Nagios from retrying the service check (the
  33. # next time the check is run it will not produce the same results).
  34. #
  35. # 2. The "notify_recovery" value for the service should be 0, so that
  36. # Nagios does not notify you of "recoveries" for the check. Since
  37. # pattern matches in the log file will only be reported once and not
  38. # the next time, there will always be "recoveries" for the service, even
  39. # though recoveries really don't apply to this type of check.
  40. #
  41. # 3. You *must* supply a different <old_file_log> for each service that
  42. # you define to use this plugin script - even if the different services
  43. # check the same <log_file> for pattern matches. This is necessary
  44. # because of the way the script operates.
  45. #
  46. # Examples:
  47. #
  48. # Check for login failures in the syslog...
  49. #
  50. # check_log /var/log/messages ./check_log.badlogins.old "LOGIN FAILURE"
  51. #
  52. # Check for port scan alerts generated by Psionic's PortSentry software...
  53. #
  54. # check_log /var/log/message ./check_log.portscan.old "attackalert"
  55. #
  56. # Paths to commands used in this script. These
  57. # may have to be modified to match your system setup.
  58. PATH="@TRUSTED_PATH@"
  59. export PATH
  60. PROGNAME=$(basename "$0")
  61. PROGPATH=$(echo "$0" | sed -e 's,[\\/][^\\/][^\\/]*$,,')
  62. REVISION="@NP_VERSION@"
  63. PATH="@TRUSTED_PATH@"
  64. export PATH
  65. . "$PROGPATH"/utils.sh
  66. print_usage() {
  67. echo "Usage: $PROGNAME -F logfile -O oldlog -q query"
  68. echo "Usage: $PROGNAME --help"
  69. echo "Usage: $PROGNAME --version"
  70. echo " Additional parameter:"
  71. echo " -w (--max_warning) If used, determines the maximum matching value to return"
  72. echo " as warning, when finding more matching lines than this parameter will"
  73. echo " return as critical. If not used, will consider as default 0 (any matching"
  74. echo " will consider as critical)"
  75. echo "Usage: $PROGNAME -F logfile -O oldlog -q query -w <number>"
  76. }
  77. print_help() {
  78. print_revision "$PROGNAME" $REVISION
  79. echo ""
  80. print_usage
  81. echo ""
  82. echo "Log file pattern detector plugin for Nagios"
  83. echo ""
  84. support
  85. }
  86. # Make sure the correct number of command line
  87. # arguments have been supplied
  88. if [ $# -lt 1 ]; then
  89. print_usage
  90. exit "$STATE_UNKNOWN"
  91. fi
  92. # Grab the command line arguments
  93. #logfile=$1
  94. #oldlog=$2
  95. #query=$3
  96. exitstatus=$STATE_WARNING #default
  97. while test -n "$1"; do
  98. case "$1" in
  99. --help)
  100. print_help
  101. exit "$STATE_OK"
  102. ;;
  103. -h)
  104. print_help
  105. exit "$STATE_OK"
  106. ;;
  107. --version)
  108. print_revision "$PROGNAME" $REVISION
  109. exit "$STATE_OK"
  110. ;;
  111. -V)
  112. print_revision "$PROGNAME" $REVISION
  113. exit "$STATE_OK"
  114. ;;
  115. --filename)
  116. logfile=$2
  117. shift
  118. ;;
  119. -F)
  120. logfile=$2
  121. shift
  122. ;;
  123. --oldlog)
  124. oldlog=$2
  125. shift
  126. ;;
  127. -O)
  128. oldlog=$2
  129. shift
  130. ;;
  131. --max_warning)
  132. MAX_WARNING=$2
  133. shift
  134. ;;
  135. -w)
  136. MAX_WARNING=$2
  137. shift
  138. ;;
  139. --query)
  140. query=$2
  141. shift
  142. ;;
  143. -q)
  144. query=$2
  145. shift
  146. ;;
  147. -x)
  148. exitstatus=$2
  149. shift
  150. ;;
  151. --exitstatus)
  152. exitstatus=$2
  153. shift
  154. ;;
  155. -t)
  156. TMPDIR=$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 [ "$oldlog" = "" ]; then
  168. echo "Log check error: You must supply an Old Log File name using '-O'!"
  169. exit "$STATE_UNKNOWN"
  170. fi
  171. rc=`echo "$oldlog" | grep -q -- "^-"; echo $?`
  172. if [ $rc -eq 0 ]; then
  173. echo "Log check error: You must supply an Old Log File name using '-O'!"
  174. exit "$STATE_UNKNOWN"
  175. fi
  176. # If the source log file doesn't exist, exit
  177. if [ ! -e "$logfile" ]; then
  178. echo "Log check error: Log file $logfile does not exist!"
  179. exit "$STATE_UNKNOWN"
  180. elif [ ! -r "$logfile" ] ; then
  181. echo "Log check error: Log file $logfile is not readable!"
  182. exit "$STATE_UNKNOWN"
  183. fi
  184. # Only use /tmp as a fallback if $TMPDIR doesn't exist
  185. if [ ! -d "$TMPDIR" ];then
  186. TMPDIR="/tmp"
  187. fi
  188. echo "$TMPDIR"
  189. # Copy the logfile to a temporary file, to prevent diff from
  190. # never finishing when $logfile continues to be written to
  191. # during the diff
  192. templog="${TMPDIR}/temp_check_log.tmp"
  193. if [ -x /bin/mktemp ]; then
  194. templog=$(/bin/mktemp "${TMPDIR}/temp_check_log.XXXXXXXXXX")
  195. else
  196. templog=$(/bin/date '+%H%M%S')
  197. templog="${TMPDIR}/temp_check_log.${templog}"
  198. fi
  199. cp "$logfile" "$templog"
  200. logfile=$templog
  201. # If the old log file doesn't exist, this must be the first time
  202. # we're running this test, so copy the original log file over to
  203. # the old diff file and exit
  204. if [ ! -e "$oldlog" ]; then
  205. cat "$logfile" > "$oldlog"
  206. echo "Log check data initialized..."
  207. exit "$STATE_OK"
  208. fi
  209. # The old log file exists, so compare it to the original log now
  210. # The temporary file that the script should use while
  211. # processing the log file.
  212. if [ -x /bin/mktemp ]; then
  213. tempdiff=$(/bin/mktemp "${TMPDIR}/check_log.XXXXXXXXXX")
  214. else
  215. tempdiff=$(/bin/date '+%H%M%S')
  216. tempdiff="${TMPDIR}/check_log.${tempdiff}"
  217. touch "$tempdiff"
  218. chmod 600 "$tempdiff"
  219. fi
  220. diff "$logfile" "$oldlog" | grep -v "^>" > "$tempdiff"
  221. # Count the number of matching log entries we have and handle errors when grep fails
  222. count=$(grep -c "$query" "$tempdiff" 2>&1)
  223. if [ $? -gt 1 ];then
  224. echo "Log check error: $count"
  225. exit "$STATE_UNKNOWN"
  226. fi
  227. # Get the last matching entry in the diff file
  228. lastentry=$(egrep "$query" "$tempdiff" | tail -1)
  229. rm -f "$tempdiff"
  230. cat "$logfile" > "$oldlog"
  231. if [ "$count" = "0" ]; then # no matches, exit with no error
  232. echo "Log check ok - 0 pattern matches found|match=$count;;;0"
  233. exitstatus=$STATE_OK
  234. else # Print total matche count and the last entry we found
  235. echo "($count) $lastentry|match=$count;;;0"
  236. if [ "$MAX_WARNING" ] && [ "$count" -le "$MAX_WARNING" ] ; then
  237. exitstatus=$STATE_WARNING
  238. else
  239. exitstatus=$STATE_CRITICAL
  240. fi
  241. fi
  242. exit "$exitstatus"