5
# Squid-1.0 version by martin hamilton <m.t.hamilton@lut.ac.uk>
6
# Squid-1.1 version by Bertold Kolics <bertold@tohotom.vein.hu>
8
# Check the Squid-1.1.x cache directory for stale objects - i.e. those
9
# which exist on disk but aren't listed in cached's log file.
11
# $Id: check_cache.pl,v 1.5 2003/01/23 00:37:06 robertc Exp $
15
# -c : the full path to squid.conf
16
# -d : turn on debugging
17
# -r : actually remove stale files
18
# -t tmpdir : temporary directory
19
# -v : list stale files
23
print "Usage: check_cache.pl -drvh -c squid.conf\n";
24
print "\t-c the full path to squid.conf\n";
25
print "\t-d turn on debugging\n";
26
print "\t-r actually remove stale files\n";
27
print "\t-t temporary directory\n";
28
print "\t-v list stale files\n";
29
print "\t-h print the help\n";
33
$squidconf = $opt_c || "/usr/local/squid/etc/squid.conf";
34
open (squidconf) || die "$squidconf: $!\n";
41
if (/^cache_dir\s+(.*)/) {
43
} elsif (/cache_swap_log\s+(.*)/) {
45
} elsif (/swap_level1_dirs/) {
47
} elsif (/swap_level21_dirs/) {
52
push (@cachedir, '/usr/local/squid/cache') unless ($#cachedir > $[-1);
53
$swaplog = $cachedir[0] . '/log' unless ($swaplog);
54
$no_cachedir = $#cachedir + 1;
55
print "$no_cachedir CACHE DIRS: ", join(' ', @cachedir), "\n" if ($opt_d);
56
print "SWAP LOG: $swaplog\n" if ($opt_d);
58
$tmpdir = $opt_t || $ENV{TMPDIR} || "/var/tmp";
61
# snarf file numbers from Squid log & sort em
62
system("cut -f1 -d' ' $swaplog |tr [a-z] [A-Z] >pl$$");
63
system("sort -T $tmpdir pl$$ >spl$$; rm pl$$");
65
# get list of files in cache & sort em
66
for ($i = 0 ; $i < $no_cachedir; $i++) {
68
system("find ./ -print -type f > $tmpdir/fp$$");
70
# this cut prints only the lines with 4 fields so unnecessary lines
72
system("cut -d'/' -f4 -s fp$$ >> cd$$ ; rm fp$$")
74
system("sort -T $tmpdir cd$$ >scd$$; rm cd$$");
76
# get list of objects on disk (scd$$) but not in the log (spl$$)
77
system("comm -13 spl$$ scd$$ >comm$$; rm spl$$ scd$$");
81
open(IN, "comm$$") || die "Can't open temporary file $tmpdir/comm$$: $!";
87
# calculate the full path of the current filename
88
$fileno = hex($filename);
89
$dirno = $fileno % $no_cachedir;
90
$a = $fileno / $no_cachedir;
91
$level1 = sprintf("%02X", $a % $level1dirno);
92
$level2 = sprintf("%02X", $a / $level1dirno % $level2dirno);
93
$filename = "$cachedir[dirno]/$level1/$level2/$filename";
95
next if -d "$filename"; # don't want directories
97
print "$filename\n" if $opt_v; # print filename if asked
99
# skip if cached file appeared since script started running
100
if (-M $filename < 0) {
101
print STDERR "skipping $filename\n" if $opt_d;
104
print "Orphan: $filename\n";
105
unlink($filename) if $opt_r; # only remove if asked!