tomcat日志文件 访问IP统计

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了tomcat日志文件 访问IP统计相关的知识,希望对你有一定的参考价值。

tomcat ?localhost_access_log.2018-09-19.txt日志文件 访问IP统计

172.16.8.11 - - [19/Sep/2018:12:35:21 +0800] "GET /console/stat/onlineVisitorRefresh HTTP/1.1" 200 7613
172.16.8.1 - - [19/Sep/2018:12:43:08 +0800] "GET /images/loading.gif HTTP/1.1" 200 404
172.16.8.1 - - [19/Sep/2018:12:43:08 +0800] "POST /init HTTP/1.1" 200 207
172.16.8.1 - - [19/Sep/2018:12:43:12 +0800] "POST /init HTTP/1.1" 200 207
172.16.8.1 - - [19/Sep/2018:12:43:16 +0800] "GET / HTTP/1.1" 200 7612
172.16.8.1 - - [19/Sep/2018:12:43:16 +0800] "GET /css/default-init.css?1537325099834 HTTP/1.1" 304 -
172.16.8.1 - - [19/Sep/2018:12:43:16 +0800] "GET /js/lib/jquery/jquery.min.js HTTP/1.1" 304 -
172.16.8.1 - - [19/Sep/2018:12:43:16 +0800] "GET /images/logo.png HTTP/1.1" 304 -
172.16.8.11 - - [19/Sep/2018:12:45:21 +0800] "GET /console/stat/onlineVisitorRefresh HTTP/1.1" 200 7612
172.16.8.11 - - [19/Sep/2018:12:55:21 +0800] "GET /console/stat/onlineVisitorRefresh HTTP/1.1" 200 7612
172.16.8.11 - - [19/Sep/2018:13:05:21 +0800] "GET /console/stat/onlineVisitorRefresh HTTP/1.1" 200 7612
172.16.8.11 - - [19/Sep/2018:13:15:21 +0800] "GET /console/stat/onlineVisitorRefresh HTTP/1.1" 200 7613
172.16.8.11 - - [19/Sep/2018:13:25:21 +0800] "GET /console/stat/onlineVisitorRefresh HTTP/1.1" 200 7613
172.16.8.11 - - [19/Sep/2018:13:35:21 +0800] "GET /console/stat/onlineVisitorRefresh HTTP/1.1" 200 7613
172.16.8.11 - - [19/Sep/2018:13:45:21 +0800] "GET /console/stat/onlineVisitorRefresh HTTP/1.1" 200 7613
172.16.8.11 - - [19/Sep/2018:13:55:21 +0800] "GET /console/stat/onlineVisitorRefresh HTTP/1.1" 200 7612
172.16.8.11 - - [19/Sep/2018:14:05:21 +0800] "GET /console/stat/onlineVisitorRefresh HTTP/1.1" 200 7612
172.16.8.11 - - [19/Sep/2018:14:15:21 +0800] "GET /console/stat/onlineVisitorRefresh HTTP/1.1" 200 7612
172.16.8.11 - - [19/Sep/2018:14:25:21 +0800] "GET /console/stat/onlineVisitorRefresh HTTP/1.1" 200 7612
172.16.8.11 - - [19/Sep/2018:14:35:21 +0800] "GET /console/stat/onlineVisitorRefresh HTTP/1.1" 200 7612
172.16.8.11 - - [19/Sep/2018:14:45:21 +0800] "GET /console/stat/onlineVisitorRefresh HTTP/1.1" 200 7613

SHELL脚本

[[email protected] logs]# awk ‘{aaa[$1]++;} END{for(i in aaa) { printf("%s %s ", aaa[i], i); }}‘ localhost_access_log.2018-09-19.txt | sort -bn
2?? ?127.0.0.1
24?? ?172.16.8.1
26?? ?172.16.8.11
[[email protected] logs]# grep -i -o -E -r -e "([0-9]{1,3}.){3}[0-9]{1,3}" localhost_access_log.2018-09-19.txt | sort -n | uniq -c | sort -n
? ? ? 2 127.0.0.1
? ? ?24 172.16.8.1
? ? ?26 172.16.8.11
[[email protected] logs]# awk ‘{print $1}‘ localhost_access_log.2018-09-19.txt | grep -E "([0-9]{1,3}.){3}[0-9]{1,3}" | sort | uniq -c | sort -g
? ? ? 2 127.0.0.1
? ? ?24 172.16.8.1
? ? ?26 172.16.8.11
PYTHON脚本

cat log.py?
import re
mydict = {}
with open(‘localhost_access_log.2018-09-19.txt‘) as f:
? ? ? ? for line in f:
? ? ? ? ? ? ? ? match = re.match(r‘([0-9]{1,3}.){3}[0-9]{1,3}‘, line)
? ? ? ? ? ? ? ? if match:
? ? ? ? ? ? ? ? ? ? ? ? ip = match.group()
? ? ? ? ? ? ? ? ? ? ? ? if ip in mydict.keys():
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? mydict[ip] += 1
? ? ? ? ? ? ? ? ? ? ? ? else:
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? mydict[ip] = 1

print mydict

执行结果
[[email protected] logs]# python log.py?
{‘172.16.8.1‘: 24, ‘172.16.8.11‘: 27, ‘127.0.0.1‘: 2}

以上是关于tomcat日志文件 访问IP统计的主要内容,如果未能解决你的问题,请参考以下文章

日志分析_使用shell完整日志分析案例

统计web 访问日志的请求数据

日志监控文件中获取ip,每一分钟统计一次,超过200次的计入黑名单

Nginx 分析access日志文件

取消tomcat 控制台所有日志打印输出

切分Nginx日志,完成网站访问量的自动统计