From 5c4d3eca4da61bcf81ed79a4d7d58521b5306cb9 Mon Sep 17 00:00:00 2001 From: Nicolas Hennion Date: Tue, 11 Feb 2014 16:44:59 +0100 Subject: [PATCH] Manage args between all module --- conf/glances-monitor.conf | 8 ++--- glances/__init__.py | 3 +- glances/core/glances_main.py | 24 ++++++++++++--- glances/core/glances_standalone.py | 8 ++--- glances/core/glances_stats.py | 25 ++++++++-------- glances/plugins/glances_cpu.py | 2 +- glances/plugins/glances_load.py | 2 +- glances/plugins/glances_mem.py | 2 +- glances/plugins/glances_memswap.py | 2 +- glances/plugins/glances_network.py | 48 ++++++++++++++++++++++++++---- glances/plugins/glances_plugin.py | 6 ++-- glances/plugins/glances_system.py | 2 +- glances/plugins/glances_uptime.py | 2 +- 13 files changed, 94 insertions(+), 40 deletions(-) diff --git a/conf/glances-monitor.conf b/conf/glances-monitor.conf index dee997cf..ba8a92ef 100644 --- a/conf/glances-monitor.conf +++ b/conf/glances-monitor.conf @@ -52,6 +52,10 @@ careful=50 warning=70 critical=90 +[network] +# Define the list of hidden network interfaces (comma separeted) +#hide=lo + [temperature] # Temperatures in °C for sensors # Default values if not defined: 60/70/80 @@ -89,10 +93,6 @@ mem_critical=90 # Define the list of hidden disks (comma separeted) #hide=sda2,sda5 -[network] -# Define the list of hidden network interfaces (comma separeted) -#hide=lo - [monitor] # Define the list of processes to monitor # *** This section is optional *** diff --git a/glances/__init__.py b/glances/__init__.py index a3393639..89fbdfbc 100644 --- a/glances/__init__.py +++ b/glances/__init__.py @@ -32,7 +32,8 @@ def main(argv=None): from .core.glances_standalone import GlancesStandalone # Init the standalone mode - standalone = GlancesStandalone(config=core.config, + standalone = GlancesStandalone(config=core.get_config(), + args=core.get_args(), refresh_time=core.refresh_time, use_bold=core.use_bold) diff --git a/glances/core/glances_main.py b/glances/core/glances_main.py index ad0df71a..ae2f00ab 100644 --- a/glances/core/glances_main.py +++ b/glances/core/glances_main.py @@ -78,7 +78,7 @@ class GlancesMain(object): def __init__(self): # Init and manage command line arguments self.init_arg() - self.parse_arg() + self.args = self.parse_arg() # Read the configuration file if (self.conf_file_tag): @@ -220,6 +220,7 @@ class GlancesMain(object): self.parser.add_argument('-f', '--file', help=_('set the html output folder or csv file')) + def parse_arg(self): """ Parse command line argument @@ -227,7 +228,10 @@ class GlancesMain(object): args = self.parser.parse_args() - # Change global variables regarding to user args + # Default refresh time is 3 seconds + if (args.time is None): args.time = 3 + + # !!! Change global variables regarding to user args # !!! To be refactor to use directly the args list in the code if (args.time is not None): self.refresh_time = args.time self.network_bytepersec_tag = args.byte @@ -263,9 +267,10 @@ class GlancesMain(object): if (args.file is not None): output_file = args.file output_folder = args.file + # /!!! + + return args - # !!! Debug - # print args def __get_password(self, description='', confirm=False): """ @@ -288,26 +293,37 @@ class GlancesMain(object): sys.stdout.write(_("[Warning] Passwords did not match, please try again...\n")) return self.__get_password(description=description, confirm=confirm) + def is_standalone(self): """ Return True if Glances is running in standalone mode """ return not self.client_tag and not self.server_tag + def is_client(self): """ Return True if Glances is running in client mode """ return self.client_tag and not self.server_tag + def is_server(self): """ Return True if Glances is running in sserver mode """ return not self.client_tag and self.server_tag + def get_config(self): """ Return configuration file object """ return self.config + + + def get_args(self): + """ + Return the arguments + """ + return self.args \ No newline at end of file diff --git a/glances/core/glances_standalone.py b/glances/core/glances_standalone.py index 0bf45f7b..40fab08f 100644 --- a/glances/core/glances_standalone.py +++ b/glances/core/glances_standalone.py @@ -37,6 +37,7 @@ class GlancesStandalone(): def __init__(self, config=None, + args=None, refresh_time=3, use_bold=True): # Init the limits @@ -52,7 +53,7 @@ class GlancesStandalone(): # !!! The first time Glances display wrong CPU information self.stats.update() - self.refresh_time = refresh_time + self.refresh_time = args.time # Init HTML output # !!! TODO @@ -67,10 +68,7 @@ class GlancesStandalone(): # refresh_time=refresh_time) # Init screen - # !!! TODO - # !!! Is refresh_time mandatory for this class ? - self.screen = glancesCurses(refresh_time=refresh_time, - use_bold=use_bold) + self.screen = glancesCurses(args=args) def serve_forever(self): diff --git a/glances/core/glances_stats.py b/glances/core/glances_stats.py index fe793db3..a0194136 100644 --- a/glances/core/glances_stats.py +++ b/glances/core/glances_stats.py @@ -52,7 +52,6 @@ class GlancesStats(object): Overwrite the getattr in case of attribute is not found The goal is to dynamicaly generate the following methods: - getPlugname(): return Plugname stat in JSON format - - cursePlugname(): return Plugname stat in STR (for curses) """ # Check if the attribute starts with 'get' @@ -67,17 +66,6 @@ class GlancesStats(object): else: # The method get_stats is not found for the plugin raise AttributeError(item) - elif (item.startswith('curse')): - # Get the plugin name - plugname = item[len('curse'):].lower() - # Get the plugin instance - plugin = self._plugins[plugname] - if (hasattr(plugin, 'get_curse')): - # The method get_curse exist, return it - return getattr(plugin, 'get_curse') - else: - # The method get_curse is not found for the plugin - raise AttributeError(item) else: # Default behavior raise AttributeError(item) @@ -130,6 +118,19 @@ class GlancesStats(object): self.__update__(input_stats) + def get_plugin_list(self): + # Return the plugin list + self._plugins + + + def get_plugin(self, plugin_name): + # Return the plugin name + if (plugin_name in self._plugins): + return self._plugins[plugin_name] + else: + return None + + class GlancesStatsServer(GlancesStats): def __init__(self): diff --git a/glances/plugins/glances_cpu.py b/glances/plugins/glances_cpu.py index 8b2b31b9..8d78e111 100644 --- a/glances/plugins/glances_cpu.py +++ b/glances/plugins/glances_cpu.py @@ -104,7 +104,7 @@ class Plugin(GlancesPlugin): return self.stats - def msg_curse(self): + def msg_curse(self, args=None): """ Return the dict to display in the curse interface """ diff --git a/glances/plugins/glances_load.py b/glances/plugins/glances_load.py index 4f1bc0a1..5fe8e836 100644 --- a/glances/plugins/glances_load.py +++ b/glances/plugins/glances_load.py @@ -65,7 +65,7 @@ class Plugin(GlancesPlugin): return self.stats - def msg_curse(self): + def msg_curse(self, args=None): """ Return the dict to display in the curse interface """ diff --git a/glances/plugins/glances_mem.py b/glances/plugins/glances_mem.py index efbfbe31..35254caa 100644 --- a/glances/plugins/glances_mem.py +++ b/glances/plugins/glances_mem.py @@ -101,7 +101,7 @@ class Plugin(GlancesPlugin): self.stats = {} - def msg_curse(self): + def msg_curse(self, args=None): """ Return the dict to display in the curse interface """ diff --git a/glances/plugins/glances_memswap.py b/glances/plugins/glances_memswap.py index ff5d6622..19831948 100644 --- a/glances/plugins/glances_memswap.py +++ b/glances/plugins/glances_memswap.py @@ -75,7 +75,7 @@ class Plugin(GlancesPlugin): self.stats = {} - def msg_curse(self): + def msg_curse(self, args=None): """ Return the dict to display in the curse interface """ diff --git a/glances/plugins/glances_network.py b/glances/plugins/glances_network.py index daefdd44..5f54ebce 100644 --- a/glances/plugins/glances_network.py +++ b/glances/plugins/glances_network.py @@ -44,6 +44,15 @@ class Plugin(GlancesPlugin): def __init__(self): GlancesPlugin.__init__(self) + # We want to display the stat in the curse interface + self.display_curse = True + # Set the message position + # It is NOT the curse position but the Glances column/line + # Enter -1 to right align + self.column_curse = 0 + # Enter -1 to diplay bottom + self.line_curse = 2 + def update(self): """ @@ -100,8 +109,37 @@ class Plugin(GlancesPlugin): self.stats = network - def get_stats(self): - # Return the stats object for the RPC API - # !!! Sort it by interface name (why do it here ? Better in client side ?) - self.stats = sorted(self.stats, key=lambda network: network['interface_name']) - return GlancesPlugin.get_stats(self) + def msg_curse(self, args=None): + """ + Return the dict to display in the curse interface + """ + # Init the return message + ret = [] + + # Build the string message + # Header + msg = "{0:6}".format(_("NETWORK")) + ret.append(self.curse_add_line(msg, "TITLE")) + msg = "{0:>7} {1:>7}".format(_("Rx/s"), _("Tx/s")) + ret.append(self.curse_add_line(msg)) + # Interface list (sorted by name) + for i in sorted(self.stats, key=lambda network: network['interface_name']): + # Format stats + ifname = i['interface_name'].split(':')[0] + if (args.byte): + rxps = self.auto_unit(int(i['rx'] // i['time_since_update'])) + txps = self.auto_unit(int(i['tx'] // i['time_since_update'])) + else: + rxps = self.auto_unit(int(i['rx'] // i['time_since_update'] * 8)) + "b" + txps = self.auto_unit(int(i['tx'] // i['time_since_update'] * 8)) + "b" + + # !!! TODO: manage the hide tag + + # New line + ret.append(self.curse_new_line()) + msg = "{0:7}".format(ifname) + ret.append(self.curse_add_line(msg)) + msg = "{0:>7} {1:>7}".format(rxps, txps) + ret.append(self.curse_add_line(msg)) + + return ret diff --git a/glances/plugins/glances_plugin.py b/glances/plugins/glances_plugin.py index 1f703fe6..2e4d186b 100644 --- a/glances/plugins/glances_plugin.py +++ b/glances/plugins/glances_plugin.py @@ -155,14 +155,14 @@ class GlancesPlugin(object): return self.limits[self.plugin_name + '_' + 'careful'] - def msg_curse(self): + def msg_curse(self, args): """ Return default string to display in the curse interface """ return [ self.curse_add_line(str(self.stats)) ] - def get_curse(self): + def get_curse(self, args=None): # Return a dict with all the information needed to display the stat # key | description #---------------------------- @@ -183,7 +183,7 @@ class GlancesPlugin(object): line_curse = self.line_curse return { 'display': display_curse, - 'msgdict': self.msg_curse(), + 'msgdict': self.msg_curse(args), 'column': column_curse, 'line': line_curse } diff --git a/glances/plugins/glances_system.py b/glances/plugins/glances_system.py index 8c8e0378..29eff3f2 100644 --- a/glances/plugins/glances_system.py +++ b/glances/plugins/glances_system.py @@ -72,7 +72,7 @@ class Plugin(GlancesPlugin): self.stats['os_version'] = "" - def msg_curse(self): + def msg_curse(self, args=None): """ Return the string to display in the curse interface """ diff --git a/glances/plugins/glances_uptime.py b/glances/plugins/glances_uptime.py index deac01ad..637f7e74 100644 --- a/glances/plugins/glances_uptime.py +++ b/glances/plugins/glances_uptime.py @@ -67,7 +67,7 @@ class Plugin(GlancesPlugin): self.stats = str(self.uptime).split('.')[0] - def msg_curse(self): + def msg_curse(self, args=None): """ Return the string to display in the curse interface """ -- GitLab