From d40f01e0edba329d9be2120f0eeee797d7e28dd8 Mon Sep 17 00:00:00 2001 From: Brandon Adams Date: Tue, 7 Jul 2015 12:08:17 -0500 Subject: [PATCH 1/2] Support InfluxDB 0.9.x This adds support for the JSON payload schema used in 0.9.x. This also adds support for the tags feature available in 0.9.x via the config option tags. Tags is a comma separated list of colon separated key:value pairs. An example: ``` [influxdb] tags = foo:bar,spam:eggs,region:us-east-1 ``` --- glances/exports/glances_influxdb.py | 35 ++++++++++++++++++++++++++++- 1 file changed, 34 insertions(+), 1 deletion(-) diff --git a/glances/exports/glances_influxdb.py b/glances/exports/glances_influxdb.py index 1b759303..f8683db6 100644 --- a/glances/exports/glances_influxdb.py +++ b/glances/exports/glances_influxdb.py @@ -35,6 +35,9 @@ 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' +INFLUXDB_08 = '0.8' class Export(GlancesExport): @@ -76,13 +79,33 @@ class Export(GlancesExport): return False else: logger.debug("Load InfluxDB from the Glances configuration file") + # Prefix is optional try: self.prefix = self.config.get_value(section, 'prefix') except NoOptionError: pass + + # Tags are optional, comma separated key:value pairs. + try: + self.tags = self.config.get_value(section, 'tags') + except NoOptionError: + self.tags = '' + return True + def parse_tags(self): + """ Parses some tags into a dict""" + if self.tags: + try: + self.tags = dict([x.split(':') for x in self.tags.split(',')]) + except ValueError: + # one of the keyvalue pairs was missing + logger.info('invalid tags passed: %s', self.tags) + self.tags = {} + else: + self.tags = {} + def init(self): """Init the connection to the InfluxDB server.""" if not self.export_enable: @@ -95,6 +118,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 except InfluxDBClientError: # https://github.com/influxdb/influxdb-python/issues/138 logger.info("Trying fallback to InfluxDB v0.8") @@ -104,6 +128,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_08 except InfluxDBClientError08 as e: logger.critical("Cannot connect to InfluxDB database '%s' (%s)" % (self.db, e)) sys.exit(2) @@ -114,6 +139,9 @@ class Export(GlancesExport): else: logger.critical("InfluxDB database '%s' did not exist. Please create it" % self.db) sys.exit(2) + + self.parse_tags() + return db def export(self, name, columns, points): @@ -123,7 +151,12 @@ class Export(GlancesExport): name = self.prefix + '.' + name # logger.info(self.prefix) # Create DB input - data = [{'name': name, 'columns': columns, 'points': [points]}] + if self.version == INFLUXDB_09: + data = [{'measurement': name, + '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) From 8b74f42c8545253ae1c34a75265bfdf501d35f97 Mon Sep 17 00:00:00 2001 From: Brandon Adams Date: Tue, 7 Jul 2015 12:14:53 -0500 Subject: [PATCH 2/2] Update documentation mentioning 0.9.x tags --- docs/glances-doc.rst | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/docs/glances-doc.rst b/docs/glances-doc.rst index 44deb491..727f287c 100644 --- a/docs/glances-doc.rst +++ b/docs/glances-doc.rst @@ -812,6 +812,20 @@ and run Glances with: $ glances --export-influxdb +InfluxDB 0.9.x also supports an optional tags configuration parameter +specified as comma separated, key:value pairs. For example: + +.. code-block:: + + [influxdb] + host=localhost + port=8086 + user=root + password=root + db=glances + tags=foo:bar,spam:eggs + + For Grafana users, Glances provides a dedicated `dashboard`_. Just import the file in your ``Grafana`` web interface.