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