当前位置:网站首页>将 APACHE 日志解析到 SQL 数据库中
将 APACHE 日志解析到 SQL 数据库中
2022-07-30 16:48:00 【allway2】
背景
将 Apache HTTPD 访问日志解析到数据库结构中,您可以轻松地针对它运行查询和报告,以更好地了解 Web 流量并检测问题。通过使用 pip 可安装的 apache_log_parser [ 1 ]包和 sqlite3 python 模块[ 2 ]作为标准 python 库的一部分,我们可以快速解析这些访问日志并将条目插入到 sqlite3 数据库中。
方法说明
关于代码中采用的方法需要注意以下几点:
- 选择 sqlite3 是因为它的原生支持和易用性,但其他 DBMS 也可以类似使用
- 模式中不包含自动递增的 id 列,因为 sqlite3 不推荐这样的列,除非必要[ 4 ]。您可能希望为其他数据库系统添加一个
- apache_log_parser 生成 apache 日志条目部分的字典。鉴于此,可用的字典键取决于提供给 make_parser() 函数的 access.log 的日志模式
- 代码片段中的列只是解析器返回的全部键/值的一个子集。您可以通过调用 pprint() 轻松查看从给定行生成的所有键和值
- 在代码中添加了一个额外的“日期”键和值(以 sqlite 日期函数友好的方式),以允许基于日期的查询和标准
代码
#!/usr/bin/env python3importsysimportsqlite3importapache_log_parseriflen(sys.argv) !=2: print("Usage:", sys.argv[0], "/path/to/access.log") exit(1)conn =sqlite3.connect('/tmp/logs.db')cur =conn.cursor()cur.execute(""" CREATE TABLE IF NOT EXISTS logs ( status INTEGER, request_method TEXT, request_url TEXT, date TEXT ) """)# Pattern below is from the LogFormat setting in apache2.conf/httpd.conf file# You will likely need to change this value to the pattern your system usesparser =apache_log_parser.make_parser( "%h %l %u %t \"%r\" %>s %O \"%{Referer}i\" \"%{User-Agent}i\"" )log_file =sys.argv[1]with open(log_file) as f: forline inf: d =parser(line) # Line below adds minimalistic date stamp column # in format that sqlite3 date functions can work with d['date'] =d['time_received_datetimeobj'].date().isoformat() cur.execute(""" INSERT INTO logs ( status, request_method, request_url, date) VALUES (:status, :request_method, :request_url, :date) """, d)cur.close()conn.commit();conn.close(); |
示例运行
示例 access.log 条目:
# cat /var/log/apache2/access.log::1 - - [25/Nov/2018:20:13:28 -0500] "GET /index.html HTTP/1.1" 200 3380 "-" "User agent string"::1 - - [25/Nov/2018:12:15:26 -0500] "GET /notthere HTTP/1.1" 404 498 "-" "User agent string" |
运行上面的代码:
$ ./httpd_log_parser.py /var/log/apache2/access.log |
检查结果:
$ sqlite3 -header -column /tmp/logs.db 'SELECT * FROM logs'status request_method request_url date ---------- -------------- ----------- ----------200 GET /index.html 2018-11-25404 GET /notthere 2018-11-25 |
示例查询
加载日志条目后,您可以开始对它们运行有用的查询。
- 按响应状态显示请求数:
SELECTcount(1)asnReqs,statusFROMlogsGROUPBY(status)ORDERBYnReqsDESC;nReqs status---------- ----------4 2002 404 - 什么是最常见的 404 网址:
SELECTcount(1)asnReqs,request_urlFROMlogsWHEREstatus = 404GROUPBY(request_url)ORDERBYnReqsDESC;nReqs request_url---------- -----------2 /notthere
最后的想法
上述方法在日志文件被 apache 进程写出后对其进行处理。这可能特别有用,因为可以将日志文件卸载到不同的系统并使用您选择的数据库进行分析,而不会影响网络服务器的性能。这种方法的替代方法是使用类似mod_log_sql(又名libapache2-mod-log-sql)的东西,这是一个专门构建的 apache 模块,可在请求时将请求直接记录到 MySQL DB。任何一种选择都需要权衡取舍,但希望所提供的信息能让您在旅途中抢占先机。保重,祝你好运!
参考
- GitHub - rory/apache-log-parser - GitHub - amandasaurus/apache-log-parser: Parses log lines from an apache log
- [Python] sqlite3 — SQLite 数据库的 DB-API 2.0 接口 - sqlite3 — DB-API 2.0 interface for SQLite databases — Python 3.10.5 documentation
- [SQLite] 日期和时间函数 - Date And Time Functions
- SQLite 自动增量 - SQLite Autoincrement
边栏推荐
- 万华化学精细化工创新产品大会
- MySQL索引常见面试题(2022版)
- 微信小程序picker滚动选择器使用详解
- 数据的存储
- [NCTF2019]Fake XML cookbook-1|XXE漏洞|XXE信息介绍
- Security business revenue growth rate exceeds 70% 360 builds digital security leader
- 哎,这要人老命的缓存一致问题啊
- 阿里巴巴中国站获得1688商品分类 API
- PHP message feedback management system source code
- Public Key Retrieval is not allowed报错解决方案
猜你喜欢

华为云数据治理生产线DataArts,让“数据‘慧’说话”

MySQL 8.0.29 解压版安装教程(亲测有效)

登录模块调试-软件调试入门
![[TypeScript]简介、开发环境搭建、基本类型](/img/d7/b3175ab538906ac1b658a9f361ba44.png)
[TypeScript]简介、开发环境搭建、基本类型
![[MRCTF2020]Ezaudit](/img/80/d4656abdff20703591ffdc3f5a5ebc.png)
[MRCTF2020]Ezaudit

PHP message feedback management system source code

哎,这要人老命的缓存一致问题啊

Nervegrowold d2l (7) kaggle housing forecast model, numerical stability and the initialization and activation function

23. Please talk about the difference between IO synchronization, asynchronous, blocking and non-blocking

DTSE Tech Talk丨第2期:1小时深度解读SaaS应用系统设计
随机推荐
mysql进制安装与mysql密码破解
bert-base调试心得
论文阅读 (63):Get To The Point: Summarization with Pointer-Generator Networks
arcpy tutorial
What does a good resume look like in the eyes of a big factory interviewer?
Navisworks切换语言
如何在 UE4 中用代码去控制角色移动
(一)云计算技术学习--虚拟化vSphere学习
DTSE Tech Talk丨第2期:1小时深度解读SaaS应用系统设计
3D激光SLAM:LeGO-LOAM论文解读---系统概述部分
HUAWEI CLOUD data governance production line DataArts, let "data 'wisdom' speak"
Scheduling_Channel_Access_Based_on_Target_Wake_Time_Mechanism_in_802.11ax_WLANs
支付系统架构设计详解,精彩!
数据库的三大范式
Leetcode 119. 杨辉三角 II
JVM学习----垃圾回收
如何写一份高可读性的软件工程设计文档
Chapter 6: Decisive Autumn Moves
Mysql进阶优化篇01——四万字详解数据库性能分析工具(深入、全面、详细,收藏备用)
3D激光SLAM:LeGO-LOAM论文解读---激光雷达里程计与建图