summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authornicolargo <nicolas@nicolargo.com>2020-09-19 16:16:17 +0200
committernicolargo <nicolas@nicolargo.com>2020-09-19 16:16:17 +0200
commit7c074902b3824261c0ec0011291ad7089905a8cd (patch)
tree7926219ab5ad8c679b6eac731ca3076265b1c6c4
parent47b7dc9d5d76e2b6ee116c28656ca2d0b1403711 (diff)
Add atrributes to stdout processlist plugin #1733
-rw-r--r--docs/quickstart.rst6
-rw-r--r--glances/compat.py35
-rw-r--r--glances/outputs/glances_stdout.py45
3 files changed, 49 insertions, 37 deletions
diff --git a/docs/quickstart.rst b/docs/quickstart.rst
index 796d1707..a8f960fe 100644
--- a/docs/quickstart.rst
+++ b/docs/quickstart.rst
@@ -28,13 +28,17 @@ It is also possible to display RAW JSON stats directly to stdout using:
.. code-block:: console
- $ glances --stdout cpu.user,mem.used,load
+ $ glances --stdout cpu.user,mem.used,load,processlist.pid=456.name,processlist.pid=456.cpu_percent
cpu.user: 30.7
mem.used: 3278204928
load: {'cpucore': 4, 'min1': 0.21, 'min5': 0.4, 'min15': 0.27}
+ processlist.pid=456.name: terminator
+ processlist.pid=456.cpu_percent: 2.2
cpu.user: 3.4
mem.used: 3275251712
load: {'cpucore': 4, 'min1': 0.19, 'min5': 0.39, 'min15': 0.27}
+ processlist.pid=456.name: terminator
+ processlist.pid=456.cpu_percent: 2.6
...
or in a CSV format thanks to the stdout-csv option:
diff --git a/glances/compat.py b/glances/compat.py
index e16e8286..f9c97f34 100644
--- a/glances/compat.py
+++ b/glances/compat.py
@@ -264,3 +264,38 @@ def is_admin():
else:
# Check for root on Posix
return os.getuid() == 0
+
+
+def get_stat_from_path(stats, stat_path):
+ """
+ For a path ['cpu, 'user'] or ['processlist', 'pid:534', 'name]
+ Get the value in the stats.
+ """
+ ret = None
+ if len(stat_path) == 0:
+ ret = stats
+ elif len(stat_path) == 1 and stats:
+ try:
+ ret = stats[stat_path[0]]
+ except KeyError:
+ logger.error(
+ 'Key {} does not exist. Should be one of: {}'.format(stat_path[0],
+ ', '.join(stats.keys())))
+ elif stats:
+ if '=' in stat_path[0]:
+ stat, match = stat_path[0].split('=')
+ try:
+ match_dict = next(
+ (sub for sub in stats if str(sub[stat]) == str(match)), None)
+ except KeyError:
+ if len(stats) > 0:
+ logger.error(
+ 'Key {} does not exist. Should be one of: {}'.format(stat,
+ ', '.join(stats[0].keys())))
+ else:
+ logger.error('Key {} does not exist'.format(stat))
+ else:
+ ret = get_stat_from_path(match_dict, stat_path[1:])
+ else:
+ ret = get_stat_from_path(stats[stat_path[0]], stat_path[1:])
+ return ret
diff --git a/glances/outputs/glances_stdout.py b/glances/outputs/glances_stdout.py
index c3c1f390..e56dfe39 100644
--- a/glances/outputs/glances_stdout.py
+++ b/glances/outputs/glances_stdout.py
@@ -22,7 +22,7 @@
import time
from glances.logger import logger
-from glances.compat import printandflush
+from glances.compat import printandflush, get_stat_from_path
class GlancesStdout(object):
@@ -36,48 +36,21 @@ class GlancesStdout(object):
self.config = config
self.args = args
- # Build the list of plugin and/or plugin.attribute to display
- self.plugins_list = self.build_list()
-
- def build_list(self):
- """Return a list of tuples taken from self.args.stdout
- [(plugin, attribute), ... ]"""
- ret = []
- for p in self.args.stdout.split(','):
- if '.' in p:
- p, a = p.split('.')
- else:
- a = None
- ret.append((p, a))
- return ret
-
def end(self):
pass
- def update(self,
- stats,
- duration=3):
- """Display stats to stdout.
- Refresh every duration second.
- """
- for plugin, attribute in self.plugins_list:
- # Check if the plugin exist and is enable
+ def update(self, stats, duration=3):
+ for stat_name in self.args.stdout.split(','):
+ stat_path = stat_name.split('.')
+ plugin = stat_path[0]
+ stat_path = stat_path[1:]
if plugin in stats.getPluginsList() and \
stats.get_plugin(plugin).is_enable():
- stat = stats.get_plugin(plugin).get_export()
+ printandflush("{}: {}".format(stat_name,
+ get_stat_from_path(stats.get_plugin(plugin).get_export(),
+ stat_path)))
else:
continue
- # Display stats
- if attribute is not None:
- # With attribute
- try:
- printandflush("{}.{}: {}".format(plugin, attribute,
- stat[attribute]))
- except KeyError as err:
- logger.error("Can not display stat {}.{} ({})".format(plugin, attribute, err))
- else:
- # Without attribute
- printandflush("{}: {}".format(plugin, stat))
# Wait until next refresh
if duration > 0: