Correct client.py complexity

pull/893/head
Nicolargo 2016-05-10 12:14:38 +02:00
parent 8a3d7e49a3
commit ef1dbb3fef
1 changed files with 77 additions and 65 deletions

View File

@ -55,20 +55,20 @@ class GlancesClient(object):
# Build the URI # Build the URI
if args.password != "": if args.password != "":
uri = 'http://{0}:{1}@{2}:{3}'.format(args.username, args.password, self.uri = 'http://{0}:{1}@{2}:{3}'.format(args.username, args.password,
args.client, args.port) args.client, args.port)
else: else:
uri = 'http://{0}:{1}'.format(args.client, args.port) self.uri = 'http://{0}:{1}'.format(args.client, args.port)
logger.debug("Try to connect to {0}".format(uri)) logger.debug("Try to connect to {0}".format(self.uri))
# Try to connect to the URI # Try to connect to the URI
transport = GlancesClientTransport() transport = GlancesClientTransport()
# Configure the server timeout # Configure the server timeout
transport.set_timeout(timeout) transport.set_timeout(timeout)
try: try:
self.client = ServerProxy(uri, transport=transport) self.client = ServerProxy(self.uri, transport=transport)
except Exception as e: except Exception as e:
self.log_and_exit("Client couldn't create socket {0}: {1}".format(uri, e)) self.log_and_exit("Client couldn't create socket {0}: {1}".format(self.uri, e))
def log_and_exit(self, msg=''): def log_and_exit(self, msg=''):
"""Log and exit.""" """Log and exit."""
@ -92,12 +92,8 @@ class GlancesClient(object):
""" """
self._client_mode = mode self._client_mode = mode
def login(self): def _login_glances(self):
"""Logon to the server.""" """Login to a Glances server"""
ret = True
if not self.args.snmp_force:
# First of all, trying to connect to a Glances server
client_version = None client_version = None
try: try:
client_version = self.client.init() client_version = self.client.init()
@ -105,14 +101,14 @@ class GlancesClient(object):
# Fallback to SNMP # Fallback to SNMP
self.client_mode = 'snmp' self.client_mode = 'snmp'
logger.error("Connection to Glances server failed ({0} {1})".format(err.errno, err.strerror)) logger.error("Connection to Glances server failed ({0} {1})".format(err.errno, err.strerror))
fallbackmsg = 'No Glances server found. Trying fallback to SNMP...' fallbackmsg = 'No Glances server found on {}. Trying fallback to SNMP...'.format(self.uri)
if not self.return_to_browser: if not self.return_to_browser:
print(fallbackmsg) print(fallbackmsg)
else: else:
logger.info(fallbackmsg) logger.info(fallbackmsg)
except ProtocolError as err: except ProtocolError as err:
# Other errors # Other errors
msg = "Connection to server failed" msg = "Connection to server {} failed".format(self.uri)
if err.errcode == 401: if err.errcode == 401:
msg += " (Bad username/password)" msg += " (Bad username/password)"
else: else:
@ -132,11 +128,10 @@ class GlancesClient(object):
Client version: {0} / Server version: {1}".format(__version__, client_version)) Client version: {0} / Server version: {1}".format(__version__, client_version))
return False return False
else: return True
self.client_mode = 'snmp'
# SNMP mode def _login_snmp(self):
if self.client_mode == 'snmp': """Login to a SNMP server"""
logger.info("Trying to grab stats by SNMP...") logger.info("Trying to grab stats by SNMP...")
from glances.stats_client_snmp import GlancesStatsClientSNMP from glances.stats_client_snmp import GlancesStatsClientSNMP
@ -148,7 +143,24 @@ class GlancesClient(object):
self.log_and_exit("Connection to SNMP server failed") self.log_and_exit("Connection to SNMP server failed")
return False return False
if ret: return True
def login(self):
"""Logon to the server."""
if self.args.snmp_force:
# Force SNMP instead of Glances server
self.client_mode = 'snmp'
else:
# First of all, trying to connect to a Glances server
if not self._login_glances():
return False
# Try SNMP mode
if self.client_mode == 'snmp':
if not self._login_snmp():
return False
# Load limits from the configuration file # Load limits from the configuration file
# Each client can choose its owns limits # Each client can choose its owns limits
logger.debug("Load limits from the client configuration file") logger.debug("Load limits from the client configuration file")
@ -157,8 +169,8 @@ class GlancesClient(object):
# Init screen # Init screen
self.screen = GlancesCursesClient(config=self.config, args=self.args) self.screen = GlancesCursesClient(config=self.config, args=self.args)
# Return result # Return True: OK
return ret return True
def update(self): def update(self):
"""Update stats from Glances/SNMP server.""" """Update stats from Glances/SNMP server."""