From dca3db941e049d79c6ffcbd26a235a512d29eea1 Mon Sep 17 00:00:00 2001 From: sebres Date: Mon, 7 Mar 2016 17:28:37 +0100 Subject: [PATCH] merge getTime2 functionality to getTime --- fail2ban/server/datedetector.py | 49 +++++++------------------- fail2ban/server/filter.py | 4 +-- fail2ban/tests/datedetectortestcase.py | 4 +-- 3 files changed, 17 insertions(+), 40 deletions(-) diff --git a/fail2ban/server/datedetector.py b/fail2ban/server/datedetector.py index ae6d71d9..0c5da94b 100644 --- a/fail2ban/server/datedetector.py +++ b/fail2ban/server/datedetector.py @@ -202,13 +202,13 @@ class DateDetector(object): # not found: return (None, None) - def getTime(self, line): + def getTime(self, line, timeMatch=None): """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 - 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 ---------- @@ -221,6 +221,15 @@ class DateDetector(object): The Unix timestamp returned from the first successfully matched 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: for template in self.__templates: try: @@ -235,38 +244,6 @@ class DateDetector(object): pass 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): """Reorder template (bubble up) in template list if hits grows enough. diff --git a/fail2ban/server/filter.py b/fail2ban/server/filter.py index 8dbb6a7a..b7de21c8 100644 --- a/fail2ban/server/filter.py +++ b/fail2ban/server/filter.py @@ -479,7 +479,7 @@ class Filter(JailThread): self.__lastDate = date elif timeText: - dateTimeMatch = self.dateDetector.getTime2(timeText, tupleLine[3]) + dateTimeMatch = self.dateDetector.getTime(timeText, tupleLine[3]) if dateTimeMatch is None: logSys.error("findFailure failed to parse timeText: " + timeText) @@ -769,7 +769,7 @@ class FileFilter(Filter): break (timeMatch, template) = self.dateDetector.matchTime(line) 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: nextp = container.tell() if nextp > maxp: diff --git a/fail2ban/tests/datedetectortestcase.py b/fail2ban/tests/datedetectortestcase.py index ab7310ba..dc01fd49 100644 --- a/fail2ban/tests/datedetectortestcase.py +++ b/fail2ban/tests/datedetectortestcase.py @@ -128,9 +128,9 @@ class DateDetectorTest(LogCaptureTestCase): self.assertEqual(logMatch.group(), sdate) else: 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) - logtime = self.__datedetector.getTime2(log, matchTime) + logtime = self.__datedetector.getTime(log, matchTime) if should_match: self.assertNotEqual(logtime, None, "getTime retrieved nothing: failure for %s, anchored: %r, log: %s" % ( sdate, anchored, log)) ( logUnix, logMatch ) = logtime