diff options
Diffstat (limited to 'glances/exports/glances_influxdb.py')
-rw-r--r-- | glances/exports/glances_influxdb.py | 41 |
1 files changed, 21 insertions, 20 deletions
diff --git a/glances/exports/glances_influxdb.py b/glances/exports/glances_influxdb.py index 04cb5a10..65be773d 100644 --- a/glances/exports/glances_influxdb.py +++ b/glances/exports/glances_influxdb.py @@ -19,15 +19,10 @@ """InfluxDB interface class.""" -# Import sys libs import sys -try: - from configparser import NoOptionError, NoSectionError -except ImportError: # Python 2 - from ConfigParser import NoOptionError, NoSectionError -# Import Glances lib -from glances.core.glances_logging import logger +from glances.compat import NoOptionError, NoSectionError +from glances.logger import logger from glances.exports.glances_export import GlancesExport from influxdb import InfluxDBClient @@ -35,9 +30,10 @@ from influxdb.client import InfluxDBClientError from influxdb.influxdb08 import InfluxDBClient as InfluxDBClient08 from influxdb.influxdb08.client import InfluxDBClientError as InfluxDBClientError08 -# Constants for tracking behavior -INFLUXDB_09 = '0.9' +# Constants for tracking specific behavior INFLUXDB_08 = '0.8' +INFLUXDB_09PLUS = '0.9+' + class Export(GlancesExport): @@ -45,7 +41,7 @@ class Export(GlancesExport): def __init__(self, config=None, args=None): """Init the InfluxDB export IF.""" - GlancesExport.__init__(self, config=config, args=args) + super(Export, self).__init__(config=config, args=args) # Load the InfluxDB configuration file self.host = None @@ -54,6 +50,7 @@ class Export(GlancesExport): self.password = None self.db = None self.prefix = None + self.tags = None self.export_enable = self.load_conf() if not self.export_enable: sys.exit(2) @@ -90,7 +87,7 @@ class Export(GlancesExport): try: self.tags = self.config.get_value(section, 'tags') except NoOptionError: - self.tags = '' + pass return True @@ -106,7 +103,7 @@ class Export(GlancesExport): password=self.password, database=self.db) get_all_db = [i['name'] for i in db.get_list_database()] - self.version = INFLUXDB_09 + self.version = INFLUXDB_09PLUS except InfluxDBClientError: # https://github.com/influxdb/influxdb-python/issues/138 logger.info("Trying fallback to InfluxDB v0.8") @@ -128,9 +125,6 @@ class Export(GlancesExport): logger.critical("InfluxDB database '%s' did not exist. Please create it" % self.db) sys.exit(2) - # Read tags - self.parse_tags() - return db def export(self, name, columns, points): @@ -139,14 +133,21 @@ class Export(GlancesExport): # Manage prefix if self.prefix is not None: name = self.prefix + '.' + name - # logger.info(self.prefix) # Create DB input - if self.version == INFLUXDB_09: + if self.version == INFLUXDB_08: + data = [{'name': name, 'columns': columns, 'points': [points]}] + else: + # Convert all int to float (mandatory for InfluxDB>0.9.2) + # Correct issue#750 and issue#749 + for i, _ in enumerate(points): + try: + points[i] = float(points[i]) + except (TypeError, ValueError) as e: + logger.debug("InfluxDB error diring stat convertion %s=%s (%s)" % (columns[i], points[i], e)) + data = [{'measurement': name, - 'tags': self.tags, + 'tags': self.parse_tags(self.tags), 'fields': dict(zip(columns, points))}] - else: - data = [{'name': name, 'columns': columns, 'points': [points]}] # Write input to the InfluxDB database try: self.client.write_points(data) |