#!/usr/bin/perl # # Walk Apache cache and report stats in the form # total class # where the class depends on the type of data: # HOST hostname of URL # TYPE content-type # use strict qw(vars); use cs::Misc; use cs::Source; use cs::Upd; use cs::URL; use cs::RFC822; use cs::Hier; use File::Find; $::Blocksize=1024; $::Usage="Usage: $::cmd class [dirs...] class HOST or TYPE "; my($badopts)=0; if (! @ARGV) { warn "$::cmd: missing class\n"; $badopts=1; } else { $::Type=uc(shift(@ARGV)); @ARGV='.' unless @ARGV; } die $::Usage if $badopts; $cs::Upd::This=new cs::Upd main::STDERR; # default upd structure undef %::Tally; find(sub { return 0 if ! stat; return 1 if -d _; return 0 if ! -f _; $::Silent || out($File::Find::name); my($s)=cs::Source::open($_); if (! defined $s) { warn "$::cmd: can't open $File::Find::name: possible error: $!\n"; return 0; } { my($line); return 0 if ! defined ($line=$s->GetLine()) || ! length $line; my($urlhdr); return 0 if ! defined ($urlhdr=$s->GetLine()) || ! length $urlhdr; return 0 if ! defined ($line=$s->GetLine()) || ! length $line; $s->UnGet($urlhdr); } my($h); return 0 if ! defined ($h=new cs::RFC822 $s); undef $s; tally($h,$File::Find::name); 1; }, @ARGV); out(''); for (sort keys %::Tally) { print "$::Tally{$_}\t$_\n"; } exit 0; sub tally($) { my($h,$path)=@_; ## warn "h=".cs::Hier::h2a($h,1)."\n"; my($cl)=scalar($h->Hdr(CONTENT_LENGTH)); my($size)=int(($cl+$::Blocksize/2)/$::Blocksize); return if $size < 1; my($key); if ($::Type eq HOST) { my($url)=$h->Hdr(X_URL); if (! defined $url || ! length $url) { warn "$::cmd: no URL for $path\n"; } my($u)=new cs::URL $url; $key=lc($url->HostPart()); } elsif ($::Type eq TYPE) { $key=lc($h->Hdr(CONTENT_TYPE)); } else { die "$::cmd: unsupported class \"$::Type\"\n"; } $::Tally{$key}+=$size; }