mirror of https://github.com/fail2ban/fail2ban
- Bug fix: check for log rotation using the file size. Not the best solution...
git-svn-id: https://fail2ban.svn.sourceforge.net/svnroot/fail2ban/trunk@86 a942ae1a-1317-0410-a47c-b1dcaea8d6050.6
parent
dabc1e9681
commit
064ef28dc4
|
@ -44,6 +44,8 @@ class LogReader:
|
|||
self.lastModTime = 0
|
||||
self.logSys = logSys
|
||||
self.lastPos = 0
|
||||
self.lastSize = 0
|
||||
self.logStats = None
|
||||
|
||||
def setName(self, name):
|
||||
""" Sets the name of the log reader.
|
||||
|
@ -80,18 +82,33 @@ class LogReader:
|
|||
""" Checks if the log file has been modified using os.stat().
|
||||
"""
|
||||
try:
|
||||
logStats = os.stat(self.logPath)
|
||||
self.logStats = os.stat(self.logPath)
|
||||
except OSError:
|
||||
self.logSys.error("Unable to get stat on "+self.logPath)
|
||||
sys.exit(-1)
|
||||
|
||||
if self.lastModTime == logStats.st_mtime:
|
||||
if self.lastModTime == self.logStats.st_mtime:
|
||||
return False
|
||||
else:
|
||||
self.logSys.debug(self.logPath+" has been modified")
|
||||
self.lastModTime = logStats.st_mtime
|
||||
self.lastModTime = self.logStats.st_mtime
|
||||
return True
|
||||
|
||||
def setFilePos(self, file):
|
||||
""" 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
|
||||
order to detect this. Not the best solution yet.
|
||||
"""
|
||||
if self.lastSize > self.logStats.st_size:
|
||||
self.logSys.debug("Size " + `self.logStats.st_size` + " is " +
|
||||
"smaller than " + `self.lastSize`)
|
||||
self.logSys.debug("Log rotation detected")
|
||||
self.lastPos = 0
|
||||
|
||||
self.lastSize = self.logStats.st_size
|
||||
self.logSys.debug("Setting file position to " + `self.lastPos`)
|
||||
file.seek(self.lastPos)
|
||||
|
||||
def getFailures(self):
|
||||
""" Gets all the failure in the log file which are
|
||||
newer than time.time()-self.findTime.
|
||||
|
@ -101,8 +118,7 @@ class LogReader:
|
|||
"""
|
||||
ipList = dict()
|
||||
logFile = self.openLogFile()
|
||||
self.logSys.debug("Setting file position to " + `self.lastPos`)
|
||||
logFile.seek(self.lastPos)
|
||||
self.setFilePos(logFile)
|
||||
for line in logFile.readlines():
|
||||
failList = self.findFailure(line)
|
||||
for element in failList:
|
||||
|
|
Loading…
Reference in New Issue