check_log2.pl 6.0 KB

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