Skip to content

Instantly share code, notes, and snippets.

@meeuw
Created October 31, 2014 20:05
Show Gist options
  • Save meeuw/75f5b72d7f98d1014c82 to your computer and use it in GitHub Desktop.
Save meeuw/75f5b72d7f98d1014c82 to your computer and use it in GitHub Desktop.
match error_log and access_log tailing
import tailer
import time
import datetime
import re
dt = datetime.timedelta(0, 1)
with file('/var/log/httpd/error_log') as error_log:
with file('/var/log/httpd/access_log') as access_log:
tailer_error_log = tailer.follow(error_log, 0)
tailer_access_log = tailer.follow(access_log, 0)
error_log_queue = []
access_log_queue = []
while 1:
nothing = True
line = tailer_error_log.next()
if line:
m = re.match(r'\[(.{24})\] \[([^\]]*)\] \[client ([^\]]*)', line)
error_log_queue.append((datetime.datetime.strptime(m.group(1), "%a %b %d %H:%M:%S %Y"), m.group(3), line))
nothing = False
line = tailer_access_log.next()
if line:
m = re.match(r'([^ ]*) - ([^ ]*) \[(.{20})', line)
if not m: print line
access_log_queue.append((datetime.datetime.strptime(m.group(3), "%d/%b/%Y:%H:%M:%S"), m.group(1), line))
if len(access_log_queue) > 50: del access_log_queue[0]
nothing = False
if nothing:
time.sleep(1)
continue
if error_log_queue:
error_log_s = error_log_queue[0]
found = False
for access_log_s in access_log_queue:
if error_log_s[1] == access_log_s[1] and \
error_log_s[0] >= access_log_s[0]-dt and \
error_log_s[0] <= access_log_s[0]+dt:
print access_log_s[2]
found = True
if found:
print error_log_s[2]
print '---'
del error_log_queue[0]
else:
print 'cannot find match request for'
print error_log_queue[0]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment