4
0

check_file_age.pl 3.4 KB

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