Merge pull request #2245 from benrubson/loglevel

badips.py: extended option `loglevel` to supply different log-level to the summary
pull/2356/head
Sergey G. Brester 2019-02-22 14:25:03 +01:00 committed by GitHub
commit af18993ba2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 20 additions and 7 deletions

View File

@ -62,6 +62,9 @@ ver. 0.10.5-dev-1 (20??/??/??) - development edition
attempts (failure) for IP (resp. failure-ID), see gh-2351; attempts (failure) for IP (resp. failure-ID), see gh-2351;
Syntax: Syntax:
- `fail2ban-client set <jail> attempt <ip> [<failure-message1> ... <failure-messageN>]` - `fail2ban-client set <jail> attempt <ip> [<failure-message1> ... <failure-messageN>]`
* `action.d/badips.py`: option `loglevel` extended with level of summary message,
following example configuration logging summary with NOTICE and rest with DEBUG log-levels:
`action = badips.py[loglevel="debug, notice"]`
ver. 0.10.4 (2018/10/04) - ten-four-on-due-date-ten-four ver. 0.10.4 (2018/10/04) - ten-four-on-due-date-ten-four

View File

@ -32,7 +32,7 @@ else: # pragma: 3.x no cover
from urllib import urlencode from urllib import urlencode
from fail2ban.server.actions import ActionBase from fail2ban.server.actions import ActionBase
from fail2ban.helpers import str2LogLevel from fail2ban.helpers import splitwords, str2LogLevel
@ -75,6 +75,9 @@ class BadIPsAction(ActionBase): # pragma: no cover - may be unavailable
loglevel : int/str, optional loglevel : int/str, optional
Log level of the message when an IP is (un)banned. Log level of the message when an IP is (un)banned.
Default `DEBUG`. Default `DEBUG`.
Can be also supplied as two-value list (comma- or space separated) to
provide level of the summary message when a group of IPs is (un)banned.
Example `DEBUG,INFO`.
agent : str, optional agent : str, optional
User agent transmitted to server. User agent transmitted to server.
Default `Fail2Ban/ver.` Default `Fail2Ban/ver.`
@ -91,8 +94,8 @@ class BadIPsAction(ActionBase): # pragma: no cover - may be unavailable
return Request(url, headers={'User-Agent': self.agent}, **argv) return Request(url, headers={'User-Agent': self.agent}, **argv)
def __init__(self, jail, name, category, score=3, age="24h", key=None, def __init__(self, jail, name, category, score=3, age="24h", key=None,
banaction=None, bancategory=None, bankey=None, updateperiod=900, loglevel='DEBUG', agent="Fail2Ban", banaction=None, bancategory=None, bankey=None, updateperiod=900,
timeout=TIMEOUT): loglevel='DEBUG', agent="Fail2Ban", timeout=TIMEOUT):
super(BadIPsAction, self).__init__(jail, name) super(BadIPsAction, self).__init__(jail, name)
self.timeout = timeout self.timeout = timeout
@ -104,7 +107,9 @@ class BadIPsAction(ActionBase): # pragma: no cover - may be unavailable
self.banaction = banaction self.banaction = banaction
self.bancategory = bancategory or category self.bancategory = bancategory or category
self.bankey = bankey self.bankey = bankey
self.loglevel = str2LogLevel(loglevel) loglevel = splitwords(loglevel)
self.sumloglevel = str2LogLevel(loglevel[-1])
self.loglevel = str2LogLevel(loglevel[0])
self.updateperiod = updateperiod self.updateperiod = updateperiod
self._bannedips = set() self._bannedips = set()
@ -350,9 +355,13 @@ class BadIPsAction(ActionBase): # pragma: no cover - may be unavailable
s = ips - self._bannedips s = ips - self._bannedips
p = len(s) p = len(s)
self._banIPs(s) self._banIPs(s)
self._logSys.log(self.loglevel, if m != 0 or p != 0:
"Updated IPs for jail '%s' (-%d/+%d). Update again in %i seconds", self._logSys.log(self.sumloglevel,
self._jail.name, m, p, self.updateperiod) "Updated IPs for jail '%s' (-%d/+%d)",
self._jail.name, m, p)
self._logSys.debug(
"Next update for jail '%' in %i seconds",
self._jail.name, self.updateperiod)
finally: finally:
self._timer = threading.Timer(self.updateperiod, self.update) self._timer = threading.Timer(self.updateperiod, self.update)
self._timer.start() self._timer.start()

View File

@ -55,6 +55,7 @@ if sys.version_info >= (2,7): # pragma: no cover - may be unavailable
pythonModule = None pythonModule = None
modAction = None modAction = None
@skip_if_not_available
def setUp(self): def setUp(self):
"""Call before every test case.""" """Call before every test case."""
super(BadIPsActionTest, self).setUp() super(BadIPsActionTest, self).setUp()