Merge branch 'issue1168' into develop

This commit is contained in:
nicolargo 2018-02-13 21:30:04 +01:00
commit 6096abaef9
7 changed files with 119 additions and 2 deletions

3
NEWS
View File

@ -20,6 +20,7 @@ Enhancements and new features:
* Take advantage of the PSUtil issue #1025 (Add process_iter(attrs, ad_value)) #1105
* Nice Process Priority Configuration #1218
* Display debug message if dep lib is not found #1224
* Add a new output mode to stdout #1168
Bugs corrected:
@ -56,6 +57,8 @@ News command line options:
Disable a list of comma separated plugins
--export exporter1,exporter2
Export stats to a comma separated exporters
--stdout plugin1,plugin2.attribute
Display stats to stdout
News configuration keys in the glances.conf file:

View File

@ -30,6 +30,10 @@ Command-Line Options
disable PLUGIN (comma separed list)
.. option:: --stdout PLUGINS_STATS
display stats to stdout (comma separated list of plugins/plugins.attribute)
.. option:: --export EXPORT
enable EXPORT module (comma separed list)

View File

@ -1,6 +1,6 @@
.\" Man page generated from reStructuredText.
.
.TH "GLANCES" "1" "Jan 18, 2018" "3.0_DEV" "Glances"
.TH "GLANCES" "1" "Feb 11, 2018" "3.0.dev0" "Glances"
.SH NAME
glances \- An eye on your system
.
@ -78,6 +78,11 @@ disable PLUGIN (comma separed list)
.UNINDENT
.INDENT 0.0
.TP
.B \-\-stdout PLUGINS_STATS
display stats to stdout (comma separated list of plugins/plugins.attribute)
.UNINDENT
.INDENT 0.0
.TP
.B \-\-export EXPORT
enable EXPORT module (comma separed list)
.UNINDENT

View File

@ -24,6 +24,19 @@ Glances should start (press 'q' or 'ESC' to exit):
.. image:: _static/screenshot-wide.png
It is also possible to display stats directly to stdout using:
.. code-block:: console
$ glances --stdout cpu.user,mem.used,load
cpu.user: 30.7
mem.used: 3278204928
load: {'cpucore': 4, 'min1': 0.21, 'min5': 0.4, 'min15': 0.27}
cpu.user: 3.4
mem.used: 3275251712
load: {'cpucore': 4, 'min1': 0.19, 'min5': 0.39, 'min15': 0.27}
...
Client/Server Mode
------------------

View File

@ -91,6 +91,9 @@ Examples of use:
Start the client browser (browser mode):
$ glances --browser
Display stats to stdout:
$ glances --stdout cpu.user,mem.used,load
Disable some plugins (any modes):
$ glances --disable-plugin network,ports
"""
@ -210,6 +213,8 @@ Examples of use:
dest='process_filter', help='set the process filter pattern (regular expression)')
parser.add_argument('--process-short-name', action='store_true', default=False,
dest='process_short_name', help='force short name for processes name')
parser.add_argument('--stdout', default=None,
dest='stdout', help='display stats to stdout (comma separated list of plugins/plugins.attribute)')
if not WINDOWS:
parser.add_argument('--hide-kernel-threads', action='store_true', default=False,
dest='no_kernel_threads', help='hide kernel threads in process list (not available on Windows)')

View File

@ -0,0 +1,82 @@
# -*- coding: utf-8 -*-
#
# This file is part of Glances.
#
# Copyright (C) 2018 Nicolargo <nicolas@nicolargo.com>
#
# Glances is free software; you can redistribute it and/or modify
# it under the terms of the GNU Lesser General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# Glances is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
"""Stdout interface class."""
import time
from glances.logger import logger
class GlancesStdout(object):
"""
This class manages the Stdout display.
"""
def __init__(self, config=None, args=None):
# Init
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
if plugin in stats.getPluginsList() and \
stats.get_plugin(plugin).is_enable():
stat = stats.get_plugin(plugin).get_export()
else:
continue
# Display stats
if attribute is not None:
# With attribute
try:
print("{}.{}: {}".format(plugin, attribute,
stat[attribute]))
except KeyError as err:
logger.error("Can not display stat {}.{} ({})".format(plugin, attribute, err))
else:
# Without attribute
print("{}: {}".format(plugin, stat))
# Wait until next refresh
time.sleep(duration)

View File

@ -26,6 +26,7 @@ from glances.logger import logger
from glances.processes import glances_processes
from glances.stats import GlancesStats
from glances.outputs.glances_curses import GlancesCursesStandalone
from glances.outputs.glances_stdout import GlancesStdout
from glances.outdated import Outdated
from glances.timer import Counter
@ -68,9 +69,13 @@ class GlancesStandalone(object):
self.stats.update()
if self.quiet:
logger.info("Quiet mode is ON: Nothing will be displayed")
logger.info("Quiet mode is ON, nothing will be displayed")
# In quiet mode, nothing is displayed
glances_processes.max_processes = 0
elif args.stdout:
logger.info("Stdout mode is ON, following stats will be displayed: {}".format(args.stdout))
# Init screen
self.screen = GlancesStdout(config=config, args=args)
else:
# Default number of processes to displayed is set to 50
glances_processes.max_processes = 50