Skip to content

Instantly share code, notes, and snippets.

@kevinquinnyo
Created August 14, 2011 21:31
Show Gist options
  • Select an option

  • Save kevinquinnyo/1145334 to your computer and use it in GitHub Desktop.

Select an option

Save kevinquinnyo/1145334 to your computer and use it in GitHub Desktop.
isolate time range from apache log file(s)
#!/usr/bin/python
import sys
import re
import time
from datetime import timedelta, datetime
now = datetime.now()
d_format = '%d/%b/%Y:%H:%M'
try:
back_minutes = timedelta(minutes=int(sys.argv[2])) #no more trash var
raw_bad_process_time = now.strftime('%d/%b/%Y:') + sys.argv[1] #gets d/m/y
bad_process_time = datetime.strptime(raw_bad_process_time, d_format)
back_time = bad_process_time - back_minutes
except (TypeError, IndexError):
sys.stderr.write("Example: cat apache.log | badproc.py 00:02 7 \n")
for line in sys.stdin:
m = re.search('[0-9]+\/\w+\/[0-9]+:[0-9]+:[0-9]+', line)
if m:
the_match = m.group()
try:
date_in_log = datetime.strptime(the_match.split()[0], d_format)
if back_time <= date_in_log <= bad_process_time:
sys.stdout.write(line)
except (ValueError, IndexError):
pass
@kevinquinnyo

Copy link
Copy Markdown
Author

Usage:
badproc.py
badproc.py 19:38 3

This uses python's higher level date processing to avoid the headache when the hour rolls over/month changes/etc...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment