Add the --snmp-force tag to the command line

This commit is contained in:
Nicolas Hennion 2014-06-30 17:19:37 +02:00
parent a2c319079f
commit 664ecc0fa9
2 changed files with 28 additions and 21 deletions

View File

@ -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:

View File

@ -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,