- Improved client

git-svn-id: https://fail2ban.svn.sourceforge.net/svnroot/fail2ban/trunk@282 a942ae1a-1317-0410-a47c-b1dcaea8d605
0.x
Cyril Jaquier 2006-08-20 21:59:27 +00:00
parent d7682360bc
commit 9fe1efcc45
1 changed files with 34 additions and 16 deletions

View File

@ -51,6 +51,7 @@ class Fail2banClient:
self.conf = dict() self.conf = dict()
self.conf["dump"] = False self.conf["dump"] = False
self.conf["force"] = False self.conf["force"] = False
self.conf["verbose"] = 2
def dispUsage(self): def dispUsage(self):
""" Prints Fail2Ban command line options and exits """ Prints Fail2Ban command line options and exits
@ -62,6 +63,8 @@ class Fail2banClient:
print print
print " -c <DIR> configuration directory" print " -c <DIR> configuration directory"
print " -d dump configuration" print " -d dump configuration"
print " -v increase verbosity"
print " -q decrease verbosity"
print " -x force execution of the server" print " -x force execution of the server"
print " -h display this help message" print " -h display this help message"
print print
@ -74,28 +77,35 @@ class Fail2banClient:
for opt in optList: for opt in optList:
if opt[0] == "-d": if opt[0] == "-d":
self.conf["dump"] = True self.conf["dump"] = True
if opt[0] == "-x": elif opt[0] == "-v":
self.conf["verbose"] = self.conf["verbose"] + 1
elif opt[0] == "-q":
self.conf["verbose"] = self.conf["verbose"] - 1
elif opt[0] == "-x":
self.conf["force"] = True self.conf["force"] = True
if opt[0] in ["-h", "--help"]: elif opt[0] in ["-h", "--help"]:
self.dispUsage() self.dispUsage()
def ping(self): def ping(self):
return self.processCmd([["ping"]]) return self.processCmd([["ping"]], False)
@staticmethod @staticmethod
def processCmd(cmd): def processCmd(cmd, showRet = True):
for c in cmd: for c in cmd:
try: try:
client = CSocket() client = CSocket()
except Exception, e: except Exception, e:
logSys.error(e) if showRet:
logSys.error("Arrggh... Start the server first") logSys.error(e)
logSys.error("Arrggh... Start the server first")
return False return False
ret = client.send(c) ret = client.send(c)
if ret[0] == 0: if ret[0] == 0:
logSys.info("OK : " + `ret[1]`) logSys.debug("OK : " + `ret[1]`)
if showRet:
print `ret[1]`
else: else:
logSys.info("NOK: " + `ret[1].args`) logSys.error("NOK: " + `ret[1].args`)
return False return False
return True return True
@ -114,7 +124,7 @@ class Fail2banClient:
if len(sys.argv) < 2: if len(sys.argv) < 2:
self.dispUsage() self.dispUsage()
if cmd[0] == "start" and len(cmd) == 1: if len(cmd) == 1 and cmd[0] == "start":
if self.ping(): if self.ping():
logSys.info("Server already running") logSys.info("Server already running")
return False return False
@ -126,17 +136,17 @@ class Fail2banClient:
# Wait for the server to start # Wait for the server to start
self.waitOnServer() self.waitOnServer()
# Configure the server # Configure the server
self.processCmd(self.stream) self.processCmd(self.stream, False)
return True return True
except ServerExecutionException: except ServerExecutionException:
logSys.error("Could not start server. Try -x option") logSys.error("Could not start server. Try -x option")
return False return False
elif cmd[0] == "reload" and len(cmd) == 1: elif len(cmd) == 1 and cmd[0] == "reload":
if self.ping(): if self.ping():
self.readConfig() self.readConfig()
self.processCmd(['stop', 'all']) self.processCmd(['stop', 'all'], False)
# Configure the server # Configure the server
self.processCmd(self.stream) self.processCmd(self.stream, False)
else: else:
logSys.error("Could not find server") logSys.error("Could not find server")
else: else:
@ -177,7 +187,7 @@ class Fail2banClient:
# Reads the command line options. # Reads the command line options.
try: try:
cmdOpts = 'hc:xd' cmdOpts = 'hc:xdvq'
cmdLongOpts = ['help'] cmdLongOpts = ['help']
optList, args = getopt.getopt(self.argv[1:], cmdOpts, cmdLongOpts) optList, args = getopt.getopt(self.argv[1:], cmdOpts, cmdLongOpts)
except getopt.GetoptError: except getopt.GetoptError:
@ -185,11 +195,19 @@ class Fail2banClient:
self.getCmdLineOptions(optList) self.getCmdLineOptions(optList)
logSys.setLevel(logging.DEBUG) verbose = self.conf["verbose"]
if verbose <= 0:
logSys.setLevel(logging.ERROR)
elif verbose == 1:
logSys.setLevel(logging.WARN)
elif verbose == 2:
logSys.setLevel(logging.INFO)
else:
logSys.setLevel(logging.DEBUG)
# Add the default logging handler # Add the default logging handler
stdout = logging.StreamHandler(sys.stdout) stdout = logging.StreamHandler(sys.stdout)
# set a format which is simpler for console use # set a format which is simpler for console use
formatter = logging.Formatter('%(name)-16s: %(levelname)-6s %(message)s') formatter = logging.Formatter('%(levelname)-6s %(message)s')
# tell the handler to use this format # tell the handler to use this format
stdout.setFormatter(formatter) stdout.setFormatter(formatter)
logSys.addHandler(stdout) logSys.addHandler(stdout)