Add a Json stdout option #2060

pull/2064/head
nicolargo 2022-06-12 10:11:46 +02:00
parent f24c8f4077
commit e403b0e4c3
5 changed files with 105 additions and 6 deletions

View File

@ -414,6 +414,15 @@ or in a CSV format thanks to the stdout-csv option:
2018-12-08 22:04:23 CEST,5.4,5949136896,4,1.04,0.99,1.04
...
or in a JSON format thanks to the stdout-json option (attibute not supported in this mode in order to have a real JSON object in output):
.. code-block:: console
$ glances --stdout-json cpu,mem
cpu: {"total": 29.0, "user": 24.7, "nice": 0.0, "system": 3.8, "idle": 71.4, "iowait": 0.0, "irq": 0.0, "softirq": 0.0, "steal": 0.0, "guest": 0.0, "guest_nice": 0.0, "time_since_update": 1, "cpucore": 4, "ctx_switches": 0, "interrupts": 0, "soft_interrupts": 0, "syscalls": 0}
mem: {"total": 7837949952, "available": 2919079936, "percent": 62.8, "used": 4918870016, "free": 2919079936, "active": 2841214976, "inactive": 3340550144, "buffers": 546799616, "cached": 3068141568, "shared": 788156416}
...
and RTFM, always.
Documentation

View File

@ -24,7 +24,7 @@ Glances should start (press 'q' or 'ESC' to exit):
.. image:: _static/screenshot-wide.png
It is also possible to display RAW JSON stats directly to stdout using:
It is also possible to display RAW (Python) stats directly to stdout using:
.. code-block:: console
@ -47,6 +47,15 @@ or in a CSV format thanks to the stdout-csv option:
2018-12-08 22:04:23 CEST,5.4,5949136896,4,1.04,0.99,1.04
...
or as a JSON format thanks to the stdout-json option (attibute not supported in this mode):
.. code-block:: console
$ glances --stdout-json cpu,mem
cpu: {"total": 29.0, "user": 24.7, "nice": 0.0, "system": 3.8, "idle": 71.4, "iowait": 0.0, "irq": 0.0, "softirq": 0.0, "steal": 0.0, "guest": 0.0, "guest_nice": 0.0, "time_since_update": 1, "cpucore": 4, "ctx_switches": 0, "interrupts": 0, "soft_interrupts": 0, "syscalls": 0}
mem: {"total": 7837949952, "available": 2919079936, "percent": 62.8, "used": 4918870016, "free": 2919079936, "active": 2841214976, "inactive": 3340550144, "buffers": 546799616, "cached": 3068141568, "shared": 788156416}
...
Note: It will display one line per stat per refresh.
Client/Server Mode
@ -179,14 +188,14 @@ You can set a password to access to the server using the ``--password``.
By default, the login is ``glances`` but you can change it with
``--username``.
If you want, the SHA password will be stored in ``<login>.pwd`` file (in
the same folder where the Glances configuration file is stored, so
If you want, the SHA password will be stored in ``<login>.pwd`` file (in
the same folder where the Glances configuration file is stored, so
~/.config/glances/ on GNU Linus operating system).
Next time your run the server/client, password will not be asked. To set a
specific username you can used the -u <username> option.
It is also possible to set the default password in the Glances configuration
It is also possible to set the default password in the Glances configuration
file:
.. code-block:: ini

View File

@ -83,9 +83,12 @@ Examples of use:
Start the client browser (browser mode):
$ glances --browser
Display stats to stdout (one stat per line):
Display stats to stdout (one stat per line, possible to go inside stats using plugin.attribute):
$ glances --stdout now,cpu.user,mem.used,load
Display JSON stats to stdout (one stats per line):
$ glances --stdout-json now,cpu,mem,load
Display CSV stats to stdout (all stats in one line):
$ glances --stdout-csv now,cpu.user,mem.used,load
@ -408,11 +411,17 @@ Examples of use:
dest='stdout',
help='display stats to stdout, one stat per line (comma separated list of plugins/plugins.attribute)',
)
parser.add_argument(
'--stdout-json',
default=None,
dest='stdout_json',
help='display stats to stdout, JSON format (comma separated list of plugins/plugins.attribute)',
)
parser.add_argument(
'--stdout-csv',
default=None,
dest='stdout_csv',
help='display stats to stdout, csv format (comma separated list of plugins/plugins.attribute)',
help='display stats to stdout, CSV format (comma separated list of plugins/plugins.attribute)',
)
parser.add_argument(
'--issue',

View File

@ -0,0 +1,66 @@
# -*- coding: utf-8 -*-
#
# This file is part of Glances.
#
# Copyright (C) 2022 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
from glances.compat import printandflush
class GlancesStdoutJson(object):
"""This class manages the Stdout JSON display."""
def __init__(self, config=None, args=None):
# Init
self.config = config
self.args = args
# Build the list of plugin to display
self.plugins_list = self.build_list()
def build_list(self):
"""Return a list of tuples taken from self.args.stdout_json
:return: A list of tuples. Example -[(plugin, attribute), ... ]
"""
return self.args.stdout_json.split(',')
def end(self):
pass
def update(self, stats, duration=3):
"""Display stats in JSON format to stdout.
Refresh every duration second.
"""
for plugin in self.plugins_list:
# Check if the plugin exist and is enable
if plugin in stats.getPluginsList() and stats.get_plugin(plugin).is_enabled():
stat = stats.get_plugin(plugin).get_json()
else:
continue
# Display stats
printandflush('{}: {}'.format(plugin, stat))
# Wait until next refresh
if duration > 0:
time.sleep(duration)

View File

@ -24,10 +24,12 @@ import time
from glances.globals import WINDOWS
from glances.logger import logger
from glances.outputs.glances_stdout_json import GlancesStdoutJson
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.outputs.glances_stdout_json import GlancesStdoutJson
from glances.outputs.glances_stdout_csv import GlancesStdoutCsv
from glances.outputs.glances_stdout_issue import GlancesStdoutIssue
from glances.outputs.glances_stdout_apidoc import GlancesStdoutApiDoc
@ -96,6 +98,10 @@ class GlancesStandalone(object):
logger.info("Stdout mode is ON, following stats will be displayed: {}".format(args.stdout))
# Init screen
self.screen = GlancesStdout(config=config, args=args)
elif args.stdout_json:
logger.info("Stdout JSON mode is ON, following stats will be displayed: {}".format(args.stdout_json))
# Init screen
self.screen = GlancesStdoutJson(config=config, args=args)
elif args.stdout_csv:
logger.info("Stdout CSV mode is ON, following stats will be displayed: {}".format(args.stdout_csv))
# Init screen