#!/usr/bin/perl

use strict;

use Getopt::Long;
use constant CHECK_DIR   => "/exports/prodpdfserve/pdfserve";
use constant STALE_TIME  => 10; # Minutes

# Nagios stuff
use lib "/usr/lib64/nagios/plugins";
use utils qw (%ERRORS &print_revision &support);

my %stale_files = ();
my $total       = 0;

#
# Get any provided adjustments and set our thresholds
#
my ($opt_w, $opt_c);
GetOptions ( "w=i"    => \$opt_w, "warn"     => \$opt_w,
             "c=i"    => \$opt_c, "critical" => \$opt_c);
$opt_w = 30 unless defined ($opt_w);
$opt_c = 60 unless defined ($opt_c);

#
# Read the directory and see what we have.
#
opendir DH, CHECK_DIR;
while ( my $file = readdir(DH) )
{
  my $fpath = CHECK_DIR . "/$file";

  next unless ( -f $fpath );
  
  # atime = 8, mtime = 9, ctime = 10
  my $ftime = (stat($fpath))[9];

  my $time = time();

  if ( ($time - $ftime) > (STALE_TIME * 60) )
  {
    $stale_files{$file}->{'path'}  = $fpath;
    $stale_files{$file}->{'mtime'} = $ftime;
  }

  # Increment the counter of total files
  $total++;
}
closedir DH;

#
# Predefine our variables
#
my $result = "OK";
my $stale  = scalar(keys(%stale_files));
my $mesg   = "$stale stale, $total total files in ${\CHECK_DIR}";
#my $mesg   = (($total > $opt_c || $total > $opt_w) ? "$total total" : "$stale stale") . " files in ${\CHECK_DIR}";

if ( $stale > $opt_c || $total > $opt_c  )
{
  $result = "CRITICAL";
}
elsif ( $stale > $opt_w || $total > $opt_w )
{
  $result = "WARNING";
}

print "TOMCAT $result: $mesg\n";
exit $ERRORS{$result};


