From c9bb6ebd918ece3187a9cf4f03df5c4456c2343d Mon Sep 17 00:00:00 2001 From: Diego Pino Garcia Date: Sun, 26 Oct 2014 23:48:39 +0100 Subject: [PATCH] Avoid division by zero in calculation of loss rate If there are no packets sent, print loss rate '0' instead of 'NaN'. --- src/core/app.lua | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/src/core/app.lua b/src/core/app.lua index 7480072548..cd550eb017 100644 --- a/src/core/app.lua +++ b/src/core/app.lua @@ -269,10 +269,16 @@ function breathe () end function report (options) + local function loss_rate(drop, sent) + sent = tonumber(sent) + if not sent or sent == 0 then return 0 end + return tonumber(drop) * 100 / sent + end if not options or options.showlinks then print("link report") for name, l in pairs(link_table) do - print(lib.comma_value(tostring(tonumber(l.stats.txpackets))), "sent on", name, "(loss rate: " .. (tonumber(l.stats.txdrop) * 100 / tonumber(l.stats.txpackets)) .. "%)") + print(("%s sent on %s (loss rate: %d%%))"):format(l.stats.txpackets, + name, loss_rate(l.stats.txdrop, l.stats.txpackets))) end end if options and options.showapps then