aboutsummaryrefslogtreecommitdiffstats
path: root/doc/stats_dabmod_munin.py
blob: d3b52ed075520bb9b796c12edcdf4531758ef68a (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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
#!/usr/bin/env python2
#
# present statistics from ODR-DabMod's
# RC interface to munin

import sys
import json
import zmq
import os
import re

# Values monitored:

config_all = ""

#default data type is GAUGE

# One GAUGE multigraph from 0% to 100% with
#   ofdm clip_stats clip_ratio
#   ofdm clip_stats errorclip_ratio
config_all += """
multigraph ofdm_clip_stats
graph_title OFDM CFR clip stats
graph_order clip_ratio errorclip_ratio
graph_vlabel number of samples/errors clipped during last ${{graph_period}}
graph_category dabmod
graph_info This graph shows CFR clipping statistics

clip_ratio.info Number of samples clipped
clip_ratio.label Number of samples clipped
clip_ratio.min 0
clip_ratio.max 100
errorclip_ratio.info Number of errors clipped
errorclip_ratio.label Number of errors clipped
errorclip_ratio.min 0
errorclip_ratio.max 100"""

# One GAUGE multigraph
#   ofdm clip_stats mer
config_all += """
multigraph ofdm_clip_stats_mer
graph_title OFDM MER after CFR
graph_order mer
graph_vlabel MER in dB after CFR
graph_category dabmod
graph_info This graph shows MER after CFR

mer.info MER dB
mer.label MER dB
mer.min 0
mer.max 100"""

# One GAUGE multigraph in dB for
#   ofdm papr before-cfr
#   ofdm papr after-cfr
config_all += """
multigraph ofdm_papr
graph_title OFDM PAPR stats
graph_order before_cfr after_cfr
graph_args --base 1000
graph_vlabel Averate PAPR before/after CFR during last ${{graph_period}}
graph_category dabmod
graph_info This graph shows the Peak-to-Average Power Ratio before and after CFR

before_cfr.info PAPR before CFR
before_cfr.label PAPR before CFR
before_cfr.min 0
after_cfr.info PAPR after CFR
after_cfr.label PAPR after CFR
after_cfr.min 0"""

# One GAUGE graph for
#   tist offset
config_all += """
multigraph tist_offset
graph_title TIST configured offset
graph_order offset
graph_args --base 1000
graph_vlabel Configured offset
graph_category dabmod
graph_info This graph shows the configured TIST offset

offset.info Configured offset
offset.label Configured offset
offset.min 0
offset.max 300"""

# One DDERIVE graph for
#   tist timestamp timestamps
config_all += """
multigraph tist_timestamp
graph_title TIST timestamp
graph_order timestamp
graph_args --base 1000
graph_vlabel timestamp value
graph_category dabmod
graph_info This graph shows the timestamp value in seconds

timestamp.info timestamp
timestamp.label timestamp
timestamp.type DDERIVE
timestamp.min 0"""

# One DERIVE (min 0) multigraph for
#   sdr underruns
#   sdr latepackets
config_all += """
multigraph sdr_stats
graph_title SDR device statistics
graph_order underruns latepackets
graph_args --base 1000
graph_vlabel Number of underruns and late packets
graph_category dabmod
graph_info This graph shows the number of underruns and late packets

underruns.info Number of SoapySDR/UHD underruns
underruns.label Number of SoapySDR/UHD underruns
underruns.type DERIVE
underruns.min 0
latepackets.info Number of SoapySDR/UHD late packets
latepackets.label Number of SoapySDR/UHD late packets
latepackets.type DERIVE
latepackets.min 0"""

# One DERIVE (min 0) graph for
#   sdr frames
config_all += """
multigraph sdr_frames
graph_title SDR number of frames transmitted
graph_order frames
graph_args --base 1000
graph_vlabel Number of frames transmitted
graph_category dabmod
graph_info This graph shows the number of frames transmitted

frames.info Number of SoapySDR/UHD frames
frames.label Number of SoapySDR/UHD frames
frames.type DERIVE
frames.min 0"""

ctx = zmq.Context()

class RCException(Exception):
    pass

if not os.environ.get("MUNIN_CAP_MULTIGRAPH"):
    sys.stderr.write("This needs munin version 1.4 at least\n")
    sys.exit(1)

def do_transaction(message_parts, sock):
    """To a send + receive transaction, quit whole program on timeout"""
    if isinstance(message_parts, str):
        sys.stderr.write("do_transaction expects a list!\n");
        sys.exit(1)

    for i, part in enumerate(message_parts):
        if i == len(message_parts) - 1:
            f = 0
        else:
            f = zmq.SNDMORE
        sock.send(part, flags=f)

    poller = zmq.Poller()
    poller.register(sock, zmq.POLLIN)

    socks = dict(poller.poll(1000))
    if socks:
        if socks.get(sock) == zmq.POLLIN:
            rxpackets = sock.recv_multipart()
            return rxpackets

    raise RCException("Could not receive data for command '{}'\n".format(
        message_parts))

def connect():
    """Create a connection to the dabmod RC

    returns: the socket"""

    sock = zmq.Socket(ctx, zmq.REQ)
    sock.set(zmq.LINGER, 5)
    sock.connect("tcp://localhost:9400")

    try:
        ping_answer = do_transaction([b"ping"], sock)

        if not ping_answer == [b"ok"]:
            sys.stderr.write("Wrong answer to ping\n")
            sys.exit(1)
    except RCException as e:
        print("connect failed because: {}".format(e))
        sys.exit(1)

    return sock

def get_rc_value(module, name, sock):
    try:
        parts = do_transaction([b"get", module.encode(), name.encode()], sock)
        if len(parts) != 1:
            sys.stderr.write("Received unexpected multipart message {}\n".format(
                parts))
            sys.exit(1)
        return parts[0].decode()
    except RCException as e:
        print("get {} {} fail: {}".format(module, name, e))
        return ""

def handle_re(graph_name, re, rc_value, group_number=1):
    match = re.search(rc_value)
    if match:
        return "{} {}\n".format(graph_name, match.group(group_number))
    else:
        return "{} U\n".format(graph_name)

re_double_value = re.compile(r"(\d+\.\d+)", re.X)
re_int_value = re.compile(r"(\d+)", re.X)

if len(sys.argv) == 1:
    sock = connect()

    munin_values = ""

    munin_values += "multigraph ofdm_clip_stats\n"
    ofdm_clip_stats = get_rc_value("ofdm", "clip_stats", sock)
    re_clip_samples = re.compile(r"(\d+\.\d+)%\ samples\ clipped", re.X)
    munin_values += handle_re("clip_ratio.value ", re_clip_samples, ofdm_clip_stats)

    re_clip_errors = re.compile(r"(\d+\.\d+)%\ errors\ clipped", re.X)
    munin_values += handle_re("errorclip_ratio.value",
            re_clip_errors, ofdm_clip_stats)

    munin_values += "multigraph ofdm_clip_stats_mer\n"
    re_clip_mer = re.compile(r"MER\ after\ CFR:\ (\d+\.\d+)", re.X)
    munin_values += handle_re("mer.value",
            re_clip_mer, ofdm_clip_stats)

    munin_values += "multigraph ofdm_papr\n"
    ofdm_papr_stats = get_rc_value("ofdm", "papr", sock)

    def muninise_papr(papr):
        if "N/A" in papr:
            return "U"
        else:
            return float(papr.strip())

    # Format is as follows:
    # "PAPR [dB]: " << std::fixed <<
    #   (papr_before == 0 ? string("N/A") : to_string(papr_before)) <<
    #   ", " <<
    #   (papr_after == 0 ? string("N/A") : to_string(papr_after));
    try:
        _, _, both_papr = ofdm_papr_stats.partition(":")
        papr_before, papr_after = both_papr.split(",")
        papr_before = muninise_papr(papr_before)
        munin_values += "before_cfr.value {}\n".format(papr_before)
    except:
        munin_values += "before_cfr.value U\n"

    try:
        _, _, both_papr = ofdm_papr_stats.partition(":")
        papr_before, papr_after = both_papr.split(",")
        papr_after = muninise_papr(papr_after)
        munin_values += "after_cfr.value {}\n".format(papr_after)
    except:
        munin_values += "after_cfr.value U\n"


    munin_values += "multigraph tist_offset\n"
    tist_offset = get_rc_value("tist", "offset", sock)
    munin_values += handle_re("offset.value", re_double_value, tist_offset)

    # Plotting FCT is not useful because it overflows in 6s, and the poll
    # interval is usually 5min

    munin_values += "multigraph tist_timestamp\n"
    munin_values += handle_re("timestamp.value", re_tist_timestamp, tist_timestamp, 1)

    munin_values += "multigraph sdr_stats\n"
    sdr_underruns = get_rc_value("sdr", "underruns", sock)
    munin_values += handle_re("underruns.value", re_int_value, sdr_underruns)
    sdr_latepackets = get_rc_value("sdr", "latepackets", sock)
    munin_values += handle_re("latepacket.value", re_int_value, sdr_latepackets)

    munin_values += "multigraph sdr_frames\n"
    sdr_frames = get_rc_value("sdr", "frames", sock)
    munin_values += handle_re("frames.value", re_int_value, sdr_frames)

    print(munin_values)

elif len(sys.argv) == 2 and sys.argv[1] == "config":
    # No need to connect
    print(config_all)
else:
    sys.stderr.write("Invalid command line arguments")
    sys.exit(1)