Started core37.
[people/pmueller/ipfire-2.x.git] / config / updxlrator / lscache
CommitLineData
0a638396
CS
1#!/usr/bin/perl
2#
3# This code is distributed under the terms of the GPL
4#
5# (c) 2006-2008 marco.s - http://update-accelerator.advproxy.net
6#
7# $Id: lscache,v 1.0 2007/09/11 00:00:00 marco.s Exp $
8#
9
10use strict;
11use Getopt::Std;
12
13my $swroot='/var/ipfire';
14my $apphome="$swroot/updatexlrator";
15my $repository='/var/updatecache';
16my $updatefile='';
17my $sourceurl='';
18my $filesize=0;
19my $totalfilesize=0;
20my $cachedtraffic=0;
21my @updatelist=();
22my @sources=();
23my @requests=();
24my @filelist=();
25my $objectdir='';
26my @tmp;
27my $counts=0;
28my $filedate;
29my $lastaccess;
30my $vendorid;
31my @vendors=();
32my $numfiles=0;
33my $cachehits=0;
34my $efficiency='0.0';
35my %vendorstats=();
36my $maxlength_filesize=0;
37my $maxlength_request=0;
38my $maxlength_vendorid=0;
39
40
41getopts('adfs');
42
43foreach (<$repository/*>)
44{
45 if (-d $_)
46 {
47 unless ((/^$repository\/download$/) || (/^$repository\/lost\+found$/)) { push(@sources,$_); }
48 }
49}
50
51foreach (@sources)
52{
53 $vendorid=substr($_,rindex($_,'/')+1,length($_));
54 push(@vendors,$vendorid);
55 @updatelist=<$_/*>;
56 foreach $objectdir (@updatelist)
57 {
58 if (-e "$objectdir/source.url")
59 {
60 open (FILE,"$objectdir/source.url");
61 $sourceurl=<FILE>;
62 close FILE;
63 chomp($sourceurl);
64 $updatefile = substr($sourceurl,rindex($sourceurl,'/')+1,length($sourceurl));
65 #
66 # Get filesize and calculate max length for output
67 #
68 $filesize = (-s "$objectdir/$updatefile");
69 if (length($filesize) > $maxlength_filesize) { $maxlength_filesize = length($filesize); }
70 #
71 # Total file size
72 #
73 $totalfilesize += $filesize;
74 #
75 # File size for this source
76 #
77 $vendorstats{$vendorid . "_filesize"} += $filesize;
78 #
79 # Number of requests from cache for this source
80 #
81 open (FILE,"$objectdir/access.log");
82 @requests=<FILE>;
83 close FILE;
84 chomp(@requests);
85 $counts = @requests;
86 $counts--;
87 $vendorstats{$vendorid . "_requests"} += $counts;
88 $cachehits += $counts;
89 #
90 # Calculate cache hits max length for output
91 #
92 if (length($cachehits) > $maxlength_request) { $maxlength_request = length($cachehits); }
93 #
94 # Get last cache access date
95 #
96 my ($SECdt,$MINdt,$HOURdt,$DAYdt,$MONTHdt,$YEARdt) = localtime($requests[-1]);
97 $DAYdt = sprintf ("%.02d",$DAYdt);
98 $MONTHdt = sprintf ("%.02d",$MONTHdt+1);
99 $YEARdt = sprintf ("%.04d",$YEARdt+1900);
100 if (($counts) && ($requests[-1] =~ /^\d+/) && ($requests[-1] >= 1))
101 {
102 $lastaccess = $YEARdt."-".$MONTHdt."-".$DAYdt;
103 } else {
104 $lastaccess = ' ';
105 }
106 #
107 # Get file modification time
108 #
109 ($SECdt,$MINdt,$HOURdt,$DAYdt,$MONTHdt,$YEARdt) = localtime(&getmtime("$objectdir/$updatefile"));
110 $DAYdt = sprintf ("%.02d",$DAYdt);
111 $MONTHdt = sprintf ("%.02d",$MONTHdt+1);
112 $YEARdt = sprintf ("%.04d",$YEARdt+1900);
113 $filedate = $YEARdt."-".$MONTHdt."-".$DAYdt;
114 #
115 # Total number of files in cache
116 #
117 $numfiles++;
118 #
119 # Number of files for this source
120 #
121 $vendorstats{$vendorid . "_files"}++;
122 #
123 # Count cache status occurences
124 #
125 open (FILE,"$objectdir/status");
126 $_=<FILE>;
127 close FILE;
128 chomp;
129 $vendorstats{$vendorid . "_" . $_}++;
130 #
131 # Calculate cached traffic for this source
132 #
133 $vendorstats{$vendorid . "_cachehits"} += $counts * $filesize;
134 #
135 # Calculate total cached traffic
136 #
137 $cachedtraffic += $counts * $filesize;
138 #
139 # Calculate vendor ID max length for output
140 #
141 if (length($vendorid) > $maxlength_vendorid) { $maxlength_vendorid = length($vendorid); }
142 #
143 # Add record to filelist
144 #
145 push (@filelist,"$filesize;$filedate;$counts;$lastaccess;$vendorid;$objectdir;$updatefile");
146 }
147 }
148}
149
150@filelist || die "No matching files found in cache\n";
151
152#
153# Process statistics for output
154#
155
156if ($Getopt::Std::opt_s)
157{
158 foreach (@vendors)
159 {
160 print "$_\n";
161 printf "%5d %s", $vendorstats{$_ . "_files"}, "files in cache\n";
162 printf "%5d %s", $vendorstats{$_ . "_requests"}, "files from cache\n";
163 printf "%5d %s", $vendorstats{$_ . "_1"}, "files 'Ok'\n";
164 printf "%5d %s", $vendorstats{$_ . "_3"}, "files 'No source'\n";
165 printf "%5d %s", $vendorstats{$_ . "_2"}, "files 'Outdated'\n";
166 printf "%5d %s", $vendorstats{$_ . "_0"}, "files 'Unknown'\n";
167 unless ($vendorstats{$_ . "_filesize"}) { $vendorstats{$_ . "_filesize"} = '0'; }
168 1 while $vendorstats{$_ . "_filesize"} =~ s/^(-?\d+)(\d{3})/$1.$2/;
169 printf "%15s %s", $vendorstats{$_ . "_filesize"}, "bytes in cache\n";
170 unless ($vendorstats{$_ . "_cachehits"}) { $vendorstats{$_ . "_cachehits"} = '0'; }
171 1 while $vendorstats{$_ . "_cachehits"} =~ s/^(-?\d+)(\d{3})/$1.$2/;
172 printf "%15s %s", $vendorstats{$_ . "_cachehits"}, "bytes from cache\n\n";
173 }
174
175 if ($numfiles) { $efficiency = sprintf("%.1f", $cachehits / $numfiles); }
176 1 while $totalfilesize =~ s/^(-?\d+)(\d{3})/$1.$2/;
177 1 while $cachedtraffic =~ s/^(-?\d+)(\d{3})/$1.$2/;
178 print "\nTotal files in cache: $numfiles\n";
179 print "Total cache size: $totalfilesize bytes\n";
180 print "Delivered from cache: $cachedtraffic bytes\n";
181 print "Cache efficiency index: $efficiency\n";
182
183 exit;
184}
185
186#
187# Process filelist for output
188#
189
190foreach (@filelist)
191{
192 @tmp = split(';');
193 printf "%$maxlength_filesize\d %s ",$tmp[0],$tmp[1];
194 if ($Getopt::Std::opt_a) { printf "%$maxlength_request\d %s ",$tmp[2],$tmp[3]; }
195 if ($Getopt::Std::opt_d) { printf "%-$maxlength_vendorid\s ",$tmp[4]; }
196 if ($Getopt::Std::opt_f) { print "$tmp[5]/"; }
197 print "$tmp[6]\n";
198}
199
200
201# -------------------------------------------------------------------
202
203sub getmtime
204{
205 my ($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size,$atime,$mtime,$ctime,$blksize,$blocks) = stat($_[0]);
206
207 return $mtime;
208}
209
210# -------------------------------------------------------------------
211
212sub VERSION_MESSAGE
213{
214 $Getopt::Std::STANDARD_HELP_VERSION=1;
215 print <<EOM
216lscache (Update Accelerator coreutils) 1.00
217Copyright (c) 2006-2008 marco.s - http://update-accelerator.advproxy.net
218EOM
219;
220}
221
222# -------------------------------------------------------------------
223
224sub HELP_MESSAGE
225{
226 print <<EOM
227
228Usage: lscache [-adf | -s]
229
230Shows details about the Update Accelerator cache content
231
232File listing:
233 -a list number of cache hits and last access date
234 -d list download source
235 -f list full cache path
236
237Statistics:
238 -s show statistics by source
239
240 --help display this help
241 --version output version information
242EOM
243;
244}
245
246# -------------------------------------------------------------------