nginx日志的python分析脚本
2022-09-28 19:17 第二个卿老师 阅读(458) 评论(0) 编辑 收藏 举报因为容量场景需要统计峰值时间段的业务分布情况,即是统计每个接口请求以及对应的数量,这里记录下之前的实现脚本。
nginx的日志格式如下:
代码如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 | import os,json import pandas as pd import datetime ''' 全局参数 ''' # 日志文件存放目录 logDir = r "C:\Users\qgc\Desktop" # 源日志文件,3天汇总 logFile = r "C:\Users\qgc\Desktop\nginx.log" # 清洗完的文件绝对路径 date = datetime.datetime.strftime(datetime.datetime.now(), '%H%M' ) print (date) resultName = logDir + "\\result-" + date + ".xlsx" print ( '开始数据预处理' ) # 定义过滤函数,过滤掉无效uri filt = [ '.js' , 'css' , 'images' , 'static' ] filt2 = [ '/deme/' ] def filter_invalid_str(s,filt): if s.isdigit() or s = = '/' or s = = '/null' : return 0 for i in filt: if i in s: return 0 for j in filt2: if j not in s: return 0 else : return 1 print ( '开始数据分析' ) # 统计各个uri的访问量,算出日业务量,过滤掉请求次数为0的uri def uri_statistics(time): result = {} count = 0 with open (logFile, 'r' , encoding = "utf-8" ) as fr: for i in fr: line = i.split() if time in line[ 3 ]: k = line[ 6 ].split( "?" )[ 0 ] if filter_invalid_str(k,filt): count + = 1 if k not in result.keys(): result[k] = 1 elif k in result.keys(): result[k] + = 1 else : print ( "%s 存入字典时,key没有找到!" % k) print (f "共分析{count}条数据,找到{len(result)}接口数据" ) if len (result) > 0 : # 将清洗完的接口统计数据写入目标文件 if os.path.exists(resultName): os.remove(resultName) pandas_to_excel(resultName, result) else : print ( "未生成txt数据" ) def pandas_to_excel(f, data): file_path = pd.ExcelWriter(f) try : df = pd.DataFrame(pd.Series(data), columns = [ 'total' ]) df = df.reset_index().rename(columns = { 'index' : 'apiUri' }) df.to_excel(file_path, encoding = 'utf-8' ) file_path.save() except Exception as e: print ( "Error:" , e) print ( "excel处理数据成功" ) if __name__ = = '__main__' : time = "13/Jul/2022:09" / / 自定义日期时间,这里是指定 13 日 9 点的一小时数据。 uri_statistics(time) |
代码运行结果如下:
上面这个表格,可以用excel自带的计算公式计算下百分比,就可以得到业务的分布比例。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· C#/.NET/.NET Core优秀项目和框架2025年2月简报
· Manus爆火,是硬核还是营销?
· 终于写完轮子一部分:tcp代理 了,记录一下
· 【杭电多校比赛记录】2025“钉耙编程”中国大学生算法设计春季联赛(1)
2017-09-28 面试如何谈笑风生,软件测试基础理论整理