summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJude <jude@wrapp.com>2017-08-01 18:16:20 +0200
committerJude <jude@wrapp.com>2017-08-01 19:19:14 +0200
commit12ae6708112f11e1b592aac50c6e369f2b039b1b (patch)
tree4fd40378b915b51758979529c2542e3f368f0359
parent38d66874807c45603dd66c5636a4035ec9dfe980 (diff)
Add JSON export plugin
-rw-r--r--NEWS1
-rw-r--r--docs/gw/index.rst1
-rw-r--r--docs/gw/json.rst16
-rw-r--r--glances/exports/glances_json.py64
-rw-r--r--glances/main.py2
5 files changed, 84 insertions, 0 deletions
diff --git a/NEWS b/NEWS
index 1968a010..90f835a9 100644
--- a/NEWS
+++ b/NEWS
@@ -9,6 +9,7 @@ Enhancements and new features:
* [WIP] Refactoring of the WebUI
* New export plugin: standard and configurable Restfull exporter (issue #1129)
+ * Add a JSON export module
Bugs corrected:
diff --git a/docs/gw/index.rst b/docs/gw/index.rst
index 4e9c1e23..c4a18528 100644
--- a/docs/gw/index.rst
+++ b/docs/gw/index.rst
@@ -10,6 +10,7 @@ to providing stats to multiple services (see list below).
:maxdepth: 2
csv
+ json
cassandra
couchdb
elastic
diff --git a/docs/gw/json.rst b/docs/gw/json.rst
new file mode 100644
index 00000000..44330d12
--- /dev/null
+++ b/docs/gw/json.rst
@@ -0,0 +1,16 @@
+.. _json:
+
+JSON
+====
+
+It's possible to export stats to a JSON file to be processed later by an
+external system.
+
+.. code-block:: console
+
+ $ glances --export-json /tmp/glances.json
+
+JSON file description:
+
+Each line would contain a JSON glance of the system. If this file needs to be
+processed it should be processed in a line by line basis.
diff --git a/glances/exports/glances_json.py b/glances/exports/glances_json.py
new file mode 100644
index 00000000..e54202a5
--- /dev/null
+++ b/glances/exports/glances_json.py
@@ -0,0 +1,64 @@
+"""JSON interface class."""
+
+import sys
+import json
+
+from glances.compat import PY3, listkeys
+from glances.logger import logger
+from glances.exports.glances_export import GlancesExport
+
+
+class Export(GlancesExport):
+
+ """This class manages the JSON export module."""
+
+ def __init__(self, config=None, args=None):
+ """Init the JSON export IF."""
+ super(Export, self).__init__(config=config, args=args)
+
+ # JSON file name
+ self.json_filename = args.export_json
+
+ # Set the JSON output file
+ try:
+ if PY3:
+ self.json_file = open(self.json_filename, 'w')
+ else:
+ self.json_file = open(self.json_filename, 'wb')
+ except IOError as e:
+ logger.critical("Cannot create the JSON file: {}".format(e))
+ sys.exit(2)
+
+ logger.info("Exporting stats to file: {}".format(self.json_filename))
+
+ self.export_enable = True
+
+ # Buffer for dict of stats
+ self.buffer = {}
+
+ def exit(self):
+ """Close the JSON file."""
+ logger.debug("Finalise export interface %s" % self.export_name)
+ self.json_file.close()
+
+ def export(self, name, columns, points):
+ """Export the stats to the JSON file."""
+
+ # Check for completion of loop for all exports
+ if name == self.plugins_to_export()[0] and self.buffer != {}:
+ # One whole loop has been completed
+ # Flush stats to file
+ logger.debug("Exporting stats ({}) to JSON file ({})".format(
+ listkeys(self.buffer),
+ self.json_filename)
+ )
+
+ # Export stats to JSON file
+ data_json = json.dumps(self.buffer)
+ self.json_file.write("{}\n".format(data_json))
+
+ # Reset buffer
+ self.buffer = {}
+
+ # Add current stat to the buffer
+ self.buffer[name] = dict(zip(columns, points))
diff --git a/glances/main.py b/glances/main.py
index 058e6a79..c63d91a3 100644
--- a/glances/main.py
+++ b/glances/main.py
@@ -173,6 +173,8 @@ Examples of use:
dest='path_graph', help='set the export path for graphs (default is {})'.format(tempfile.gettempdir()))
parser.add_argument('--export-csv', default=None,
dest='export_csv', help='export stats to a CSV file')
+ parser.add_argument('--export-json', default=None,
+ dest='export_json', help='export stats to a JSON file')
parser.add_argument('--export-cassandra', action='store_true', default=False,
dest='export_cassandra', help='export stats to a Cassandra or Scylla server (cassandra lib needed)')
parser.add_argument('--export-couchdb', action='store_true', default=False,