diff options
author | nicolargo <nicolas@nicolargo.com> | 2017-07-27 23:18:59 +0200 |
---|---|---|
committer | nicolargo <nicolas@nicolargo.com> | 2017-07-27 23:18:59 +0200 |
commit | e372fa2b07776feb9e4fc5ca2aa386e02859f3f2 (patch) | |
tree | 42b557e29d14717d7b50dc9faaa431e58b7414b0 | |
parent | 00531b5d24c76e43dd9fc7494a4807fd3f73b67b (diff) |
First implementation, need answer https://github.com/nicolargo/glances/issues/1129#issuecomment-318489088
-rw-r--r-- | conf/glances.conf | 8 | ||||
-rw-r--r-- | glances/exports/glances_restful.py | 80 | ||||
-rw-r--r-- | glances/main.py | 2 |
3 files changed, 90 insertions, 0 deletions
diff --git a/conf/glances.conf b/conf/glances.conf index a7dc1cb4..3282f6b9 100644 --- a/conf/glances.conf +++ b/conf/glances.conf @@ -404,6 +404,14 @@ host=localhost port=9091 prefix=glances +[restful] +# Configuration for the --export-restful option +# Example, export to http://localhost:6789/ +host=localhost +port=6789 +protocol=http +path=/ + ############################################################################## # AMPS # * enable: Enable (true) or disable (false) the AMP diff --git a/glances/exports/glances_restful.py b/glances/exports/glances_restful.py new file mode 100644 index 00000000..8abe5f6f --- /dev/null +++ b/glances/exports/glances_restful.py @@ -0,0 +1,80 @@ +# -*- coding: utf-8 -*- +# +# This file is part of Glances. +# +# Copyright (C) 2017 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/>. + +"""Restful interface class.""" + +import sys +from numbers import Number + +from glances.compat import range +from glances.logger import logger +from glances.exports.glances_export import GlancesExport + +from requests import post + + +class Export(GlancesExport): + + """This class manages the Restful export module.""" + + def __init__(self, config=None, args=None): + """Init the Restful export IF.""" + super(Export, self).__init__(config=config, args=args) + + # Mandatories configuration keys (additional to host and port) + self.protocol = None + self.path = None + + # Load the Restful section in the configuration file + self.export_enable = self.load_conf('restful', + mandatories=['host', 'port', 'protocol', 'path']) + if not self.export_enable: + sys.exit(2) + + # Init the Statsd client + self.client = self.init() + + def init(self): + """Init the connection to the restful server.""" + if not self.export_enable: + return None + # Build the Restful URL where the stats will be posted + url = '{}://{}:{}{}'.format(self.protocol, + self.host, + self.port, + self.path) + logger.info( + "Stats will be exported to the restful URL: {}".format(url)) + return url + + def export(self, name, columns, points): + """Export the stats to the Statsd server.""" + post(self.client, json=dict(zip(columns, points)), allow_redirects=True) + logger.debug("Export {} stats to Restful endpoint".format(name)) + + +def normalize(name): + """Normalize name for the Statsd convention""" + + # Name should not contain some specials chars (issue #1068) + ret = name.replace(':', '') + ret = ret.replace('%', '') + ret = ret.replace(' ', '_') + + return ret diff --git a/glances/main.py b/glances/main.py index 9322a16c..058e6a79 100644 --- a/glances/main.py +++ b/glances/main.py @@ -189,6 +189,8 @@ Examples of use: dest='export_prometheus', help='export stats to a Prometheus exporter (prometheus_client lib needed)') parser.add_argument('--export-rabbitmq', action='store_true', default=False, dest='export_rabbitmq', help='export stats to rabbitmq broker (pika lib needed)') + parser.add_argument('--export-restful', action='store_true', default=False, + dest='export_restful', help='export stats to a Restful endpoint (requests lib needed)') parser.add_argument('--export-riemann', action='store_true', default=False, dest='export_riemann', help='export stats to riemann broker (bernhard lib needed)') parser.add_argument('--export-statsd', action='store_true', default=False, |