source: squid-ssl/trunk/fuentes/scripts/check_cache.pl @ 5495

Last change on this file since 5495 was 5495, checked in by Juanma, 2 years ago

Initial release

  • Property svn:executable set to *
File size: 3.3 KB
Line 
1#!/usr/local/bin/perl
2#
3## Copyright (C) 1996-2015 The Squid Software Foundation and contributors
4##
5## Squid software is distributed under GPLv2+ license and includes
6## contributions from numerous individuals and organizations.
7## Please see the COPYING and CONTRIBUTORS files for details.
8##
9
10# check_cache.pl
11#
12# Squid-1.0 version by martin hamilton <m.t.hamilton@lut.ac.uk>
13# Squid-1.1 version by Bertold Kolics <bertold@tohotom.vein.hu>
14#
15# Check the Squid-1.1.x cache directory for stale objects - i.e. those
16# which exist on disk but aren't listed in cached's log file.
17
18require "getopts.pl";
19&Getopts("c:drt:vh");
20# -c            : the full path to squid.conf
21# -d            : turn on debugging
22# -r            : actually remove stale files
23# -t tmpdir     : temporary directory
24# -v            : list stale files
25# -h            : print the help
26
27if ($opt_h) {
28        print "Usage: check_cache.pl -drvh -c squid.conf\n";
29        print "\t-c the full path to squid.conf\n";
30        print "\t-d turn on debugging\n";
31        print "\t-r actually remove stale files\n";
32        print "\t-t temporary directory\n";
33        print "\t-v list stale files\n";
34        print "\t-h print the help\n";
35        exit;
36}
37
38$squidconf = $opt_c || "/usr/local/squid/etc/squid.conf";
39open (squidconf) || die "$squidconf: $!\n";
40$no_cachedir = 0;
41$swaplog = '';
42$level1dirno = 16;
43$level2dirno = 256;
44while (<squidconf>) {
45        chop;
46        if (/^cache_dir\s+(.*)/) {
47                push (@cachedir, $1);
48        } elsif (/cache_swap_log\s+(.*)/) {
49                $swaplog = $1;
50        } elsif (/swap_level1_dirs/) {
51                $level1dirno = $1;
52        } elsif (/swap_level21_dirs/) {
53                $level2dirno = $1;
54        }
55}
56close (squidconf);
57push (@cachedir, '/usr/local/squid/cache') unless ($#cachedir > $[-1);
58$swaplog = $cachedir[0] . '/log' unless ($swaplog);
59$no_cachedir = $#cachedir + 1;
60print "$no_cachedir CACHE DIRS: ", join(' ', @cachedir), "\n" if ($opt_d);
61print "SWAP LOG: $swaplog\n" if ($opt_d);
62
63$tmpdir = $opt_t || $ENV{TMPDIR} || "/var/tmp";
64chdir($tmpdir);
65
66# snarf file numbers from Squid log & sort em
67system("cut -f1 -d' ' $swaplog |tr [a-z] [A-Z] >pl$$");
68system("sort -T $tmpdir pl$$ >spl$$; rm pl$$");
69
70# get list of files in cache & sort em
71for ($i = 0 ; $i < $no_cachedir; $i++) {
72        chdir($cachedir[i]);
73        system("find ./ -print -type f > $tmpdir/fp$$");
74        chdir($tmpdir);
75# this cut prints only the lines with 4 fields so unnecessary lines
76# are supressed
77        system("cut -d'/' -f4 -s fp$$ >> cd$$ ; rm fp$$")
78}
79system("sort -T $tmpdir cd$$ >scd$$; rm cd$$");
80
81# get list of objects on disk (scd$$) but not in the log (spl$$)
82system("comm -13 spl$$ scd$$ >comm$$; rm spl$$ scd$$");
83
84chdir($tmpdir);
85# iterate through it
86open(IN, "comm$$") || die "Can't open temporary file $tmpdir/comm$$: $!";
87unlink("comm$$");
88while(<IN>) {
89        chop;
90        $filename = $_;
91
92# calculate the full path of the current filename
93        $fileno = hex($filename);
94        $dirno = $fileno % $no_cachedir;
95        $a = $fileno / $no_cachedir;
96        $level1 = sprintf("%02X", $a % $level1dirno);
97        $level2 = sprintf("%02X", $a / $level1dirno % $level2dirno);
98        $filename = "$cachedir[dirno]/$level1/$level2/$filename";
99
100        next if -d "$filename"; # don't want directories
101
102        print "$filename\n" if $opt_v; # print filename if asked
103
104        # skip if cached file appeared since script started running
105        if (-M $filename < 0) {
106                print STDERR "skipping $filename\n" if $opt_d;
107                next;
108        }
109        print "Orphan: $filename\n";
110        unlink($filename) if $opt_r; # only remove if asked!
111}
112close(IN);
Note: See TracBrowser for help on using the repository browser.