check_file_age.pl 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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);
  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. foreach $filename (@filelist) {
  73. unless (-e $filename) {
  74. if ($opt_i) {
  75. $result = 'OK';
  76. print "FILE_AGE $result: $filename doesn't exist, but ignore-missing was set\n";
  77. exit $ERRORS{$result};
  78. } else {
  79. print "FILE_AGE CRITICAL: File not found - $filename\n";
  80. exit $ERRORS{'CRITICAL'};
  81. }
  82. }
  83. $st = File::stat::stat($filename);
  84. $age = time - $st->mtime;
  85. $size = $st->size;
  86. $perfdata = $perfdata . "age=${age}s;${opt_w};${opt_c} size=${size}B;${opt_W};${opt_C};0 ";
  87. $result = 'OK';
  88. if (($opt_c and $age > $opt_c) or ($opt_C and $size < $opt_C)) {
  89. $result = 'CRITICAL';
  90. }
  91. elsif (($opt_w and $age > $opt_w) or ($opt_W and $size < $opt_W)) {
  92. $result = 'WARNING';
  93. }
  94. $output = $output . "FILE_AGE $result: $filename is $age seconds old and $size bytes ";
  95. }
  96. print "$output | $perfdata\n";
  97. exit $ERRORS{$result};
  98. sub print_usage () {
  99. print "Usage:\n";
  100. print " $PROGNAME [-w <secs>] [-c <secs>] [-W <size>] [-C <size>] [-i] -f <file>\n";
  101. print " $PROGNAME [-h | --help]\n";
  102. print " $PROGNAME [-V | --version]\n";
  103. }
  104. sub print_help () {
  105. print_revision($PROGNAME, '@NP_VERSION@');
  106. print "Copyright (c) 2003 Steven Grimm\n\n";
  107. print_usage();
  108. print "\n";
  109. print " -i | --ignore-missing : return OK if the file does not exist\n";
  110. print " <secs> File must be no more than this many seconds old (default: warn 240 secs, crit 600)\n";
  111. print " <size> File must be at least this many bytes long (default: crit 0 bytes)\n";
  112. print "\n";
  113. support();
  114. }