mirror of https://github.com/nicolargo/glances.git
Add JSON export plugin
This commit is contained in:
parent
38d6687480
commit
12ae670811
1
NEWS
1
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:
|
||||
|
||||
|
|
|
|||
|
|
@ -10,6 +10,7 @@ to providing stats to multiple services (see list below).
|
|||
:maxdepth: 2
|
||||
|
||||
csv
|
||||
json
|
||||
cassandra
|
||||
couchdb
|
||||
elastic
|
||||
|
|
|
|||
|
|
@ -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.
|
||||
|
|
@ -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))
|
||||
|
|
@ -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,
|
||||
|
|
|
|||
Loading…
Reference in New Issue