Count traffic from an apache log file

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:

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:

Total  28.4232 MB - Average  0.0108485 Mo - Access 2620

If you need to search for a particular date:

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:

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:

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 }'