首页 > 代码库 > 如何分析服务器日志哪些元素占用流量过大?

如何分析服务器日志哪些元素占用流量过大?

测试数据如下:

59.33.26.105 - - [08/Dec/2010:15:43:56 +0800] "GET /static/images/photos/2.jpg HTTP/1.1" 200 11299 "http://oldboy.blog.51cto.com/static/web/column/17/index.shtml?courseId=43" "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 2.0.50727; .NET CLR 3.0.4506.2152; .NET CLR 3.5.30729)" 

59.33.26.105 - - [08/Dec/2010:15:43:56 +0800] "GET /static/images/photos/2.jpg HTTP/1.1" 200 11299 "http://oldboy.blog.51cto.com/static/web/column/17/index.shtml?courseId=43" "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 2.0.50727; .NET CLR 3.0.4506.2152; .NET CLR 3.5.30729)" 

59.33.26.105 - - [08/Dec/2010:15:44:02 +0800] "GET /static/flex/vedioLoading.swf HTTP/1.1" 200 3583 "http://oldboy.blog.51cto.com/static/flex/AdobeVideoPlayer.swf?width=590&height=328&url=/[[DYNAMIC]]/2" "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 2.0.50727; .NET CLR 3.0.4506.2152; .NET CLR 3.5.30729)" 

124.115.4.18 - - [08/Dec/2010:15:44:15 +0800] "GET /?= HTTP/1.1" 200 46232 "-" "-" 

124.115.4.18 - - [08/Dec/2010:15:44:25 +0800] "GET /static/js/web_js.js HTTP/1.1" 200 4460 "-" "-" 

124.115.4.18 - - [08/Dec/2010:15:44:25 +0800] "GET /static/js/jquery.lazyload.js HTTP/1.1" 200 1627 "-" "-"


需要输出三个指标:【访问次数】【访问次数*单个文件大小】 【文件名(可以带URL)】。

解决方案,通过awk取出文件名,文件大小;用数组进行 访问次数,及访问量的统计,最后排序输出结果。

代码如下: 

[root@oldboy32 data]# awk ‘{a[$7] += $10; ++b[$7];
total += $10}END {for( i in a) print b[i],a[i],i}‘ images.log |sort -rnk2
1 46232 /?=
2 22598 /static/images/photos/2.jpg
1 4460 /static/js/web_js.js
1 3583 /static/flex/vedioLoading.swf
1 1627 /static/js/jquery.lazyload.js






如何分析服务器日志哪些元素占用流量过大?