glances/docs/gw/zeromq.rst

58 lines
1.3 KiB
ReStructuredText
Raw Normal View History

2016-10-15 16:24:01 +03:00
.. _zeromq:
ZeroMQ
======
You can export statistics to a ``ZeroMQ`` server.
2016-10-15 20:16:24 +03:00
2016-10-15 16:24:01 +03:00
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
2016-10-15 20:16:24 +03:00
Note: Glances `envelopes`_ the stats before publishing it.
The message is composed of three frames.
2016-10-15 16:24:01 +03:00
2016-10-15 20:16:24 +03:00
- first frame containing the prefix configured in the [zeromq] section (as STRING)
2016-10-15 16:24:01 +03:00
- second frame with the Glances plugin name (as STRING)
- third frame with the Glances plugin stats (as JSON)
Run Glances with:
.. code-block:: console
2016-10-15 20:16:24 +03:00
$ glances --export-zeromq -C <path>/glances.conf
2016-10-15 16:24:01 +03:00
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()
2016-10-15 20:16:24 +03:00
.. _envelopes: http://zguide.zeromq.org/page:all#Pub-Sub-Message-Envelopes