nginx acces.log日志分析
2024-10-21 09:54:58
1,统计各访问IP的总数
awk '{if($9>0 && $9==200 && substr($6,2)== "GET") a[$1]++}END{for(i in a)print i,a[i]}' access.log|sort -t ' ' -k2 -rn|head -n 10
2,统计包含xx字符的总数
cat access.log | grep 'GET /adsview/cqgd/img/tan/cq_320.png' | grep '10/Jun/2019:15' -c
3,查看实时包含xx字符的数据
tail -f access.log | grep 'cq_mb.html'
4,使用python获取日志,并保存到MongoDB进行分析
执行命令:
python logPy.py ./access.log
logPy.py
import re
import sys
import argparse
from collections import Counter
import pymongo
parser = argparse.ArgumentParser(description='python for access.log')
parser.add_argument('log_file', metavar='LOG_FILE', type=argparse.FileType('r'),
help='Path to the Apache log file')
# Regex for the common Apache log format.
parts = [
r'(?P<host>\S+)', # host %h
r'\S+', # indent %l (unused)
r'(?P<user>\S+)', # user %u
r'\[(?P<time>.+)\]', # time %t
r'"(?P<request>.*)"', # request "%r"
r'(?P<status>[0-9]+)', # status %>s
r'(?P<size>\S+)', # size %b (careful, can be '-')
r'"(?P<referrer>.*)"', # referrer "%{Referer}i"
r'"(?P<agent>.*)"', # user agent "%{User-agent}i"
]
pattern = re.compile(r'\s+'.join(parts)+r'\s*\Z')
# Initiazlie required variables
args = parser.parse_args()
log_data = [] # Get components from each line of the log file into a structured dict
for line in args.log_file:
if pattern.match(line):
log_data.append(pattern.match(line).groupdict())
client = pymongo.MongoClient('localhost')
db = client['access']
db['cq_ads'].insert_many(log_data) # Using a counter to get stats on the status in log entries
# Refer = http://docs.python.org/2/library/collections.html#collections.Counter
# status_counter = Counter(x['status'] for x in log_data)
# Printing the STATUS count sorted by highest to lowest count
# print ("Most common STATUSes in the Apache log file %s are:" % args.log_file.name)
# for x in status_counter.most_common():
# print ("\t%s Status %d times" % x)
最新文章
- ***CodeIgniter集成微信支付(转)
- iOS 中对各种视图的截屏以及分享
- 机器学习 —— 概率图模型(Homework: Representation)
- SpringJUnit4加载类目录下(src)和WEF-INF目录下的配置文件
- PLSQL Developer调试 存储过程和触发器
- sql 语句总结
- 线程 (detach的作用)
- 【开源】NodeJS仿WebApi路由
- 弹性盒布局display:flex详解
- Java就业企业面试问题-ssh框架
- 深透清晰理解Java高并发概述
- 《前端之路》之 JavaScript 进阶技巧之高阶函数(下)
- LeetCode 34 - 在排序数组中查找元素的第一个和最后一个位置 - [二分][lower_bound和upper_bound]
- 细说SpringDI Setter注入
- IDEA如何导入一个web+maven以及如何运行项目
- WPF实战案例-MVVM模式下在Xaml中弹出窗体
- ispoweroftwo 判断2的次幂【转】
- Linux Redis安装,Linux如何安装Redis,Linux Redis自动启动,Redis开机启动
- 应用jfinal时要注意区分Db.query和Db.find
- Django-自动HTML转义
热门文章
- pgloader 学习(二)特性矩阵&;&;命令行
- C语言博客作业00--我的第一篇博客
- GoCN每日新闻(2019-10-21)
- Css3美化【让你的网页独一无二!】
- python设计模式---绪论
- D3.js的v5版本入门教程(第三章)—— 选择元素和绑定数据
- Pygame 贪吃蛇
- better-scroll在vue项目中的使用
- tensorflow build failed on Centos with Error: suffix or operands invalid for ";";
- Spark(五十三):Spark RPC初尝试使用