美文网首页
nginx日志时间段检测

nginx日志时间段检测

作者: 木火应 | 来源:发表于2022-05-31 16:44 被阅读0次
  • 为避免从头到尾读取文件,这里使用pythonfile_read_backwards模块从尾部读取文件

    from file_read_backwards import FileReadBackwards
    with FileReadBackwards(filepath) as f:
        ...
    
  • nginx日志每行长这样:

    25/May/2022:14:04:54 +0800    200 0.000   10425   -   -   36.43.97.178    -   GET caiyun.feixin.10086.cn:7071 /test/api/public/js/chunk123.js Mozilla/5.0 (Linux; Android 9; V1901A Build/P00610; wv) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/79.0.3999.119 KsWebView/1.6.79.249 Mobile Safari/537.36 Yoda/2.5.6-rc4 StatusHT/28 CV/null Kwai/9.7.20.21207 OS_PRO_BIT/64 MAX_PHY_MEM/3727 AZPREFIX/yz ICFO/0 TitleHT/50 AllowKsCallApp NetType/WIFI ISLP/0 ISDM/0 ISLB/0 locale/zh-cn CT/0  gzip, deflate   https://127.0.0.1:8000/portal/caiyunMcloudActive/index.html?path=shanXiShare&WT.mc_id=xstHIRmkil2i3okWwCHw&callback=kabykE-6t4UPfZfdbD-mk3ufcZOz0gQEOluZ06RXrUGhie0M7WOca94TLty5taCCUz12GlfTJvWB70Ww8R_EguyDQ8pIA3siwCyR8qJV93hlSf712a1RsiiMVEo0nDNvqMg6ArzAQLT8o-zly6GsbA%3D%3D    -   User_request_status:200 -
    
  • 需要通过第一列25/May/2022:14:04:54进行过滤,使用python datetime模块格式化第一列并输出为datetime对象

    line = line.strip()
    arr = line.split()
    firsthead = arr[0]
    hour = datetime.strptime(firsthead, '%d/%b/%Y:%H:%M:%S')
    
  • 完整代码为:

      def read_access_log(self,filepath="access.log"):
          self.CurrentHourInit = datetime.now()
          self.LastHourInit = self.CurrentHourInit - timedelta(hours=1)
          self.LastHourInit = self.LastHourInit.strftime("%Y-%m-%d %H:00:00")
          self.CurrentHourInit = self.CurrentHourInit.strftime("%Y-%m-%d %H:00:00")
          with FileReadBackwards(filepath) as f:
              line = f.readline()
              while line:
                  line = line.strip()
                  arr = line.split()
                  # 过滤上一个小时整点时间段的数据
                  firsthead = arr[0]
                  hour = datetime.strptime(firsthead, '%d/%b/%Y:%H:%M:%S')
                  self.LastHour = datetime.strptime(self.LastHourInit, "%Y-%m-%d %H:%M:%S")
                  self.CurrentHour = datetime.strptime(self.CurrentHourInit,
                                                       "%Y-%m-%d %H:%M:%S")
                  # break
                  if hour <self.LastHour:
                      break
                  # continue
                  if hour==self.CurrentHour:
                      line = f.readline()
                      continue
                  result = (float(arr[3])*1000, arr[11])
                  yield result
                  line = f.readline()
              f.close()
    

相关文章

网友评论

      本文标题:nginx日志时间段检测

      本文链接:https://www.haomeiwen.com/subject/gogiprtx.html