diff --git a/glances/core/glances_client.py b/glances/core/glances_client.py index 27fdeeae..ba0c9fbe 100644 --- a/glances/core/glances_client.py +++ b/glances/core/glances_client.py @@ -81,29 +81,33 @@ class GlancesClient(object): """Logon to the server.""" ret = True - # First of all, trying to connect to a Glances server - try: - client_version = self.client.init() + if not self.args.snmp_force: + # First of all, trying to connect to a Glances server self.set_mode('glances') - except socket.error as err: - # print(_("Error: Connection to {0} server failed").format(self.get_mode())) - # Fallback to SNMP - self.set_mode('snmp') - except ProtocolError as err: - # Others errors - if str(err).find(" 401 ") > 0: - print(_("Error: Connection to server failed: Bad password")) - else: - print(_("Error: Connection to server failed: {0}").format(err)) - sys.exit(2) + try: + client_version = self.client.init() + except socket.error as err: + # Fallback to SNMP + print (_("Info: Connection to Glances server failed. Trying fallback to SNMP...")) + self.set_mode('snmp') + except ProtocolError as err: + # Others errors + if str(err).find(" 401 ") > 0: + print(_("Error: Connection to server failed: Bad password")) + else: + print(_("Error: Connection to server failed: {0}").format(err)) + sys.exit(2) - if self.get_mode() == 'glances' and version[:3] == client_version[:3]: - # Init stats - self.stats = GlancesStatsClient() - self.stats.set_plugins(json.loads(self.client.getAllPlugins())) - elif self.get_mode() == 'snmp': - print (_("Info: Connection to Glances server failed. Trying fallback to SNMP...")) - # Then fallback to SNMP if needed + if self.get_mode() == 'glances' and version[:3] == client_version[:3]: + # Init stats + self.stats = GlancesStatsClient() + self.stats.set_plugins(json.loads(self.client.getAllPlugins())) + else: + print (_("Info: Trying to grab stats by SNMP...")) + self.set_mode('snmp') + + if self.get_mode() == 'snmp': + # Fallback to SNMP if needed from glances.core.glances_stats import GlancesStatsClientSNMP # Init stats @@ -113,6 +117,7 @@ class GlancesClient(object): print(_("Error: Connection to SNMP server failed")) sys.exit(2) else: + # Unknow mode... ret = False if ret: diff --git a/glances/core/glances_main.py b/glances/core/glances_main.py index 649124f0..0138cf40 100644 --- a/glances/core/glances_main.py +++ b/glances/core/glances_main.py @@ -100,6 +100,8 @@ class GlancesMain(object): help=_('SNMP username (only for SNMPv3)')) parser.add_argument('--snmp-auth', default='password', dest='snmp_auth', help=_('SNMP authentication key (only for SNMPv3)')) + parser.add_argument('--snmp-force', action='store_true', default=False, + dest='snmp_force', help=_('force SNMP mode')) parser.add_argument('-t', '--time', default=self.refresh_time, type=int, dest='time', help=_('set refresh time in seconds [default: {0} sec]').format(self.refresh_time)) parser.add_argument('-w', '--webserver', action='store_true', default=False,