#!/usr/bin/perl # ------------------------------------------------------------------------------ # "THE BEER-WARE LICENSE" (Revision 42): # wrote this file. As long as you retain this notice you # can do whatever you want with this stuff. If we meet some day, and you think # this stuff is worth it, you can buy me a beer in return. Anders Giversen # ------------------------------------------------------------------------------ use strict; my $Debug = 0; my $CONFIGDIR = "/etc/awstats"; my $DATADIR = "/var/lib/awstats"; my $OUTPUTDIR = "/var/www"; my %monthStr = ('01'=>'Jan', '02'=>'Feb', '03'=>'Mar', '04'=>'Apr', '05'=>'May', '06'=>'Jun', '07'=>'Jul', '08'=>'Aug', '09'=>'Sep', '10'=>'Oct', '11'=>'Nov', '12'=>'Dec'); my ($sec, $min, $hour, $day, $month, $year, $wday, $yday, $isdst) = localtime(time); if ($year < 100) { $year += 2000; } else { $year += 1900; } if (++$month < 10) { $month = "0$month"; } my $period = $year; my $total = 0; my $max = 0; my %summary = (); sub debug { my $level = $_[1] || 1; if ($Debug >= $level) { print localtime(time) . " - DEBUG $level - $_[0]\n"; } } sub error { print STDERR "ERROR: $_[0]\n"; exit 1; } sub formatBytes { my $bytes = shift || 0; if ($bytes >= 1073741824) { return sprintf "%.2f GB", $bytes/1073741824; } elsif ($bytes >= 1048576) { return sprintf "%.2f MB", $bytes/1048576; } elsif ($bytes >= 1024) { return sprintf "%.2f KB", $bytes/1024; } else { return sprintf "%.2f", $bytes; } } if (@ARGV) { $period = $ARGV[0]; } debug("Report period: " . $period); opendir(DIR, $CONFIGDIR) || error("Can't scan directory $CONFIGDIR - $!"); my @filesindir = grep { /^awstats\.(.+)\.conf$/ } sort readdir(DIR); closedir(DIR); my %traffic = (); foreach my $file (@filesindir) { if ($file =~ /^awstats\.(.+)\.conf$/) { my $conf=$1; $conf =~ s/\.$//; if ($conf eq 'model') { next; } $traffic{$1} = 0; } } debug("List of qualified domains: " . join(", ", sort keys(%traffic))); my $OutputFile = "$OUTPUTDIR/$period.html"; open(OUTPUT, ">$OutputFile") || error("Couldn't open file $OutputFile - $!"); print OUTPUT < Web traffic

Web traffic $period

Last Update: $day $monthStr{$month} $year

EOF foreach my $domain ( sort keys(%traffic) ) { opendir(DIR, $DATADIR) || error("Can't scan directory $DATADIR - $!"); my @filesindir = grep { /^awstats(.+)$period\.$domain\.txt$/ } sort readdir(DIR); closedir(DIR); foreach my $DataFile (@filesindir) { my $monthTraffic = 0; my @field = (); my $month = $DataFile; $month =~ s/awstats(.+)$period\.$domain\.txt/$1/; open(DATA, "$DATADIR/$DataFile") || error("Couldn't open file $DataFile - $!"); while () { chomp $_; s/\r//; @field = split(/\s+/, $_); if ($field[0] eq 'BEGIN_TIME') { $field[0] = ''; do { if ($field[0] ne '') { $monthTraffic += int($field[3]); } $_ = ; chomp $_; s/\r//; @field = split(/\s+/, $_); } until ($field[0] eq 'END_TIME' || ! $_); } } close(DATA); $traffic{$domain} += $monthTraffic; $summary{$domain} .= "$monthStr{$month}: " . formatBytes($monthTraffic) . "
"; } if ($traffic{$domain} > $max) { $max = $traffic{$domain}; } $total += $traffic{$domain}; } print OUTPUT "\n"; foreach my $domain ( sort { $traffic{$a} <=> $traffic{$b} } keys %traffic ) { printf OUTPUT "\n", $domain, $domain, $summary{$domain}, ($traffic{$domain}/$max * 100), formatBytes($traffic{$domain}); } printf OUTPUT "\n", formatBytes($total); print OUTPUT < EOF close(OUTPUT); 0;
%s" . "
" . "%s
%s
" . "%s
Total:%s