Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Measure traffic from apache access log

Is there any way to measure how much traffic there were used in one Apache log file?

Format:

66.249.72.214 - - [05/Nov/2011:12:47:37 +0200] "GET /produktas/565638 HTTP/1.1" 200 4699 "-" "Mozilla/5.0 (compatible; Googlebot/2.1; +http://www.google.com/bot.html)"

How I understand 4699 are bytes that were transferred excluding headers.

I need a simple solution (maybe a little bash script) to sum bytes in every log's line.

like image 336
Justinas Lelys Avatar asked Nov 05 '11 11:11

Justinas Lelys


4 Answers

Try this. I tested it on a local file but cannot tell if it works on all configurations/locales/...

cat apache.log | perl -e 'my $sum=0; while(<>) { my ($traffic) = m/\[.+\] ".+" \d+ (\d+)/; $sum += $traffic}; print "$sum\n"'

Update Jan 2017: Meanwhile I've learned some more Perl and that's how I'd do it today:

cat apache.log | perl -nE '/\[.+\] ".+" \d+ (\d+)/; $sum += $1; END {say $sum}'
like image 54
Daniel Böhmer Avatar answered Oct 14 '22 03:10

Daniel Böhmer


Apache Access Log — Global bandwidth usage :

awk '{ s += $10 } END { print "Total ", s/1024/1024 " Mo", "- Moyenne ", s/NR/1024/1024 " Mo", "- Accès ", NR }' access.log

And for a file :

grep NAME_OF_RESOURCE_HERE /var/log/apache2/access.log* | awk '{ s += $10 } END { print "Total ", s/1024/1024 " Mo", "- Moyenne ", s/NR/1024/1024 " Mo", "- Accès ", NR }'


You get something like this : Total 301.985 Mo - Moyenne 0.0430055 Mo - Accès 7022

like image 43
Atika Avatar answered Oct 14 '22 04:10

Atika


For detailed log file monitoring and actual bandwidth usage, go for AWStats.

It takes the Apache log file as input and give you very detailed analysis of the visitors and bandwidth, with graphs.

You can also try GoAccess.

like image 4
Farhan Avatar answered Oct 14 '22 04:10

Farhan


I think you need to use apachetop utility, try to install from APT with next command:

sudo apt-get install apachetop

And then run it with command:

sudo apachetop -f /path/to/access.log

And you are rock! :)

like image 2
Victor Bocharsky Avatar answered Oct 14 '22 03:10

Victor Bocharsky