From d1b74c21d8c6f816179cd700dd5859f51bf01d88 Mon Sep 17 00:00:00 2001
From: "Matthias P. Braendli" <matthias.braendli@mpb.li>
Date: Thu, 1 Feb 2018 04:03:10 +0100
Subject: Add show_dabmux_stats script to doc

---
 doc/README.md            |  3 +-
 doc/show_dabmux_stats.py | 83 ++++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 85 insertions(+), 1 deletion(-)
 create mode 100755 doc/show_dabmux_stats.py

diff --git a/doc/README.md b/doc/README.md
index fe4748e..45669f4 100644
--- a/doc/README.md
+++ b/doc/README.md
@@ -50,7 +50,8 @@ An explanation on how to use the remote control is in *remote_control.txt*, and
 control interface.
 
 Two scripts are used for monitoring systems: *stats_dabmux_multi.py* for Munin,
-and *retodrs.pl* for Xymon.
+and *retodrs.pl* for Xymon. You can use *show_dabmux_stats.py* to print the
+statistics to console.
 
 *DabMux.1* is an old manpage that describes the command line options that
 existed in past versions. It is kept for archive.
diff --git a/doc/show_dabmux_stats.py b/doc/show_dabmux_stats.py
new file mode 100755
index 0000000..f781839
--- /dev/null
+++ b/doc/show_dabmux_stats.py
@@ -0,0 +1,83 @@
+#!/usr/bin/env python2
+#
+# present statistics from dabmux Stats Server
+# to standard output.
+#
+# If you are looking for munin integration, use
+# ODR-DabMux/doc/stats_dabmux_multi.py
+
+import sys
+import json
+import zmq
+import os
+
+ctx = zmq.Context()
+
+def connect():
+    """Create a connection to the dabmux stats server
+
+    returns: the socket"""
+
+    sock = zmq.Socket(ctx, zmq.REQ)
+    sock.connect("tcp://localhost:12720")
+
+    sock.send("info")
+    version = json.loads(sock.recv())
+
+    if not version['service'].startswith("ODR-DabMux"):
+        sys.stderr.write("Wrong version\n")
+        sys.exit(1)
+
+    return sock
+
+if len(sys.argv) == 1:
+    sock = connect()
+    sock.send("values")
+
+    poller = zmq.Poller()
+    poller.register(sock, zmq.POLLIN)
+
+    socks = dict(poller.poll(1000))
+    if socks:
+        if socks.get(sock) == zmq.POLLIN:
+
+            data = sock.recv()
+            values = json.loads(data)['values']
+
+            tmpl = "{ident:20}{maxfill:>8}{minfill:>8}{under:>8}{over:>8}{peakleft:>8}{peakright:>8}{state:>16}"
+            print(tmpl.format(
+                ident="id",
+                maxfill="max",
+                minfill="min",
+                under="under",
+                over="over",
+                peakleft="peak L",
+                peakright="peak R",
+                state="state"))
+
+            for ident in values:
+                v = values[ident]['inputstat']
+
+                if 'state' not in v:
+                    v['state'] = None
+
+                print(tmpl.format(
+                    ident=ident,
+                    maxfill=v['max_fill'],
+                    minfill=v['min_fill'],
+                    under=v['num_underruns'],
+                    over=v['num_overruns'],
+                    peakleft=v['peak_left'],
+                    peakright=v['peak_right'],
+                    state=v['state']))
+
+
+elif len(sys.argv) == 2 and sys.argv[1] == "config":
+    sock = connect()
+
+    sock.send("config")
+
+    config = json.loads(sock.recv())
+
+    print(config['config'])
+
-- 
cgit v1.2.3