from CoreGraphics import *
import math
import string
import sys, os
import re
def parselog(inFile):
f = open(inFile)
hunt = 'getTime'
ipList = {}
querySource = {}
plotPoints = []
maxTime=0
minTime = 36*60*60
spaceExp = re.compile(r'\s+')
print "Reading " + inFile
while 1:
lines = f.readlines(100000)
if not lines:
break
for line in lines:
if (hunt == 'skip'):
if (line == '\n' or line == '\r' or line ==''):
hunt = 'getTime'
elif (hunt == 'getTime'):
if (line == "^C\n" ):
break
time = line.split(' ')[0].split(':')
if (len(time)<3):
hunt = 'skip'
else:
hunt = 'getIP'
elif (hunt == 'getIP'):
ip = line.split(' ',1)
ip = ip[0]
secs=0
for t in time:
secs = secs*60 +float(t)
if (secs>maxTime):
maxTime=secs
if (secs<minTime):
minTime=secs
if (not ip in ipList):
ipList[ip] = [len(ipList), "", ""]
hunt = 'getQA'
elif (hunt == 'getQA'):
qaList = spaceExp.split(line)
if (qaList[0] == ip):
if (qaList[1] == '(QU)' or qaList[1] == '(LQ)' or qaList[1] == '(PU)'):
plotPoints.append([secs, ipList[ip][0], (qaList[1])[1:-1]])
elif (qaList[1] == '(QM)'):
plotPoints.append([secs, ipList[ip][0], (qaList[1])[1:-1]])
querySource[qaList[3]] = len(plotPoints)-1
elif (qaList[1] == '(PM)'):
plotPoints.append([secs, ipList[ip][0], (qaList[1])[1:-1]])
querySource[qaList[4]] = len(plotPoints)-1
elif (qaList[1] == '(AN)' or qaList[1] == '(AN+)' or qaList[1] == '(DE)'):
plotPoints.append([secs, ipList[ip][0], (qaList[1])[1:-1]])
try:
theQuery = querySource[qaList[4]]
theDelta = secs - plotPoints[theQuery][0]
if (theDelta < 1.0):
plotPoints[-1].append(querySource[qaList[4]])
except:
pass
elif (qaList[1] != '(KA)' and qaList[1] != '(AD)' and qaList[1] != '(AD+)'):
print "Operation unknown", qaList
if (qaList[1] == '(AN)' or qaList[1] == '(AN+)' or qaList[1] == '(AD)' or qaList[1] == '(AD+)'):
if (qaList[2] == 'HINFO'):
ipList[ip][1] = qaList[4]
ipList[ip][2] = string.join(qaList[6:])
elif (qaList[2] == 'AAAA'):
if (ipList[ip][1] == ""):
ipList[ip][1] = qaList[4]
ipList[ip][2] = "Panther"
elif (qaList[2] == 'Addr'):
if (ipList[ip][1] == ""):
ipList[ip][1] = qaList[4]
ipList[ip][2] = "Jaguar"
else:
if (line == '\n'):
hunt = 'getTime'
else:
hunt = 'skip'
f.close()
if (maxTime < minTime + 10.0):
maxTime = minTime + 10.0
typesize = 12
width=20.0*(maxTime-minTime)
pageHeight=(len(ipList)+1) * typesize
scale = width/(maxTime-minTime)
leftMargin = typesize * 60
bottomMargin = typesize
pageRect = CGRectMake (-leftMargin, -bottomMargin, leftMargin + width, bottomMargin + pageHeight) outFile = "%s.pdf" % (".".join(inFile.split('.')[:-1]))
c = CGPDFContextCreateWithFilename (outFile, pageRect)
print "Writing " + outFile
ourColourSpace = c.getColorSpace()
colourLookup = {"L":(0.0,0.0,1.0), "Q":(1.0,0.0,0.0), "P":(1.0,0.8,0.0), "A":(0.0,1.0,0.0), "D":(0.0,1.0,0.0), "?":(1.0,1.0,1.0)}
c.beginPage (pageRect)
c.setRGBFillColor(.75,0.0,0.0,1.0)
c.setRGBStrokeColor(.25,0.75,0.25,1.0)
c.setLineWidth(0.25)
for point in plotPoints:
c.addArc((point[0]-minTime)*scale,point[1]*typesize+6,typesize/4,0,2*math.pi,1)
theColour = colourLookup[(point[2])[0]]
if (((point[2])[0]) != "L") and (((point[2])[0]) != "Q") and (((point[2])[0]) != "P") and (((point[2])[0]) != "A") and (((point[2])[0]) != "D"):
print "Unknown", point
if ((point[2])[-1] == "M" or (point[2])[0]== "A"):
c.setRGBFillColor(theColour[0],theColour[1],theColour[2],.5)
c.fillPath()
else:
c.setRGBStrokeColor(theColour[0],theColour[1],theColour[2],.5)
c.setLineWidth(1.0)
c.strokePath()
c.setRGBStrokeColor(.25,0.75,0.25,1.0)
c.setLineWidth(0.25)
for index in point[3:]:
c.beginPath()
c.moveToPoint((point[0]-minTime)*scale,point[1]*typesize+6)
c.addLineToPoint(((plotPoints[index])[0]-minTime)*scale,(plotPoints[index])[1]*typesize+6)
c.closePath()
c.strokePath()
c.setRGBFillColor (0,0,0, 1)
c.setTextDrawingMode (kCGTextFill)
c.setTextMatrix (CGAffineTransformIdentity)
c.selectFont ('Gill Sans', typesize, kCGEncodingMacRoman)
c.setRGBStrokeColor(0.25,0.0,0.0,1.0)
c.setLineWidth(0.1)
for ip,[height,hname,hinfo] in ipList.items():
c.beginPath()
c.moveToPoint(pageRect.origin.x,height*typesize+6)
c.addLineToPoint(width,height*typesize+6)
c.closePath()
c.strokePath()
c.showTextAtPoint(pageRect.origin.x + 2, height*typesize + 2, ip, len(ip))
c.showTextAtPoint(pageRect.origin.x + 2 + typesize*8, height*typesize + 2, hname, len(hname))
c.showTextAtPoint(pageRect.origin.x + 2 + typesize*25, height*typesize + 2, hinfo, len(hinfo))
for time in range (int(minTime),int(maxTime)+1):
c.beginPath()
c.moveToPoint((time-minTime)*scale,pageRect.origin.y)
c.addLineToPoint((time-minTime)*scale,pageHeight)
c.closePath()
if (int(time) % 10 == 0):
theHours = time/3600
theMinutes = time/60 % 60
theSeconds = time % 60
theTimeString = '%d:%02d:%02d' % (theHours, theMinutes, theSeconds)
theStringWidth = typesize * 3.5
c.showTextAtPoint((time-minTime)*scale - theStringWidth/2, pageRect.origin.y + 2, theTimeString, len(theTimeString))
c.setLineWidth(0.3)
else:
c.setLineWidth(0.1)
c.strokePath()
c.endPage()
c.finish()
for arg in sys.argv[1:]:
parselog(arg)