Daily Stats with AWStats
Awstats is a nice software for stats.
This page explains setting it up and adding a custom page to be able to see not only monthly stats but also daily stats.
Installing awstats
Installing awstats
sudo apt-get install awstats cd /usr/share/doc/awstats/examples/ cp apache.conf /etc/apache2/sites-available/awstats.conf ln -s /etc/apache2/sites-available/awstats.conf /etc/apache2/sites-enabled/ sudo /etc/init.d/apache2 restart
Then edit /etc/awstats/awstats.conf
and configure the basic settings, i recommanded the following settings to start with
# unless you have HUGE log files, yearly stats are nice. AllowFullYearView=3 # start testing with DNS off, because the first time you run with it it will take ages, enable it later. DNSLookup=0
Running the report
First we can test awstats by running a report:
awstats
/usr/lib/cgi-bin/awstats.pl --config=/etc/awstats/awstats.conf
Then you can go to
Custom daily reports
I like to see daily stats, so i had to customize/add a few features to make that usable.
First we need to tell awstats to create those daily reports
Creating a cron job
First we create the little script that will run our reports:
vi /root/updatestats.sh
#!/bin/sh /usr/lib/cgi-bin/awstats.pl --config=/etc/awstats/awstats.conf -DatabaseBreak=day /usr/lib/cgi-bin/awstats.pl --config=/etc/awstats/awstats.conf -DatabaseBreak=month /usr/lib/cgi-bin/awstats.pl --config=/etc/awstats/awstats.conf -DatabaseBreak=year
chmod +x /root/updatestats.sh
Then make this script run once a day or whatever in your cron
crontab -e
0 2 * * * /root/updatestats.sh &
Custom Daily links page
Now we have Awstats create our daily data, unfortunately awstats doesn not give you links to it (you have to manually craft the URL !)
So i decided to make a small page with links for the following:
- current day
- previous day
- current month
- previous month
- current year
- last year
- Calendar view, of the whole current month (with daily links)
- Calendar view, of the whole Previous month (with daily links)
Create index.cgi in the same folder as awstats.pl
vi /usr/lib/cgi-bin/index.cgi
#!/usr/bin/perl
#### Thibaut Colar 07/2007 ####
# The awstats config file name
$CONFIG="awstats";
### Begin program ###
@now=localtime(time);
$today_day=$now[3];
$today_month=$now[4]+1;
$today_year=$now[5]+1900;
@yesterday=localtime(time-3600*24);
$ytd_day=$yesterday[3];
$ytd_month=$yesterday[4]+1;
$ytd_year=$yesterday[5]+1900;
$lastmonth=$today_month-1;
$lastmonth_year=$today_year;
if($lastmonth<1)
{
$lastmonth=1;
$lastmonth_year=$today_year-1;
}
$lastyear=$today_year-1;
print "Content-type: text/html\n\n";
print "<html><body>\n";
print "<a href='".getLink($today_year,$today_month,$today_day)."'>Today</a> ";
print "<a href='".getLink($ytd_year,$ytd_month,$ytd_day)."'>Yesterday</a> ";
print "<a href='".getLink($today_year,$today_month)."'>ThisMonth</a> ";
print "<a href='".getLink($lastmonth_year,$lastmonth)."'>LastMonth</a> ";
print "<a href='".getLink($today_year)."'>ThisYear</a> ";
print "<a href='".getLink($lastyear)."'>LastYear</a> ";
print "\n<hr/>\n";
printCal($lastmonth_year, $lastmonth);
print "\n<br>\n";
printCal($today_year, $today_month);
print "\n<hr/></body></html>\n";
##### Methods ######
sub getLink
{
my($year, $month, $day)=@_;
$query="";
if($day)
{
$query="DatabaseBreak=day&day=MISSING_VALUE !&month=MISSING_VALUE !&year=MISSING_VALUE !";
}
elsif($month)
{
$query="month=MISSING_VALUE !&year=MISSING_VALUE !";
}
elsif($year)
{
$query="year=MISSING_VALUE !&month=all";
}
return "awstats.pl?config=MISSING_VALUE !&$query";
}
sub printCal
{
my($y, $m)=@_;
open(CAL, "cal $m $y |");
@days = <CAL>;
close(CAL);
$month = $days[0];
$month=~ s/\s\s\s*//g;
$mbg="";
if($m==$today_month && $y==$today_year)
{
$mbg="bgcolor='#ffaaaa'";
}
print "<table border=1><tr><td colspan=7 $mbg><a href='".getLink($y, $m)."'>$month</a></td></tr><tr>\n";
foreach $dy (split(/ /, $days[1]))
{
print "<td>$dy</td>";
}
print "</tr>\n";
shift(@days);
shift(@days);
foreach $line (@days)
{
chomp $line;
$line =~ s/^\s+//;
$line =~ s/\s+$//;
print "<tr>";
foreach $d (split(/\s+/, $line))
{
$bg="";
if($d==$today_day && $m==$today_month && $y==$today_year)
{
$bg="bgcolor='#ffaaaa'";
}
print "<td $bg><a href='".getLink($y, $m, $d)."'>$d</a></td>";
}
print"</tr>\n";
}
print "</table>\n";
}
you might have to change $CONFIG="awstats"; to whatever you called your awstats config file, if not "awstats.conf"
Then save it and set the permissions properly.
Ex: chmod 755 index.cgi chown www-data index.cgi
You can now go to

