Count traffic from an apache log file

Submit to reddit

I had to count traffic made by a resource from the apache log files. If awstats had been installed it would have been an easy job, but it wasn’t hard without it either. I used hints from here to build the following:

1
awk '{ s += $10 } END { print "Total ", s/1024/1024 " MB", "- Average ", s/NR/1024/1024 " MB", "- Access ", NR }' access_log

This prints a result like this:

1
Total  28.4232 MB - Average  0.0108485 Mo - Access 2620

If you need to search for a particular date:

1
grep "02/Mar" access_log | awk '{ s += $10 } END { print "Total ", s/1024/1024 " MB", "- Average ", s/NR/1024/1024 " MB", "- Access ", NR }'

If you need to search for a particular resource:

1
grep "silly_large_cat_picture.jpg" access_log | awk '{ s += $10 } END { print "Total ", s/1024/1024 " MB", "- Average ", s/NR/1024/1024 " MB", "- Access ", NR }'

Of course you can combine:

1
grep "silly_large_cat_picture.jpg" access_log | grep "02/Mar" | awk '{ s += $10 } END { print "Total ", s/1024/1024 " MB", "- Average ", s/NR/1024/1024 " MB", "- Access ", NR }'