From d2ffee0b2d445ed5102dd2ed7f00b1b66c1412b5 Mon Sep 17 00:00:00 2001 From: Yaroslav Halchenko Date: Fri, 10 Feb 2012 22:19:44 -0500 Subject: [PATCH] ENH: minor, just pythonized some parts of fail2ban-regex summary --- fail2ban-regex | 80 ++++++++++++++++---------------------------------- 1 file changed, 26 insertions(+), 54 deletions(-) diff --git a/fail2ban-regex b/fail2ban-regex index fa02a961..ffaca6ec 100755 --- a/fail2ban-regex +++ b/fail2ban-regex @@ -29,7 +29,7 @@ import getopt, sys, time, logging, os # Inserts our own modules path first in the list # fix for bug #343821 -sys.path.insert(1, "/usr/share/fail2ban") +#sys.path.insert(1, "/usr/share/fail2ban") from client.configparserinc import SafeConfigParserWithIncludes from ConfigParser import NoOptionError, NoSectionError, MissingSectionHeaderError @@ -248,50 +248,29 @@ class Fail2banRegex: print "Results" print "=======" print - + + def print_failregexes(title, failregexes): + # Print title + print title + print "|- Regular expressions:" + for cnt, failregex in enumerate(failregexes): + print "| [%d] %s" % (cnt+1, failregex.getFailRegex()) + print "|" + + # Print stats + total = 0 + print "`- Number of matches:" + for cnt, failregex in enumerate(failregexes): + match = failregex.getStats() + total += match + print " [%d] %s match(es)" % (cnt+1, match) + print + return total + # Print title - cnt = 1 - print "Failregex" - print "|- Regular expressions:" - for failregex in self.__failregex: - print "| [" + str(cnt) + "] " + failregex.getFailRegex() - cnt += 1 - cnt = 1 + total = print_failregexes("Failregex", self.__failregex) + _ = print_failregexes("Ignoreregex", self.__ignoreregex) - print "|" - - # Print stats - cnt = 1 - total = 0 - print "`- Number of matches:" - for failregex in self.__failregex: - match = failregex.getStats() - total += match - print " [" + str(cnt) + "] " + str(match) + " match(es)" - cnt += 1 - - print - - # Print title - cnt = 1 - print "Ignoreregex" - print "|- Regular expressions:" - for failregex in self.__ignoreregex: - print "| [" + str(cnt) + "] " + failregex.getFailRegex() - cnt += 1 - cnt = 1 - - print "|" - - # Print stats - cnt = 1 - print "`- Number of matches:" - for failregex in self.__ignoreregex: - match = failregex.getStats() - print " [" + str(cnt) + "] " + str(match) + " match(es)" - cnt += 1 - - print print "Summary" print "=======" print @@ -304,26 +283,19 @@ class Fail2banRegex: return False else: # Print stats - cnt = 1 print "Addresses found:" - for failregex in self.__failregex: - print "[" + str(cnt) + "]" + for cnt, failregex in enumerate(self.__failregex): + print "[%d]" % (cnt+1) for ip in failregex.getIPList(): timeTuple = time.localtime(ip[1]) timeString = time.strftime("%a %b %d %H:%M:%S %Y", timeTuple) - if ip[2]: - dup = " (already matched)" - else: - dup = "" - print " " + ip[0] + " (" + timeString + ")" + dup - cnt += 1 - + print " %s (%s)%s" % ( + ip[0], timeString, ip[2] and " (already matched)" or "") print print "Date template hits:" for template in self.__filter.dateDetector.getTemplates(): print `template.getHits()` + " hit(s): " + template.getName() - print print "Success, the total number of match is " + str(total)