Back to top
Title: My 2 cents.
Hello, I've been using awstats for some years now and have a suggestion for viewing "Daily" stats from within the native AWStats browser page. I've added code to the awstats.pl file. Between lines 173 and 174 Ive added:
################################################################
#Added code for "day" or "Month" reporting?
if ( $ENV{QUERY_STRING} ne '') {
my $file = $ENV{QUERY_STRING};
my ($one, $two, $three, $four, $five, $six, $seven) = split (/&/, $file);
my ($month1, $month2) = split (/=/, $one);
my ($day1, $day) = split (/=/, $two);
my ($year1, $year2) = split (/=/, $three);
#$DatabaseBreak='day';
if ($day ne 'all')
{
$DatabaseBreak='day';
}
}
$myawstatsver = 'awstats70';
################################################################
and between lines ~9888 and 9889 I've added:
##############################################################################################################
#Modification added By John Ridgway: Added "Day" pulldown to html page that only lists days for existing data
#Also adds "- Full Month ? " option to see regular monthly output
print "<select class=\"aws_formfield\" name=\"day\">\n";
my $Diritem = "";
my $file = $ENV{QUERY_STRING};
my ($one, $two, $three, $four, $five, $six, $seven) = split (/&/, $file);
my ($month1, $month2) = split (/=/, $one);
my ($day1, $day) = split (/=/, $two);
my ($year1, $year2) = split (/=/, $three);
my ($reportname1, $reportname2) = split(/=/, $five);
my $FullMonth = "";
if ($AllowFullYearView >= 2) {
print "<option value=\"all\">- Full Month -</option>\n";
}
if ($day eq 'all') {
my $FullMonth = $day;
}
foreach (1..9) {
if (! DateIsValid($_,$month2,$year2)) { next; }
my $daynum = "0".$_;
my $checkfile = "E:\\".$myawstatsver."\\".$reportname2."\\awstats".$month2.$year2.$daynum.".".$reportname2.".txt";
#my $dayix=sprintf("%02s",$_);
if (-e $checkfile) {print "<option".("$daynum" eq "$day"?" selected=\"true\"":"")." value=\"$daynum\">$daynum</option>\n"}; #if -e $checkfile;
}
foreach (10..31) {
if (! DateIsValid($_,$month2,$year2)) { next; }
my $daynum = $_;
my $checkfile = "E:\\".$myawstatsver."\\".$reportname2."\\awstats".$month2.$year2.$daynum.".".$reportname2.".txt";
#my $dayix=sprintf("%02s",$_);
if (-e $checkfile) {print "<option".("$daynum" eq "$day"?" selected=\"true\"":"")." value=\"$daynum\">$daynum</option>\n"}; #if -e $checkfile;
}
if ($FullMonth eq 'all') {print "<option value=\"$day\">$day</option>\n"};
#
#
##############################################################################################################
Don't know if this is helpful to anyone or if it's already been done, but it works for me.