Correct check issue error in client/server mode (issue #459) + exit on check failed + correct another issue on Linux CPU SNMP

This commit is contained in:
Nicolargo 2014-12-18 21:23:27 +01:00
parent 56e35e4b04
commit 96d2e841de
4 changed files with 31 additions and 28 deletions

1
NEWS
View File

@ -7,6 +7,7 @@ Version 2.X
* Fix incorrect kernel thread detection with --hide-kernel-threads (issue #457)
* Handle IOError exception if no /etc/os-release to use Glances on Synology DSM (issue #458)
* Check issue error in client/server mode (issue #459)
Version 2.2
===========

View File

@ -61,6 +61,9 @@ class GlancesClient(object):
# Client mode:
self.set_mode()
# Return to browser or exit
self.return_to_browser = return_to_browser
# Build the URI
if args.password != "":
uri = 'http://{0}:{1}@{2}:{3}'.format(args.username, args.password,
@ -76,12 +79,15 @@ class GlancesClient(object):
try:
self.client = ServerProxy(uri, transport=transport)
except Exception as e:
msg = "Client couldn't create socket {0}: {1}".format(uri, e)
if not return_to_browser:
logger.critical(msg)
sys.exit(2)
else:
logger.error(msg)
self.log_and_exit("Client couldn't create socket {0}: {1}".format(uri, e))
def log_and_exit(self, msg=''):
"""Log and (exit)"""
if not self.return_to_browser:
logger.critical(msg)
sys.exit(2)
else:
logger.error(msg)
def set_mode(self, mode='glances'):
"""Set the client mode.
@ -100,7 +106,7 @@ class GlancesClient(object):
"""
return self.mode
def login(self, return_to_browser=False):
def login(self):
"""Logon to the server."""
ret = True
@ -115,7 +121,7 @@ class GlancesClient(object):
logger.error("Connection to Glances server failed (%s)" % err)
self.set_mode('snmp')
fallbackmsg = _("Trying fallback to SNMP...")
if not return_to_browser:
if not self.return_to_browser:
print(fallbackmsg)
else:
logger.info(fallbackmsg)
@ -125,22 +131,18 @@ class GlancesClient(object):
msg = "Connection to server failed (bad password)"
else:
msg = "Connection to server failed ({0})".format(err)
if not return_to_browser:
logger.critical(msg)
sys.exit(2)
else:
logger.error(msg)
return False
self.log_and_exit(msg)
return False
if self.get_mode() == 'glances' and version[:3] == client_version[:3]:
if self.get_mode() == 'glances' and version.split('.')[0] == client_version.split('.')[0]:
# Init stats
self.stats = GlancesStatsClient()
self.stats.set_plugins(json.loads(self.client.getAllPlugins()))
logger.debug(
"Client version: %s / Server version: %s" % (version, client_version))
else:
logger.error(
"Client and server not compatible: Client version: %s / Server version: %s" % (version, client_version))
elif self.get_mode() == 'glances':
self.log_and_exit("Client and server not compatible: Client version: %s / Server version: %s" % (version, client_version))
return False
else:
self.set_mode('snmp')
@ -154,11 +156,8 @@ class GlancesClient(object):
self.stats = GlancesStatsClientSNMP(args=self.args)
if not self.stats.check_snmp():
logger.error("Connection to SNMP server failed")
if not return_to_browser:
sys.exit(2)
else:
return False
self.log_and_exit("Connection to SNMP server failed")
return False
if ret:
# Load limits from the configuration file
@ -221,7 +220,7 @@ class GlancesClient(object):
# Grab success
return "SNMP"
def serve_forever(self, return_to_browser=False):
def serve_forever(self):
"""Main client loop."""
exitkey = False
@ -233,7 +232,7 @@ class GlancesClient(object):
# Update the screen
exitkey = self.screen.update(self.stats,
cs_status=cs_status,
return_to_browser=return_to_browser)
return_to_browser=self.return_to_browser)
return self.get_mode()

View File

@ -180,10 +180,11 @@ class GlancesClientBrowser(object):
args_server.username = self.get_servers_list()[self.screen.get_active()]['username']
args_server.password = self.get_servers_list()[self.screen.get_active()]['password']
client = GlancesClient(config=self.config,
args=args_server)
args=args_server,
return_to_browser=True)
# Test if client and server are in the same major version
if not client.login(return_to_browser=True):
if not client.login():
self.screen.display_popup(_("Sorry, cannot connect to %s (see log file for additional information)" % v['name']))
# Set the ONLINE status for the selected server
@ -191,7 +192,7 @@ class GlancesClientBrowser(object):
else:
# Start the client loop
# Return connection type: 'glances' or 'snmp'
connection_type = client.serve_forever(return_to_browser=True)
connection_type = client.serve_forever()
try:
logger.debug("Disconnect Glances client from the %s server" %

View File

@ -104,6 +104,7 @@ class Plugin(GlancesPlugin):
if self.stats['nb_log_core'] > 0:
self.stats['idle'] = self.stats['idle'] / self.stats['nb_log_core']
self.stats['idle'] = 100 - self.stats['idle']
self.stats['total'] = 100 - self.stats['idle']
else:
# Default behavor
@ -119,6 +120,7 @@ class Plugin(GlancesPlugin):
# Convert SNMP stats to float
for key in list(self.stats.keys()):
self.stats[key] = float(self.stats[key])
self.stats['total'] = 100 - self.stats['idle']
# Update the history list
self.update_stats_history()