mirror of https://github.com/fail2ban/fail2ban
merge getTime2 functionality to getTime
parent
134c33cc6d
commit
dca3db941e
|
@ -202,13 +202,13 @@ class DateDetector(object):
|
||||||
# not found:
|
# not found:
|
||||||
return (None, None)
|
return (None, None)
|
||||||
|
|
||||||
def getTime(self, line):
|
def getTime(self, line, timeMatch=None):
|
||||||
"""Attempts to return the date on a log line using templates.
|
"""Attempts to return the date on a log line using templates.
|
||||||
|
|
||||||
Obsolete: Use "getTime2" instead.
|
|
||||||
|
|
||||||
This uses the templates' `getDate` method in an attempt to find
|
This uses the templates' `getDate` method in an attempt to find
|
||||||
a date.
|
a date.
|
||||||
|
For the faster usage, always specify a parameter timeMatch (the previous tuple result
|
||||||
|
of the matchTime), then this will work without locking and without cycle over templates.
|
||||||
|
|
||||||
Parameters
|
Parameters
|
||||||
----------
|
----------
|
||||||
|
@ -221,6 +221,15 @@ class DateDetector(object):
|
||||||
The Unix timestamp returned from the first successfully matched
|
The Unix timestamp returned from the first successfully matched
|
||||||
template or None if not found.
|
template or None if not found.
|
||||||
"""
|
"""
|
||||||
|
if timeMatch:
|
||||||
|
template = timeMatch[1]
|
||||||
|
if template is not None:
|
||||||
|
date = template.getDate(line, timeMatch[0])
|
||||||
|
if date is not None:
|
||||||
|
if logSys.getEffectiveLevel() <= logLevel:
|
||||||
|
logSys.log(logLevel, "Got time %f for \"%r\" using template %s",
|
||||||
|
date[0], date[1].group(), template.name)
|
||||||
|
return date
|
||||||
with self.__lock:
|
with self.__lock:
|
||||||
for template in self.__templates:
|
for template in self.__templates:
|
||||||
try:
|
try:
|
||||||
|
@ -235,38 +244,6 @@ class DateDetector(object):
|
||||||
pass
|
pass
|
||||||
return None
|
return None
|
||||||
|
|
||||||
def getTime2(self, line, timeMatch = None):
|
|
||||||
"""Attempts to return the date on a log line using given template.
|
|
||||||
|
|
||||||
This uses the templates' `getDate` method in an attempt to find
|
|
||||||
a date.
|
|
||||||
Method 'getTime2' is a little bit faster as 'getTime' if template was specified (cause works without locking and without cycle)
|
|
||||||
|
|
||||||
Parameters
|
|
||||||
----------
|
|
||||||
line : str
|
|
||||||
Line which is searched by the date templates.
|
|
||||||
timeMatch (timeMatch, template) : (Match, DateTemplate)
|
|
||||||
Time match and template previously returned from matchTime
|
|
||||||
|
|
||||||
Returns
|
|
||||||
-------
|
|
||||||
float
|
|
||||||
The Unix timestamp returned from the first successfully matched
|
|
||||||
template or None if not found.
|
|
||||||
"""
|
|
||||||
date = None
|
|
||||||
if timeMatch:
|
|
||||||
template = timeMatch[1]
|
|
||||||
if template is not None:
|
|
||||||
date = template.getDate(line, timeMatch[0])
|
|
||||||
if date is not None:
|
|
||||||
if logSys.getEffectiveLevel() <= logLevel:
|
|
||||||
logSys.log(logLevel, "Got time(2) %f for \"%r\" using template %s",
|
|
||||||
date[0], date[1].group(), template.name)
|
|
||||||
return date
|
|
||||||
return self.getTime(line)
|
|
||||||
|
|
||||||
def _reorderTemplate(self, num):
|
def _reorderTemplate(self, num):
|
||||||
"""Reorder template (bubble up) in template list if hits grows enough.
|
"""Reorder template (bubble up) in template list if hits grows enough.
|
||||||
|
|
||||||
|
|
|
@ -479,7 +479,7 @@ class Filter(JailThread):
|
||||||
self.__lastDate = date
|
self.__lastDate = date
|
||||||
elif timeText:
|
elif timeText:
|
||||||
|
|
||||||
dateTimeMatch = self.dateDetector.getTime2(timeText, tupleLine[3])
|
dateTimeMatch = self.dateDetector.getTime(timeText, tupleLine[3])
|
||||||
|
|
||||||
if dateTimeMatch is None:
|
if dateTimeMatch is None:
|
||||||
logSys.error("findFailure failed to parse timeText: " + timeText)
|
logSys.error("findFailure failed to parse timeText: " + timeText)
|
||||||
|
@ -769,7 +769,7 @@ class FileFilter(Filter):
|
||||||
break
|
break
|
||||||
(timeMatch, template) = self.dateDetector.matchTime(line)
|
(timeMatch, template) = self.dateDetector.matchTime(line)
|
||||||
if timeMatch:
|
if timeMatch:
|
||||||
dateTimeMatch = self.dateDetector.getTime2(line[timeMatch.start():timeMatch.end()], (timeMatch, template))
|
dateTimeMatch = self.dateDetector.getTime(line[timeMatch.start():timeMatch.end()], (timeMatch, template))
|
||||||
else:
|
else:
|
||||||
nextp = container.tell()
|
nextp = container.tell()
|
||||||
if nextp > maxp:
|
if nextp > maxp:
|
||||||
|
|
|
@ -128,9 +128,9 @@ class DateDetectorTest(LogCaptureTestCase):
|
||||||
self.assertEqual(logMatch.group(), sdate)
|
self.assertEqual(logMatch.group(), sdate)
|
||||||
else:
|
else:
|
||||||
self.assertEqual(logtime, None, "getTime should have not matched for %r Got: %s" % (sdate, logtime))
|
self.assertEqual(logtime, None, "getTime should have not matched for %r Got: %s" % (sdate, logtime))
|
||||||
# with matchTime and getTime2 (this combination used in filter) :
|
# with getTime(matchTime) - this combination used in filter:
|
||||||
matchTime = self.__datedetector.matchTime(log)
|
matchTime = self.__datedetector.matchTime(log)
|
||||||
logtime = self.__datedetector.getTime2(log, matchTime)
|
logtime = self.__datedetector.getTime(log, matchTime)
|
||||||
if should_match:
|
if should_match:
|
||||||
self.assertNotEqual(logtime, None, "getTime retrieved nothing: failure for %s, anchored: %r, log: %s" % ( sdate, anchored, log))
|
self.assertNotEqual(logtime, None, "getTime retrieved nothing: failure for %s, anchored: %r, log: %s" % ( sdate, anchored, log))
|
||||||
( logUnix, logMatch ) = logtime
|
( logUnix, logMatch ) = logtime
|
||||||
|
|
Loading…
Reference in New Issue