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.
# start testing with DNS off, because the first time you run with it it will take ages, enable it later.

Running the report

First we can test awstats by running a report:
/usr/lib/cgi-bin/awstats.pl --config=/etc/awstats/awstats.conf

Then you can go to http://yourhost/cgi-bin/awstats.pl and see your nice report.

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
/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

#### Thibaut Colar 07/2007 ####

# The awstats config file name

### Begin program ###




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)=@_;
	return "awstats.pl?config=${CONFIG}&$query";

sub printCal
my($y, $m)=@_;
open(CAL, "cal $m $y |");
@days = <CAL>;

$month = $days[0]; 
$month=~ s/\s\s\s*//g;
if($m==$today_month && $y==$today_year)
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";
foreach $line (@days)
	chomp $line;
	$line =~ s/^\s+//;
	$line =~ s/\s+$//;
	print "<tr>";
	foreach	$d (split(/\s+/, $line))
		if($d==$today_day && $m==$today_month && $y==$today_year)
		print "<td $bg><a href='".getLink($y, $m, $d)."'>$d</a></td>";
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 http://yourhost/cgi-bin/awstats.pl and have the daily stats links.




Add a new Comment