summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authornicolargo <nicolas@nicolargo.com>2022-06-12 10:11:46 +0200
committernicolargo <nicolas@nicolargo.com>2022-06-12 10:11:46 +0200
commite403b0e4c38a8f738d53b466ae82a282f705af1b (patch)
treea7a89fa744e7fe8373025a60e5b4efaab5f5c49f
parentf24c8f4077cad59afd08fc5ed2b1db850fdf0d87 (diff)
Add a Json stdout option #2060
-rw-r--r--README.rst9
-rw-r--r--docs/quickstart.rst17
-rw-r--r--glances/main.py13
-rw-r--r--glances/outputs/glances_stdout_json.py66
-rw-r--r--glances/standalone.py6
5 files changed, 105 insertions, 6 deletions
diff --git a/README.rst b/README.rst
index ca552967..c06a4206 100644
--- a/README.rst
+++ b/README.rst
@@ -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
diff --git a/docs/quickstart.rst b/docs/quickstart.rst
index 796d1707..a32a42c6 100644
--- a/docs/quickstart.rst
+++ b/docs/quickstart.rst
@@ -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
diff --git a/glances/main.py b/glances/main.py
index 5b8d63a9..75417c1f 100644
--- a/glances/main.py
+++ b/glances/main.py
@@ -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
@@ -409,10 +412,16 @@ Examples of use:
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',
diff --git a/glances/outputs/glances_stdout_json.py b/glances/outputs/glances_stdout_json.py
new file mode 100644
index 00000000..cb8e8507
--- /dev/null
+++ b/glances/outputs/glances_stdout_json.py
@@ -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)
diff --git a/glances/standalone.py b/glances/standalone.py
index 2240ecf9..1c3590f4 100644
--- a/glances/standalone.py
+++ b/glances/standalone.py
@@ -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