File: dir_file_age.monitor

package info (click to toggle)
mon-contrib 1.0%2Bdfsg-5
  • links: PTS, VCS
  • area: main
  • in suites: sid, trixie
  • size: 892 kB
  • sloc: perl: 5,510; sh: 383; python: 118; makefile: 72
file content (88 lines) | stat: -rwxr-xr-x 2,420 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
#!/usr/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
#

$RCSid = q{$Id: dir_file_age.monitor,v 1.1 2000/12/25 18:30:09 meekj Exp meekj $};
#
#    Author: Jon Meek, originally 25-Dec-2000
#    Copyright (C) 2011, Jon Meek (meekj at ieee.org)
#
#    This program is free software; you can redistribute it and/or modify
#    it under the terms of the GNU General Public License as published by
#    the Free Software Foundation; either version 2 of the License, or
#    (at your option) any later version.
#
#    This program is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU General Public License for more details.
#
#    You should have received a copy of the GNU General Public License
#    along with this program; if not, write to the Free Software
#    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
#



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++;
  }