mirror of
https://github.com/nicolargo/glances.git
synced 2024-12-29 20:21:35 +03:00
Merge pull request #540 from Katyucha/develop
RabbitMQ export (3rd try) Thk for the contrib !
This commit is contained in:
commit
e0205a9e4a
@ -177,3 +177,10 @@ db=glances
|
||||
host=localhost
|
||||
port=8125
|
||||
#prefix=glances
|
||||
|
||||
[rabbitmq]
|
||||
host=localhost
|
||||
port=5672
|
||||
user=glances
|
||||
password=glances
|
||||
queue=glances_queue
|
||||
|
@ -159,6 +159,8 @@ Command-Line Options
|
||||
export stats to an InfluxDB server
|
||||
--export-statsd
|
||||
export stats to a Statsd server
|
||||
--export-rabbitmq
|
||||
export stats to a RabbitMQ server
|
||||
-c CLIENT, --client CLIENT
|
||||
connect to a Glances server by IPv4/IPv6 address or
|
||||
hostname
|
||||
@ -824,6 +826,25 @@ Glances will generate stats as:
|
||||
'glances.load.min1': 0.19,
|
||||
...
|
||||
|
||||
*RabbitMQ*
|
||||
|
||||
You can export statistics to an RabbitMQ server (AMQP Broker). The connection should be defined in the Glances configuration file as following:
|
||||
|
||||
.. code-block::
|
||||
|
||||
[rabbitmq]
|
||||
host=localhost
|
||||
port=5672
|
||||
user=glances
|
||||
password=glances
|
||||
queue=glances_queue
|
||||
|
||||
and run Glances with:
|
||||
|
||||
.. code-block:: console
|
||||
|
||||
$ glances --export-rabbitmq
|
||||
|
||||
|
||||
APIs Documentations
|
||||
===================
|
||||
|
@ -139,6 +139,7 @@ Start the client browser (browser mode):\n\
|
||||
dest='export_influxdb', help=_('export stats to an InfluxDB server (need InfluDB lib)'))
|
||||
parser.add_argument('--export-statsd', action='store_true', default=False,
|
||||
dest='export_statsd', help=_('export stats to a Statsd server (need StatsD lib)'))
|
||||
parser.add_argument('--export-rabbitmq', action='store_true', default=False, dest='export_rabbitmq', help=_('export stats to rabbitmq broker (need pika lib or python3-pika lib)'))
|
||||
# Client/Server option
|
||||
parser.add_argument('-c', '--client', dest='client',
|
||||
help=_('connect to a Glances server by IPv4/IPv6 address or hostname'))
|
||||
|
105
glances/exports/glances_rabbitmq.py
Normal file
105
glances/exports/glances_rabbitmq.py
Normal file
@ -0,0 +1,105 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
#
|
||||
# This file is part of Glances.
|
||||
#
|
||||
# Copyright (C) 2015 Nicolargo <nicolas@nicolargo.com>
|
||||
#
|
||||
# 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 <http://www.gnu.org/licenses/>.
|
||||
|
||||
"""JMS interface class."""
|
||||
|
||||
# Import sys libs
|
||||
import sys, socket, datetime
|
||||
from numbers import Number
|
||||
|
||||
# Import Glances lib
|
||||
from glances.core.glances_logging import logger
|
||||
try:
|
||||
from configparser import NoOptionError, NoSectionError
|
||||
except ImportError: # Python 2
|
||||
from ConfigParser import NoOptionError, NoSectionError
|
||||
from glances.exports.glances_export import GlancesExport
|
||||
|
||||
# Import pika for RabbitMQ
|
||||
import pika
|
||||
|
||||
|
||||
class Export(GlancesExport):
|
||||
|
||||
"""This class manages the rabbitMQ export module."""
|
||||
|
||||
def __init__(self, config=None, args=None):
|
||||
"""Init the RabbitMQ export IF."""
|
||||
GlancesExport.__init__(self, config=config, args=args)
|
||||
|
||||
# Load the rabbitMQ configuration file
|
||||
self.rabbitmq_host = None
|
||||
self.rabbitmq_port = None
|
||||
self.rabbitmq_user = None
|
||||
self.rabbitmq_password = None
|
||||
self.rabbitmq_queue = None
|
||||
self.hostname = socket.gethostname()
|
||||
self.export_enable = self.load_conf()
|
||||
if not self.export_enable:
|
||||
sys.exit(2)
|
||||
|
||||
# Init the rabbitmq client
|
||||
self.client = self.init()
|
||||
|
||||
def load_conf(self, section="rabbitmq"):
|
||||
"""Load the rabbitmq configuration in the Glances configuration file"""
|
||||
if self.config is None:
|
||||
return False
|
||||
try:
|
||||
self.rabbitmq_host = self.config.get_raw_option(section, "host")
|
||||
self.rabbitmq_port = self.config.get_raw_option(section, "port")
|
||||
self.rabbitmq_user = self.config.get_raw_option(section, "user")
|
||||
self.rabbitmq_password = self.config.get_raw_option(section, "password")
|
||||
self.rabbitmq_queue = self.config.get_raw_option(section, "queue")
|
||||
except NoSectionError:
|
||||
logger.critical("No rabbitmq configuration found")
|
||||
return False
|
||||
except NoOptionError as e:
|
||||
logger.critical("Error in the RabbitM configuration (%s)" % e)
|
||||
return False
|
||||
else:
|
||||
logger.debug("Load RabbitMQ from the Glances configuration file")
|
||||
return True
|
||||
|
||||
def init(self):
|
||||
"""Init the connection to the rabbitmq server"""
|
||||
if not self.export_enable:
|
||||
return None
|
||||
try:
|
||||
parameters = pika.URLParameters("amqp://"+self.rabbitmq_user+":"+self.rabbitmq_password+"@"+self.rabbitmq_host+":"+self.rabbitmq_port+"/")
|
||||
connection = pika.BlockingConnection(parameters)
|
||||
channel = connection.channel()
|
||||
return channel
|
||||
except Exception as e:
|
||||
logger.critical("Connection to rabbitMQ failed : %s " % e)
|
||||
return None
|
||||
|
||||
def export(self, name, columns, points):
|
||||
"""Write the points in RabbitMQ"""
|
||||
data = "hostname="+self.hostname+", name="+name+", dateinfo="+datetime.datetime.utcnow().isoformat()
|
||||
for i in range(0, len(columns)):
|
||||
if not isinstance(points[i], Number):
|
||||
continue
|
||||
else:
|
||||
data += ", "+columns[i]+"="+str(points[i])
|
||||
logger.debug(data)
|
||||
try:
|
||||
self.client.basic_publish(exchange='', routing_key="glances_queue", body=data)
|
||||
except Exception as e:
|
||||
logger.error("Can not export stats to RabbitMQ (%s)" % e)
|
@ -80,6 +80,9 @@ export stats to an InfluxDB server
|
||||
.B \-\-export-statsd
|
||||
export stats to a Statsd server
|
||||
.TP
|
||||
.B \-\-export-rabbitmq
|
||||
export stats to a RabbitMQ server
|
||||
.TP
|
||||
.B \-s, \-\-server
|
||||
run Glances in server mode
|
||||
.TP
|
||||
@ -234,6 +237,9 @@ Monitor local machine and export stats to a CSV file (standalone mode):
|
||||
Monitor local machine and export stats to a InfluxDB server with 5s refresh time (standalone mode):
|
||||
.B $ glances -t 5 --export-influxdb
|
||||
.PP
|
||||
Monitor local machine and export stats to a RabbitMQ server with 5s refresh time (standalone mode):
|
||||
.B $ glances -t 5 --export-rabbitmq
|
||||
.PP
|
||||
Start a Glances server (server mode):
|
||||
.B $ glances -s
|
||||
.PP
|
||||
|
Loading…
Reference in New Issue
Block a user