Add args visibility to the plugins to handle with the SNMP server properties (address, community...)

This commit is contained in:
Nicolargo 2014-05-07 11:06:53 +02:00
parent 2fec33adf3
commit c3ba3f64e6
25 changed files with 132 additions and 119 deletions

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@ -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()

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@ -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())

View File

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

View File

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

View File

@ -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()

View File

@ -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()

View File

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

View File

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