mirror of
https://github.com/nicolargo/glances.git
synced 2024-12-28 11:41:46 +03:00
Merge pull request #630 from CuriosityApp/emidln/influxdb_09x
Support InfluxDB 0.9.x
This commit is contained in:
commit
2e5114d318
@ -812,6 +812,20 @@ and run Glances with:
|
|||||||
|
|
||||||
$ glances --export-influxdb
|
$ 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
|
For Grafana users, Glances provides a dedicated `dashboard`_. Just import
|
||||||
the file in your ``Grafana`` web interface.
|
the file in your ``Grafana`` web interface.
|
||||||
|
|
||||||
|
@ -35,6 +35,9 @@ from influxdb.client import InfluxDBClientError
|
|||||||
from influxdb.influxdb08 import InfluxDBClient as InfluxDBClient08
|
from influxdb.influxdb08 import InfluxDBClient as InfluxDBClient08
|
||||||
from influxdb.influxdb08.client import InfluxDBClientError as InfluxDBClientError08
|
from influxdb.influxdb08.client import InfluxDBClientError as InfluxDBClientError08
|
||||||
|
|
||||||
|
# Constants for tracking behavior
|
||||||
|
INFLUXDB_09 = '0.9'
|
||||||
|
INFLUXDB_08 = '0.8'
|
||||||
|
|
||||||
class Export(GlancesExport):
|
class Export(GlancesExport):
|
||||||
|
|
||||||
@ -76,13 +79,33 @@ class Export(GlancesExport):
|
|||||||
return False
|
return False
|
||||||
else:
|
else:
|
||||||
logger.debug("Load InfluxDB from the Glances configuration file")
|
logger.debug("Load InfluxDB from the Glances configuration file")
|
||||||
|
|
||||||
# Prefix is optional
|
# Prefix is optional
|
||||||
try:
|
try:
|
||||||
self.prefix = self.config.get_value(section, 'prefix')
|
self.prefix = self.config.get_value(section, 'prefix')
|
||||||
except NoOptionError:
|
except NoOptionError:
|
||||||
pass
|
pass
|
||||||
|
|
||||||
|
# Tags are optional, comma separated key:value pairs.
|
||||||
|
try:
|
||||||
|
self.tags = self.config.get_value(section, 'tags')
|
||||||
|
except NoOptionError:
|
||||||
|
self.tags = ''
|
||||||
|
|
||||||
return True
|
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):
|
def init(self):
|
||||||
"""Init the connection to the InfluxDB server."""
|
"""Init the connection to the InfluxDB server."""
|
||||||
if not self.export_enable:
|
if not self.export_enable:
|
||||||
@ -95,6 +118,7 @@ class Export(GlancesExport):
|
|||||||
password=self.password,
|
password=self.password,
|
||||||
database=self.db)
|
database=self.db)
|
||||||
get_all_db = [i['name'] for i in db.get_list_database()]
|
get_all_db = [i['name'] for i in db.get_list_database()]
|
||||||
|
self.version = INFLUXDB_09
|
||||||
except InfluxDBClientError:
|
except InfluxDBClientError:
|
||||||
# https://github.com/influxdb/influxdb-python/issues/138
|
# https://github.com/influxdb/influxdb-python/issues/138
|
||||||
logger.info("Trying fallback to InfluxDB v0.8")
|
logger.info("Trying fallback to InfluxDB v0.8")
|
||||||
@ -104,6 +128,7 @@ class Export(GlancesExport):
|
|||||||
password=self.password,
|
password=self.password,
|
||||||
database=self.db)
|
database=self.db)
|
||||||
get_all_db = [i['name'] for i in db.get_list_database()]
|
get_all_db = [i['name'] for i in db.get_list_database()]
|
||||||
|
self.version = INFLUXDB_08
|
||||||
except InfluxDBClientError08 as e:
|
except InfluxDBClientError08 as e:
|
||||||
logger.critical("Cannot connect to InfluxDB database '%s' (%s)" % (self.db, e))
|
logger.critical("Cannot connect to InfluxDB database '%s' (%s)" % (self.db, e))
|
||||||
sys.exit(2)
|
sys.exit(2)
|
||||||
@ -114,6 +139,9 @@ class Export(GlancesExport):
|
|||||||
else:
|
else:
|
||||||
logger.critical("InfluxDB database '%s' did not exist. Please create it" % self.db)
|
logger.critical("InfluxDB database '%s' did not exist. Please create it" % self.db)
|
||||||
sys.exit(2)
|
sys.exit(2)
|
||||||
|
|
||||||
|
self.parse_tags()
|
||||||
|
|
||||||
return db
|
return db
|
||||||
|
|
||||||
def export(self, name, columns, points):
|
def export(self, name, columns, points):
|
||||||
@ -123,7 +151,12 @@ class Export(GlancesExport):
|
|||||||
name = self.prefix + '.' + name
|
name = self.prefix + '.' + name
|
||||||
# logger.info(self.prefix)
|
# logger.info(self.prefix)
|
||||||
# Create DB input
|
# 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
|
# Write input to the InfluxDB database
|
||||||
try:
|
try:
|
||||||
self.client.write_points(data)
|
self.client.write_points(data)
|
||||||
|
Loading…
Reference in New Issue
Block a user