1、日志文件的位置及格式:
1
2
3
4
5
6
7
|
[root@VM_177_116_tlinux /var/log/nginx ] # ls #Nginx的访问日志(简洁版)
access.log-20140401.gz error.log-20140403.gz access.log-20140402.gz error.log-20140227.gz error.log-20140404.gz access.log-20140403.gz error.log-20140228.gz error.log-20140405.gz access.log-20140404.gz access.log-20140319.gz access.log-20140405.gz access.log-20140406.gz access.log-20140408 access.log-20140409 access.log-20140410 access.log-20140411 |
2、日志的内容及需求:
1
2
3
|
[root@VM_177_116_tlinux / var /log/nginx]# vim access.log- 20140409 #日志内容信息
124.118 . 47.46 - - [ 08 /Apr/ 2014 : 03 : 45 : 03 + 0800 ] "GET **********敏感信息***********" 200 2056 "http://qzs.qzone.qq.com/open/fusion/app_redirect.htm" "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.1; WOW64; Trident/5.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0; .NET4.0C; .NET4.0E)" "-"
124.118 . 47.46 - - [ 08 /Apr/ 2014 : 03 : 45 : 03 + 0800 ] "GET /js/qzone.js HTTP/1.1" 200 6705 "**********敏感信息***********" "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.1; WOW64; Trident/5.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0; .NET4.0C; .NET4.0E)" "-"
|
需求:查出访问jquery.js;other.js;qzone.js;swfobject.j的IP地址并统计个数
注意:
1
2
3
4
5
6
7
8
9
10
11
|
[root@VM_177_116_tlinux /var/log/nginx ] # grep "08/Apr/2014" access.log-20140408 | head -5 #8号的日志里面有记录8号凌晨的信息
110.16.12.61 - - [08 /Apr/2014 :00:00:03 +0800] "GET /js/other.js HTTP/1.1" 200 7250 " **********敏感信息*********** " Mozilla /5 .0 (Windows NT 5.1) AppleWebKit /537 .1 (KHTML, like Gecko) Chrome /21 .0.1180.89 Safari /537 .1 " " -"
180.111.38.125 - - [08 /Apr/2014 :00:00:06 +0800] "***********敏感信息***********" http: //qzs .qzone.qq.com /open/fusion/app_redirect .htm " " Mozilla /5 .0 (Windows NT 6.2; WOW64) AppleWebKit /537 .1 (KHTML, like Gecko) Chrome /21 .0.1180.89 Safari /537 .1 " " -"
180.112.219.107 - - [08 /Apr/2014 :00:00:14 +0800] "***********敏感信息***********" 200 2056 "http://qzs.qzone.qq.com/open/fusion/app_redirect.htm" "Mozilla/5.0 (Windows NT 5.1) AppleWebKit/537.1 (KHTML, like Gecko) Chrome/21.0.1180.89 Safari/537.1" "-"
180.112.219.107 - - [08 /Apr/2014 :00:00:14 +0800] "GET /js/qzone.js HTTP/1.1" 304 0 "***********敏感信息***********" "Mozilla/5.0 (Windows NT 5.1) AppleWebKit/537.1 (KHTML, like Gecko) Chrome/21.0.1180.89 Safari/537.1" "-"
[root@VM_177_116_tlinux /var/log/nginx ] # grep "08/Apr/2014" access.log-20140409 | head -5 #9号里面主要记录8号白天的信息
124.118.47.46 - - [08 /Apr/2014 :03:45:03 +0800] "GET ***********敏感信息***********" "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.1; WOW64; Trident/5.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0; .NET4.0C; .NET4.0E)" "-"
124.118.47.46 - - [08 /Apr/2014 :03:45:03 +0800] "GET /js/qzone.js HTTP/1.1" 200 6705 "***********敏感信息***********" "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.1; WOW64; Trident/5.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0; .NET4.0C; .NET4.0E)" "-"
124.118.47.46 - - [08 /Apr/2014 :03:45:03 +0800] "GET /js/other.js HTTP/1.1" 200 7250 "***********敏感信息***********" "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.1; WOW64; Trident/5.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0; .NET4.0C; .NET4.0E)" "-"
124.118.47.46 - - [08 /Apr/2014 :03:45:04 +0800] "GET /js/swfobject.js HTTP/1.1" 200 26351 "***********敏感信息***********" "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.1; WOW64; Trident/5.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0; .NET4.0C; .NET4.0E)" "-"
124.118.47.46 - - [08 /Apr/2014 :03:45:04 +0800] "GET /js/jquery.js HTTP/1.1" 200 72174 "***********敏感信息***********" "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.1; WOW64; Trident/5.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0; .NET4.0C; .NET4.0E)" "-"
|
3、脚本展示:
1
2
3
4
5
6
7
8
9
10
11
12
|
[root@VM_177_116_tlinux /var/log/nginx ] # vim log.sh
#!/bin/bash date =$1
list="jquery.js other.js qzone.js swfobject.js" date1=$(($ date +1))
for i in $list
do echo $((` egrep "0${date}/Apr/2014" /var/log/nginx/access .log-2014040$ date | grep "$i" | awk '{print $1}' | sort -n| uniq | wc -l`+` egrep "$0{date}/Apr/2014" /var/log/nginx/access .log-2014040$date1| grep "$i" | awk '{print $1}' | sort -n| uniq | wc -l`))
done |
4、初步执行结果:
1
2
3
4
5
|
[root@VM_177_116_tlinux / var /log/nginx]# ./log.sh 8
78923 71233 78295 62850 |
5、疑惑:
1
2
3
4
5
6
7
8
9
|
[root@VM_177_116_tlinux /var/log/nginx ] # ./log.sh 9
egrep : /var/log/nginx/access .log-201404010: No such file or directory
5762 egrep : /var/log/nginx/access .log-201404010: No such file or directory
5428 egrep : /var/log/nginx/access .log-201404010: No such file or directory
8328 egrep : /var/log/nginx/access .log-201404010: No such file or directory
5369 |
6、解决疑惑脚本:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
[root@VM_177_116_tlinux /var/log/nginx ] # vim log.sh
#!/bin/bash
date =$1
list="jquery.js other.js qzone.js swfobject.js" date1=$(($ date +1))
if [[ $ date =~ ^.$ ]]; then
date =0$ date
if [[ $date1 =~ ^.$ ]]; then
date1=0$date1
fi
fi for i in $list
do echo $((` egrep "${date}/Apr/2014" /var/log/nginx/access .log-201404$ date | grep "$i" | awk '{print $1}' | sort -n| uniq | wc -l`+` egrep "${date}/Apr/2014" /var/log/nginx/access .log-201404$date1| grep "$i" | awk '{print $1}' | sort -n| uniq | wc -l`))
done |
本文转自 z永 51CTO博客,原文链接:http://blog.51cto.com/pangge/1394315