mirror of https://github.com/fail2ban/fail2ban
- The optimization in log reading should work
git-svn-id: https://fail2ban.svn.sourceforge.net/svnroot/fail2ban/trunk@92 a942ae1a-1317-0410-a47c-b1dcaea8d6050.6
parent
c442955e30
commit
7f62f9df89
|
@ -34,7 +34,8 @@ class LogReader:
|
||||||
attempt.
|
attempt.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
def __init__(self, logSys, logPath, timeregex, timepattern, failregex, findTime = 3600):
|
def __init__(self, logSys, logPath, timeregex, timepattern, failregex,
|
||||||
|
findTime = 3600):
|
||||||
self.logPath = logPath
|
self.logPath = logPath
|
||||||
self.timeregex = timeregex
|
self.timeregex = timeregex
|
||||||
self.timepattern = timepattern
|
self.timepattern = timepattern
|
||||||
|
@ -44,7 +45,7 @@ class LogReader:
|
||||||
self.lastModTime = 0
|
self.lastModTime = 0
|
||||||
self.logSys = logSys
|
self.logSys = logSys
|
||||||
self.lastPos = 0
|
self.lastPos = 0
|
||||||
self.lastSize = 0
|
self.lastDate = 0
|
||||||
self.logStats = None
|
self.logStats = None
|
||||||
|
|
||||||
def setName(self, name):
|
def setName(self, name):
|
||||||
|
@ -57,6 +58,11 @@ class LogReader:
|
||||||
"""
|
"""
|
||||||
return self.name
|
return self.name
|
||||||
|
|
||||||
|
def getFindTime(self):
|
||||||
|
""" Gets the find time.
|
||||||
|
"""
|
||||||
|
return self.findTime
|
||||||
|
|
||||||
def addIgnoreIP(self, ip):
|
def addIgnoreIP(self, ip):
|
||||||
""" Adds an IP to the ignore list.
|
""" Adds an IP to the ignore list.
|
||||||
"""
|
"""
|
||||||
|
@ -96,16 +102,16 @@ class LogReader:
|
||||||
|
|
||||||
def setFilePos(self, file):
|
def setFilePos(self, file):
|
||||||
""" Sets the file position. We must take care of log file rotation
|
""" Sets the file position. We must take care of log file rotation
|
||||||
and reset the position to 0 in that case. Use the file size in
|
and reset the position to 0 in that case. Use the log message
|
||||||
order to detect this. Not the best solution yet.
|
timestamp in order to detect this.
|
||||||
"""
|
"""
|
||||||
if self.lastSize > self.logStats.st_size:
|
line = file.readline()
|
||||||
self.logSys.debug("Size " + `self.logStats.st_size` + " is " +
|
if self.lastDate < self.getTime(line):
|
||||||
"smaller than " + `self.lastSize`)
|
self.logSys.debug("Date " + `self.lastDate` + " is " +
|
||||||
|
"smaller than " + `self.getTime(line)`)
|
||||||
self.logSys.debug("Log rotation detected")
|
self.logSys.debug("Log rotation detected")
|
||||||
self.lastPos = 0
|
self.lastPos = 0
|
||||||
|
|
||||||
self.lastSize = self.logStats.st_size
|
|
||||||
self.logSys.debug("Setting file position to " + `self.lastPos`)
|
self.logSys.debug("Setting file position to " + `self.lastPos`)
|
||||||
file.seek(self.lastPos)
|
file.seek(self.lastPos)
|
||||||
|
|
||||||
|
@ -118,8 +124,10 @@ class LogReader:
|
||||||
"""
|
"""
|
||||||
ipList = dict()
|
ipList = dict()
|
||||||
logFile = self.openLogFile()
|
logFile = self.openLogFile()
|
||||||
#self.setFilePos(logFile)
|
self.setFilePos(logFile)
|
||||||
|
lastLine = ''
|
||||||
for line in logFile.readlines():
|
for line in logFile.readlines():
|
||||||
|
lastLine = line
|
||||||
failList = self.findFailure(line)
|
failList = self.findFailure(line)
|
||||||
for element in failList:
|
for element in failList:
|
||||||
ip = element[0]
|
ip = element[0]
|
||||||
|
@ -134,7 +142,8 @@ class LogReader:
|
||||||
ipList[ip] = (ipList[ip][0]+1, unixTime)
|
ipList[ip] = (ipList[ip][0]+1, unixTime)
|
||||||
else:
|
else:
|
||||||
ipList[ip] = (1, unixTime)
|
ipList[ip] = (1, unixTime)
|
||||||
#self.lastPos = logFile.tell()
|
self.lastPos = logFile.tell()
|
||||||
|
self.lastDate = self.getTime(lastLine)
|
||||||
logFile.close()
|
logFile.close()
|
||||||
return ipList
|
return ipList
|
||||||
|
|
||||||
|
@ -157,6 +166,15 @@ class LogReader:
|
||||||
failList.append([ip, date])
|
failList.append([ip, date])
|
||||||
return failList
|
return failList
|
||||||
|
|
||||||
|
def getTime(self, line):
|
||||||
|
""" Gets the time of a log message.
|
||||||
|
"""
|
||||||
|
date = 0
|
||||||
|
timeMatch = re.search(self.timeregex, line)
|
||||||
|
if timeMatch:
|
||||||
|
date = self.getUnixTime(timeMatch.group())
|
||||||
|
return date
|
||||||
|
|
||||||
def getUnixTime(self, value):
|
def getUnixTime(self, value):
|
||||||
""" Returns the Unix timestamp of the given value.
|
""" Returns the Unix timestamp of the given value.
|
||||||
Pattern should describe the date construction of
|
Pattern should describe the date construction of
|
||||||
|
|
Loading…
Reference in New Issue