check_file_age.pl 2.9 KB

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