mirror of https://github.com/nicolargo/glances.git
Report implementation of #1733 to stdout-csv
This commit is contained in:
parent
ca88320b5b
commit
cadd32dac5
|
|
@ -31,6 +31,9 @@ It is also possible to display RAW JSON stats directly to stdout using:
|
|||
$ glances --stdout load,cpu.user,mem.used,network:lo,processlist:456.cpu_percent
|
||||
|
||||
Syntax (comma separated stats):
|
||||
|
||||
.. code-block:: console
|
||||
|
||||
- <plugin>
|
||||
- <plugin>.<attribute>
|
||||
- <plugin>:<key value>
|
||||
|
|
|
|||
|
|
@ -303,43 +303,3 @@ def get_stat_from_path(stats, stat_path):
|
|||
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
|
||||
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:
|
||||
try:
|
||||
ret = get_stat_from_path(stats[stat_path[0]], stat_path[1:])
|
||||
except TypeError:
|
||||
logger.error(
|
||||
'Multiple {}, please select one using {}=<foo>'.format(stat_path[0], stat_path[0]))
|
||||
return ret
|
||||
|
|
|
|||
|
|
@ -48,21 +48,3 @@ class GlancesStdout(object):
|
|||
# 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 \
|
||||
stats.get_plugin(plugin).is_enable():
|
||||
printandflush("{}: {}".format(stat_name,
|
||||
get_stat_from_path(stats.get_plugin(plugin).get_export(),
|
||||
stat_path)))
|
||||
else:
|
||||
logger.error('Plugin {} does not exist or is disabled'.format(plugin))
|
||||
continue
|
||||
|
||||
# Wait until next refresh
|
||||
if duration > 0:
|
||||
time.sleep(duration)
|
||||
|
|
|
|||
|
|
@ -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, iteritems
|
||||
|
||||
|
||||
class GlancesStdoutCsv(object):
|
||||
|
|
@ -42,70 +42,9 @@ class GlancesStdoutCsv(object):
|
|||
# Display the header only on the first line
|
||||
self.header = True
|
||||
|
||||
# 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_csv.split(','):
|
||||
if '.' in p:
|
||||
p, a = p.split('.')
|
||||
else:
|
||||
a = None
|
||||
ret.append((p, a))
|
||||
return ret
|
||||
|
||||
def end(self):
|
||||
pass
|
||||
|
||||
def build_header(self, plugin, attribute, stat):
|
||||
"""Build and return the header line"""
|
||||
line = ''
|
||||
|
||||
if attribute is not None:
|
||||
line += '{}.{}{}'.format(plugin, attribute, self.separator)
|
||||
else:
|
||||
if isinstance(stat, dict):
|
||||
for k in stat.keys():
|
||||
line += '{}.{}{}'.format(plugin,
|
||||
str(k),
|
||||
self.separator)
|
||||
elif isinstance(stat, list):
|
||||
for i in stat:
|
||||
if isinstance(i, dict) and 'key' in i:
|
||||
for k in i.keys():
|
||||
line += '{}.{}.{}{}'.format(plugin,
|
||||
str(i['key']),
|
||||
str(k),
|
||||
self.separator)
|
||||
else:
|
||||
line += '{}{}'.format(plugin, self.separator)
|
||||
|
||||
return line
|
||||
|
||||
def build_data(self, plugin, attribute, stat):
|
||||
"""Build and return the data line"""
|
||||
line = ''
|
||||
|
||||
if attribute is not None:
|
||||
line += '{}{}'.format(str(stat.get(attribute, self.na)),
|
||||
self.separator)
|
||||
else:
|
||||
if isinstance(stat, dict):
|
||||
for v in stat.values():
|
||||
line += '{}{}'.format(str(v), self.separator)
|
||||
elif isinstance(stat, list):
|
||||
for i in stat:
|
||||
if isinstance(i, dict) and 'key' in i:
|
||||
for v in i.values():
|
||||
line += '{}{}'.format(str(v), self.separator)
|
||||
else:
|
||||
line += '{}{}'.format(str(stat), self.separator)
|
||||
|
||||
return line
|
||||
|
||||
def update(self,
|
||||
stats,
|
||||
duration=3):
|
||||
|
|
@ -113,26 +52,17 @@ class GlancesStdoutCsv(object):
|
|||
Refresh every duration second.
|
||||
"""
|
||||
# Build the stats list
|
||||
line = ''
|
||||
for plugin, attribute in self.plugins_list:
|
||||
# Check if the plugin exist and is enable
|
||||
if plugin in stats.getPluginsList() and \
|
||||
stats.get_plugin(plugin).is_enable():
|
||||
stat = stats.get_plugin(plugin).get_export()
|
||||
else:
|
||||
continue
|
||||
stat_flatten = dict()
|
||||
for stat_name in self.args.stdout_csv.split(','):
|
||||
stat_path = stat_name.split('.')
|
||||
stat_flatten.update(get_stat_from_path(stats, stat_path))
|
||||
|
||||
# Build the line to display (header or data)
|
||||
if self.header:
|
||||
line += self.build_header(plugin, attribute, stat)
|
||||
else:
|
||||
line += self.build_data(plugin, attribute, stat)
|
||||
if self.header:
|
||||
# Display header one time
|
||||
printandflush(self.separator.join(stat_flatten.keys()))
|
||||
self.header = False
|
||||
|
||||
# Display the line (without the last 'separator')
|
||||
printandflush(line[:-1])
|
||||
|
||||
# Display header one time
|
||||
self.header = False
|
||||
printandflush(self.separator.join([str(i) for i in stat_flatten.values()]))
|
||||
|
||||
# Wait until next refresh
|
||||
if duration > 0:
|
||||
|
|
|
|||
Loading…
Reference in New Issue