InfluxDB connection parameter in the Glances configuration file

This commit is contained in:
Nicolargo 2014-12-28 22:41:33 +01:00
parent ace22f7112
commit 751fb8948c
6 changed files with 80 additions and 22 deletions

View File

@ -143,3 +143,10 @@ server_3_alias=Another PC on my network
server_3_port=61209
server_4_name=pasbon
server_4_port=61237
[influxdb]
host=localhost
port=8086
user=root
password=root
db=glances

View File

@ -143,3 +143,10 @@ mem_critical=90
#server_3_port=61209
#server_4_name=pasbon
#server_4_port=61237
[influxdb]
host=localhost
port=8086
user=root
password=root
db=glances

View File

@ -126,7 +126,7 @@ class GlancesStats(object):
# The key is the module name
# for example, the file glances_xxx.py
# generate self._exports_list["xxx"] = ...
self._exports[export_name] = export_module.Export(args=args)
self._exports[export_name] = export_module.Export(args=args, config=self.config)
# Log plugins list
logger.debug("Available exports modules list: {0}".format(self.getAllExports()))
return True

View File

@ -33,9 +33,9 @@ class Export(GlancesExport):
"""This class manages the CSV export module."""
def __init__(self, args=None):
def __init__(self, config=None, args=None):
"""Init the CSV export IF."""
GlancesExport.__init__(self, args=args)
GlancesExport.__init__(self, config=config, args=args)
# CSV file name
self.csv_filename = args.export_csv

View File

@ -34,13 +34,14 @@ class GlancesExport(object):
"""Main class for Glances' export IF."""
def __init__(self, args=None):
def __init__(self, config=None, args=None):
"""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)
# Init the args
# Init the config & args
self.config = config
self.args = args
def exit(self):

View File

@ -20,12 +20,13 @@
"""InfluxDB interface class."""
# Import sys libs
from influxdb import InfluxDBClient
from influxdb import InfluxDBClient, client
import sys
# Import Glances lib
from glances.core.glances_globals import is_py3
from glances.core.glances_logging import logger
from ConfigParser import NoSectionError, NoOptionError
from glances.exports.glances_export import GlancesExport
@ -33,30 +34,70 @@ class Export(GlancesExport):
"""This class manages the InfluxDB export module."""
def __init__(self, args=None):
def __init__(self, config=None, args=None):
"""Init the CSV export IF."""
GlancesExport.__init__(self, args=args)
GlancesExport.__init__(self, config=config, args=args)
# InfluxDB server configuration
# self.influxdb_host = args.influxdb_host
self.influxdb_host = 'localhost'
self.influxdb_port = '8086'
self.influxdb_user = 'root'
self.influxdb_password = 'root'
self.influxdb_db = 'glances'
# Load the InfluxDB configuration file
self.influxdb_host = None
self.influxdb_port = None
self.influxdb_user = None
self.influxdb_password = None
self.influxdb_db = None
self.export_enable = self.load_conf()
if not self.export_enable:
sys.exit(2)
# Init the InfluxDB client
self.client = InfluxDBClient(self.influxdb_host,
self.influxdb_port,
self.influxdb_user,
self.influxdb_password,
self.influxdb_db)
self.client = self.init()
logger.info(
"Stats exported to InfluxDB server: {0}".format(self.client._baseurl))
def load_conf(self, section="influxdb"):
"""Load the InfluxDb configuration in the Glances configuration file"""
if self.config is None:
return False
try:
self.influxdb_host = self.config.get_raw_option(section, "host")
self.influxdb_port = self.config.get_raw_option(section, "port")
self.influxdb_user = self.config.get_raw_option(section, "user")
self.influxdb_password = self.config.get_raw_option(section, "password")
self.influxdb_db = self.config.get_raw_option(section, "db")
except NoSectionError:
logger.critical("No InfluxDB configuration found")
return False
except NoOptionError as e:
logger.critical("Error in the InfluxDB configuration (%s)" % e)
return False
else:
logger.debug("Load InfluxDB from the Glances configuration file")
return True
def init(self):
"""Init the connection to the InfluxDB server"""
if not self.export_enable:
return None
db = InfluxDBClient(self.influxdb_host,
self.influxdb_port,
self.influxdb_user,
self.influxdb_password,
self.influxdb_db)
try:
get_all_db = db.get_database_list()[0].values()
except client.InfluxDBClientError as e:
logger.critical("Can not connect to InfluxDB database '%s' (%s)" % (self.influxdb_db, e))
sys.exit(2)
if self.influxdb_db in get_all_db:
logger.info(
"Stats will be exported to InfluxDB server: {0}".format(db._baseurl))
else:
logger.critical("InfluxDB database '%s' did not exist. Please create it" % self.influxdb_db)
sys.exit(2)
return db
def update(self, stats):
"""Update stats to the InfluxDB server."""
if not self.export_enable:
return False
# Get the stats
all_stats = stats.getAll()
@ -78,6 +119,8 @@ class Export(GlancesExport):
self.write_to_influxdb(plugin, export_names, export_values)
i += 1
return True
def write_to_influxdb(self, name, columns, points):
"""Write the points to the InfluxDB server"""
data = [