/
expire
executable file
·62 lines (52 loc) · 1.56 KB
/
expire
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
#!/usr/bin/perl -w
use strict;
use POSIX qw(strftime);
sub dirname($)
{
my $filename = shift @_;
$filename =~ m{(.*)/([^/]+)} && return $1;
return ".";
}
my $homedir = dirname($0);
chdir $homedir
or die("Can't find home directory '$homedir'\n");
chdir 'data'
or die("No data/ subdir!");
# delete really old files
foreach my $file (glob("*/*")) {
if (-M $file > 14) {
unlink($file);
}
}
# create a warning if the newest file in a dir is too old
foreach my $dir (glob("*")) {
my $newest = 1000;
my $newest_stamp = 1000;
my @files = glob("$dir/*");
# the .stamp file notes that the dir has been updated, even if there are
# new new interesting entries
if (-e "$dir/.stamp") {
$newest_stamp = -M "$dir/.stamp";
} elsif (scalar(@files) == 0) {
# maybe a brand new dir
system("touch", "$dir/.stamp");
system("touch", "$dir/00-Initial-Entry.ok");
$newest_stamp = $newest = 0;
}
foreach my $file (glob("$dir/*")) {
my $n = -M $file;
$newest = $n if ($newest > $n);
$newest_stamp = $n if ($newest_stamp > $n);
}
my $date = strftime("%Y%m%d-%H%M%S", localtime());
my $efile = "$dir/$date." . int(rand(100000));
if ($newest_stamp > 0.3 || $newest > 1.1) {
open my $fh, ">$efile" or die("$efile: $!\n");
my $host = `hostname`;
$host =~ s/\s+//g;
print $fh "WARNING (host=$host): No recent entries in this directory!\n";
print $fh "\n";
printf $fh "Latest stamp is %.2f days old.\n", $newest_stamp;
printf $fh "Latest file is %.2f days old.\n", $newest;
}
}