提交 74593f13 编写于 作者: N nicolargo

Add a ZeroMQ export module #939

上级 0c49fecf
......@@ -7,6 +7,7 @@ Version 2.8
Enhancements and new features:
* Add ZeroMQ exporter (issue #939)
* Add CouchDB exporter (issue #928)
* Highlight max stats in the processes list (issue #878)
* Docker alerts and actions (issue #875)
......
......@@ -109,14 +109,14 @@ features (like the Web interface, exports modules, sensors...):
.. code-block:: console
pip install bottle requests batinfo https://bitbucket.org/gleb_zhulik/py3sensors/get/tip.tar.gz zeroconf netifaces pymdstat influxdb elasticsearch potsdb statsd pystache docker-py pysnmp pika py-cpuinfo bernhard cassandra-driver scandir
pip install bottle requests batinfo https://bitbucket.org/gleb_zhulik/py3sensors/get/tip.tar.gz zeroconf netifaces pymdstat influxdb elasticsearch potsdb statsd pystache docker-py pysnmp pika py-cpuinfo bernhard cassandra-driver scandir pyzmq
To upgrade Glances to the latest version:
.. code-block:: console
pip install --upgrade glances
pip install --upgrade bottle requests batinfo https://bitbucket.org/gleb_zhulik/py3sensors/get/tip.tar.gz zeroconf netifaces pymdstat influxdb elasticsearch potsdb statsd pystache docker-py pysnmp pika py-cpuinfo bernhard cassandra-driver scandir
pip install --upgrade bottle requests batinfo https://bitbucket.org/gleb_zhulik/py3sensors/get/tip.tar.gz zeroconf netifaces pymdstat influxdb elasticsearch potsdb statsd pystache docker-py pysnmp pika py-cpuinfo bernhard cassandra-driver scandir pyzmq
If you need to install Glances in a specific user location, use:
......
......@@ -319,6 +319,17 @@ db=glances
#user=root
#password=root
[zeromq]
# Configuration for the --export-zeromq option
# http://www.zeromq.org
host=127.0.0.1
port=5678
# Glances envelopes the stats in a publish message with two frames:
# - First frame containing the following prefix (STRING)
# - Second frame with the Glances plugin name (STRING)
# - Third frame with the Glances plugin stats (JSON)
prefix=G
##############################################################################
# AMPS
# * enable: Enable (true) or disable (false) the AMP
......
.. _zeromq:
ZeroMQ
======
You can export statistics to a ``ZeroMQ`` server.
The connection should be defined in the Glances configuration file as
following:
.. code-block:: ini
[zeromq]
host=127.0.0.1
port=5678
prefix=G
Note: Glances envelopes the stats in a publish message with two frames.
- first frame containing the following prefix (as STRING)
- second frame with the Glances plugin name (as STRING)
- third frame with the Glances plugin stats (as JSON)
Run Glances with:
.. code-block:: console
$ glances --export-zeromq
Following is a simple Python client to subscribe to the Glances stats:
.. code-block:: python
# -*- coding: utf-8 -*-
#
# ZeroMQ subscriber for Glances
#
import json
import zmq
context = zmq.Context()
subscriber = context.socket(zmq.SUB)
subscriber.setsockopt(zmq.SUBSCRIBE, 'G')
subscriber.connect("tcp://127.0.0.1:5678")
while True:
_, plugin, data_raw = subscriber.recv_multipart()
data = json.loads(data_raw)
print('{} => {}'.format(plugin, data))
subscriber.close()
context.term()
......@@ -22,7 +22,7 @@ features (like the Web interface, exports modules, sensors...):
.. code-block:: console
pip install bottle requests batinfo py3sensors zeroconf netifaces pymdstat influxdb elasticsearch potsdb statsd pystache docker-py pysnmp pika py-cpuinfo bernhard cassandra scandir couchdb
pip install bottle requests batinfo py3sensors zeroconf netifaces pymdstat influxdb elasticsearch potsdb statsd pystache docker-py pysnmp pika py-cpuinfo bernhard cassandra scandir couchdb pyzmq
To upgrade Glances to the latest version:
......
.\" Man page generated from reStructuredText.
.
.TH "GLANCES" "1" "Oct 10, 2016" "2.8_DEVELOP" "Glances"
.TH "GLANCES" "1" "Oct 15, 2016" "2.8_DEVELOP" "Glances"
.SH NAME
glances \- An eye on your system
.
......
# -*- coding: utf-8 -*-
#
# This file is part of Glances.
#
# Copyright (C) 2016 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/>.
"""ZeroMQ interface class."""
import sys
from datetime import datetime
import time
import json
from glances.compat import NoOptionError, NoSectionError
from glances.logger import logger
from glances.exports.glances_export import GlancesExport
import zmq
class Export(GlancesExport):
"""This class manages the ZeroMQ export module."""
def __init__(self, config=None, args=None):
"""Init the ZeroMQ export IF."""
super(Export, self).__init__(config=config, args=args)
# Load the ZeroMQ configuration file section ([export_zeromq])
self.host = None
self.port = None
self.export_enable = self.load_conf()
if not self.export_enable:
sys.exit(2)
# Init the ZeroMQ context
self.client = self.init()
def load_conf(self, section="zeromq"):
"""Load the ZeroMQ configuration in the Glances configuration file."""
if self.config is None:
return False
try:
self.host = self.config.get_value(section, 'host')
self.port = self.config.get_value(section, 'port')
self.prefix = self.config.get_value(section, 'prefix')
except NoSectionError:
logger.critical("No ZeroMQ configuration found")
return False
except NoOptionError as e:
logger.critical("Error in the ZeroMQ configuration (%s)" % e)
return False
else:
logger.debug("Load ZeroMQ from the Glances configuration file")
return True
def init(self):
"""Init the connection to the CouchDB server."""
if not self.export_enable:
return None
server_uri = 'tcp://{}:{}'.format(self.host, self.port)
try:
context = zmq.Context()
publisher = context.socket(zmq.PUB)
publisher.bind(server_uri)
except Exception as e:
logger.critical("Cannot connect to ZeroMQ server %s (%s)" % (server_uri, e))
sys.exit(2)
else:
logger.info("Connected to the ZeroMQ server %s" % server_uri)
return publisher
def exit(self):
"""Close the socket"""
self.client.close()
def export(self, name, columns, points):
"""Write the points to the ZeroMQ server."""
logger.debug("Export {} stats to ZeroMQ".format(name))
# Create DB input
data = dict(zip(columns, points))
# Do not publish empty stats
if data == {}:
return False
# Glances envelopes the stats in a publish message with two frames:
# - First frame containing the following prefix (STRING)
# - Second frame with the Glances plugin name (STRING)
# - Third frame with the Glances plugin stats (JSON)
message = [str(self.prefix),
name,
json.dumps(data)]
# Write data to the ZeroMQ bus
# Result can be view: tcp://host:port
try:
self.client.send_multipart(message)
except Exception as e:
logger.error("Cannot export {} stats to ZeroMQ ({})".format(name, e))
return True
......@@ -2,7 +2,7 @@
#
# This file is part of Glances.
#
# Copyright (C) 2015 Nicolargo <nicolas@nicolargo.com>
# Copyright (C) 2016 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
......@@ -176,6 +176,8 @@ Start the client browser (browser mode):\n\
dest='export_riemann', help='export stats to riemann broker (bernhard lib needed)')
parser.add_argument('--export-couchdb', action='store_true', default=False,
dest='export_couchdb', help='export stats to a CouchDB server (couch lib needed)')
parser.add_argument('--export-zeromq', action='store_true', default=False,
dest='export_zeromq', help='export stats to a ZeroMQ server (pyzmq lib needed)')
# Client/Server option
parser.add_argument('-c', '--client', dest='client',
help='connect to a Glances server by IPv4/IPv6 address or hostname')
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册