Linux 测量来自 apache 访问日志的流量
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8019742/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Measure traffic from apache access log
提问by Justinas Lelys
Is there any way to measure how much traffic there were used in one Apache log file?
有没有办法测量一个 Apache 日志文件中使用了多少流量?
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.
我的理解4699
是传输的字节数不包括标头。
I need a simple solution (maybe a little bash script) to sum bytes in every log's line.
我需要一个简单的解决方案(可能是一个小 bash 脚本)来对每个日志行中的字节求和。
采纳答案by Daniel B?hmer
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:
2017 年 1 月更新:与此同时,我学到了更多 Perl,这就是我今天的做法:
cat apache.log | perl -nE '/\[.+\] ".+" \d+ (\d+)/; $sum += ; END {say $sum}'
回答by Farhan
For detailed log file monitoring and actual bandwidth usage, go for AWStats.
有关详细的日志文件监控和实际带宽使用情况,请访问AWStats。
It takes the Apache log file as input and give you very detailed analysis of the visitors and bandwidth, with graphs.
它以 Apache 日志文件作为输入,并通过图表为您提供非常详细的访问者和带宽分析。
You can also try GoAccess.
您也可以尝试GoAccess。
回答by Atika
Apache Access Log — Global bandwidth usage :
Apache 访问日志 — 全局带宽使用情况:
awk '{ s += } 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 += } 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
你会得到这样的结果:总计 301.985 Mo - Moyenne 0.0430055 Mo - Accès 7022
回答by Victor Bocharsky
回答by Stefan Leisten
We needed to get the traffic of last X days. I'm really not into perl, so what i did is:
我们需要获取过去 X 天的流量。我真的不喜欢 perl,所以我所做的是:
zcat $(find -name yourvhost_access.log*.gz -mtime -3 2>/dev/null| xargs ) \
| awk ' ~ /^[0-9]+$/ {print }' \
| paste -sd+ \
| bc
Steps:
脚步:
- find last 3 achived accesslogs
- print position 10 if its a Number - payload should be here
- put it together with "+"
- calculate
- 找到最后 3 个访问日志
- 如果是数字,则打印位置 10 - 负载应该在此处
- 用“+”放在一起
- 计算