check_file_age.pl 3.5 KB

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