check_file_age.pl 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. #!/bin/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., 59 Temple Place - Suite 330,
  21. # Boston, MA 02111-1307, 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);
  32. my ($result, $message, $age, $size, $st);
  33. $PROGNAME="check_file_age";
  34. $opt_w = 240;
  35. $opt_c = 600;
  36. $opt_W = 0;
  37. $opt_C = 0;
  38. $opt_f = "";
  39. Getopt::Long::Configure('bundling');
  40. GetOptions(
  41. "V" => \$opt_V, "version" => \$opt_V,
  42. "h" => \$opt_h, "help" => \$opt_h,
  43. "f=s" => \$opt_f, "file" => \$opt_f,
  44. "w=f" => \$opt_w, "warning-age=f" => \$opt_w,
  45. "W=f" => \$opt_W, "warning-size=f" => \$opt_W,
  46. "c=f" => \$opt_c, "critical-age=f" => \$opt_c,
  47. "C=f" => \$opt_C, "critical-size=f" => \$opt_C);
  48. if ($opt_V) {
  49. print_revision($PROGNAME, '@NP_VERSION@');
  50. exit $ERRORS{'OK'};
  51. }
  52. if ($opt_h) {
  53. print_help();
  54. exit $ERRORS{'OK'};
  55. }
  56. $opt_f = shift unless ($opt_f);
  57. if (! $opt_f) {
  58. print "FILE_AGE UNKNOWN: No file specified\n";
  59. exit $ERRORS{'UNKNOWN'};
  60. }
  61. # Check that file exists (can be directory or link)
  62. unless (-e $opt_f) {
  63. print "FILE_AGE CRITICAL: File not found - $opt_f\n";
  64. exit $ERRORS{'CRITICAL'};
  65. }
  66. $st = File::stat::stat($opt_f);
  67. $age = time - $st->mtime;
  68. $size = $st->size;
  69. $result = 'OK';
  70. if (($opt_c and $age > $opt_c) or ($opt_C and $size < $opt_C)) {
  71. $result = 'CRITICAL';
  72. }
  73. elsif (($opt_w and $age > $opt_w) or ($opt_W and $size < $opt_W)) {
  74. $result = 'WARNING';
  75. }
  76. print "FILE_AGE $result: $opt_f is $age seconds old and $size bytes\n";
  77. exit $ERRORS{$result};
  78. sub print_usage () {
  79. print "Usage:\n";
  80. print " $PROGNAME [-w <secs>] [-c <secs>] [-W <size>] [-C <size>] -f <file>\n";
  81. print " $PROGNAME [-h | --help]\n";
  82. print " $PROGNAME [-V | --version]\n";
  83. }
  84. sub print_help () {
  85. print_revision($PROGNAME, '@NP_VERSION@');
  86. print "Copyright (c) 2003 Steven Grimm\n\n";
  87. print_usage();
  88. print "\n";
  89. print " <secs> File must be no more than this many seconds old (default: warn 240 secs, crit 600)\n";
  90. print " <size> File must be at least this many bytes long (default: crit 0 bytes)\n";
  91. print "\n";
  92. support();
  93. }