Daily statistics in AwStats

AwStats is a log file parser written in perl, with which you can keep statistics of site visits based on information from log files.

As a rule, awstats displays general information for the week, month. But what to do if you need to display information about visiting the site for a specific day.

A similar trick will be discussed further on the example of a server based on Linux Ubuntu with Apache2.

First, install AwStats:

apt-get install awstats libapache2-mod-perl2

Create website config:

cp /etc/awstats/awstats.conf /etc/awstats/awstats.**website.com**.conf

Edit the file accordingly:

LogFile="/var/log/apache2/access.log" #path to logfile;
LogFormat=1 #for full statistics;
SiteDomain="website.com” #domainname;
HostAliases="www.website.com website.com" #site aliases
AllowFullYearView=3
DNSLookup=0

That is it. Next enable AwStats in Apache:

cp /usr/share/doc/awstats/examples/apache.conf /etc/apache2/conf.d/awstats.conf  
/etc/init.d/apache2 reload

Edit cron tab:

crontab -e

Create the following cron jobs:

0 2 * * * /usr/lib/cgi-bin/awstats.pl --config=/etc/awstats/awstats.website.com.conf -DatabaseBreak=day > /dev/null
0 2 * * * /usr/lib/cgi-bin/awstats.pl --config=/etc/awstats/awstats.website.com.conf -DatabaseBreak=month > /dev/null
0 2 * * * /usr/lib/cgi-bin/awstats.pl --config=/etc/awstats/awstats.website.com.conf -DatabaseBreak=year > /dev/null

It remains to create index.cgi that will provide the ability to select a range for displaying statistics. Create an index.cgi file in /usr/lib/cgi-bin/

The contents of the file are in the spoiler.

/usr/lib/cgi-bin/index.cgi
#!/usr/bin/perl

# 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=${day}&month=${month}&year=${year}";
	}
	elsif($month)
	{
		$query="month=${month}&year=${year}";
	}
	elsif($year)
	{
		$query="year=${year}&month=all";
	}
	return "awstats.pl?config=${CONFIG}&$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";
}

Make it executable and change the owner:

chmod 755 /usr/lib/cgi-bin/index.cgi
chown www-data:www-data /usr/lib/cgi-bin/index.cgi

Edit /etc/apache2/conf.d/awstats.conf with the following:

DirectoryIndex index.cgi

I got the same note for NginX