liuxiaoyun111 发表于 2018-11-23 07:26:13

apache 下access.log日志提取分析

  我想统计今天产生的日志进行分析,也就是15/Aug/2011
我想出来的结果:awk '$4 ~/15\/Aug/ {print}' access_log
  172.16.119.8 - admin "PROPFIND /svn/EAGLE HTTP/1.1" 207 649
172.16.119.8 - admin "PROPFIND /svn/EAGLE/!svn/vcc/default HTTP/1.1" 207 401
172.16.119.8 - admin "PROPFIND /svn/EAGLE/!svn/bln/31 HTTP/1.1" 207 454
172.16.119.8 - admin "PROPFIND /svn/EAGLE HTTP/1.1" 207 649
172.16.119.8 - admin "PROPFIND /svn/EAGLE/!svn/vcc/default HTTP/1.1" 207 454

  

  awk '$4 ~/27\/Mar\/2014/ {print $1}' access_log |uniq -c|awk '{sum+=$1}END{print sum}'
  统计同访问量
  /27\/Mar\/2014/      表示 统计2014-3-17日的总数
  /Mar\/2014/   表示统计2014年3月正个月的访问量
  awk '{sum+=$1}END{print sum}'   表示对第一列数值求和,也就得到访问量了
  
  awk '$4 ~/27\/Mar\/2014/ {print $1}' access_log |uniq -c| wc -l      统计不同IP个数

  

  bianliang=24/Feb/2014;awk -v bianliang=$bianliang '$4~bianliang' access_log
  
  

  TODAY=$(date +%d/%b/%Y);grep $TODAY access_log
  
  例如:
  #cat 1.txt
6
8
8
12
15
17
141

#cat 1.txt | awk '{print a=a+$1}end{print a}' | tail -n1
207
  或者:awk '{sum+=$1}END{print sum}'filename
  

  将数字放到文本文件a中
将下面的代码拷贝到b中
执行b得出结果
#!/bin/bash
result=0;
for num in `cat a`
do
    let result=$result+$num;
done
echo $result
  

  

  通过apache 访问日志access.log 统计IP 和每个地址访问的次数,按访问量列出 前10 名
  

  cat access_log |awk '{print $1}'|uniq -c |sort -rn |head -10
  

apache 日志分析
  

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

awk '{print $1}' apache_log |sort |uniq -c|sort -nr|head -n 10
awk 首先将每条日志中的IP抓出来,如日志格式被自定义过,可以 -F 定义分隔符和 print指定列;
sort进行初次排序,为的使相同的记录排列到一起;
upiq -c 合并重复的行,并记录重复次数。
head进行前十名筛选;
sort -nr按照数字进行倒叙排序。

2、在apache日志中找到访问最多的页面:
awk '{print $11}' apache_log |sed 's/^.*cn\(.*\)\"/\1/g'|sort |uniq -c|sort -rn|head

3、在apache日志中找出访问次数最多的几个分钟。
awk '{print$4}' access_log |cut -c 14-18|sort|uniq -c|sort -nr|head


4、在apache日志中找到访问最多的页面:
awk '{print $11}' apache_log |sed 's/^.*cn/(.*/)/"//1/g'|sort |uniq -c|sort -rn|head


5、在apache日志中找出访问次数最多(负载最重)的几个时间段(以分钟为单位),然后在看看这些时间哪几个IP访问的最多?
版本1
#!/bin/bash
# analysis apache access log
# histroy
# caoyamengversion0.1 2010/01/24
if (test -z $1) ;then
read -p "Specifylogfile:" LOG
else
      LOG=$1
fi
if [ ! -e $LOG ];then
echo "I cann't find apache log file."
exit 0
fi
awk '{print$4}' $LOG |cut -c 14-18|sort|uniq -c|sort -nr|head>timelog
for   i in`awk '{print $2}' timelog`
do
all=`grep $i timelog|awk '{print $1}'`
echo" $i$all"
IP=`grep $i $LOG| awk '{print $1}' |sort |uniq -c|sort -nr|head`
echo"$IP"
done
rm-f timelog


另一个版本的解决方法,其实就是换了下for的计算方式
#!/bin/bash
# analysis apache access log
# histroy
# caoyamengversion0.2 2010/01/24
if (test -z $1) ;then
read -p "Specifylogfile:" LOG
else
      LOG=$1
fi
if [ ! -e $LOG ];then
echo "I cann't find apache log file."
exit 0
fi
awk '{print$4}' $LOG |cut -c 14-18|sort|uniq -c|sort -nr|head>timelog
for (( i=1; i
页: [1]
查看完整版本: apache 下access.log日志提取分析