【问题标题】:python Script to parse server log between two different timestampspython脚本在两个不同的时间戳之间解析服务器日志
【发布时间】:2013-08-31 10:53:40
【问题描述】:

我想解析服务器访问日志以查找 4xx 和 5xx 错误计数。但未能在两个时间戳之间获取数据。某处出现逻辑错误。

时间戳形成日期/月/年/小时/分钟/秒:27/Aug/2013:12:45:54

if time >= start_timestamp and  time <= end_timestamp : 
      if check_point_flag == 0  :
      start_time = request['request'].split(" ")[0].split('[')[1]
      check_point_flag=1
      total_patient_count, total_encounter_count = self.get_count_information( access_token, request , resource_name, total_patient_count, total_encounter_count)
      count_patient_5xx ,count_encounter_5xx = self.get_count_information(access_token,request,resource_name,count_patient_5xx,count_encounter_5xx, '500', '600')
      count_patient_4xx ,count_encounter_4xx = self.get_count_information(access_token,request, resource_name,count_patient_4xx,count_encounter_4xx, '400', '500')             
      count_patient_2xx ,count_encounter_2xx = self.get_count_information(access_token,request, resource_name,count_patient_2xx,count_encounter_2xx, '200', '300')             
      end_time = request['request'].split(" ")[0].split('[')[1]

这是更改为 groupdict() 后的一些日志

{'status': '404', 'protocol': 'HTTP/1.0', 'request_time': '0.000', 'mongo_exec_time': '-', 'url': '/ready', 'request': '[19/Aug/2013:16:31:20 -0400]', 'hostname': 'xxxxxx', 'bytes_sent': '168', 'audit_response_time': '-', 'application': '-', 'user_agent': '-', 'upstream_response_time': '-', 'queries_count': '-', 'clientip': '12.255.25.13', 'hma_exec_time': '-', 'method': 'GET', 'user': '-'}
{'status': '404', 'protocol': 'HTTP/1.0', 'request_time': '0.000', 'mongo_exec_time': '-', 'url': '/ready', 'request': '[19/Aug/2013:16:31:22 -0400]', 'hostname': ''xxxxxx', 'bytes_sent': '168', 'audit_response_time': '-', 'application': '-', 'user_agent': '-', 'upstream_response_time': '-', 'queries_count': '-', 'clientip': '13.255.25.13', 'hma_exec_time': '-', 'method': 'GET', 'user': '-'}
{'status': '404', 'protocol': 'HTTP/1.0', 'request_time': '0.000', 'mongo_exec_time': '-', 'url': '/ready', 'request': '[19/Aug/2013:16:31:52 -0400]', 'hostname': ''xxxxxx', 'bytes_sent': '168', 'audit_response_time': '-', 'application': '-', 'user_agent': '-', 'upstream_response_time': '-', 'queries_count': '-', 'clientip': '13.255.25.13', 'hma_exec_time': '-', 'method': 'GET', 'user': '-'}
{'status': '404', 'protocol': 'HTTP/1.0', 'request_time': '0.000', 'mongo_exec_time': '-', 'url': '/ready', 'request': '[19/Aug/2013:16:31:54 -0400]', 'hostname': ''xxxxxx', 'bytes_sent': '168', 'audit_response_time': '-', 'application': '-', 'user_agent': '-', 'upstream_response_time': '-', 'queries_count': '-', 'clientip': '13.255.25.13', 'hma_exec_time': '-', 'method': 'GET', 'user': '-'}
{'status': '200', 'protocol': 'HTTP/1.1', 'request_time': '0.000', 'mongo_exec_time': '-', 'url': '//nginx_stub_status', 'request': '[19/Aug/2013:16:31:55 -0400]', 'hostname': ''xxxxxx', 'bytes_sent': '109', 'audit_response_time': '-', 'application': '-', 'user_agent': 'python-requests/1.2.3 CPython/2.6.6 Linux/2.6.32-358.14.1.el6.x86_64', 'upstream_response_time': '-', 'queries_count': '-', 'clientip': '127.0.0.1', 'hma_exec_time': '-', 'method': 'GET', 'user': '-'}

请帮助找到在两个时间戳之间解析文件的逻辑

【问题讨论】:

  • 请更具体。显示一些日志记录以及您想要的输出。
  • 我添加了日志示例。请检查

标签: python python-2.7 timestamp logparser


【解决方案1】:

如果我正确理解了您的问题,您正试图找到一种基于时间戳过滤日志消息的方法。你可以使用the datetime module:

import datetime
time = datetime.datetime.strptime('27/Aug/2013:12:45:54', '%d/%b/%Y:%H:%M:%S')
start_timestamp = datetime.datetime.strptime('20/Aug/2013:12:45:54', '%d/%b/%Y:%H:%M:%S')
end_timestamp = datetime.datetime.strptime('30/Aug/2013:12:45:54', '%d/%b/%Y:%H:%M:%S')

if time >= start_timestamp and  time <= end_timestamp: print 'it worked'

【讨论】:

  • 非常感谢。我实际上是在写我自己的逻辑而不是这个,我遇到了问题
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2020-08-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-05-21
  • 2019-09-15
相关资源
最近更新 更多