diff --git a/glances/core/glances_client.py b/glances/core/glances_client.py index c611155d7cfc09aa48717d55abd2b9ea0889733a..dd7b8d28422d1bff09c7210dd6c1cb5ac6bae4fb 100644 --- a/glances/core/glances_client.py +++ b/glances/core/glances_client.py @@ -90,7 +90,7 @@ class GlancesClient(): client_version = self.client.init() self.set_mode('glances') except socket.error as err: - print(_("Error: Connection to {0} server failed").format(self.get_mode())) + # print(_("Error: Connection to {0} server failed").format(self.get_mode())) # Fallback to SNMP self.set_mode('snmp') except ProtocolError as err: @@ -106,23 +106,16 @@ class GlancesClient(): 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 - from glances.core.glances_snmp import GlancesSNMPClient - - # Test if SNMP is available on the server side - clientsnmp = GlancesSNMPClient() - - # !!! Simple request with system name - # !!! Had to have a standard method to check SNMP server - print(_("Trying {0}...").format(self.get_mode())) - if (clientsnmp.get_by_oid("1.3.6.1.2.1.1.5.0") == {}): - print(_("Error: Connection to {0} server failed").format(self.get_mode())) - sys.exit(2) - from glances.core.glances_stats import GlancesStatsClientSNMP # Init stats - self.stats = GlancesStatsClientSNMP() + self.stats = GlancesStatsClientSNMP(args=self.args) + + if not self.stats.check_snmp(): + print(_("Error: Connection to SNMP server failed")) + sys.exit(2) else: ret = False diff --git a/glances/core/glances_snmp.py b/glances/core/glances_snmp.py index 58b11a265f304a97a73f6d3ab919b64ce6f6be51..673cf6b532fb49ce74ad4eca88a9439d81173799 100644 --- a/glances/core/glances_snmp.py +++ b/glances/core/glances_snmp.py @@ -30,10 +30,10 @@ except ImportError, e: class GlancesSNMPClient(object): """ SNMP client class (based on PySNMP) """ - def __init__(self, host = "localhost", - port = 161, - community = "public", - version = "SNMPv2-MIB"): + def __init__(self, host="localhost", + port=161, + community="public", + version="SNMPv2-MIB"): super(GlancesSNMPClient, self).__init__() self.cmdGen = cmdgen.CommandGenerator() self.host = host diff --git a/glances/core/glances_stats.py b/glances/core/glances_stats.py index 4cb5578c7c69b8d944119f3e3535165ddd6289a3..562df18a778de6be602d340f75d404479c58793e 100644 --- a/glances/core/glances_stats.py +++ b/glances/core/glances_stats.py @@ -64,7 +64,7 @@ class GlancesStats(object): # Default behavior raise AttributeError(item) - def load_plugins(self): + def load_plugins(self, args=None): """ Load all plugins in the "plugins" folder """ @@ -85,7 +85,7 @@ class GlancesStats(object): # for example, the file glances_xxx.py # generate self._plugins_list["xxx"] = ... plugname = os.path.basename(plug)[len(header):-3].lower() - self._plugins[plugname] = m.Plugin() + self._plugins[plugname] = m.Plugin(args=args) def getAllPlugins(self): """ @@ -221,15 +221,31 @@ class GlancesStatsClientSNMP(GlancesStats): This class store, update and give stats for the SNMP client """ - def __init__(self, config=None): + def __init__(self, config=None, args=None): # Init the plugin list dict self._plugins = collections.defaultdict(dict) # Init the configuration self.config = config + # Init the arguments + self.args = args + # Load plugins - self.load_plugins() + self.load_plugins(args=self.args) + + def check_snmp(self): + """ + Chek if SNMP is available on the server + """ + + # Import the SNMP client class + from glances.core.glances_snmp import GlancesSNMPClient + + # Create an instance of the SNMP client + clientsnmp = GlancesSNMPClient(host=self.args.client) + + return clientsnmp.get_by_oid("1.3.6.1.2.1.1.5.0") != {} def update(self): """ diff --git a/glances/plugins/glances_alert.py b/glances/plugins/glances_alert.py index 8ac2897d277dfe8f5a02f28f89281119f08a8fdb..f3ce9f3ead61708afbde9ae374b895e272f41c38 100644 --- a/glances/plugins/glances_alert.py +++ b/glances/plugins/glances_alert.py @@ -32,8 +32,8 @@ class Plugin(GlancesPlugin): Only for display """ - def __init__(self): - GlancesPlugin.__init__(self) + def __init__(self, args=None): + GlancesPlugin.__init__(self, args=args) # We want to display the stat in the curse interface self.display_curse = True diff --git a/glances/plugins/glances_batpercent.py b/glances/plugins/glances_batpercent.py index d36269b4ec3885a2960a583f5583c85a03e60483..9993d3bf9fc79a81854d20b7268426f85520fd57 100644 --- a/glances/plugins/glances_batpercent.py +++ b/glances/plugins/glances_batpercent.py @@ -37,8 +37,8 @@ class Plugin(GlancesPlugin): stats is a list """ - def __init__(self): - GlancesPlugin.__init__(self) + def __init__(self, args=None): + GlancesPlugin.__init__(self, args=args) #!!! TODO: display plugin... diff --git a/glances/plugins/glances_core.py b/glances/plugins/glances_core.py index 906d489da0f775d9b46f8a64875bc9a8f4040ce2..d6096ff0ff1d91c76e8d2c6d42ecc31d32407fd8 100644 --- a/glances/plugins/glances_core.py +++ b/glances/plugins/glances_core.py @@ -30,8 +30,8 @@ class Plugin(GlancesPlugin): stats is integer (number of core) """ - def __init__(self): - GlancesPlugin.__init__(self) + def __init__(self, args=None): + GlancesPlugin.__init__(self, args=args) # We dot not want to display the stat in the curse interface # The core number is displayed by the load plugin diff --git a/glances/plugins/glances_cpu.py b/glances/plugins/glances_cpu.py index 41e2dd86f92be56e6af9deb348068c8af9e56ea4..e14805824f10b3136e682a319647384b5875fac7 100644 --- a/glances/plugins/glances_cpu.py +++ b/glances/plugins/glances_cpu.py @@ -39,8 +39,8 @@ class Plugin(GlancesPlugin): stats is a dict """ - def __init__(self): - GlancesPlugin.__init__(self) + def __init__(self, args=None): + GlancesPlugin.__init__(self, args=args) # We want to display the stat in the curse interface self.display_curse = True diff --git a/glances/plugins/glances_diskio.py b/glances/plugins/glances_diskio.py index 3e790d4330d885388b1de81df2c72466bef317e5..55e141550b7bc63397a871f6dbd3d83b3323716b 100644 --- a/glances/plugins/glances_diskio.py +++ b/glances/plugins/glances_diskio.py @@ -34,8 +34,8 @@ class Plugin(GlancesPlugin): stats is a list """ - def __init__(self): - GlancesPlugin.__init__(self) + def __init__(self, args=None): + GlancesPlugin.__init__(self, args=args) # We want to display the stat in the curse interface self.display_curse = True diff --git a/glances/plugins/glances_fs.py b/glances/plugins/glances_fs.py index 8a4fc58e84cbdc4600935b015a826c1f2c7daca1..7f3248b0b1d02c750292b8bd9d737354bf3483ef 100644 --- a/glances/plugins/glances_fs.py +++ b/glances/plugins/glances_fs.py @@ -50,8 +50,8 @@ class Plugin(GlancesPlugin): stats is a list """ - def __init__(self): - GlancesPlugin.__init__(self) + def __init__(self, args=None): + GlancesPlugin.__init__(self, args=args) # We want to display the stat in the curse interface self.display_curse = True diff --git a/glances/plugins/glances_hddtemp.py b/glances/plugins/glances_hddtemp.py index 67f0893fb54ccc1182cd4b13e9c3b70dbfaa2cba..d75c9562e383ad20a55ed20e1dbb46f6cf11400c 100644 --- a/glances/plugins/glances_hddtemp.py +++ b/glances/plugins/glances_hddtemp.py @@ -30,8 +30,9 @@ class Plugin(GlancesPlugin): stats is a list """ - def __init__(self): - GlancesPlugin.__init__(self) + + def __init__(self, args=None): + GlancesPlugin.__init__(self, args=args) # Init the sensor class self.glancesgrabhddtemp = glancesGrabHDDTemp() diff --git a/glances/plugins/glances_help.py b/glances/plugins/glances_help.py index 1144ea1140da4c25af1c81f45ad5ac2b50a326f1..ca47b3d978110d78acf4cf81a43e2b8cd252958b 100644 --- a/glances/plugins/glances_help.py +++ b/glances/plugins/glances_help.py @@ -35,8 +35,8 @@ class Plugin(GlancesPlugin): Glances' Help Plugin """ - def __init__(self): - GlancesPlugin.__init__(self) + def __init__(self, args=None): + GlancesPlugin.__init__(self, args=args) # We want to display the stat in the curse interface self.display_curse = True diff --git a/glances/plugins/glances_load.py b/glances/plugins/glances_load.py index 43cdf93ba2f59601b990acaab713ff9e3bcb48cf..1b00c2f199564c6510ae03e6b075bee71e55bf38 100644 --- a/glances/plugins/glances_load.py +++ b/glances/plugins/glances_load.py @@ -43,8 +43,8 @@ class Plugin(GlancesPlugin): stats is a dict """ - def __init__(self): - GlancesPlugin.__init__(self) + def __init__(self, args=None): + GlancesPlugin.__init__(self, args=args) # We want to display the stat in the curse interface self.display_curse = True diff --git a/glances/plugins/glances_mem.py b/glances/plugins/glances_mem.py index 6a3fbf8bd5c96da1d6fac3fc83f94cf846c0d79d..2bda09bf59bd8e42e3614d8a96b219920a95e1c7 100644 --- a/glances/plugins/glances_mem.py +++ b/glances/plugins/glances_mem.py @@ -46,8 +46,8 @@ class Plugin(GlancesPlugin): stats is a dict """ - def __init__(self): - GlancesPlugin.__init__(self) + def __init__(self, args=None): + GlancesPlugin.__init__(self, args=args) # We want to display the stat in the curse interface self.display_curse = True diff --git a/glances/plugins/glances_memswap.py b/glances/plugins/glances_memswap.py index 10a4a49df09333ab3ba21d03e73c96c6a62b91bd..c8b202a1f5c9552b8512f9ef31b7d144f994c26f 100644 --- a/glances/plugins/glances_memswap.py +++ b/glances/plugins/glances_memswap.py @@ -38,8 +38,8 @@ class Plugin(GlancesPlugin): stats is a dict """ - def __init__(self): - GlancesPlugin.__init__(self) + def __init__(self, args=None): + GlancesPlugin.__init__(self, args=args) # We want to display the stat in the curse interface self.display_curse = True diff --git a/glances/plugins/glances_monitor.py b/glances/plugins/glances_monitor.py index b8a497a5c9d7730bfbfb9b7f59eb5be42dd54db4..649fbcfff812c16a70b222fd331ecbe7650c13e3 100644 --- a/glances/plugins/glances_monitor.py +++ b/glances/plugins/glances_monitor.py @@ -27,8 +27,8 @@ class Plugin(GlancesPlugin): Glances's monitor Plugin """ - def __init__(self): - GlancesPlugin.__init__(self) + def __init__(self, args=None): + GlancesPlugin.__init__(self, args=args) # We want to display the stat in the curse interface self.display_curse = True diff --git a/glances/plugins/glances_network.py b/glances/plugins/glances_network.py index 847f88528c202b9a4b046f57b566353616bc1761..d3eae0ad8bc444318bc41448285084ae57135f22 100644 --- a/glances/plugins/glances_network.py +++ b/glances/plugins/glances_network.py @@ -42,8 +42,8 @@ class Plugin(GlancesPlugin): stats is a list """ - def __init__(self): - GlancesPlugin.__init__(self) + def __init__(self, args=None): + GlancesPlugin.__init__(self, args=args) # We want to display the stat in the curse interface self.display_curse = True diff --git a/glances/plugins/glances_now.py b/glances/plugins/glances_now.py index 7f9e76a0aebc50f0737f9460c90843cac2fba508..d4c751bfa85942a0bc47d9521f7e0e81ee601476 100644 --- a/glances/plugins/glances_now.py +++ b/glances/plugins/glances_now.py @@ -32,8 +32,8 @@ class Plugin(GlancesPlugin): stats is (string) """ - def __init__(self): - GlancesPlugin.__init__(self) + def __init__(self, args=None): + GlancesPlugin.__init__(self, args=args) # We want to display the stat in the curse interface self.display_curse = True diff --git a/glances/plugins/glances_percpu.py b/glances/plugins/glances_percpu.py index ae5801dc4aad83d813d0cd6633ebca61f14265eb..14e07c8c770b322d118f51cb521065671c5e3e37 100644 --- a/glances/plugins/glances_percpu.py +++ b/glances/plugins/glances_percpu.py @@ -34,8 +34,8 @@ class Plugin(GlancesPlugin): stats is a list """ - def __init__(self): - GlancesPlugin.__init__(self) + def __init__(self, args=None): + GlancesPlugin.__init__(self, args=args) # We want to display the stat in the curse interface self.display_curse = True diff --git a/glances/plugins/glances_plugin.py b/glances/plugins/glances_plugin.py index b5b62474e500e3960910cdb32e07a451b7cff3ad..66c08433f397ad74bfbd5538bbb961508546f6d2 100644 --- a/glances/plugins/glances_plugin.py +++ b/glances/plugins/glances_plugin.py @@ -33,10 +33,13 @@ class GlancesPlugin(object): Main class for Glances' plugin """ - def __init__(self): + def __init__(self, args=None): # Plugin name (= module name without glances_) self.plugin_name = self.__class__.__module__[len('glances_'):] + # Init the args + self.args = args + # Init the stats list self.stats = None @@ -61,7 +64,7 @@ class GlancesPlugin(object): from glances.core.glances_snmp import GlancesSNMPClient # Init the SNMP request - clientsnmp = GlancesSNMPClient() + clientsnmp = GlancesSNMPClient(host=self.args.client) # Process the SNMP request snmpresult = clientsnmp.get_by_oid(*snmp_oid.values()) diff --git a/glances/plugins/glances_processcount.py b/glances/plugins/glances_processcount.py index 66b48d3a122ff5d5a4edd64f8ad8dfc36b945a02..64a3152fef7ddbff81b79b1e4c4ffca9e5fac46f 100644 --- a/glances/plugins/glances_processcount.py +++ b/glances/plugins/glances_processcount.py @@ -29,8 +29,8 @@ class Plugin(GlancesPlugin): stats is a list """ - def __init__(self): - GlancesPlugin.__init__(self) + def __init__(self, args=None): + GlancesPlugin.__init__(self, args=args) # We want to display the stat in the curse interface self.display_curse = True diff --git a/glances/plugins/glances_processlist.py b/glances/plugins/glances_processlist.py index c188d4f9ceb4f23fe5a119d35903a26bf03237ab..1026034ed40135de80a781b62d9d597cb9698ed0 100644 --- a/glances/plugins/glances_processlist.py +++ b/glances/plugins/glances_processlist.py @@ -32,8 +32,8 @@ class Plugin(GlancesPlugin): stats is a list """ - def __init__(self): - GlancesPlugin.__init__(self) + def __init__(self, args=None): + GlancesPlugin.__init__(self, args=args) # We want to display the stat in the curse interface self.display_curse = True diff --git a/glances/plugins/glances_psutilversion.py b/glances/plugins/glances_psutilversion.py index c593a8ce053ec29010ad800a3881024203e43c3f..55e6756256b49717dc95ecef32e4844954258b81 100644 --- a/glances/plugins/glances_psutilversion.py +++ b/glances/plugins/glances_psutilversion.py @@ -29,8 +29,8 @@ class Plugin(GlancesPlugin): stats is a tuple """ - def __init__(self): - GlancesPlugin.__init__(self) + def __init__(self, args=None): + GlancesPlugin.__init__(self, args=args) self.reset() diff --git a/glances/plugins/glances_sensors.py b/glances/plugins/glances_sensors.py index ac0fa067fa9157666d0d11d983711051bdc350a9..30ec3cfd719c517da8eb39a05b7daf18bf708277 100644 --- a/glances/plugins/glances_sensors.py +++ b/glances/plugins/glances_sensors.py @@ -30,59 +30,6 @@ from glances.plugins.glances_hddtemp import Plugin as HddTempPlugin from glances.plugins.glances_plugin import GlancesPlugin -class glancesGrabSensors: - """ - Get sensors stats using the PySensors library - """ - - def __init__(self): - """ - Init sensors stats - """ - try: - sensors.init() - except Exception: - self.initok = False - else: - self.initok = True - - # Init the stats - self.reset() - - def reset(self): - """ - Reset/init the stats - """ - self.sensors_list = [] - - def __update__(self): - """ - Update the stats - """ - # Reset the list - self.reset() - - # grab only temperature stats - if self.initok: - for chip in sensors.iter_detected_chips(): - for feature in chip: - sensors_current = {} - if feature.name.startswith(b'temp'): - sensors_current['label'] = feature.label - sensors_current['value'] = int(feature.get_value()) - self.sensors_list.append(sensors_current) - - return self.sensors_list - - def get(self): - self.__update__() - return self.sensors_list - - def quit(self): - if self.initok: - sensors.cleanup() - - class Plugin(GlancesPlugin): """ Glances' sensors plugin @@ -92,8 +39,8 @@ class Plugin(GlancesPlugin): The hard disks are already sorted by name """ - def __init__(self): - GlancesPlugin.__init__(self) + def __init__(self, args=None): + GlancesPlugin.__init__(self, args=args) # Init the sensor class self.glancesgrabsensors = glancesGrabSensors() @@ -170,3 +117,56 @@ class Plugin(GlancesPlugin): ret.append(self.curse_add_line(msg)) return ret + + +class glancesGrabSensors: + """ + Get sensors stats using the PySensors library + """ + + def __init__(self): + """ + Init sensors stats + """ + try: + sensors.init() + except Exception: + self.initok = False + else: + self.initok = True + + # Init the stats + self.reset() + + def reset(self): + """ + Reset/init the stats + """ + self.sensors_list = [] + + def __update__(self): + """ + Update the stats + """ + # Reset the list + self.reset() + + # grab only temperature stats + if self.initok: + for chip in sensors.iter_detected_chips(): + for feature in chip: + sensors_current = {} + if feature.name.startswith(b'temp'): + sensors_current['label'] = feature.label + sensors_current['value'] = int(feature.get_value()) + self.sensors_list.append(sensors_current) + + return self.sensors_list + + def get(self): + self.__update__() + return self.sensors_list + + def quit(self): + if self.initok: + sensors.cleanup() diff --git a/glances/plugins/glances_system.py b/glances/plugins/glances_system.py index 9fe622dba7feefe0fdbe7a6feb7b4de4599ced8d..a676333aafc4f92336e0c16a093fea52442aa159 100644 --- a/glances/plugins/glances_system.py +++ b/glances/plugins/glances_system.py @@ -38,8 +38,8 @@ class Plugin(GlancesPlugin): stats is a dict """ - def __init__(self): - GlancesPlugin.__init__(self) + def __init__(self, args=None): + GlancesPlugin.__init__(self, args=args) # We want to display the stat in the curse interface self.display_curse = True diff --git a/glances/plugins/glances_uptime.py b/glances/plugins/glances_uptime.py index 96b9a7b69fb8809287f065d722a012c1ae0f7181..3343367176682e0497372d987da534a4d2a635ac 100644 --- a/glances/plugins/glances_uptime.py +++ b/glances/plugins/glances_uptime.py @@ -37,8 +37,8 @@ class Plugin(GlancesPlugin): stats is date (string) """ - def __init__(self): - GlancesPlugin.__init__(self) + def __init__(self, args=None): + GlancesPlugin.__init__(self, args=args) # We want to display the stat in the curse interface self.display_curse = True