]> git.ipfire.org Git - thirdparty/squid.git/blob - scripts/check_cache.pl
merge from trunk r12441
[thirdparty/squid.git] / scripts / check_cache.pl
1 #!/usr/local/bin/perl
2
3 # check_cache.pl
4 #
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>
7 #
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.
10
11 require "getopts.pl";
12 &Getopts("c:drt:vh");
13 # -c : the full path to squid.conf
14 # -d : turn on debugging
15 # -r : actually remove stale files
16 # -t tmpdir : temporary directory
17 # -v : list stale files
18 # -h : print the help
19
20 if ($opt_h) {
21 print "Usage: check_cache.pl -drvh -c squid.conf\n";
22 print "\t-c the full path to squid.conf\n";
23 print "\t-d turn on debugging\n";
24 print "\t-r actually remove stale files\n";
25 print "\t-t temporary directory\n";
26 print "\t-v list stale files\n";
27 print "\t-h print the help\n";
28 exit;
29 }
30
31 $squidconf = $opt_c || "/usr/local/squid/etc/squid.conf";
32 open (squidconf) || die "$squidconf: $!\n";
33 $no_cachedir = 0;
34 $swaplog = '';
35 $level1dirno = 16;
36 $level2dirno = 256;
37 while (<squidconf>) {
38 chop;
39 if (/^cache_dir\s+(.*)/) {
40 push (@cachedir, $1);
41 } elsif (/cache_swap_log\s+(.*)/) {
42 $swaplog = $1;
43 } elsif (/swap_level1_dirs/) {
44 $level1dirno = $1;
45 } elsif (/swap_level21_dirs/) {
46 $level2dirno = $1;
47 }
48 }
49 close (squidconf);
50 push (@cachedir, '/usr/local/squid/cache') unless ($#cachedir > $[-1);
51 $swaplog = $cachedir[0] . '/log' unless ($swaplog);
52 $no_cachedir = $#cachedir + 1;
53 print "$no_cachedir CACHE DIRS: ", join(' ', @cachedir), "\n" if ($opt_d);
54 print "SWAP LOG: $swaplog\n" if ($opt_d);
55
56 $tmpdir = $opt_t || $ENV{TMPDIR} || "/var/tmp";
57 chdir($tmpdir);
58
59 # snarf file numbers from Squid log & sort em
60 system("cut -f1 -d' ' $swaplog |tr [a-z] [A-Z] >pl$$");
61 system("sort -T $tmpdir pl$$ >spl$$; rm pl$$");
62
63 # get list of files in cache & sort em
64 for ($i = 0 ; $i < $no_cachedir; $i++) {
65 chdir($cachedir[i]);
66 system("find ./ -print -type f > $tmpdir/fp$$");
67 chdir($tmpdir);
68 # this cut prints only the lines with 4 fields so unnecessary lines
69 # are supressed
70 system("cut -d'/' -f4 -s fp$$ >> cd$$ ; rm fp$$")
71 }
72 system("sort -T $tmpdir cd$$ >scd$$; rm cd$$");
73
74 # get list of objects on disk (scd$$) but not in the log (spl$$)
75 system("comm -13 spl$$ scd$$ >comm$$; rm spl$$ scd$$");
76
77 chdir($tmpdir);
78 # iterate through it
79 open(IN, "comm$$") || die "Can't open temporary file $tmpdir/comm$$: $!";
80 unlink("comm$$");
81 while(<IN>) {
82 chop;
83 $filename = $_;
84
85 # calculate the full path of the current filename
86 $fileno = hex($filename);
87 $dirno = $fileno % $no_cachedir;
88 $a = $fileno / $no_cachedir;
89 $level1 = sprintf("%02X", $a % $level1dirno);
90 $level2 = sprintf("%02X", $a / $level1dirno % $level2dirno);
91 $filename = "$cachedir[dirno]/$level1/$level2/$filename";
92
93 next if -d "$filename"; # don't want directories
94
95 print "$filename\n" if $opt_v; # print filename if asked
96
97 # skip if cached file appeared since script started running
98 if (-M $filename < 0) {
99 print STDERR "skipping $filename\n" if $opt_d;
100 next;
101 }
102 print "Orphan: $filename\n";
103 unlink($filename) if $opt_r; # only remove if asked!
104 }
105 close(IN);