check_file_age.pl 3.4 KB

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