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)

最新文章

  1. ***CodeIgniter集成微信支付(转)
  2. iOS 中对各种视图的截屏以及分享
  3. 机器学习 —— 概率图模型(Homework: Representation)
  4. SpringJUnit4加载类目录下(src)和WEF-INF目录下的配置文件
  5. PLSQL Developer调试 存储过程和触发器
  6. sql 语句总结
  7. 线程 (detach的作用)
  8. 【开源】NodeJS仿WebApi路由
  9. 弹性盒布局display:flex详解
  10. Java就业企业面试问题-ssh框架
  11. 深透清晰理解Java高并发概述
  12. 《前端之路》之 JavaScript 进阶技巧之高阶函数(下)
  13. LeetCode 34 - 在排序数组中查找元素的第一个和最后一个位置 - [二分][lower_bound和upper_bound]
  14. 细说SpringDI Setter注入
  15. IDEA如何导入一个web+maven以及如何运行项目
  16. WPF实战案例-MVVM模式下在Xaml中弹出窗体
  17. ispoweroftwo 判断2的次幂【转】
  18. Linux Redis安装,Linux如何安装Redis,Linux Redis自动启动,Redis开机启动
  19. 应用jfinal时要注意区分Db.query和Db.find
  20. Django-自动HTML转义

热门文章

  1. pgloader 学习(二)特性矩阵&amp;&amp;命令行
  2. C语言博客作业00--我的第一篇博客
  3. GoCN每日新闻(2019-10-21)
  4. Css3美化【让你的网页独一无二!】
  5. python设计模式---绪论
  6. D3.js的v5版本入门教程(第三章)—— 选择元素和绑定数据
  7. Pygame 贪吃蛇
  8. better-scroll在vue项目中的使用
  9. tensorflow build failed on Centos with Error: suffix or operands invalid for &quot;&quot;
  10. Spark(五十三):Spark RPC初尝试使用