提交 abec1a72 编写于 作者: A Alessio Sergi

Replace getters/setters with properties - Round 4

上级 56c69617
......@@ -183,13 +183,13 @@ class GlancesStats(object):
def getAllLimits(self):
"""Return the plugins limits list."""
return [self._plugins[p].get_limits() for p in self._plugins]
return [self._plugins[p].limits for p in self._plugins]
def getAllLimitsAsDict(self):
"""Return all the stats limits (dict)"""
ret = {}
for p in self._plugins:
ret[p] = self._plugins[p].get_limits()
ret[p] = self._plugins[p].limits
return ret
def getAllViews(self):
......@@ -387,7 +387,8 @@ class GlancesStatsClientSNMP(GlancesStats):
# For each plugins, call the update method
for p in self._plugins:
# Set the input method to SNMP
self._plugins[p].set_input('snmp', self.system_name)
self._plugins[p].input_method = 'snmp'
self._plugins[p].short_system_name = self.system_name
try:
self._plugins[p].update()
except Exception as e:
......
......@@ -234,7 +234,7 @@ class GlancesBottle(object):
try:
# Get the JSON value of the stat limits
ret = self.stats.get_plugin(plugin).get_limits()
ret = self.stats.get_plugin(plugin).limits
except Exception as e:
abort(404, "Cannot get limits for plugin %s (%s)" % (plugin, str(e)))
return ret
......
......@@ -42,7 +42,7 @@ class Plugin(GlancesPlugin):
self.display_curse = True
# Set the message position
self.set_align('bottom')
self.align = 'bottom'
# Init the stats
self.reset()
......
......@@ -61,12 +61,12 @@ class Plugin(GlancesPlugin):
# Reset stats
self.reset()
if self.get_input() == 'local':
if self.input_method == 'local':
# Update stats
self.glancesgrabbat.update()
self.stats = self.glancesgrabbat.get()
elif self.get_input() == 'snmp':
elif self.input_method == 'snmp':
# Update stats using SNMP
# Not avalaible
pass
......
......@@ -56,7 +56,7 @@ class Plugin(GlancesPlugin):
# Reset the stats
self.reset()
if self.get_input() == 'local':
if self.input_method == 'local':
# Update stats using the standard system lib
# The PSUtil 2.0 include psutil.cpu_count() and psutil.cpu_count(logical=False)
......@@ -70,7 +70,7 @@ class Plugin(GlancesPlugin):
except NameError:
self.reset()
elif self.get_input() == 'snmp':
elif self.input_method == 'snmp':
# Update stats using SNMP
# http://stackoverflow.com/questions/5662467/how-to-find-out-the-number-of-cpus-using-snmp
pass
......
......@@ -78,7 +78,7 @@ class Plugin(GlancesPlugin):
# Grab CPU stats using psutil's cpu_percent and cpu_times_percent
# methods
if self.get_input() == 'local':
if self.input_method == 'local':
# Get all possible values for CPU stats: user, system, idle,
# nice (UNIX), iowait (Linux), irq (Linux, FreeBSD), steal (Linux 2.6.11+)
# The following stats are returned by the API but not displayed in the UI:
......@@ -89,14 +89,14 @@ class Plugin(GlancesPlugin):
'irq', 'softirq', 'steal', 'guest', 'guest_nice']:
if hasattr(cpu_times_percent, stat):
self.stats[stat] = getattr(cpu_times_percent, stat)
elif self.get_input() == 'snmp':
elif self.input_method == 'snmp':
# Update stats using SNMP
if self.get_short_system_name() in ('windows', 'esxi'):
if self.short_system_name in ('windows', 'esxi'):
# Windows or VMWare ESXi
# You can find the CPU utilization of windows system by querying the oid
# Give also the number of core (number of element in the table)
try:
cpu_stats = self.get_stats_snmp(snmp_oid=snmp_oid[self.get_short_system_name()],
cpu_stats = self.get_stats_snmp(snmp_oid=snmp_oid[self.short_system_name],
bulk=True)
except KeyError:
self.reset()
......@@ -118,7 +118,7 @@ class Plugin(GlancesPlugin):
# Default behavor
try:
self.stats = self.get_stats_snmp(
snmp_oid=snmp_oid[self.get_short_system_name()])
snmp_oid=snmp_oid[self.short_system_name])
except KeyError:
self.stats = self.get_stats_snmp(
snmp_oid=snmp_oid['default'])
......
......@@ -66,7 +66,7 @@ class Plugin(GlancesPlugin):
# Reset stats
self.reset()
if self.get_input() == 'local':
if self.input_method == 'local':
# Update stats using the standard system lib
# Grab the stat using the PsUtil disk_io_counters method
# read_count: number of reads
......@@ -114,7 +114,7 @@ class Plugin(GlancesPlugin):
# Save stats to compute next bitrate
self.diskio_old = diskio_new
elif self.get_input() == 'snmp':
elif self.input_method == 'snmp':
# Update stats using SNMP
# No standard way for the moment...
pass
......
......@@ -131,7 +131,7 @@ class Plugin(GlancesPlugin):
if not docker_tag or (self.args is not None and self.args.disable_docker):
return self.stats
if self.get_input() == 'local':
if self.input_method == 'local':
# Update stats
# Exemple: {
# "KernelVersion": "3.16.4-tinycore64",
......@@ -159,7 +159,7 @@ class Plugin(GlancesPlugin):
# !!! Refresh problem
# c['network'] = self.get_docker_network(c['Id'])
elif self.get_input() == 'snmp':
elif self.input_method == 'snmp':
# Update stats using SNMP
# Not available
pass
......
......@@ -93,7 +93,7 @@ class Plugin(GlancesPlugin):
# Reset the list
self.reset()
if self.get_input() == 'local':
if self.input_method == 'local':
# Update stats using the standard system lib
# Grab the stats using the PsUtil disk_partitions
......@@ -133,19 +133,19 @@ class Plugin(GlancesPlugin):
fs_current['key'] = self.get_key()
self.stats.append(fs_current)
elif self.get_input() == 'snmp':
elif self.input_method == 'snmp':
# Update stats using SNMP
# SNMP bulk command to get all file system in one shot
try:
fs_stat = self.get_stats_snmp(snmp_oid=snmp_oid[self.get_short_system_name()],
fs_stat = self.get_stats_snmp(snmp_oid=snmp_oid[self.short_system_name],
bulk=True)
except KeyError:
fs_stat = self.get_stats_snmp(snmp_oid=snmp_oid['default'],
bulk=True)
# Loop over fs
if self.get_short_system_name() in ('windows', 'esxi'):
if self.short_system_name in ('windows', 'esxi'):
# Windows or ESXi tips
for fs in fs_stat:
# Memory stats are grabed in the same OID table (ignore it)
......
......@@ -58,7 +58,7 @@ class Plugin(GlancesPlugin):
# Reset stats
self.reset()
if self.get_input() == 'local':
if self.input_method == 'local':
# Update stats using the standard system lib
self.stats = self.glancesgrabhddtemp.get()
......
......@@ -61,7 +61,7 @@ class Plugin(GlancesPlugin):
# Reset stats
self.reset()
if self.get_input() == 'local' and netifaces_tag:
if self.input_method == 'local' and netifaces_tag:
# Update stats using the netifaces lib
try:
default_gw = netifaces.gateways()['default'][netifaces.AF_INET]
......@@ -76,7 +76,7 @@ class Plugin(GlancesPlugin):
except KeyError as e:
logger.debug("Can not grab IP information (%s)".format(e))
elif self.get_input() == 'snmp':
elif self.input_method == 'snmp':
# Not implemented yet
pass
......
......@@ -76,7 +76,7 @@ class Plugin(GlancesPlugin):
# Reset stats
self.reset()
if self.get_input() == 'local':
if self.input_method == 'local':
# Update stats using the standard system lib
# Get the load using the os standard lib
......@@ -89,7 +89,7 @@ class Plugin(GlancesPlugin):
'min5': load[1],
'min15': load[2],
'cpucore': self.nb_log_core}
elif self.get_input() == 'snmp':
elif self.input_method == 'snmp':
# Update stats using SNMP
self.stats = self.get_stats_snmp(snmp_oid=snmp_oid)
......
......@@ -78,7 +78,7 @@ class Plugin(GlancesPlugin):
# Reset stats
self.reset()
if self.get_input() == 'local':
if self.input_method == 'local':
# Update stats using the standard system lib
# Grab MEM using the PSUtil virtual_memory method
vm_stats = psutil.virtual_memory()
......@@ -112,12 +112,12 @@ class Plugin(GlancesPlugin):
self.stats['free'] += self.stats['cached']
# used=total-free
self.stats['used'] = self.stats['total'] - self.stats['free']
elif self.get_input() == 'snmp':
elif self.input_method == 'snmp':
# Update stats using SNMP
if self.get_short_system_name() in ('windows', 'esxi'):
if self.short_system_name in ('windows', 'esxi'):
# Mem stats for Windows|Vmware Esxi are stored in the FS table
try:
fs_stat = self.get_stats_snmp(snmp_oid=snmp_oid[self.get_short_system_name()],
fs_stat = self.get_stats_snmp(snmp_oid=snmp_oid[self.short_system_name],
bulk=True)
except KeyError:
self.reset()
......
......@@ -67,7 +67,7 @@ class Plugin(GlancesPlugin):
# Reset stats
self.reset()
if self.get_input() == 'local':
if self.input_method == 'local':
# Update stats using the standard system lib
# Grab SWAP using the PSUtil swap_memory method
sm_stats = psutil.swap_memory()
......@@ -84,12 +84,12 @@ class Plugin(GlancesPlugin):
'sin', 'sout']:
if hasattr(sm_stats, swap):
self.stats[swap] = getattr(sm_stats, swap)
elif self.get_input() == 'snmp':
elif self.input_method == 'snmp':
# Update stats using SNMP
if self.get_short_system_name() == 'windows':
if self.short_system_name == 'windows':
# Mem stats for Windows OS are stored in the FS table
try:
fs_stat = self.get_stats_snmp(snmp_oid=snmp_oid[self.get_short_system_name()],
fs_stat = self.get_stats_snmp(snmp_oid=snmp_oid[self.short_system_name],
bulk=True)
except KeyError:
self.reset()
......
......@@ -47,7 +47,7 @@ class Plugin(GlancesPlugin):
def update(self):
"""Update the monitored list."""
if self.get_input() == 'local':
if self.input_method == 'local':
# Monitor list only available in a full Glances environment
# Check if the glances_monitor instance is init
if self.glances_monitors is None:
......
......@@ -75,7 +75,7 @@ class Plugin(GlancesPlugin):
# Reset stats
self.reset()
if self.get_input() == 'local':
if self.input_method == 'local':
# Update stats using the standard system lib
# Grab network interface stat using the PsUtil net_io_counter method
......@@ -123,12 +123,12 @@ class Plugin(GlancesPlugin):
# Save stats to compute next bitrate
self.network_old = network_new
elif self.get_input() == 'snmp':
elif self.input_method == 'snmp':
# Update stats using SNMP
# SNMP bulk command to get all network interface in one shot
try:
netiocounters = self.get_stats_snmp(snmp_oid=snmp_oid[self.get_short_system_name()],
netiocounters = self.get_stats_snmp(snmp_oid=snmp_oid[self.short_system_name],
bulk=True)
except KeyError:
netiocounters = self.get_stats_snmp(snmp_oid=snmp_oid['default'],
......@@ -154,7 +154,7 @@ class Plugin(GlancesPlugin):
netstat = {}
# Windows: a tips is needed to convert HEX to TXT
# http://blogs.technet.com/b/networking/archive/2009/12/18/how-to-query-the-list-of-network-interfaces-using-snmp-via-the-ifdescr-counter.aspx
if self.get_short_system_name() == 'windows':
if self.short_system_name == 'windows':
try:
netstat['interface_name'] = str(base64.b16decode(net[2:-2].upper()))
except TypeError:
......
......@@ -39,7 +39,7 @@ class Plugin(GlancesPlugin):
self.display_curse = True
# Set the message position
self.set_align('bottom')
self.align = 'bottom'
def update(self):
"""Update current date/time."""
......
......@@ -53,7 +53,7 @@ class Plugin(GlancesPlugin):
# Grab per-CPU stats using psutil's cpu_percent(percpu=True) and
# cpu_times_percent(percpu=True) methods
if self.get_input() == 'local':
if self.input_method == 'local':
percpu_percent = psutil.cpu_percent(interval=0.0, percpu=True)
percpu_times_percent = psutil.cpu_times_percent(interval=0.0, percpu=True)
for cputimes in percpu_times_percent:
......
......@@ -49,11 +49,11 @@ class GlancesPlugin(object):
self.args = args
# Init the default alignement (for curses)
self.set_align('left')
self._align = 'left'
# Init the input method
self.input_method = 'local'
self.short_system_name = None
self._input_method = 'local'
self._short_system_name = None
# Init the stats list
self.stats = None
......@@ -63,7 +63,7 @@ class GlancesPlugin(object):
self.stats_history = self.init_stats_history()
# Init the limits dictionnary
self.limits = dict()
self._limits = dict()
# Init the actions
self.actions = GlancesActions()
......@@ -135,25 +135,30 @@ class GlancesPlugin(object):
"""Return the items history list"""
return self.items_history_list
def set_input(self, input_method, short_system_name=None):
@property
def input_method(self):
"""Get the input method."""
return self._input_method
@input_method.setter
def input_method(self, input_method):
"""Set the input method.
* local: system local grab (psutil or direct access)
* snmp: Client server mode via SNMP
* glances: Client server mode via Glances API
For SNMP, short_system_name is detected short OS name
"""
self.input_method = input_method
self.short_system_name = short_system_name
self._input_method = input_method
def get_input(self):
"""Get the input method."""
return self.input_method
@property
def short_system_name(self):
"""Get the short detected OS name (SNMP)."""
return self._short_system_name
def get_short_system_name(self):
"""Get the short detected OS name"""
return self.short_system_name
@short_system_name.setter
def short_system_name(self, short_name):
"""Set the short detected OS name (SNMP)."""
self._short_system_name = short_name
def set_stats(self, input_stats):
"""Set the stats to input_stats."""
......@@ -325,24 +330,24 @@ class GlancesPlugin(object):
"""Load the limits from the configuration file."""
if (hasattr(config, 'has_section') and
config.has_section(self.plugin_name)):
for s, v in config.items(self.plugin_name):
for level, v in config.items(self.plugin_name):
# Read limits
limit = '_'.join([self.plugin_name, level])
try:
self.limits[
self.plugin_name + '_' + s] = config.get_option(self.plugin_name, s)
self._limits[limit] = config.get_option(self.plugin_name, level)
except ValueError:
self.limits[
self.plugin_name + '_' + s] = config.get_raw_option(self.plugin_name, s).split(",")
logger.debug("Load limit: {0} = {1}".format(self.plugin_name + '_' + s,
self.limits[self.plugin_name + '_' + s]))
def set_limits(self, input_limits):
"""Set the limits to input_limits."""
self.limits = input_limits
self._limits[limit] = config.get_raw_option(self.plugin_name, level).split(",")
logger.debug("Load limit: {0} = {1}".format(limit, self._limits[limit]))
def get_limits(self):
@property
def limits(self):
"""Return the limits object."""
return self.limits
return self._limits
@limits.setter
def limits(self, input_limits):
"""Set the limits to input_limits."""
self._limits = input_limits
def get_alert(self, current=0, minimum=0, maximum=100, header="", log=False):
"""Return the alert status relative to a current value.
......@@ -435,11 +440,11 @@ class GlancesPlugin(object):
# Get the limit for stat + header
# Exemple: network_wlan0_rx_careful
try:
limit = self.limits[stat_name + '_' + criticity]
limit = self._limits[stat_name + '_' + criticity]
except KeyError:
# Try fallback to plugin default limit
# Exemple: network_careful
limit = self.limits[self.plugin_name + '_' + criticity]
limit = self._limits[self.plugin_name + '_' + criticity]
# Return the limit
return limit
......@@ -449,11 +454,11 @@ class GlancesPlugin(object):
# Get the action for stat + header
# Exemple: network_wlan0_rx_careful_action
try:
ret = self.limits[stat_name + '_' + criticity + '_action']
ret = self._limits[stat_name + '_' + criticity + '_action']
except KeyError:
# Try fallback to plugin default limit
# Exemple: network_careful_action
ret = self.limits[self.plugin_name + '_' + criticity + '_action']
ret = self._limits[self.plugin_name + '_' + criticity + '_action']
# Return the action list
return ret
......@@ -463,12 +468,12 @@ class GlancesPlugin(object):
# Get the log tag for stat + header
# Exemple: network_wlan0_rx_log
try:
log_tag = self.limits[stat_name + '_log']
log_tag = self._limits[stat_name + '_log']
except KeyError:
# Try fallback to plugin default log
# Exemple: network_log
try:
log_tag = self.limits[self.plugin_name + '_log']
log_tag = self._limits[self.plugin_name + '_log']
except KeyError:
# By defaukt, log are disabled
return default_action
......@@ -482,12 +487,12 @@ class GlancesPlugin(object):
plugin_name = self.plugin_name
if header == "":
try:
return self.limits[plugin_name + '_' + value]
return self._limits[plugin_name + '_' + value]
except KeyError:
return []
else:
try:
return self.limits[plugin_name + '_' + header + '_' + value]
return self._limits[plugin_name + '_' + header + '_' + value]
except KeyError:
return []
......@@ -498,7 +503,7 @@ class GlancesPlugin(object):
def has_alias(self, header):
"""Return the alias name for the relative header or None if nonexist"""
try:
return self.limits[self.plugin_name + '_' + header + '_' + 'alias'][0]
return self._limits[self.plugin_name + '_' + header + '_' + 'alias'][0]
except (KeyError, IndexError):
return None
......@@ -520,7 +525,7 @@ class GlancesPlugin(object):
if hasattr(self, 'display_curse'):
display_curse = self.display_curse
if hasattr(self, 'align'):
align_curse = self.align
align_curse = self._align
if max_width is not None:
ret = {'display': display_curse,
......@@ -566,16 +571,18 @@ class GlancesPlugin(object):
"""Go to a new line."""
return self.curse_add_line('\n')
def set_align(self, align='left'):
"""Set the Curse align"""
if align in ('left', 'right', 'bottom'):
self.align = align
else:
self.align = 'left'
@property
def align(self):
"""Get the curse align."""
return self._align
def get_align(self):
"""Get the Curse align"""
return self.align
@align.setter
def align(self, value):
"""Set the curse align.
value: left, right, bottom.
"""
self._align = value
def auto_unit(self, number, low_precision=False):
"""Make a nice human-readable string out of number.
......
......@@ -52,14 +52,14 @@ class Plugin(GlancesPlugin):
# Reset stats
self.reset()
if self.get_input() == 'local':
if self.input_method == 'local':
# Update stats using the standard system lib
# Here, update is call for processcount AND processlist
glances_processes.update()
# Return the processes count
self.stats = glances_processes.getcount()
elif self.get_input() == 'snmp':
elif self.input_method == 'snmp':
# Update stats using SNMP
# !!! TODO
pass
......
......@@ -59,7 +59,7 @@ class Plugin(GlancesPlugin):
# Reset stats
self.reset()
if self.get_input() == 'local':
if self.input_method == 'local':
# Update stats using the standard system lib
# Note: Update is done in the processcount plugin
# Just return the processes list
......@@ -67,7 +67,7 @@ class Plugin(GlancesPlugin):
self.stats = glances_processes.gettree()
else:
self.stats = glances_processes.getlist()
elif self.get_input() == 'snmp':
elif self.input_method == 'snmp':
# No SNMP grab for processes
pass
......
......@@ -45,7 +45,7 @@ class Plugin(GlancesPlugin):
self.reset()
# Return PsUtil version as a tuple
if self.get_input() == 'local':
if self.input_method == 'local':
# PsUtil version only available in local
try:
self.stats = tuple([int(num) for num in __psutil_version.split('.')])
......
......@@ -55,13 +55,13 @@ class Plugin(GlancesPlugin):
self.reset()
# Grab quicklook stats: CPU, MEM and SWAP
if self.get_input() == 'local':
if self.input_method == 'local':
# Get the latest CPU percent value
self.stats['cpu'] = cpu_percent.get()
# Use the PsUtil lib for the memory (virtual and swap)
self.stats['mem'] = psutil.virtual_memory().percent
self.stats['swap'] = psutil.swap_memory().percent
elif self.get_input() == 'snmp':
elif self.input_method == 'snmp':
# Not available
pass
......
......@@ -57,7 +57,7 @@ class Plugin(GlancesPlugin):
# Reset stats
self.reset()
if self.get_input() == 'local':
if self.input_method == 'local':
# Update stats using the PyMDstat lib (https://github.com/nicolargo/pymdstat)
try:
mds = MdStat()
......@@ -66,7 +66,7 @@ class Plugin(GlancesPlugin):
logger.debug("Can not grab RAID stats (%s)" % e)
return self.stats
elif self.get_input() == 'snmp':
elif self.input_method == 'snmp':
# Update stats using SNMP
# No standard way for the moment...
pass
......
......@@ -83,7 +83,7 @@ class Plugin(GlancesPlugin):
# Reset the stats
self.reset()
if self.get_input() == 'local':
if self.input_method == 'local':
# Update stats using the dedicated lib
self.stats = []
# Get the temperature
......@@ -123,7 +123,7 @@ class Plugin(GlancesPlugin):
# Append Batteries %
self.stats.extend(batpercent)
elif self.get_input() == 'snmp':
elif self.input_method == 'snmp':
# Update stats using SNMP
# No standard:
# http://www.net-snmp.org/wiki/index.php/Net-SNMP_and_lm-sensors_on_Ubuntu_10.04
......
......@@ -103,7 +103,7 @@ class Plugin(GlancesPlugin):
# Reset stats
self.reset()
if self.get_input() == 'local':
if self.input_method == 'local':
# Update stats using the standard system lib
self.stats['os_name'] = platform.system()
self.stats['hostname'] = platform.node()
......@@ -136,17 +136,17 @@ class Plugin(GlancesPlugin):
self.stats['os_name'], self.stats['os_version'])
self.stats['hr_name'] += ' ({0})'.format(self.stats['platform'])
elif self.get_input() == 'snmp':
elif self.input_method == 'snmp':
# Update stats using SNMP
try:
self.stats = self.get_stats_snmp(
snmp_oid=snmp_oid[self.get_short_system_name()])
snmp_oid=snmp_oid[self.short_system_name])
except KeyError:
self.stats = self.get_stats_snmp(snmp_oid=snmp_oid['default'])
# Default behavor: display all the information
self.stats['os_name'] = self.stats['system_name']
# Windows OS tips
if self.get_short_system_name() == 'windows':
if self.short_system_name == 'windows':
try:
iteritems = snmp_to_human['windows'].iteritems()
except AttributeError:
......
......@@ -47,7 +47,7 @@ class Plugin(GlancesPlugin):
self.display_curse = True
# Set the message position
self.set_align('right')
self.align = 'right'
# Init the stats
self.reset()
......@@ -61,14 +61,14 @@ class Plugin(GlancesPlugin):
# Reset stats
self.reset()
if self.get_input() == 'local':
if self.input_method == 'local':
# Update stats using the standard system lib
uptime = datetime.now() - \
datetime.fromtimestamp(psutil.boot_time())
# Convert uptime to string (because datetime is not JSONifi)
self.stats = str(uptime).split('.')[0]
elif self.get_input() == 'snmp':
elif self.input_method == 'snmp':
# Update stats using SNMP
uptime = self.get_stats_snmp(snmp_oid=snmp_oid)['_uptime']
try:
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册