Ping tool (to complement the iperf3 tool) (#440)

* Ping tool (to complement the iperf3 tool)

* Make ping-tool part of the api (like traceroute)

* Bump api version
This commit is contained in:
Tim Wilkinson 2022-07-19 18:27:44 -07:00 committed by GitHub
parent ce67430b45
commit d166393bb8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 46 additions and 1 deletions

View File

@ -304,6 +304,42 @@ function getTraceroute(target)
return routes
end
-------------------------------------
-- Returns traceroute
-------------------------------------
function getPing(target)
local pings = {}
local summary = { tx = -1, rx = -1, lossPercentage = -1, ip = "not found", minMs = -1, maxMs = -1, avgMs = -1 }
local output = capture("/bin/ping -w 10 " .. target)
local foundip = "unknown"
for _, line in ipairs(output:splitNewLine())
do
local ip = line:match("^PING %S+ %(([%d%.]+)%):")
if ip then
summary.ip = ip
else
local ip, seq, ttl, time = line:match("bytes from ([%d%.]+): seq=(%d+) ttl=(%d+) time=(%S+) ms")
if ip then
pings[#pings + 1] = { ip = ip, seq = seq, ttl = ttl, timeMs = time }
else
local tx, rx, loss = line:match("^(%d+) packets transmitted, (%d+) packets received, (%d+)%% packet loss")
if tx then
summary.tx = tx
summary.rx = rx
summary.lossPercentage = loss
else
local min, avg, max = line:match("min/avg/max = ([%d%.]+)/([%d%.]+)/([%d%.]+) ms")
if min then
summary.minMs = min
summary.maxMs = max
summary.avgMs = avg
end
end
end
end
end
return { summary = summary, pings = pings }
end
function file_trim(filename, maxl)
local lines={}

View File

@ -44,7 +44,7 @@ require("nixio")
local json = require("luci.jsonc")
require("iwinfo")
local API_VERSION="1.4"
local API_VERSION="1.5"
-- Function extensions
os.capture = capture
@ -378,6 +378,15 @@ for page, comps in pairs(qsset) do
info['pages'][page][tonode]="Invalid input!"
end
end
elseif page=="ping" then
for i,tonode in pairs(comps:split(',')) do
-- Validate that input as ip or hostname inside the mesh
if tonode:match("^[%d%.]+$") or tonode:match("^[%d%a%-%.%_]+$") then
info['pages'][page][tonode]=getPing(tonode)
else
info['pages'][page][tonode]="Invalid input!"
end
end
elseif page=="mesh" then
for i,comp in pairs(comps:split(',')) do
if comp=="sysinfo" then