diff --git a/glances/core/glances_main.py b/glances/core/glances_main.py index e66b7c4d..759ca4d1 100644 --- a/glances/core/glances_main.py +++ b/glances/core/glances_main.py @@ -94,6 +94,8 @@ class GlancesMain(object): # Export modules feature parser.add_argument('--export-csv', default=None, dest='export_csv', help=_('export stats to a CSV file')) + parser.add_argument('--export-influxdb', action='store_true', default=False, + dest='export_influxdb', help=_('export stats to an InfluxDB server')) # Client/Server option parser.add_argument('-c', '--client', dest='client', help=_('connect to a Glances server by IPv4/IPv6 address or hostname')) diff --git a/glances/core/glances_stats.py b/glances/core/glances_stats.py index 0e49c727..34571c2c 100644 --- a/glances/core/glances_stats.py +++ b/glances/core/glances_stats.py @@ -116,7 +116,8 @@ class GlancesStats(object): item.endswith(".py") and item != (header + "export.py") and item != (header + "history.py") and - args_var['export_' + export_name] is not None): + args_var['export_' + export_name] is not None and + args_var['export_' + export_name] is not False): # Import the export module export_module = __import__(os.path.basename(item)[:-3]) # Add the export to the dictionary diff --git a/glances/exports/glances_csv.py b/glances/exports/glances_csv.py index 77a4a0c9..73e57af8 100644 --- a/glances/exports/glances_csv.py +++ b/glances/exports/glances_csv.py @@ -28,10 +28,6 @@ from glances.core.glances_globals import is_py3 from glances.core.glances_logging import logger from glances.exports.glances_export import GlancesExport -# List of stats enabled in the CSV output -# !!! TODO: should be configurable from the conf file -csv_stats_list = ['cpu', 'load', 'mem', 'memswap', 'network', 'diskio', 'fs'] - class Export(GlancesExport): @@ -61,6 +57,7 @@ class Export(GlancesExport): def exit(self): """Close the CSV file.""" + logger.debug("Finalise export interface %s" % self.export_name) self.csv_file.close() def update(self, stats): @@ -75,7 +72,7 @@ class Export(GlancesExport): # Loop over available plugin i = 0 for plugin in plugins: - if plugin in csv_stats_list: + if plugin in self.plugins_to_export(): if type(all_stats[i]) is list: for item in all_stats[i]: # First line: header diff --git a/glances/exports/glances_export.py b/glances/exports/glances_export.py index 41f43fbd..5e5b68f1 100644 --- a/glances/exports/glances_export.py +++ b/glances/exports/glances_export.py @@ -42,3 +42,11 @@ class GlancesExport(object): # Init the args self.args = args + + def exit(self): + """Close the export module.""" + logger.debug("Finalise export interface %s" % self.export_name) + + def plugins_to_export(self): + """Return the list of plugins to export""" + return ['cpu', 'load', 'mem', 'memswap', 'network', 'diskio', 'fs', 'processcount'] diff --git a/glances/exports/glances_influxdb.py b/glances/exports/glances_influxdb.py new file mode 100644 index 00000000..7a0466cf --- /dev/null +++ b/glances/exports/glances_influxdb.py @@ -0,0 +1,89 @@ +# -*- coding: utf-8 -*- +# +# This file is part of Glances. +# +# Copyright (C) 2014 Nicolargo +# +# Glances is free software; you can redistribute it and/or modify +# it under the terms of the GNU Lesser General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# Glances is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU Lesser General Public License for more details. +# +# You should have received a copy of the GNU Lesser General Public License +# along with this program. If not, see . + +"""InfluxDB interface class.""" + +# Import sys libs +from influxdb import InfluxDBClient +import sys + +# Import Glances lib +from glances.core.glances_globals import is_py3 +from glances.core.glances_logging import logger +from glances.exports.glances_export import GlancesExport + + +class Export(GlancesExport): + + """This class manages the InfluxDB export module.""" + + def __init__(self, args=None): + """Init the CSV export IF.""" + GlancesExport.__init__(self, 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' + + # Init the InfluxDB client + self.client = InfluxDBClient(self.influxdb_host, + self.influxdb_port, + self.influxdb_user, + self.influxdb_password, + self.influxdb_db) + + logger.info( + "Stats exported to InfluxDB server: {0}".format(self.client._baseurl)) + + def update(self, stats): + """Update stats to the InfluxDB server.""" + + # Get the stats + all_stats = stats.getAll() + plugins = stats.getAllPlugins() + + # Loop over available plugin + i = 0 + for plugin in plugins: + if plugin in self.plugins_to_export(): + if type(all_stats[i]) is list: + for item in all_stats[i]: + export_names = map( + lambda x: item[item['key']] + '_' + x, item.keys()) + export_values = item.values() + self.write_to_influxdb(plugin, export_names, export_values) + elif type(all_stats[i]) is dict: + export_names = all_stats[i].keys() + export_values = all_stats[i].values() + self.write_to_influxdb(plugin, export_names, export_values) + i += 1 + + def write_to_influxdb(self, name, columns, points): + """Write the points to the InfluxDB server""" + data = [ + { + "name": name, + "columns": columns, + "points": [points] + }] + self.client.write_points(data)