Created
August 14, 2011 21:31
-
-
Save kevinquinnyo/1145334 to your computer and use it in GitHub Desktop.
isolate time range from apache log file(s)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/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 |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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...