From 9f2d90104029a08d8f2b95bb193c430128440865 Mon Sep 17 00:00:00 2001 From: Cyril Jaquier Date: Mon, 28 Aug 2006 20:20:18 +0000 Subject: [PATCH] - Added a beautifier git-svn-id: https://fail2ban.svn.sourceforge.net/svnroot/fail2ban/trunk@310 a942ae1a-1317-0410-a47c-b1dcaea8d605 --- client/beautifier.py | 90 ++++++++++++++++++++++++++++++++++++++++++++ fail2ban-client | 8 +++- 2 files changed, 96 insertions(+), 2 deletions(-) create mode 100644 client/beautifier.py diff --git a/client/beautifier.py b/client/beautifier.py new file mode 100644 index 00000000..24c74095 --- /dev/null +++ b/client/beautifier.py @@ -0,0 +1,90 @@ +# This file is part of Fail2Ban. +# +# Fail2Ban is free software; you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation; either version 2 of the License, or +# (at your option) any later version. +# +# Fail2Ban is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with Fail2Ban; if not, write to the Free Software +# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + +# Author: Cyril Jaquier +# +# $Revision: 288 $ + +__author__ = "Cyril Jaquier" +__version__ = "$Revision: 288 $" +__date__ = "$Date: 2006-08-22 23:59:51 +0200 (Tue, 22 Aug 2006) $" +__copyright__ = "Copyright (c) 2004 Cyril Jaquier" +__license__ = "GPL" + +from server.server import ServerUnknownJail +from server.server import ServerDuplicateJail +import logging + +# Gets the instance of the logger. +logSys = logging.getLogger("fail2ban.client.config") + +## +# Beautify the output of the client. +# +# Fail2ban server only return unformatted return codes which need to be +# converted into user readable messages. + +class Beautifier: + + def __init__(self, cmd = None): + self.inputCmd = cmd + + def setInputCmd(self, cmd): + self.inputCmd = cmd + + def getInputCmd(self): + return self.inputCmd + + def beautify(self, response): + logSys.debug("Beautify " + `response` + " with " + `self.inputCmd`) + msg = response + if self.inputCmd[0:1] == ['status']: + if len(self.inputCmd) > 1: + msg = "Status for the jail: " + self.inputCmd[1] + "\n" + msg = msg + "|- " + response[0][0] + "\n" + msg = msg + "| |- " + response[0][1][0][0] + ":\t\t" + `response[0][1][0][1]` + "\n" + msg = msg + "| `- " + response[0][1][1][0] + ":\t\t" + `response[0][1][1][1]` + "\n" + msg = msg + "`- " + response[1][0] + "\n" + msg = msg + " |- " + response[1][1][0][0] + ":\t\t" + `response[1][1][0][1]` + "\n" + msg = msg + " `- " + response[1][1][1][0] + ":\t\t" + `response[1][1][1][1]` + else: + msg = "Status\n" + msg = msg + "|- " + response[0][0] + ":\t" + `response[0][1]` + "\n" + msg = msg + "`- " + response[1][0] + ":\t\t" + response[1][1] + elif self.inputCmd[1:2] == ['loglevel']: + msg = "Current logging level is " + if response == 1: + msg = msg + "ERROR" + elif response == 2: + msg = msg + "WARN" + elif response == 3: + msg = msg + "INFO" + elif response == 4: + msg = msg + "DEBUG" + else: + msg = msg + `response` + return msg + + def beautifyError(self, response): + logSys.debug("Beautify (error) " + `response` + " with " + `self.inputCmd`) + msg = response + if isinstance(response, ServerUnknownJail): + msg = "Sorry but the jail '" + response[0] + "' does not exist" + elif isinstance(response, IndexError): + msg = "Sorry but the command is invalid" + elif isinstance(response, ServerDuplicateJail): + msg = "The jail '" + response[0] + "' already exists" + return msg diff --git a/fail2ban-client b/fail2ban-client index 0e7f159b..68fb6198 100755 --- a/fail2ban-client +++ b/fail2ban-client @@ -35,6 +35,7 @@ sys.path.insert(1, "/usr/lib/fail2ban") from version import version from client.csocket import CSocket from client.configurator import Configurator +from client.beautifier import Beautifier # Gets the instance of the logger. logSys = logging.getLogger("fail2ban.client") @@ -115,7 +116,9 @@ class Fail2banClient: @staticmethod def processCmd(cmd, showRet = True): + beautifier = Beautifier() for c in cmd: + beautifier.setInputCmd(c) try: client = CSocket() except Exception, e: @@ -127,9 +130,10 @@ class Fail2banClient: if ret[0] == 0: logSys.debug("OK : " + `ret[1]`) if showRet: - print `ret[1]` + print beautifier.beautify(ret[1]) else: - logSys.error("NOK: " + `ret[1].args`) + logSys.debug("NOK: " + `ret[1].args`) + print beautifier.beautifyError(ret[1]) return False return True