4
0

check_file_age.pl 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  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 File::Basename;
  27. use vars qw($PROGNAME);
  28. use FindBin;
  29. use lib "$FindBin::Bin";
  30. use lib '@libexecdir@';
  31. use utils qw (%ERRORS &print_revision &support);
  32. sub print_help ();
  33. sub print_usage ();
  34. my ($opt_c, $opt_f, $opt_w, $opt_C, $opt_W, $opt_h, $opt_V, $opt_i);
  35. my ($result, $message, $age, $size, $st, $perfdata, $output, @filelist, $filename, $safe_filename, $counter, $summary, $high_water_mark, $this_level, $this_result);
  36. $PROGNAME="check_file_age";
  37. $ENV{'PATH'}='@TRUSTED_PATH@';
  38. $ENV{'BASH_ENV'}='';
  39. $ENV{'ENV'}='';
  40. $opt_w = 240;
  41. $opt_c = 600;
  42. $opt_W = 0;
  43. $opt_C = 0;
  44. $opt_f = "";
  45. Getopt::Long::Configure('bundling');
  46. GetOptions(
  47. "V" => \$opt_V, "version" => \$opt_V,
  48. "h" => \$opt_h, "help" => \$opt_h,
  49. "i" => \$opt_i, "ignore-missing" => \$opt_i,
  50. "f=s" => \$opt_f, "file" => \$opt_f,
  51. "w=f" => \$opt_w, "warning-age=f" => \$opt_w,
  52. "W=f" => \$opt_W, "warning-size=f" => \$opt_W,
  53. "c=f" => \$opt_c, "critical-age=f" => \$opt_c,
  54. "C=f" => \$opt_C, "critical-size=f" => \$opt_C);
  55. if ($opt_V) {
  56. print_revision($PROGNAME, '@NP_VERSION@');
  57. exit $ERRORS{'OK'};
  58. }
  59. if ($opt_h) {
  60. print_help();
  61. exit $ERRORS{'OK'};
  62. }
  63. $opt_f = shift unless ($opt_f);
  64. if (! $opt_f) {
  65. print "FILE_AGE UNKNOWN: No file specified\n";
  66. exit $ERRORS{'UNKNOWN'};
  67. }
  68. $opt_f = '"' . $opt_f . '"' if $opt_f =~ / /;
  69. # Check that file(s) exists (can be directory or link)
  70. $perfdata = "";
  71. $output = "";
  72. @filelist = glob($opt_f);
  73. $counter = 0;
  74. $high_water_mark = 0;
  75. $result = "OK";
  76. foreach $filename (@filelist) {
  77. unless (-e $filename) {
  78. if ($opt_i) {
  79. if ($output) {
  80. $output = $output . "\n";
  81. }
  82. $output = $output . "FILE_AGE OK: $filename doesn't exist, but ignore-missing was set\n";
  83. $this_result = "OK";
  84. $this_level = 0;
  85. } else {
  86. if ($output) {
  87. $output = $output . "\n";
  88. }
  89. $output = $output . "FILE_AGE CRITICAL: File not found - $filename\n";
  90. $this_result = "CRITICAL";
  91. $this_level = 2;
  92. }
  93. if ($high_water_mark < $this_level) {
  94. $high_water_mark = $this_level;
  95. $counter = 1;
  96. $result = $this_result;
  97. }
  98. elsif($high_water_mark = $this_level) {
  99. $counter = $counter + 1;
  100. }
  101. next;
  102. }
  103. $st = File::stat::stat($filename);
  104. $age = time - $st->mtime;
  105. $size = $st->size;
  106. if (scalar @filelist == 1) {
  107. $perfdata = $perfdata . "age=${age}s;${opt_w};${opt_c} size=${size}B;${opt_W};${opt_C};0 ";
  108. }
  109. else {
  110. $safe_filename = basename($filename);
  111. $safe_filename =~ s/[='"]/_/g;
  112. $perfdata = $perfdata . "${safe_filename}_age=${age}s;${opt_w};${opt_c} ${safe_filename}_size=${size}B;${opt_W};${opt_C};0 ";
  113. }
  114. $this_result = 'OK';
  115. $this_level = 0;
  116. if (($opt_c and $age > $opt_c) or ($opt_C and $size < $opt_C)) {
  117. $this_result = 'CRITICAL';
  118. $this_level = 2;
  119. }
  120. elsif (($opt_w and $age > $opt_w) or ($opt_W and $size < $opt_W)) {
  121. $this_result = 'WARNING';
  122. $this_level = 1;
  123. }
  124. if ($high_water_mark < $this_level) {
  125. $high_water_mark = $this_level;
  126. $counter = 1;
  127. $result = $this_result;
  128. }
  129. elsif ($high_water_mark == $this_level) {
  130. $counter = $counter + 1;
  131. }
  132. if ($output) {
  133. $output = $output . "\n";
  134. }
  135. $output = $output . "FILE_AGE $this_result: $filename is $age seconds old and $size bytes ";
  136. }
  137. $summary = "$result: $counter files are $result";
  138. if (scalar @filelist == 1) {
  139. print "$output | $perfdata \n";
  140. }
  141. else {
  142. print "$summary \n$output | $perfdata \n";
  143. }
  144. exit $ERRORS{$result};
  145. sub print_usage () {
  146. print "Usage:\n";
  147. print " $PROGNAME [-w <secs>] [-c <secs>] [-W <size>] [-C <size>] [-i] -f <file>\n";
  148. print " $PROGNAME [-h | --help]\n";
  149. print " $PROGNAME [-V | --version]\n";
  150. }
  151. sub print_help () {
  152. print_revision($PROGNAME, '@NP_VERSION@');
  153. print "Copyright (c) 2003 Steven Grimm\n\n";
  154. print_usage();
  155. print "\n";
  156. print " -i | --ignore-missing : return OK if the file does not exist\n";
  157. print " <secs> File must be no more than this many seconds old (default: warn 240 secs, crit 600)\n";
  158. print " <size> File must be at least this many bytes long (default: crit 0 bytes)\n";
  159. print "\n";
  160. support();
  161. }