New syntax to access to the key

This commit is contained in:
nicolargo 2020-09-21 14:13:14 +02:00
parent a8d1ba5e43
commit ca88320b5b
4 changed files with 62 additions and 14 deletions

View File

@ -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:

View File

@ -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

View File

@ -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):
@ -40,6 +40,16 @@ class GlancesStdout(object):
pass
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]

View File

@ -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