From 5ce05cef11944a58b379a1e7dbf3b4bf8749bef1 Mon Sep 17 00:00:00 2001 From: nicolargo Date: Sat, 7 Jan 2017 16:50:35 +0100 Subject: [PATCH] Refactor init of export modules --- glances/exports/glances_cassandra.py | 10 +++++---- glances/exports/glances_couchdb.py | 10 +++++---- glances/exports/glances_elasticsearch.py | 9 +++++--- glances/exports/glances_export.py | 6 +++++- glances/exports/glances_influxdb.py | 8 ++++--- glances/exports/glances_opentsdb.py | 9 +++++--- glances/exports/glances_rabbitmq.py | 27 ++++++++++++++---------- glances/exports/glances_riemann.py | 14 ++++++++---- glances/exports/glances_statsd.py | 9 +++++--- glances/exports/glances_zeromq.py | 8 +++++-- 10 files changed, 72 insertions(+), 38 deletions(-) diff --git a/glances/exports/glances_cassandra.py b/glances/exports/glances_cassandra.py index f6aaa276..10feb873 100644 --- a/glances/exports/glances_cassandra.py +++ b/glances/exports/glances_cassandra.py @@ -40,13 +40,15 @@ class Export(GlancesExport): """Init the Cassandra export IF.""" super(Export, self).__init__(config=config, args=args) - # Load the Cassandra configuration file section - self.host = None - self.port = None - self.protocol_version = 3 + # Mandatories configuration keys (additional to host and port) self.keyspace = None + + # Optionals configuration keys + self.protocol_version = 3 self.replication_factor = 2 self.table = None + + # Load the Cassandra configuration file section self.export_enable = self.load_conf('cassandra', mandatories=['host', 'port', 'keyspace'], options=['protocol_version', diff --git a/glances/exports/glances_couchdb.py b/glances/exports/glances_couchdb.py index 69643278..68eb9489 100644 --- a/glances/exports/glances_couchdb.py +++ b/glances/exports/glances_couchdb.py @@ -38,12 +38,14 @@ class Export(GlancesExport): """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 + # Mandatories configuration keys (additional to host and port) + self.db = None + + # Optionals configuration keys self.user = None self.password = None - self.db = None + + # Load the Cassandra configuration file section self.export_enable = self.load_conf('couchdb', mandatories=['host', 'port', 'db'], options=['user', 'password']) diff --git a/glances/exports/glances_elasticsearch.py b/glances/exports/glances_elasticsearch.py index 72f28afe..9519c789 100644 --- a/glances/exports/glances_elasticsearch.py +++ b/glances/exports/glances_elasticsearch.py @@ -37,10 +37,13 @@ class Export(GlancesExport): """Init the ES export IF.""" super(Export, self).__init__(config=config, args=args) - # Load the ES configuration file - self.host = None - self.port = None + # Mandatories configuration keys (additional to host and port) self.index = None + + # Optionals configuration keys + # N/A + + # Load the ES configuration file self.export_enable = self.load_conf('elasticsearch', mandatories=['host', 'port', 'index'], options=[]) diff --git a/glances/exports/glances_export.py b/glances/exports/glances_export.py index 3aeb7035..dcd642f8 100644 --- a/glances/exports/glances_export.py +++ b/glances/exports/glances_export.py @@ -35,7 +35,7 @@ class GlancesExport(object): """Init the export class.""" # Export name (= module name without glances_) self.export_name = self.__class__.__module__[len('glances_'):] - logger.debug("Init export interface %s" % self.export_name) + logger.debug("Init export module %s" % self.export_name) # Init the config & args self.config = config @@ -45,6 +45,10 @@ class GlancesExport(object): # Had to be set to True in the __init__ class of child self.export_enable = False + # Mandatory for (most of) the export module + self.host = None + self.port = None + def exit(self): """Close the export module.""" logger.debug("Finalise export interface %s" % self.export_name) diff --git a/glances/exports/glances_influxdb.py b/glances/exports/glances_influxdb.py index 164ad48b..22775b1b 100644 --- a/glances/exports/glances_influxdb.py +++ b/glances/exports/glances_influxdb.py @@ -43,14 +43,16 @@ class Export(GlancesExport): """Init the InfluxDB export IF.""" super(Export, self).__init__(config=config, args=args) - # Load the InfluxDB configuration file - self.host = None - self.port = None + # Mandatories configuration keys (additional to host and port) self.user = None self.password = None self.db = None + + # Optionals configuration keys self.prefix = None self.tags = None + + # Load the InfluxDB configuration file self.export_enable = self.load_conf('influxdb', mandatories=['host', 'port', 'user', 'password', diff --git a/glances/exports/glances_opentsdb.py b/glances/exports/glances_opentsdb.py index 6c421f00..9b9082d2 100644 --- a/glances/exports/glances_opentsdb.py +++ b/glances/exports/glances_opentsdb.py @@ -37,11 +37,14 @@ class Export(GlancesExport): """Init the OpenTSDB export IF.""" super(Export, self).__init__(config=config, args=args) - # Load the InfluxDB configuration file - self.host = None - self.port = None + # Mandatories configuration keys (additional to host and port) + # N/A + + # Optionals configuration keys self.prefix = None self.tags = None + + # Load the InfluxDB configuration file self.export_enable = self.load_conf('opentsdb', mandatories=['host', 'port'], options=['prefix', 'tags']) diff --git a/glances/exports/glances_rabbitmq.py b/glances/exports/glances_rabbitmq.py index ca730ae4..f9f5f99e 100644 --- a/glances/exports/glances_rabbitmq.py +++ b/glances/exports/glances_rabbitmq.py @@ -40,13 +40,15 @@ class Export(GlancesExport): """Init the RabbitMQ export IF.""" super(Export, self).__init__(config=config, args=args) + # Mandatories configuration keys (additional to host and port) + self.user = None + self.password = None + self.queue = None + + # Optionals configuration keys + # N/A + # Load the rabbitMQ configuration file - self.rabbitmq_host = None - self.rabbitmq_port = None - self.rabbitmq_user = None - self.rabbitmq_password = None - self.rabbitmq_queue = None - self.hostname = socket.gethostname() self.export_enable = self.load_conf('rabbitmq', mandatories=['host', 'port', 'user', 'password', @@ -55,6 +57,9 @@ class Export(GlancesExport): if not self.export_enable: sys.exit(2) + # Get the current hostname + self.hostname = socket.gethostname() + # Init the rabbitmq client self.client = self.init() @@ -64,10 +69,10 @@ class Export(GlancesExport): return None try: parameters = pika.URLParameters( - 'amqp://' + self.rabbitmq_user + - ':' + self.rabbitmq_password + - '@' + self.rabbitmq_host + - ':' + self.rabbitmq_port + '/') + 'amqp://' + self.user + + ':' + self.password + + '@' + self.host + + ':' + self.port + '/') connection = pika.BlockingConnection(parameters) channel = connection.channel() return channel @@ -86,6 +91,6 @@ class Export(GlancesExport): data += ", " + columns[i] + "=" + str(points[i]) logger.debug(data) try: - self.client.basic_publish(exchange='', routing_key=self.rabbitmq_queue, body=data) + self.client.basic_publish(exchange='', routing_key=self.queue, body=data) except Exception as e: logger.error("Can not export stats to RabbitMQ (%s)" % e) diff --git a/glances/exports/glances_riemann.py b/glances/exports/glances_riemann.py index ca640e56..078da95b 100644 --- a/glances/exports/glances_riemann.py +++ b/glances/exports/glances_riemann.py @@ -39,16 +39,22 @@ class Export(GlancesExport): """Init the Riemann export IF.""" super(Export, self).__init__(config=config, args=args) + # Mandatories configuration keys (additional to host and port) + # N/A + + # Optionals configuration keys + # N/A + # Load the Riemann configuration - self.riemann_host = None - self.riemann_port = None - self.hostname = socket.gethostname() self.export_enable = self.load_conf('riemann', mandatories=['host', 'port'], options=[]) if not self.export_enable: sys.exit(2) + # Get the current hostname + self.hostname = socket.gethostname() + # Init the Riemann client self.client = self.init() @@ -57,7 +63,7 @@ class Export(GlancesExport): if not self.export_enable: return None try: - client = bernhard.Client(host=self.riemann_host, port=self.riemann_port) + client = bernhard.Client(host=self.host, port=self.port) return client except Exception as e: logger.critical("Connection to Riemann failed : %s " % e) diff --git a/glances/exports/glances_statsd.py b/glances/exports/glances_statsd.py index 13d2922e..abb31f17 100644 --- a/glances/exports/glances_statsd.py +++ b/glances/exports/glances_statsd.py @@ -37,10 +37,13 @@ class Export(GlancesExport): """Init the Statsd export IF.""" super(Export, self).__init__(config=config, args=args) - # Load the InfluxDB configuration file - self.host = None - self.port = None + # Mandatories configuration keys (additional to host and port) + # N/A + + # Optionals configuration keys self.prefix = None + + # Load the InfluxDB configuration file self.export_enable = self.load_conf('statsd', mandatories=['host', 'port'], options=['prefix']) diff --git a/glances/exports/glances_zeromq.py b/glances/exports/glances_zeromq.py index 855ade70..f21675c2 100644 --- a/glances/exports/glances_zeromq.py +++ b/glances/exports/glances_zeromq.py @@ -37,9 +37,13 @@ class Export(GlancesExport): """Init the ZeroMQ export IF.""" super(Export, self).__init__(config=config, args=args) + # Mandatories configuration keys (additional to host and port) + self.prefix = None + + # Optionals configuration keys + # N/A + # Load the ZeroMQ configuration file section ([export_zeromq]) - self.host = None - self.port = None self.export_enable = self.load_conf('zeromq', mandatories=['host', 'port', 'prefix'], options=[])