From c3ba3f64e6ea03e76b43ca97bd0b5b5537b7bf16 Mon Sep 17 00:00:00 2001 From: Nicolargo Date: Wed, 7 May 2014 11:06:53 +0200 Subject: [PATCH] Add args visibility to the plugins to handle with the SNMP server properties (address, community...) --- glances/core/glances_client.py | 21 ++--- glances/core/glances_snmp.py | 8 +- glances/core/glances_stats.py | 24 ++++- glances/plugins/glances_alert.py | 4 +- glances/plugins/glances_batpercent.py | 4 +- glances/plugins/glances_core.py | 4 +- glances/plugins/glances_cpu.py | 4 +- glances/plugins/glances_diskio.py | 4 +- glances/plugins/glances_fs.py | 4 +- glances/plugins/glances_hddtemp.py | 5 +- glances/plugins/glances_help.py | 4 +- glances/plugins/glances_load.py | 4 +- glances/plugins/glances_mem.py | 4 +- glances/plugins/glances_memswap.py | 4 +- glances/plugins/glances_monitor.py | 4 +- glances/plugins/glances_network.py | 4 +- glances/plugins/glances_now.py | 4 +- glances/plugins/glances_percpu.py | 4 +- glances/plugins/glances_plugin.py | 7 +- glances/plugins/glances_processcount.py | 4 +- glances/plugins/glances_processlist.py | 4 +- glances/plugins/glances_psutilversion.py | 4 +- glances/plugins/glances_sensors.py | 110 +++++++++++------------ glances/plugins/glances_system.py | 4 +- glances/plugins/glances_uptime.py | 4 +- 25 files changed, 132 insertions(+), 119 deletions(-) diff --git a/glances/core/glances_client.py b/glances/core/glances_client.py index c611155d..dd7b8d28 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 58b11a26..673cf6b5 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 4cb5578c..562df18a 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 8ac2897d..f3ce9f3e 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 d36269b4..9993d3bf 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 906d489d..d6096ff0 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 41e2dd86..e1480582 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 3e790d43..55e14155 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 8a4fc58e..7f3248b0 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 67f0893f..d75c9562 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 1144ea11..ca47b3d9 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 43cdf93b..1b00c2f1 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 6a3fbf8b..2bda09bf 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 10a4a49d..c8b202a1 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 b8a497a5..649fbcff 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 847f8852..d3eae0ad 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 7f9e76a0..d4c751bf 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 ae5801dc..14e07c8c 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 b5b62474..66c08433 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 66b48d3a..64a3152f 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 c188d4f9..1026034e 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 c593a8ce..55e67562 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 ac0fa067..30ec3cfd 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 9fe622db..a676333a 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 96b9a7b6..33433671 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 -- GitLab