check_file_age.pl 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. #!@PERL@ -w
  2. # check_file_age.pl Copyright (C) 2003 Steven Grimm <koreth-nagios@midwinter.com>
  3. #
  4. # Checks a file's size and modification time to make sure it's not empty
  5. # and that it's sufficiently recent.
  6. #
  7. #
  8. # This program is free software; you can redistribute it and/or
  9. # modify it under the terms of the GNU General Public License
  10. # as published by the Free Software Foundation; either version 2
  11. # of the License, or (at your option) any later version.
  12. #
  13. # This program is distributed in the hope that it will be useful,
  14. # but WITHOUT ANY WARRANTY; without even the implied warranty
  15. # of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. # GNU General Public License for more details.
  17. #
  18. # you should have received a copy of the GNU General Public License
  19. # along with this program (or with Nagios); if not, write to the
  20. # Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
  21. # Boston, MA 02110-1301, USA
  22. use strict;
  23. use English;
  24. use Getopt::Long;
  25. use File::stat;
  26. use vars qw($PROGNAME);
  27. use FindBin;
  28. use lib "$FindBin::Bin";
  29. use lib '@libexecdir@';
  30. use utils qw (%ERRORS &print_revision &support);
  31. sub print_help ();
  32. sub print_usage ();
  33. my ($opt_c, $opt_f, $opt_w, $opt_C, $opt_W, $opt_h, $opt_V, $opt_i);
  34. my ($result, $message, $age, $size, $st, $perfdata, $output, @filelist, $filename, $counter, $summary, $high_water_mark, $this_level, $this_result);
  35. $PROGNAME="check_file_age";
  36. $ENV{'PATH'}='@TRUSTED_PATH@';
  37. $ENV{'BASH_ENV'}='';
  38. $ENV{'ENV'}='';
  39. $opt_w = 240;
  40. $opt_c = 600;
  41. $opt_W = 0;
  42. $opt_C = 0;
  43. $opt_f = "";
  44. Getopt::Long::Configure('bundling');
  45. GetOptions(
  46. "V" => \$opt_V, "version" => \$opt_V,
  47. "h" => \$opt_h, "help" => \$opt_h,
  48. "i" => \$opt_i, "ignore-missing" => \$opt_i,
  49. "f=s" => \$opt_f, "file" => \$opt_f,
  50. "w=f" => \$opt_w, "warning-age=f" => \$opt_w,
  51. "W=f" => \$opt_W, "warning-size=f" => \$opt_W,
  52. "c=f" => \$opt_c, "critical-age=f" => \$opt_c,
  53. "C=f" => \$opt_C, "critical-size=f" => \$opt_C);
  54. if ($opt_V) {
  55. print_revision($PROGNAME, '@NP_VERSION@');
  56. exit $ERRORS{'OK'};
  57. }
  58. if ($opt_h) {
  59. print_help();
  60. exit $ERRORS{'OK'};
  61. }
  62. $opt_f = shift unless ($opt_f);
  63. if (! $opt_f) {
  64. print "FILE_AGE UNKNOWN: No file specified\n";
  65. exit $ERRORS{'UNKNOWN'};
  66. }
  67. $opt_f = '"' . $opt_f . '"' if $opt_f =~ / /;
  68. # Check that file(s) exists (can be directory or link)
  69. $perfdata = "";
  70. $output = "";
  71. @filelist = glob($opt_f);
  72. $counter = 0;
  73. $high_water_mark = 0;
  74. $result = "OK";
  75. foreach $filename (@filelist) {
  76. unless (-e $filename) {
  77. if ($opt_i) {
  78. if ($output) {
  79. $output = $output . "\n";
  80. }
  81. $output = $output . "FILE_AGE OK: $filename doesn't exist, but ignore-missing was set\n";
  82. $this_result = "OK";
  83. $this_level = 0;
  84. } else {
  85. if ($output) {
  86. $output = $output . "\n";
  87. }
  88. $output = $output . "FILE_AGE CRITICAL: File not found - $filename\n";
  89. $this_result = "CRITICAL";
  90. $this_level = 2;
  91. }
  92. if ($high_water_mark < $this_level) {
  93. $high_water_mark = $this_level;
  94. $counter = 1;
  95. $result = $this_result;
  96. }
  97. elsif($high_water_mark = $this_level) {
  98. $counter = $counter + 1;
  99. }
  100. next;
  101. }
  102. $st = File::stat::stat($filename);
  103. $age = time - $st->mtime;
  104. $size = $st->size;
  105. $perfdata = $perfdata . "age=${age}s;${opt_w};${opt_c} size=${size}B;${opt_W};${opt_C};0 ";
  106. $this_result = 'OK';
  107. $this_level = 0;
  108. if (($opt_c and $age > $opt_c) or ($opt_C and $size < $opt_C)) {
  109. $this_result = 'CRITICAL';
  110. $this_level = 2;
  111. }
  112. elsif (($opt_w and $age > $opt_w) or ($opt_W and $size < $opt_W)) {
  113. $this_result = 'WARNING';
  114. $this_level = 1;
  115. }
  116. if ($high_water_mark < $this_level) {
  117. $high_water_mark = $this_level;
  118. $counter = 1;
  119. $result = $this_result;
  120. }
  121. elsif ($high_water_mark == $this_level) {
  122. $counter = $counter + 1;
  123. }
  124. if ($output) {
  125. $output = $output . "\n";
  126. }
  127. $output = $output . "FILE_AGE $this_result: $filename is $age seconds old and $size bytes ";
  128. }
  129. $summary = "$result: $counter files are $result";
  130. if (scalar @filelist == 1) {
  131. print "$output | $perfdata \n";
  132. }
  133. else {
  134. print "$summary \n$output | $perfdata \n";
  135. }
  136. exit $ERRORS{$result};
  137. sub print_usage () {
  138. print "Usage:\n";
  139. print " $PROGNAME [-w <secs>] [-c <secs>] [-W <size>] [-C <size>] [-i] -f <file>\n";
  140. print " $PROGNAME [-h | --help]\n";
  141. print " $PROGNAME [-V | --version]\n";
  142. }
  143. sub print_help () {
  144. print_revision($PROGNAME, '@NP_VERSION@');
  145. print "Copyright (c) 2003 Steven Grimm\n\n";
  146. print_usage();
  147. print "\n";
  148. print " -i | --ignore-missing : return OK if the file does not exist\n";
  149. print " <secs> File must be no more than this many seconds old (default: warn 240 secs, crit 600)\n";
  150. print " <size> File must be at least this many bytes long (default: crit 0 bytes)\n";
  151. print "\n";
  152. support();
  153. }