From 4ffad89e8b4910382c77d9a31110a9a3600298fa Mon Sep 17 00:00:00 2001 From: nicolargo Date: Sun, 30 Jul 2017 11:05:49 +0200 Subject: New export plugin: standard and configurable Restfull exporter #1129 --- NEWS | 1 + README.rst | 4 ++-- docs/gw/index.rst | 1 + docs/gw/restful.rst | 42 ++++++++++++++++++++++++++++++++++++++ docs/man/glances.1 | 2 +- glances/exports/glances_export.py | 4 ++++ glances/exports/glances_restful.py | 8 +++++--- 7 files changed, 56 insertions(+), 6 deletions(-) create mode 100644 docs/gw/restful.rst diff --git a/NEWS b/NEWS index 8ea2ddae..1968a010 100644 --- a/NEWS +++ b/NEWS @@ -8,6 +8,7 @@ Version NEXT RELEASE Enhancements and new features: * [WIP] Refactoring of the WebUI + * New export plugin: standard and configurable Restfull exporter (issue #1129) Bugs corrected: diff --git a/README.rst b/README.rst index 5a095502..dd8a8fcb 100644 --- a/README.rst +++ b/README.rst @@ -73,7 +73,7 @@ Optional dependencies: - ``pysnmp`` (for SNMP support) - ``pystache`` (for the action script feature) - ``pyzmq`` (for the ZeroMQ export module) -- ``requests`` (for the Ports and Cloud plugins) +- ``requests`` (for the Ports, Cloud plugins and Restful export module) - ``scandir`` (for the Folders plugin) [Only for Python < 3.5] - ``statsd`` (for the StatsD export module) - ``wifi`` (for the wifi plugin) [Linux-only] @@ -353,7 +353,7 @@ Gateway to other services Glances can export stats to: ``CSV`` file, ``InfluxDB``, ``Cassandra``, ``CouchDB``, ``OpenTSDB``, ``Prometheus``, ``StatsD``, ``ElasticSearch``, ``RabbitMQ/ActiveMQ``, -``ZeroMQ``, ``Kafka`` and ``Riemann`` server. +``ZeroMQ``, ``Kafka``, ``Riemann`` and ``Restful`` server. How to contribute ? =================== diff --git a/docs/gw/index.rst b/docs/gw/index.rst index 6d0c2251..4e9c1e23 100644 --- a/docs/gw/index.rst +++ b/docs/gw/index.rst @@ -18,6 +18,7 @@ to providing stats to multiple services (see list below). opentsdb prometheus rabbitmq + restful riemann statsd zeromq diff --git a/docs/gw/restful.rst b/docs/gw/restful.rst new file mode 100644 index 00000000..269d1ac4 --- /dev/null +++ b/docs/gw/restful.rst @@ -0,0 +1,42 @@ +.. _restful: + +Restful +======= + +You can export statistics to a ``Restful`` JSON server. All the available stats +will be exported in one big (~15 KB) POST request to the Restful endpoint. + +The Restful endpoint should be defined in the Glances configuration file as +following: + +.. code-block:: ini + + [restful] + # Configuration for the --export-restful option + # Example, export to http://localhost:6789/ + host=localhost + port=6789 + protocol=http + path=/ + +URL Syntax: + +.. code-block:: ini + + http://localhost:6789/ + | | | | + | | | path + | | port + | hostname + host + +and run Glances with: + +.. code-block:: console + + $ glances --export-restful + +Glances will generate stats as a big JSON dictionary (see example `here`_). + + +.. _here: https://pastebin.com/7U3vXqvF diff --git a/docs/man/glances.1 b/docs/man/glances.1 index 7827ab3b..454c001e 100644 --- a/docs/man/glances.1 +++ b/docs/man/glances.1 @@ -1,6 +1,6 @@ .\" Man page generated from reStructuredText. . -.TH "GLANCES" "1" "Jun 27, 2017" "2.10.1_DEVELOP" "Glances" +.TH "GLANCES" "1" "Jul 30, 2017" "2.10.1_DEVELOP" "Glances" .SH NAME glances \- An eye on your system . diff --git a/glances/exports/glances_export.py b/glances/exports/glances_export.py index b402ea2e..c5f72662 100644 --- a/glances/exports/glances_export.py +++ b/glances/exports/glances_export.py @@ -23,6 +23,8 @@ I am your father... ...for all Glances exports IF. """ +import json + from glances.compat import NoOptionError, NoSectionError, iteritems, iterkeys from glances.logger import logger @@ -182,6 +184,8 @@ class GlancesExport(object): pre_key = '' # Walk through the dict for key, value in iteritems(stats): + if isinstance(value, bool): + value = json.dumps(value) if isinstance(value, list): try: value = value[0] diff --git a/glances/exports/glances_restful.py b/glances/exports/glances_restful.py index 64f8f5e0..275170bb 100644 --- a/glances/exports/glances_restful.py +++ b/glances/exports/glances_restful.py @@ -21,6 +21,7 @@ import sys +from glances.compat import listkeys from glances.logger import logger from glances.exports.glances_export import GlancesExport @@ -63,18 +64,19 @@ class Export(GlancesExport): self.port, self.path) logger.info( - "Stats will be exported to the restful URL: {}".format(url)) + "Stats will be exported to the restful endpoint {}".format(url)) return url def export(self, name, columns, points): """Export the stats to the Statsd server.""" - if name in self.buffer: + if name == self.plugins_to_export()[0] and self.buffer != {}: # One complete loop have been done + logger.debug("Export stats ({}) to Restful endpoint ({})".format(listkeys(self.buffer), + self.client)) # Export stats post(self.client, json=self.buffer, allow_redirects=True) # Reset buffer self.buffer = {} - logger.debug("Export stats to Restful endpoint ({})".format(self.client)) # Add current stat to the buffer self.buffer[name] = dict(zip(columns, points)) -- cgit v1.2.3