From d4fb48fd142f2123f73679ed032a2c223619f215 Mon Sep 17 00:00:00 2001 From: Hank Leininger Date: Fri, 14 Mar 2014 01:57:55 -0400 Subject: [PATCH] Add --print-no-{missed,ignored} and restore -all. Realized --print-all-{missed,ignored} aren't meant to be simple on/off, but are meant to enable printing all lines even when there are more than 20. This restores the behavior of --print-all-*, but preserves the memory-savings when --print-all-* are not specified. Also adds --print-no-{missed,ignored} which can be used to suppress printing of a line type entirely. --- fail2ban-regex | 35 +++++++++++++++++++++-------------- 1 file changed, 21 insertions(+), 14 deletions(-) diff --git a/fail2ban-regex b/fail2ban-regex index 941d0074..da5bcf88 100755 --- a/fail2ban-regex +++ b/fail2ban-regex @@ -111,10 +111,14 @@ Report bugs to https://github.com/fail2ban/fail2ban/issues help="Be verbose in output"), Option("-D", "--debuggex", action='store_true', help="Produce debuggex.com urls for debugging there"), + Option("--print-no-missed", action='store_true', + help="Do not print any missed lines"), + Option("--print-no-ignored", action='store_true', + help="Do not print any ignored lines"), Option("--print-all-missed", action='store_true', - help="Either to print all missed lines"), + help="Print all missed lines, no matter how many"), Option("--print-all-ignored", action='store_true', - help="Either to print all ignored lines"), + help="Print all ignored lines, no matter how many"), Option("-t", "--log-traceback", action='store_true', help="Enrich log-messages with compressed tracebacks"), Option("--full-traceback", action='store_true', @@ -178,6 +182,9 @@ class Fail2banRegex(object): def __init__(self, opts): self._verbose = opts.verbose self._debuggex = opts.debuggex + self._maxlines = 20 + self._print_no_missed = opts.print_no_missed + self._print_no_ignored = opts.print_no_ignored self._print_all_missed = opts.print_all_missed self._print_all_ignored = opts.print_all_ignored @@ -262,7 +269,7 @@ class Fail2banRegex(object): if is_ignored: self._line_stats.ignored += 1 - if self._print_all_ignored: + if not self._print_no_ignored and (self._print_all_ignored or self._line_stats.ignored <= self._maxlines + 1): self._line_stats.ignored_lines.append(line) self._line_stats.ignored_lines_timeextracted.append(line_datetimestripped) @@ -272,7 +279,7 @@ class Fail2banRegex(object): else: if not is_ignored: self._line_stats.missed += 1 - if self._print_all_missed: + if not self._print_no_missed and (self._print_all_missed or self._line_stats.missed <= self._maxlines + 1): self._line_stats.missed_lines.append(line) self._line_stats.missed_lines_timeextracted.append(line_datetimestripped) self._line_stats.tested += 1 @@ -284,9 +291,10 @@ class Fail2banRegex(object): def printLines(self, ltype): lstats = self._line_stats - assert(len(lstats.missed_lines) == lstats.tested - (lstats.matched + lstats.ignored)) + assert(self._line_stats.missed == lstats.tested - (lstats.matched + lstats.ignored)) + lines = lstats[ltype] l = lstats[ltype + '_lines'] - if len(l): + if lines: header = "%s line(s):" % (ltype.capitalize(),) if self._debuggex: if ltype == 'missed': @@ -294,21 +302,20 @@ class Fail2banRegex(object): else: regexlist = self._ignoreregex l = lstats[ltype + '_lines_timeextracted'] - lines = len(l)*len(regexlist) - if lines < 20 or getattr(self, '_print_all_' + ltype): + if lines < self._maxlines or getattr(self, '_print_all_' + ltype): ans = [[]] for arg in [l, regexlist]: ans = [ x + [y] for x in ans for y in arg ] b = map(lambda a: a[0] + ' | ' + a[1].getFailRegex() + ' | ' + debuggexURL(a[0], a[1].getFailRegex()), ans) pprint_list([x.rstrip() for x in b], header) else: - print "%s: too many to print. Use --print-all-%s " \ + print "%s too many to print. Use --print-all-%s " \ "to print all %d lines" % (header, ltype, lines) - elif len(l) < 20 or getattr(self, '_print_all_' + ltype): + elif lines < self._maxlines or getattr(self, '_print_all_' + ltype): pprint_list([x.rstrip() for x in l], header) else: - print "%s: too many to print. Use --print-all-%s " \ - "to print all %d lines" % (header, ltype, len(l)) + print "%s too many to print. Use --print-all-%s " \ + "to print all %d lines" % (header, ltype, lines) def printStats(self): print @@ -352,9 +359,9 @@ class Fail2banRegex(object): print "\nLines: %s" % self._line_stats - if self._print_all_ignored: + if not self._print_no_ignored: self.printLines('ignored') - if self._print_all_missed: + if not self._print_no_missed: self.printLines('missed') return True