diff --git a/glances/core/glances_limits.py b/glances/core/glances_limits.py index a6be900f..11d3e568 100644 --- a/glances/core/glances_limits.py +++ b/glances/core/glances_limits.py @@ -18,6 +18,13 @@ # You should have received a copy of the GNU Lesser General Public License # along with this program. If not, see . +# !!! +# Each plugin define its limits +# This class only browse all plugin and propose +# an API interface to display all the limits +# /!!! + + class glancesLimits: """ Manage limits for each stats. A limit can be: diff --git a/glances/core/glances_standalone.py b/glances/core/glances_standalone.py index 0c434702..2dd7ba42 100644 --- a/glances/core/glances_standalone.py +++ b/glances/core/glances_standalone.py @@ -27,6 +27,7 @@ import json from ..core.glances_limits import glancesLimits from ..core.glances_monitor_list import monitorList from ..core.glances_stats import GlancesStats +from ..outputs.glances_curses import glancesCurses class GlancesStandalone(): @@ -67,17 +68,17 @@ class GlancesStandalone(): # Init screen # !!! TODO # !!! Is refresh_time mandatory for this class ? - # self.screen = glancesScreen(refresh_time=refresh_time, - # use_bold=use_bold) + self.screen = glancesCurses(refresh_time=refresh_time, + use_bold=use_bold) def serve_forever(self): while True: - # Get system informations - self.update() + # Update system informations + self.stats.update() # Update the screen - self.display() + self.screen.update(self.stats) # Update the HTML output # !!! TODO @@ -88,17 +89,3 @@ class GlancesStandalone(): # !!! TODO # if csv_tag: # csvoutput.update(stats) - - - def update(self): - """ - Update the stats - """ - self.stats.update({}) - - - def display(self): - import time - time.sleep(self.refresh_time) - print self.stats.getCpu() - diff --git a/glances/core/glances_stats.py b/glances/core/glances_stats.py index 2c3eda20..d7439869 100644 --- a/glances/core/glances_stats.py +++ b/glances/core/glances_stats.py @@ -47,28 +47,34 @@ class GlancesStats: def __getattr__(self, item): """ Overwrite the getattr in case of attribute is not found - The goal is to dynamicaly generate the API get'Stats'() methods + The goal is to dynamicaly generate the following methods: + - getPlugname(): return Plugname stat in JSON format + - cursePlugname(): return Plugname stat in STR (for curses) """ - # print "!!! __getattr__ in the GlancesStats classe" - # print "!!! Method: %s" % item - header = 'get' # Check if the attribute starts with 'get' - if (item.startswith(header)): + if (item.startswith('get')): # Get the plugin name - plugname = item[len(header):].lower() + plugname = item[len('get'):].lower() # Get the plugin instance plugin = self._plugins[plugname] - # !!! Debug - # print "Check if method get_stats exist for plugin %s" % plugname - # print self._plugins - # print plugin if (hasattr(plugin, 'get_stats')): # The method get_stats exist, return it return getattr(plugin, 'get_stats') 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) diff --git a/glances/plugins/glances_core.py b/glances/plugins/glances_core.py index 4654d23c..38a144dc 100644 --- a/glances/plugins/glances_core.py +++ b/glances/plugins/glances_core.py @@ -36,6 +36,10 @@ class Plugin(GlancesPlugin): def __init__(self): GlancesPlugin.__init__(self) + # We dot not want to display the stat in the curse interface + # The core number is displayed by the load plugin + self.display_curse = False + def update(self): """ @@ -48,3 +52,5 @@ class Plugin(GlancesPlugin): self.stats = NUM_CPUS except Exception, e: self.stats = None + + return self.stats diff --git a/glances/plugins/glances_cpu.py b/glances/plugins/glances_cpu.py index c5ff0dac..95c2d692 100644 --- a/glances/plugins/glances_cpu.py +++ b/glances/plugins/glances_cpu.py @@ -35,6 +35,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 = 1 + def update(self): """ @@ -91,3 +100,78 @@ class Plugin(GlancesPlugin): self.cputime_total_old = self.cputime_total_new except Exception, err: self.stats = {} + + return self.stats + + + def msg_curse(self): + """ + Return the dict to display in the curse interface + """ + + # Init the return message + ret = [] + + # Build the string message + # Header + msg = "{0:7} ".format(_("CPU")) + ret.append(self.curse_add_line(msg, "BOLD")) + # Total CPU usage + msg = "{0}".format(format((100 - self.stats['idle']) / 100, '>6.1%')) + ret.append(self.curse_add_line(msg, "NORMAL")) + # Steal CPU usage + # !!! TODO: if steal not available + if ('steal' in self.stats): + msg = " {0:7} {1}".format( + _("steal:"), + format(self.stats['steal'] / 100, '>6.1%')) + ret.append(self.curse_add_line(msg, "NORMAL")) + else: + msg = "{0:>16}".format(" ") + ret.append(self.curse_add_line(msg, "NORMAL")) + # New line + ret.append(self.curse_new_line()) + # User CPU + if ('user' in self.stats): + msg = "{0:7} {1}".format( + _("user:"), + format(self.stats['user'] / 100, '>6.1%')) + ret.append(self.curse_add_line(msg, "NORMAL")) + # Nice CPU + if ('nice' in self.stats): + msg = " {0:7} {1}".format( + _("nice:"), + format(self.stats['user'] / 100, '>6.1%')) + ret.append(self.curse_add_line(msg, "NORMAL")) + # New line + ret.append(self.curse_new_line()) + # System CPU + if ('system' in self.stats): + msg = "{0:7} {1}".format( + _("system:"), + format(self.stats['system'] / 100, '>6.1%')) + ret.append(self.curse_add_line(msg, "NORMAL")) + # IOWait CPU + if ('iowait' in self.stats): + msg = " {0:7} {1}".format( + _("iowait:"), + format(self.stats['iowait'] / 100, '>6.1%')) + ret.append(self.curse_add_line(msg, "NORMAL")) + # New line + ret.append(self.curse_new_line()) + # Idles CPU + if ('idle' in self.stats): + msg = "{0:7} {1}".format( + _("idle:"), + format(self.stats['idle'] / 100, '>6.1%')) + ret.append(self.curse_add_line(msg, "NORMAL")) + # IRQ CPU + if ('irq' in self.stats): + msg = " {0:7} {1}".format( + _("irq:"), + format(self.stats['irq'] / 100, '>6.1%')) + ret.append(self.curse_add_line(msg, "NORMAL")) + + # Return the message with decoration + return ret + \ No newline at end of file diff --git a/glances/plugins/glances_load.py b/glances/plugins/glances_load.py index a963488f..8191c23f 100644 --- a/glances/plugins/glances_load.py +++ b/glances/plugins/glances_load.py @@ -24,6 +24,7 @@ from os import getloadavg # from ..plugins.glances_plugin import GlancesPlugin from glances_plugin import GlancesPlugin +from glances_core import Plugin as CorePlugin class Plugin(GlancesPlugin): """ @@ -35,6 +36,18 @@ class Plugin(GlancesPlugin): def __init__(self): GlancesPlugin.__init__(self) + # Instance for the CorePlugin in order to display the core number + self.core_plugin = CorePlugin() + + # 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 = 1 + # Enter -1 to diplay bottom + self.line_curse = 1 + def update(self): """ @@ -48,3 +61,44 @@ class Plugin(GlancesPlugin): 'min15': load[2]} except Exception, err: self.stats = {} + + return self.stats + + + def msg_curse(self): + """ + Return the dict to display in the curse interface + """ + # Init the return message + ret = [] + + # Build the string message + # Header + msg = "{0:4} ".format(_("LOAD")) + ret.append(self.curse_add_line(msg, "BOLD")) + # Core number + msg = "{0:3}-core".format(self.core_plugin.update()) + ret.append(self.curse_add_line(msg, "NORMAL")) + # New line + ret.append(self.curse_new_line()) + # 1min load + msg = "{0:7} {1}".format( + _("1 min:"), + format(self.stats['min1'], '>5.2f')) + ret.append(self.curse_add_line(msg, "NORMAL")) + # New line + ret.append(self.curse_new_line()) + # 5min load + msg = "{0:7} {1}".format( + _("5 min:"), + format(self.stats['min5'], '>5.2f')) + ret.append(self.curse_add_line(msg, "NORMAL")) + # New line + ret.append(self.curse_new_line()) + # 15min load + msg = "{0:7} {1}".format( + _("15 min:"), + format(self.stats['min15'], '>5.2f')) + ret.append(self.curse_add_line(msg, "NORMAL")) + + return ret \ No newline at end of file diff --git a/glances/plugins/glances_now.py b/glances/plugins/glances_now.py index 83917a94..8eb74ef8 100644 --- a/glances/plugins/glances_now.py +++ b/glances/plugins/glances_now.py @@ -35,6 +35,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 = -1 + # Enter -1 to diplay bottom + self.line_curse = -1 + def update(self): """ @@ -45,3 +54,5 @@ class Plugin(GlancesPlugin): # Had to convert it to string because datetime is not JSON serializable self.stats = datetime.now().strftime(_("%Y-%m-%d %H:%M:%S")) + return self.stats + diff --git a/glances/plugins/glances_plugin.py b/glances/plugins/glances_plugin.py index d2091db4..e69d292d 100644 --- a/glances/plugins/glances_plugin.py +++ b/glances/plugins/glances_plugin.py @@ -66,3 +66,55 @@ class GlancesPlugin(object): def get_stats(self): # Return the stats object in JSON format for the RPC API return json.dumps(self.stats) + + + def msg_curse(self): + """ + Return default string to display in the curse interface + """ + return [ self.curse_add_line(str(self.stats)) ] + + + def get_curse(self): + # Return a dict with all the information needed to display the stat + # key | description + #---------------------------- + # display | Display the stat (True or False) + # msgdict | Message to display (list of dict [{ 'msg': msg, 'decoration': decoration } ... ]) + # column | column number + # line | Line number + + display_curse = False + column_curse = -1 + line_curse = -1 + + if (hasattr(self, 'display_curse')): + display_curse = self.display_curse + if (hasattr(self, 'column_curse')): + column_curse = self.column_curse + if (hasattr(self, 'line_curse')): + line_curse = self.line_curse + + return { 'display': display_curse, + 'msgdict': self.msg_curse(), + 'column': column_curse, + 'line': line_curse } + + + def curse_add_line(self, msg, decoration="NORMAL"): + """ + Return a dict with: { 'msg': msg, 'decoration': decoration } + with: + msg: string + decoration: NORMAL (no decoration), UNDERLINE, BOLD, REVERSE + """ + + return { 'msg': msg, 'decoration': decoration } + + + def curse_new_line(self): + """ + Go to a new line + """ + + return self.curse_add_line('\n') diff --git a/glances/plugins/glances_system.py b/glances/plugins/glances_system.py index 0041203a..58c9911a 100644 --- a/glances/plugins/glances_system.py +++ b/glances/plugins/glances_system.py @@ -34,9 +34,21 @@ class Plugin(GlancesPlugin): def __init__(self): GlancesPlugin.__init__(self) - # self.update() + + # 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 = 0 + def update(self): + """ + Update the host/system info + """ self.stats = {} self.stats['os_name'] = platform.system() self.stats['hostname'] = platform.node() @@ -58,3 +70,34 @@ class Plugin(GlancesPlugin): self.stats['os_version'] = ' '.join(os_version[::2]) else: self.stats['os_version'] = "" + + + def msg_curse(self): + """ + Return the string to display in the curse interface + """ + + # Init the return message + ret = [] + + # Build the string message + if (self.stats['os_name'] == "Linux"): + msg = _("{0} ({1} {2} / {3} {4})").format( + self.stats['hostname'], + self.stats['linux_distro'], + self.stats['platform'], + self.stats['os_name'], + self.stats['os_version']) + else: + msg = _("{0} ({1} {2} {3})").format( + self.stats['hostname'], + self.stats['os_name'], + self.stats['os_version'], + self.stats['platform']) + + # Add the line with decoration + ret.append(self.curse_add_line(msg, "UNDERLINE")) + + # Return the message with decoration + return ret + \ No newline at end of file diff --git a/glances/plugins/glances_uptime.py b/glances/plugins/glances_uptime.py index 3359bb1d..8f754d38 100644 --- a/glances/plugins/glances_uptime.py +++ b/glances/plugins/glances_uptime.py @@ -32,7 +32,7 @@ from glances_plugin import GlancesPlugin class Plugin(GlancesPlugin): """ - Glances' Core Plugin + Glances' Uptime Plugin Get stats about uptime stats is date (string) @@ -41,6 +41,14 @@ 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 = -1 + # Enter -1 to diplay bottom + self.line_curse = 0 def update(self): """