check_file_age.pl 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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 lib ".";
  28. use utils qw (%ERRORS &print_revision &support);
  29. sub print_help ();
  30. sub print_usage ();
  31. my ($opt_c, $opt_f, $opt_w, $opt_C, $opt_W, $opt_h, $opt_V, $opt_i);
  32. my ($result, $message, $age, $size, $st);
  33. $PROGNAME="check_file_age";
  34. $ENV{'PATH'}='@TRUSTED_PATH@';
  35. $ENV{'BASH_ENV'}='';
  36. $ENV{'ENV'}='';
  37. $opt_w = 240;
  38. $opt_c = 600;
  39. $opt_W = 0;
  40. $opt_C = 0;
  41. $opt_f = "";
  42. Getopt::Long::Configure('bundling');
  43. GetOptions(
  44. "V" => \$opt_V, "version" => \$opt_V,
  45. "h" => \$opt_h, "help" => \$opt_h,
  46. "i" => \$opt_i, "ignore-missing" => \$opt_i,
  47. "f=s" => \$opt_f, "file" => \$opt_f,
  48. "w=f" => \$opt_w, "warning-age=f" => \$opt_w,
  49. "W=f" => \$opt_W, "warning-size=f" => \$opt_W,
  50. "c=f" => \$opt_c, "critical-age=f" => \$opt_c,
  51. "C=f" => \$opt_C, "critical-size=f" => \$opt_C);
  52. if ($opt_V) {
  53. print_revision($PROGNAME, '@NP_VERSION@');
  54. exit $ERRORS{'OK'};
  55. }
  56. if ($opt_h) {
  57. print_help();
  58. exit $ERRORS{'OK'};
  59. }
  60. $opt_f = shift unless ($opt_f);
  61. if (! $opt_f) {
  62. print "FILE_AGE UNKNOWN: No file specified\n";
  63. exit $ERRORS{'UNKNOWN'};
  64. }
  65. # Check that file exists (can be directory or link)
  66. unless (-e $opt_f) {
  67. if ($opt_i) {
  68. $result = 'OK';
  69. print "FILE_AGE $result: $opt_f doesn't exist, but ignore-missing was set\n";
  70. exit $ERRORS{$result};
  71. } else {
  72. print "FILE_AGE CRITICAL: File not found - $opt_f\n";
  73. exit $ERRORS{'CRITICAL'};
  74. }
  75. }
  76. $st = File::stat::stat($opt_f);
  77. $age = time - $st->mtime;
  78. $size = $st->size;
  79. $result = 'OK';
  80. if (($opt_c and $age > $opt_c) or ($opt_C and $size < $opt_C)) {
  81. $result = 'CRITICAL';
  82. }
  83. elsif (($opt_w and $age > $opt_w) or ($opt_W and $size < $opt_W)) {
  84. $result = 'WARNING';
  85. }
  86. print "FILE_AGE $result: $opt_f is $age seconds old and $size bytes\n";
  87. exit $ERRORS{$result};
  88. sub print_usage () {
  89. print "Usage:\n";
  90. print " $PROGNAME [-w <secs>] [-c <secs>] [-W <size>] [-C <size>] [-i] -f <file>\n";
  91. print " $PROGNAME [-h | --help]\n";
  92. print " $PROGNAME [-V | --version]\n";
  93. }
  94. sub print_help () {
  95. print_revision($PROGNAME, '@NP_VERSION@');
  96. print "Copyright (c) 2003 Steven Grimm\n\n";
  97. print_usage();
  98. print "\n";
  99. print " -i | --ignore-missing : return OK if the file does not exist\n";
  100. print " <secs> File must be no more than this many seconds old (default: warn 240 secs, crit 600)\n";
  101. print " <size> File must be at least this many bytes long (default: crit 0 bytes)\n";
  102. print "\n";
  103. support();
  104. }