Apache log文件分析/IIS log文件详解

  • 时间:2011-7-6 22:50:10  评论:0  浏览:   引用:0

IIS/网站日志文件-网站log文件详解.doc

假设apache日志格式为:

118.78.199.98 – - [09/Jan/2010:00:59:59 +0800] “GET /Public/Css/index.css HTTP/1.1″ 304 – “http://www.a.cn/common/index.php” “Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; GTB6.3)”

问题1:在apachelog中找出访问次数最多的10个IP。

 
  1. awk '{print $1}' apache_log |sort |uniq -c|sort -nr|head

awk 首先将每条日志中的IP抓出来,如日志格式被自定义过,可以 -F 定义分隔符和 print指定列;
sort进行初次排序,为的使相同的记录排列到一起;
uniq -c 合并重复的行,并记录重复次数。
head进行前十名筛选;
sort -nr按照数字进行倒叙排序。
我参考的命令是:
显示10条最常用的命令

 
  1. sed -e "s/| /\n/g" ~/.bash_history | cut -d ' ' -f 1 | sort | uniq -c | sort -nr | head

 

问题2:在apache日志中找出访问次数最多的几个分钟。

 
  1. awk '{print  $4}' apache.log |cut -c 14-18|sort|uniq -c|sort -nr|head

awk 用空格分出来的第四列是[09/Jan/2010:00:59:59;
cut -c 提取14到18个字符
剩下的内容和问题1类似。
 

问题3:在apache日志中找到访问最多的页面:

 
  1. awk '{print $11}' apache_log |sed 's/^.*cn\(.*\)\"/\1/g'|sort |uniq -c|sort -rn|head

类似问题1和2,唯一特殊是用sed的替换功能将”http://www.a.cn/common/index.php”替换成括号内的内容:”http://www.a.cn(/common/index.php)”
 

问题4:在apache日志中找出访问次数最多(负载最重)的几个时间段(以分钟为单位),然后在看看这些时间哪几个IP访问的最多?
版本1

 
  1. #!/bin/bash
  2. # analysis apache access log
  3. # histroy
  4. # caoyameng  version0.1 2010/01/24
  5.  
  6. if (test -z $1) ;then
  7.  read -p "Specify  logfile:" LOG
  8. else
  9.         LOG=$1
  10. fi
  11.  
  12. if [ ! -e $LOG ];then
  13. echo "I cann't find apache log file."
  14. exit 0
  15. fi
  16.  
  17. awk '{print  $4}' $LOG |cut -c 14-18|sort|uniq -c|sort -nr|head  >timelog
  18. for   i in  `awk '{print $2}' timelog`
  19. do
  20.  
  21. all=`grep $i timelog|awk '{print $1}'`
  22. echo  " $i  $all"
  23. IP=`grep $i $LOG| awk '{print $1}' |sort |uniq -c|sort -nr|head`
  24. echo  "$IP"
  25.  
  26. done
  27. rm  -f timelog

另一个版本的解决方法,其实就是换了下for的计算方式

 
  1. #!/bin/bash
  2. # analysis apache access log
  3. # histroy
  4. # caoyameng  version0.2 2010/01/24
  5.  
  6. if (test -z $1) ;then
  7.  read -p "Specify  logfile:" LOG
  8. else
  9.         LOG=$1
  10. fi
  11.  
  12. if [ ! -e $LOG ];then
  13. echo "I cann't find apache log file."
  14. exit 0
  15. fi
  16.  
  17. awk '{print  $4}' $LOG |cut -c 14-18|sort|uniq -c|sort -nr|head  >timelog
  18. for (( i=1; i<=10; i=i+1 ))
  19. do
  20. num=`sed -n "${i}p" timelog|awk '{print $1}'`
  21. time=`sed -n "${i}p" timelog|awk '{print $2}'`
  22. echo  "####The No.$i "
  23. echo  " "
  24. echo  " $time   $num"
  25. echo  " "
  26. full=`grep $time $LOG| awk '{print $1}' |sort |uniq -c|sort -nr|head`
  27. echo  "$full"
  28. echo " "
  29. done
  30. rm  -f timelog

 

Tags: Apache  IIS  log  
  • 相关文章:
 

发表评论:

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。