CouchDB migration from CouchDB to PyCouchDB library #2570

This commit is contained in:
nicolargo 2023-10-08 10:10:13 +02:00
parent cb4a8b80b7
commit 55d7c07543
6 changed files with 36 additions and 42 deletions

View File

@ -95,7 +95,6 @@ Optional dependencies:
- ``bottle`` (for Web server mode)
- ``cassandra-driver`` (for the Cassandra export module)
- ``chevron`` (for the action script feature)
- ``couchdb`` (for the CouchDB export module)
- ``docker`` (for the Containers Docker monitoring support)
- ``elasticsearch`` (for the Elastic Search export module)
- ``graphitesender`` (For the Graphite export module)
@ -105,6 +104,7 @@ Optional dependencies:
- ``kafka-python`` (for the Kafka export module)
- ``netifaces`` (for the IP plugin)
- ``py3nvml`` (for the GPU plugin)
- ``pycouchdb`` (for the CouchDB export module)
- ``pika`` (for the RabbitMQ/ActiveMQ export module)
- ``podman`` (for the Containers Podman monitoring support)
- ``potsdb`` (for the OpenTSDB export module)

View File

@ -595,10 +595,8 @@ topic_structure=per-metric
host=localhost
port=5984
db=glances
# user and password are optional (comment if not configured on the server side)
# If they are used, then the https protocol will be used
#user=root
#password=root
user=admin
password=admin
[mongodb]
# Configuration for the --export mongodb option

View File

@ -11,10 +11,10 @@ following:
[couchdb]
host=localhost
port=
port=5984
db=glances
user=root
password=example
db=glances
and run Glances with:
@ -26,7 +26,7 @@ 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")
- ``time``: timestamp (format: "2016-09-24T16:39:08.524Z")
Example of Couch Document for the load stats:
@ -36,7 +36,7 @@ Example of Couch Document for the load stats:
"_id": "36cbbad81453c53ef08804cb2612d5b6",
"_rev": "1-382400899bec5615cabb99aa34df49fb",
"min15": 0.33,
"time": "2016-09-24T16:39:08.524828Z",
"time": "2016-09-24T16:39:08.524Z",
"min5": 0.4,
"cpucore": 4,
"load_warning": 1,

View File

@ -9,14 +9,21 @@
"""CouchDB interface class."""
#
# How to test ?
#
# 1) docker run -d -e COUCHDB_USER=admin -e COUCHDB_PASSWORD=admin -p 5984:5984 --name my-couchdb couchdb
# 2) ./venv/bin/python -m glances -C ./conf/glances.conf --export couchdb --quiet
# 3) Result can be seen at: http://127.0.0.1:5984/_utils
#
import sys
from datetime import datetime
from glances.logger import logger
from glances.exports.export import GlancesExport
import couchdb
import couchdb.mapping
import pycouchdb
class Export(GlancesExport):
@ -27,15 +34,11 @@ class Export(GlancesExport):
"""Init the CouchDB export IF."""
super(Export, self).__init__(config=config, args=args)
# Mandatory configuration keys (additional to host and port)
self.db = None
# Optional configuration keys
self.user = None
self.password = None
# Load the Cassandra configuration file section
self.export_enable = self.load_conf('couchdb', mandatories=['host', 'port', 'db'], options=['user', 'password'])
# Load the CouchDB configuration file section
# User and Password are mandatory with CouchDB 3.0 and higher
self.export_enable = self.load_conf('couchdb',
mandatories=['host', 'port', 'db',
'user', 'password'])
if not self.export_enable:
sys.exit(2)
@ -47,35 +50,29 @@ class Export(GlancesExport):
if not self.export_enable:
return None
if self.user is None:
server_uri = 'http://{}:{}/'.format(self.host, self.port)
else:
# Force https if a login/password is provided
# Related to https://github.com/nicolargo/glances/issues/2124
server_uri = 'https://{}:{}@{}:{}/'.format(self.user, self.password, self.host, self.port)
# @TODO: https
server_uri = 'http://{}:{}@{}:{}/'.format(self.user, self.password,
self.host, self.port)
try:
s = couchdb.Server(server_uri)
s = pycouchdb.Server(server_uri)
except Exception as e:
logger.critical("Cannot connect to CouchDB server (%s)" % e)
sys.exit(2)
else:
logger.info("Connected to the CouchDB server")
logger.info("Connected to the CouchDB server version %s" % s.info()['version'])
try:
s[self.db]
s.database(self.db)
except Exception:
# Database did not exist
# Create it...
s.create(self.db)
logger.info("Create CouchDB database %s" % self.db)
else:
logger.info("There is already a %s database" % self.db)
logger.info("CouchDB database %s already exist" % self.db)
return s
def database(self):
"""Return the CouchDB database object"""
return self.client[self.db]
return s.database(self.db)
def export(self, name, columns, points):
"""Write the points to the CouchDB server."""
@ -84,13 +81,12 @@ class Export(GlancesExport):
# Create DB input
data = dict(zip(columns, points))
# Set the type to the current stat name
# Add the type and the timestamp in ISO format
data['type'] = name
data['time'] = couchdb.mapping.DateTimeField()._to_json(datetime.now())
data['time'] = datetime.now().isoformat()[:-3] + 'Z'
# Write data to the CouchDB database
# Result can be seen at: http://127.0.0.1:5984/_utils
try:
self.client[self.db].save(data)
self.client.save(data)
except Exception as e:
logger.error("Cannot export {} stats to CouchDB ({})".format(name, e))

View File

@ -6,7 +6,6 @@ bernhard
bottle
cassandra-driver
chevron
couchdb
docker>=6.1.1
elasticsearch
graphitesender
@ -21,6 +20,7 @@ pika
podman; python_version >= "3.6"
potsdb
prometheus_client
pycouchdb
pygal
pymdstat
pymongo; python_version >= "3.7"

View File

@ -56,8 +56,8 @@ def get_install_extras_require():
'browser': ['zeroconf>=0.19.1'],
'cloud': ['requests'],
'containers': ['docker>=6.1.1', 'python-dateutil', 'six', 'podman', 'packaging'],
'export': ['bernhard', 'cassandra-driver', 'couchdb', 'elasticsearch',
'graphitesender', 'influxdb>=1.0.0', 'influxdb-client', 'pymongo',
'export': ['bernhard', 'cassandra-driver', 'elasticsearch', 'graphitesender',
'ibmcloudant', 'influxdb>=1.0.0', 'influxdb-client', 'pymongo',
'kafka-python', 'pika', 'paho-mqtt', 'potsdb', 'prometheus_client',
'pyzmq', 'statsd'],
'gpu': ['py3nvml'],