summaryrefslogtreecommitdiffstats
path: root/docs/gw/zeromq.rst
blob: 3af4b7106f1461d8b59ce5162e8f4e27b27afd5a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
.. _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

Glances `envelopes`_ the stats before publishing it. The message is
composed of three frames:

1. the prefix configured in the [zeromq] section (as STRING)
2. the Glances plugin name (as STRING)
3. 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()

.. _envelopes: http://zguide.zeromq.org/page:all#Pub-Sub-Message-Envelopes