4
0

check_log2.pl 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. #!/usr/bin/perl -w
  2. #
  3. # $Id: check_log2.pl 1300 2005-12-16 18:41:45Z harpermann $
  4. #
  5. # Log file regular expression detector for Nagios.
  6. # Written by Aaron Bostick (abostick@mydoconline.com)
  7. # Last modified: 05-02-2002
  8. #
  9. # Thanks and acknowledgements to Ethan Galstad for Nagios and the check_log
  10. # plugin this is modeled after.
  11. #
  12. # Usage: check_log2 -l <log_file> -s <seek_file> -p <pattern> [-n <negpattern>]
  13. #
  14. # Description:
  15. #
  16. # This plugin will scan arbitrary text files looking for regular expression
  17. # matches. The text file to scan is specified with <log_file>.
  18. # <log_seek_file> is a temporary file used to store the seek byte position
  19. # of the last scan. This file will be created automatically on the first
  20. # scan. <pattern> can be any RE pattern that perl's s/// syntax accepte. Be
  21. # forewarned that a bad pattern will send this script into never never land!
  22. #
  23. # Output:
  24. #
  25. # This plugin returns OK when a file is successfully scanned and no pattern
  26. # matches are found. WARNING is returned when 1 or more patterns are found
  27. # along with the pattern count and the line of the last pattern matched.
  28. # CRITICAL is returned when an error occurs, such as file not found, etc.
  29. #
  30. # Notes (paraphrased from check_log's notes):
  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 <log_Seek_file> 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 error notices in messages
  50. # check_log2 -l /var/log/messages -s ./check_log2.messages.seek -p 'err'
  51. #
  52. BEGIN {
  53. if ($0 =~ s/^(.*?)[\/\\]([^\/\\]+)$//) {
  54. $prog_dir = $1;
  55. $prog_name = $2;
  56. }
  57. }
  58. require 5.004;
  59. use lib $main::prog_dir;
  60. use utils qw($TIMEOUT %ERRORS &print_revision &support &usage);
  61. use Getopt::Long;
  62. my ($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size,$atime,$mtime,$ctime,$blksize,$blocks);
  63. sub print_usage ();
  64. sub print_version ();
  65. sub print_help ();
  66. # Initialize strings
  67. $log_file = '';
  68. $seek_file = '';
  69. $critical = '';
  70. $re_pattern = '';
  71. $neg_re_pattern = '';
  72. $pattern_count = 0;
  73. $pattern_line = '';
  74. $plugin_revision = '$Revision: 1300 $ ';
  75. # Grab options from command line
  76. GetOptions
  77. ("l|logfile=s" => \$log_file,
  78. "s|seekfile=s" => \$seek_file,
  79. "c|critical" => \$critical,
  80. "p|pattern=s" => \$re_pattern,
  81. "n|negpattern:s" => \$neg_re_pattern,
  82. "v|version" => \$version,
  83. "h|help" => \$help);
  84. !($version) || print_version ();
  85. !($help) || print_help ();
  86. # Make sure log file is specified
  87. ($log_file) || usage("Log file not specified.\n");
  88. # Make sure seek file is specified
  89. ($seek_file) || usage("Seek file not specified.\n");
  90. # Make sure re pattern is specified
  91. ($re_pattern) || usage("Regular expression not specified.\n");
  92. # Open log file
  93. open (LOG_FILE, $log_file) || die "Unable to open log file $log_file: $!";
  94. # Try to open log seek file. If open fails, we seek from beginning of
  95. # file by default.
  96. if (open(SEEK_FILE, $seek_file)) {
  97. chomp(@seek_pos = <SEEK_FILE>);
  98. close(SEEK_FILE);
  99. # If file is empty, no need to seek...
  100. if ($seek_pos[0] != 0) {
  101. # Compare seek position to actual file size.
  102. # If file size is smaller
  103. # then we just start from beginning i.e. file was rotated, etc.
  104. ($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size,$atime,$mtime,$ctime,$blksize,$blocks) = stat(LOG_FILE);
  105. if ($seek_pos[0] <= $size) {
  106. seek(LOG_FILE, $seek_pos[0], 0);
  107. }
  108. }
  109. }
  110. # Loop through every line of log file and check for pattern matches.
  111. # Count the number of pattern matches and remember the full line of
  112. # the most recent match.
  113. while (<LOG_FILE>) {
  114. if ($neg_re_pattern) {
  115. if ((/$re_pattern/) && !(/$neg_re_pattern/)) {
  116. $pattern_count += 1;
  117. $pattern_line = $_;
  118. }
  119. } elsif (/$re_pattern/) {
  120. $pattern_count += 1;
  121. $pattern_line = $_;
  122. }
  123. }
  124. # Overwrite log seek file and print the byte position we have seeked to.
  125. open(SEEK_FILE, "> $seek_file") || die "Unable to open seek count file $seek_file: $!";
  126. print SEEK_FILE tell(LOG_FILE);
  127. # Close seek file.
  128. close(SEEK_FILE);
  129. # Close the log file.
  130. close(LOG_FILE);
  131. # Print result and return exit code.
  132. if ($pattern_count) {
  133. if ($critical) {
  134. print "CRITICAL: ";
  135. } else {
  136. print "WARNING: ";
  137. }
  138. print "($pattern_count): $pattern_line";
  139. if ($critical) {
  140. exit $ERRORS{'CRITICAL'};
  141. } else {
  142. exit $ERRORS{'WARNING'};
  143. }
  144. } else {
  145. print "OK - No matches found.\n";
  146. exit $ERRORS{'OK'};
  147. }
  148. #
  149. # Subroutines
  150. #
  151. sub print_usage () {
  152. print "Usage: $prog_name -l <log_file> -s <log_seek_file> -p <pattern> [-n <negpattern>] -c | --critical\n";
  153. print "Usage: $prog_name [ -v | --version ]\n";
  154. print "Usage: $prog_name [ -h | --help ]\n";
  155. }
  156. sub print_version () {
  157. print_revision($prog_name, $plugin_revision);
  158. exit $ERRORS{'OK'};
  159. }
  160. sub print_help () {
  161. print_revision($prog_name, $plugin_revision);
  162. print "\n";
  163. print "Scan arbitrary log files for regular expression matches.\n";
  164. print "\n";
  165. print_usage();
  166. print "\n";
  167. print "-l, --logfile=<logfile>\n";
  168. print " The log file to be scanned\n";
  169. print "-s, --seekfile=<seekfile>\n";
  170. print " The temporary file to store the seek position of the last scan\n";
  171. print "-p, --pattern=<pattern>\n";
  172. print " The regular expression to scan for in the log file\n";
  173. print "-n, --negpattern=<negpattern>\n";
  174. print " The regular expression to skip in the log file\n";
  175. print "-c, --critical\n";
  176. print " Return critical instead of warning on error\n";
  177. print "\n";
  178. support();
  179. exit $ERRORS{'OK'};
  180. }