summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authornicolargo <nicolas@nicolargo.com>2020-09-21 14:13:14 +0200
committernicolargo <nicolas@nicolargo.com>2020-09-21 14:13:14 +0200
commitca88320b5bff37916ee64658824dcd392bc8a86c (patch)
tree07cd6523c0b675be53b2761f346f5e8d3490f79f
parenta8d1ba5e4386b1fa68afc4a6bbe3d993db84a680 (diff)
New syntax to access to the key
-rw-r--r--docs/quickstart.rst19
-rw-r--r--glances/compat.py42
-rw-r--r--glances/outputs/glances_stdout.py12
-rw-r--r--glances/processes.py3
4 files changed, 62 insertions, 14 deletions
diff --git a/docs/quickstart.rst b/docs/quickstart.rst
index a8f960fe..566d0cfb 100644
--- a/docs/quickstart.rst
+++ b/docs/quickstart.rst
@@ -28,18 +28,13 @@ It is also possible to display RAW JSON stats directly to stdout using:
.. code-block:: console
- $ 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
- ...
+ $ glances --stdout load,cpu.user,mem.used,network:lo,processlist:456.cpu_percent
+
+Syntax (comma separated stats):
+ - <plugin>
+ - <plugin>.<attribute>
+ - <plugin>:<key value>
+ - <plugin>:<key value>.attribute
or in a CSV format thanks to the stdout-csv option:
diff --git a/glances/compat.py b/glances/compat.py
index 4f131544..00374a1d 100644
--- a/glances/compat.py
+++ b/glances/compat.py
@@ -268,7 +268,47 @@ def is_admin():
def get_stat_from_path(stats, stat_path):
"""
- For a path ['cpu, 'user'] or ['processlist', 'pid:534', 'name]
+ For a path ['cpu, 'user'] or ['network'] or ['processlist:6174', 'name']
+ Get the value in the stats.
+ """
+ ret = dict()
+ if ':' in stat_path[0]:
+ plugin, match = stat_path[0].split(':')
+ else:
+ plugin = stat_path[0]
+ match = None
+ attributes = stat_path[1:]
+
+ if plugin in stats.getPluginsList() and \
+ stats.get_plugin(plugin).is_enable():
+ # Get the stat
+ stat_export = stats.get_plugin(plugin).get_export()
+ if isinstance(stat_export, dict):
+ if len(attributes) > 0 and attributes[0] in stat_export:
+ ret['{}.{}'.format(plugin, attributes[0])] = stat_export[attributes[0]]
+ else:
+ for k, v in iteritems(stat_export):
+ ret['{}.{}'.format(plugin, k)] = v
+ elif isinstance(stat_export, list):
+ for i in stat_export:
+ if isinstance(i, dict) and 'key' in i and (not match or (match and str(i[i['key']]) == match)):
+ if len(attributes) > 0 and attributes[0] in i:
+ ret['{}.{}.{}'.format(plugin, i[i['key']], attributes[0])] = i[attributes[0]]
+ else:
+ for k, v in iteritems(i):
+ ret['{}.{}.{}'.format(plugin, i[i['key']], k)] = v
+ else:
+ ret[plugin] = stat_export
+ else:
+ logger.error('Plugin {} does not exist or is disabled'.format(plugin))
+
+ return ret
+
+
+
+def get_stat_from_path_old(stats, stat_path):
+ """
+ For a path ['cpu, 'user'] or ['processlist', 'pid=534', 'name']
Get the value in the stats.
"""
ret = None
diff --git a/glances/outputs/glances_stdout.py b/glances/outputs/glances_stdout.py
index 8291862a..d3fca1c9 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, get_stat_from_path
+from glances.compat import printandflush, get_stat_from_path, iteritems
class GlancesStdout(object):
@@ -42,6 +42,16 @@ class GlancesStdout(object):
def update(self, stats, duration=3):
for stat_name in self.args.stdout.split(','):
stat_path = stat_name.split('.')
+ stat_flatten = get_stat_from_path(stats, stat_path)
+ for k, v in iteritems(stat_flatten):
+ printandflush("{}: {}".format(k, v))
+ # Wait until next refresh
+ if duration > 0:
+ time.sleep(duration)
+
+ def update_old(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 \
diff --git a/glances/processes.py b/glances/processes.py
index f1291d59..a30c6845 100644
--- a/glances/processes.py
+++ b/glances/processes.py
@@ -340,6 +340,9 @@ class GlancesProcesses(object):
first = False
# /End of extended stats
+ # Add key
+ proc['key'] = 'pid'
+
# Time since last update (for disk_io rate computation)
proc['time_since_update'] = time_since_update