diff options
-rw-r--r-- | NEWS | 1 | ||||
-rw-r--r-- | conf/glances.conf | 10 | ||||
-rw-r--r-- | docs/cmds.rst | 4 | ||||
-rw-r--r-- | docs/gw/couchdb.rst | 49 | ||||
-rw-r--r-- | docs/install.rst | 2 | ||||
-rw-r--r-- | docs/man/glances.1 | 7 | ||||
-rw-r--r-- | glances/exports/glances_couchdb.py | 133 | ||||
-rw-r--r-- | glances/main.py | 11 |
8 files changed, 214 insertions, 3 deletions
@@ -7,6 +7,7 @@ Version 2.8 Enhancements and new features: + * Add CouchDB exporter (issue #928) * Highlight max stats in the processes list (issue #878) Bugs corrected: diff --git a/conf/glances.conf b/conf/glances.conf index 5c5cfe49..a89a6815 100644 --- a/conf/glances.conf +++ b/conf/glances.conf @@ -296,6 +296,16 @@ user=guest password=guest queue=glances_queue +[couchdb] +# Configuration for the --export-couchdb option +# https://www.couchdb.org +host=localhost +port=5984 +db=glances +# user and password are optional (comment if not configured on the server side) +#user=root +#password=root + ############################################################################## # AMPS # * enable: Enable (true) or disable (false) the AMP diff --git a/docs/cmds.rst b/docs/cmds.rst index c62803f1..9b9fd2b0 100644 --- a/docs/cmds.rst +++ b/docs/cmds.rst @@ -163,6 +163,10 @@ Command-Line Options export stats to an Elasticsearch server (elasticsearch lib needed) +.. option:: --export-couchdb + + export stats to a CouchDB server (couchdb lib needed) + .. option:: -c CLIENT, --client CLIENT connect to a Glances server by IPv4/IPv6 address or hostname diff --git a/docs/gw/couchdb.rst b/docs/gw/couchdb.rst new file mode 100644 index 00000000..d6eb07f2 --- /dev/null +++ b/docs/gw/couchdb.rst @@ -0,0 +1,49 @@ +.. _couchdb: + +CouchDB +======= + +You can export statistics to a ``CouchDB`` server. +The connection should be defined in the Glances configuration file as +following: + +.. code-block:: ini + + [couchdb] + host=localhost + port=5984 + user=root + password=root + db=glances + +and run Glances with: + +.. code-block:: console + + $ glances --export-couchdb + +Documents are stored in native JSON format. Glances adds "type" and "time" entries. + +- type: plugin name +- time: timestamp (format: "2016-09-24T16:39:08.524828Z") + +Example of Couch Document for the load stats: + +.. code-block:: console + + { + "_id": "36cbbad81453c53ef08804cb2612d5b6", + "_rev": "1-382400899bec5615cabb99aa34df49fb", + "min15": 0.33, + "time": "2016-09-24T16:39:08.524828Z", + "min5": 0.4, + "cpucore": 4, + "load_warning": 1, + "min1": 0.5, + "history_size": 28800, + "load_critical": 5, + "type": "load", + "load_careful": 0.7 + } + +You can view the result using the CouchDB utils URL (http://127.0.0.1:5984/_utils/database.html?glances). diff --git a/docs/install.rst b/docs/install.rst index 496d9f09..e67fc01a 100644 --- a/docs/install.rst +++ b/docs/install.rst @@ -22,7 +22,7 @@ features (like the Web interface, exports modules, sensors...): .. code-block:: console - pip install bottle requests batinfo https://bitbucket.org/gleb_zhulik/py3sensors/get/tip.tar.gz zeroconf netifaces pymdstat influxdb elasticsearch potsdb statsd pystache docker-py pysnmp pika py-cpuinfo bernhard cassandra scandir + pip install bottle requests batinfo py3sensors zeroconf netifaces pymdstat influxdb elasticsearch potsdb statsd pystache docker-py pysnmp pika py-cpuinfo bernhard cassandra scandir couchdb To upgrade Glances to the latest version: diff --git a/docs/man/glances.1 b/docs/man/glances.1 index 5b965869..4e65242d 100644 --- a/docs/man/glances.1 +++ b/docs/man/glances.1 @@ -1,6 +1,6 @@ .\" Man page generated from reStructuredText. . -.TH "GLANCES" "1" "Sep 11, 2016" "2.7.1" "Glances" +.TH "GLANCES" "1" "Sep 24, 2016" "2.8_DEVELOP" "Glances" .SH NAME glances \- An eye on your system . @@ -244,6 +244,11 @@ export stats to an Elasticsearch server (elasticsearch lib needed) .UNINDENT .INDENT 0.0 .TP +.B \-\-export\-couchdb +export stats to a CouchDB server (couchdb lib needed) +.UNINDENT +.INDENT 0.0 +.TP .B \-c CLIENT, \-\-client CLIENT connect to a Glances server by IPv4/IPv6 address or hostname .UNINDENT diff --git a/glances/exports/glances_couchdb.py b/glances/exports/glances_couchdb.py new file mode 100644 index 00000000..f78f9acc --- /dev/null +++ b/glances/exports/glances_couchdb.py @@ -0,0 +1,133 @@ +# -*- coding: utf-8 -*- +# +# This file is part of Glances. +# +# Copyright (C) 2016 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/>. + +"""CouchDB interface class.""" + +import sys +from datetime import datetime + +from glances.compat import NoOptionError, NoSectionError +from glances.logger import logger +from glances.exports.glances_export import GlancesExport + +import couchdb +import couchdb.mapping + + +class Export(GlancesExport): + + """This class manages the CouchDB export module.""" + + def __init__(self, config=None, args=None): + """Init the CouchDB export IF.""" + super(Export, self).__init__(config=config, args=args) + + # Load the Couchdb configuration file section ([export_couchdb]) + self.host = None + self.port = None + self.user = None + self.password = None + self.db = None + self.export_enable = self.load_conf() + if not self.export_enable: + sys.exit(2) + + # Init the CouchDB client + self.client = self.init() + + def load_conf(self, section="couchdb"): + """Load the CouchDB configuration in the Glances configuration file.""" + if self.config is None: + return False + try: + self.host = self.config.get_value(section, 'host') + self.port = self.config.get_value(section, 'port') + self.db = self.config.get_value(section, 'db') + except NoSectionError: + logger.critical("No CouchDB configuration found") + return False + except NoOptionError as e: + logger.critical("Error in the CouchDB configuration (%s)" % e) + return False + else: + logger.debug("Load CouchDB from the Glances configuration file") + + # user and password are optional + try: + self.user = self.config.get_value(section, 'user') + self.password = self.config.get_value(section, 'password') + except NoOptionError: + pass + + return True + + def init(self): + """Init the connection to the CouchDB server.""" + if not self.export_enable: + return None + + if self.user is None: + server_uri = 'http://{}:{}/'.format(self.host, + self.port) + else: + server_uri = 'http://{}:{}@{}:{}/'.format(self.user, + self.password, + self.host, + self.port) + + try: + s = couchdb.Server(server_uri) + except Exception as e: + logger.critical("Cannot connect to CouchDB server %s (%s)" % (server_uri, e)) + sys.exit(2) + else: + logger.info("Connected to the CouchDB server %s" % server_uri) + + try: + s[self.db] + except Exception as e: + # Database did not exist + # Create it... + s.create(self.db) + else: + logger.info("There is already a %s database" % self.db) + + return s + + def database(self): + """Return the CouchDB database object""" + return self.client[self.db] + + def export(self, name, columns, points): + """Write the points to the CouchDB server.""" + logger.debug("Export {} stats to CouchDB".format(name)) + + # Create DB input + data = dict(zip(columns, points)) + + # Set the type to the current stat name + data['type'] = name + data['time'] = couchdb.mapping.DateTimeField()._to_json(datetime.now()) + + # Write input to the CouchDB database + # Result can be view: http://127.0.0.1:5984/_utils + try: + self.client[self.db].save(data) + except Exception as e: + logger.error("Cannot export {} stats to CouchDB ({})".format(name, e)) diff --git a/glances/main.py b/glances/main.py index 87593df6..1c3b47d3 100644 --- a/glances/main.py +++ b/glances/main.py @@ -175,6 +175,8 @@ Start the client browser (browser mode):\n\ dest='export_rabbitmq', help='export stats to rabbitmq broker (pika 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-couchdb', action='store_true', default=False, + dest='export_couchdb', help='export stats to a CouchDB server (couch lib needed)') # Client/Server option parser.add_argument('-c', '--client', dest='client', help='connect to a Glances server by IPv4/IPv6 address or hostname') @@ -334,7 +336,14 @@ Start the client browser (browser mode):\n\ self.args = args # Export is only available in standalone or client mode (issue #614) - export_tag = args.export_csv or args.export_elasticsearch or args.export_statsd or args.export_influxdb or args.export_cassandra or args.export_opentsdb or args.export_rabbitmq + export_tag = args.export_csv or \ + args.export_elasticsearch or \ + args.export_statsd or \ + args.export_influxdb or \ + args.export_cassandra or \ + args.export_opentsdb or \ + args.export_rabbitmq or \ + args.export_couchdb if not (self.is_standalone() or self.is_client()) and export_tag: logger.critical("Export is only available in standalone or client mode") sys.exit(2) |