#!/usr/local/bin/perl
#
# mon monitor to watch for "old" files in one or more directories
# original use was to monitor DNS zone transfers,
#  but there is nothing DNS specific here except that the directory
#  is removed from the failure list leaving only the file (zone) name
#
# Jon Meek, originally 25-Dec-2000

$RCSid = q{$Id: dir_file_age.monitor,v 1.1 2000/12/25 18:30:09 meekj Exp $};

use Getopt::Long;
use File::Find;

GetOptions(
	   "d" => \$opt_d,    # Debug/test flag
	   "T=f" => \$MaxAge, # Maximum age in days, default is one day
);

@Failures = ();
@Dirs = @ARGV; # Directory names are left on the command line after Getopt

$MaxAge = 1 unless $MaxAge;

foreach $d (@Dirs) {
  $CurrentDir = $d;
  print "Directory: $d\n" if $opt_d;
  find(\&wanted, $d);

  foreach $f (sort {$Age{$b} <=> $Age{$a}} keys %Age) {
    print "dbg: $f  $Age{$f}\n" if $opt_d;
    if ($Age{$f} > $MaxAge) {
      push(@Failures, $f);
    }
  }

  undef %Size; # Initialize for next directory in list
}

if (@Failures == 0) { # No "old" files, all is OK
    exit 0;
}

print "@Failures\n";

foreach $f (@Failures) {
    printf "%s %0.1fd\n", $f, $Age{$f};
}
print "\n";

exit 1;

sub wanted {
    my ($rdfile);
    $rdfile = $File::Find::name;
    return if (-d $rdfile); # Skip directories
    return if (-l $rdfile); # Skip symbolic links, for now

    $age = -M $rdfile;
    $size = -s $rdfile;

    $rdfile =~ s/^$CurrentDir\///; # Remove base directory, may need to be optional

    $Age{$rdfile} = $age;
    $Size{$rdfile} = $size; # Used to track the files, will be undef'ed between dirs
#    $FileCount++;
  }