From a0377961c4dfb540481d8c41397c3643978f8f37 Mon Sep 17 00:00:00 2001 From: root Date: Mon, 4 Aug 2025 16:34:46 +0200 Subject: [PATCH] Output transfer rate in humand readable warning message --- check_iperf3.py | 47 +++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 43 insertions(+), 4 deletions(-) diff --git a/check_iperf3.py b/check_iperf3.py index 078a238..54314c5 100755 --- a/check_iperf3.py +++ b/check_iperf3.py @@ -154,6 +154,44 @@ def run_iperf3(options) -> Union[dict, NoReturn]: nagexit(3, "what happended?") +def sizeof_fmt(num, suffix="B", units=None, power=None, sep="", precision=2, sign=False): + sign = "+" if sign and num > 0 else "" + fmt = "{0:{1}.{2}f}{3}{4}{5}" + prec = 0 + for unit in units[:-1]: + if abs(round(num, precision)) < power: + break + num /= float(power) + prec = precision + else: + unit = units[-1] + return fmt.format(num, sign, prec, sep, unit, suffix) + + +def sizeof_fmt_iec(num, suffix="B", sep="", precision=2, sign=False): + return sizeof_fmt( + num, + suffix=suffix, + sep=sep, + precision=precision, + sign=sign, + units=["", "Ki", "Mi", "Gi", "Ti", "Pi", "Ei", "Zi", "Yi"], + power=1024, + ) + + +def sizeof_fmt_decimal(num, suffix="B", sep="", precision=2, sign=False): + return sizeof_fmt( + num, + suffix=suffix, + sep=sep, + precision=precision, + sign=sign, + units=["", "k", "M", "G", "T", "P", "E", "Z", "Y"], + power=1000, + ) + + def determine_result(options, json_data) -> Tuple[int, Optional[List[str]]]: """ Evaluates iperf3 test results against performance thresholds and determines the Nagios status. @@ -196,21 +234,22 @@ def determine_result(options, json_data) -> Tuple[int, Optional[List[str]]]: bps_avg_down = json_end['sum_sent_bidir_reverse']['bits_per_second'] if options.bidir else \ json_data['end']['sum_sent']['bits_per_second'] if options.downstream else None + fmt = lambda s: sizeof_fmt_iec(s, suffix="B/s", sep=" ", precision=0) if bps_avg_up is not None: if options.rate_up_crit and bps_avg_up <= options.rate_up_crit: rc = max(rc, 2) - statuslines.append("upstream rate below critical threshold") + statuslines.append(f"upstream rate below critical threshold: {fmt(bps_avg_up)} < {fmt(options.rate_up_crit)}") elif options.rate_up_warn and bps_avg_up <= options.rate_up_warn: rc = max(rc, 1) - statuslines.append("upstream rate below warning threshold") + statuslines.append(f"upstream rate below warning threshold: {fmt(bps_avg_up)} < {fmt(options.rate_up_warn)}") if bps_avg_down is not None: if options.rate_down_crit and bps_avg_down <= options.rate_down_crit: rc = max(rc, 2) - statuslines.append("downpstream rate below critical threshold") + statuslines.append(f"downpstream rate below critical threshold: {fmt(bps_avg_down)} < {fmt(options.rate_down_crit)}") elif options.rate_down_warn and bps_avg_down <= options.rate_down_warn: rc = max(rc, 1) - statuslines.append("downstream rate below warning threshold") + statuslines.append(f"downstream rate below warning threshold: {fmt(bps_avg_down)} < {fmt(options.rate_down_warn)}") if not options.udp and options.retrans_crit and options.retrans_warn: retransmits = json_data["end"]["sum_sent"]["retransmits"]