mirror of
https://github.com/ntop/ntopng.git
synced 2026-04-30 16:09:32 +00:00
numHosts was suppressed in an earlier commit but this caused issues with pagination of results.
210 lines
5.4 KiB
Lua
210 lines
5.4 KiB
Lua
--
|
|
-- (C) 2014-15 - ntop.org
|
|
--
|
|
|
|
-- ########
|
|
|
|
function updateKey(hash_name, key_name, value)
|
|
io.write("# "..key_name.." += "..value.."\n")
|
|
|
|
if(hash_name[key_name] == nil) then
|
|
hash_name[key_name] = value
|
|
else
|
|
hash_name[key_name] = hash_name[key_name] + value
|
|
end
|
|
end
|
|
|
|
-- ######
|
|
|
|
function hash2json(hash_name, label)
|
|
print "[\n"
|
|
|
|
n = 0
|
|
for k, v in pairs(hash_name) do
|
|
if(n > 0) then print(',\n') end
|
|
print('{ "label": "'..k..'", "url": "'..label..'='..k..'", "value": '..v..' }\n')
|
|
n = n + 1
|
|
end
|
|
|
|
print("]")
|
|
end
|
|
|
|
-- ######
|
|
|
|
function sliceHash(hash_name, max_num_entries, sort_direction, url, url_trailer)
|
|
local sortedKeys
|
|
|
|
if(sort_direction == "desc") then
|
|
sortedKeys = getKeysSortedByValue(hash_name, function(a, b) return a > b end)
|
|
else
|
|
sortedKeys = getKeysSortedByValue(hash_name, function(a, b) return a < b end)
|
|
end
|
|
|
|
print "[\n"
|
|
n = 0
|
|
|
|
for _, key in ipairs(sortedKeys) do
|
|
if((max_num_entries > 0) and (n > max_num_entries)) then
|
|
break
|
|
else
|
|
if(url == "") then
|
|
print('{ "label": "'..key..'", "url": "", "value": '..hash_name[key]..' }\n')
|
|
else
|
|
print('{ "label": "'..key..'", "url": "'..url..'='..key..url_trailer..'", "value": '..hash_name[key]..' }\n')
|
|
end
|
|
end
|
|
n = n + 1
|
|
end
|
|
|
|
print("]")
|
|
end
|
|
|
|
|
|
-- ######
|
|
|
|
-- host, vlan, unit (bytes/packets), mode (rcvd/sent/both), max_num_entries (10), sort(desc/asc)
|
|
function getTalkers(ifname, vlan, unit, mode, max_num_entries, sort_direction)
|
|
interface.select(ifname)
|
|
hosts_stats = interface.getHostsInfo()
|
|
hosts_stats = hosts_stats["hosts"]
|
|
|
|
hosts = { }
|
|
for key, value in pairs(hosts_stats) do
|
|
skip = false
|
|
|
|
io.write(key.."\n")
|
|
if(hosts_stats[key]["ip"] == nil) then
|
|
skip = true
|
|
else
|
|
if(vlan ~= 0) then
|
|
if(hosts_stats[key]["vlan"] ~= vlan) then
|
|
skip = true
|
|
end
|
|
end
|
|
end
|
|
|
|
if(skip == false) then
|
|
if((mode == "recv") or (mode == "sent")) then
|
|
v = hosts_stats[key][unit.."."..mode]
|
|
else
|
|
v = hosts_stats[key][unit..".sent"] + hosts_stats[key][unit..".rcvd"]
|
|
end
|
|
|
|
updateKey(hosts, key, v)
|
|
end
|
|
end
|
|
|
|
sliceHash(hosts, max_num_entries, sort_direction, "/lua/host_details.lua?host", "")
|
|
end
|
|
|
|
-- ########
|
|
|
|
-- vlan, unit (bytes/packets), mode (rcvd/sent/both), max_num_entries (10), sort(desc/asc)
|
|
function getVLANTraffic(ifname, vlan, unit, mode, max_num_entries, sort_direction)
|
|
interface.select(ifname)
|
|
hosts_stats = interface.getHostsInfo()
|
|
hosts_stats = hosts_stats["hosts"]
|
|
|
|
hosts = { }
|
|
for key, value in pairs(hosts_stats) do
|
|
skip = false
|
|
|
|
io.write(key.."\n")
|
|
if(hosts_stats[key]["ip"] == nil) then
|
|
skip = true
|
|
else
|
|
if(vlan ~= 0) then
|
|
if(hosts_stats[key]["vlan"] ~= vlan) then
|
|
skip = true
|
|
end
|
|
end
|
|
end
|
|
|
|
if(skip == false) then
|
|
if((mode == "recv") or (mode == "sent")) then
|
|
v = hosts_stats[key][unit.."."..mode]
|
|
else
|
|
v = hosts_stats[key][unit..".sent"] + hosts_stats[key][unit..".rcvd"]
|
|
end
|
|
|
|
updateKey(hosts, hosts_stats[key]["vlan"], v)
|
|
end
|
|
end
|
|
|
|
sliceHash(hosts, max_num_entries, sort_direction, "", "")
|
|
end
|
|
|
|
-- ########
|
|
|
|
-- AS, unit (bytes/packets), mode (rcvd/sent/both), max_num_entries (10), sort(desc/asc)
|
|
function getASTraffic(ifname, vlan, as, unit, mode, max_num_entries, sort_direction)
|
|
interface.select(ifname)
|
|
hosts_stats = interface.getHostsInfo()
|
|
hosts_stats = hosts_stats["hosts"]
|
|
|
|
hosts = { }
|
|
for key, value in pairs(hosts_stats) do
|
|
skip = false
|
|
|
|
io.write(key.."\n")
|
|
if(hosts_stats[key]["ip"] == nil) then
|
|
skip = true
|
|
else
|
|
if(vlan ~= 0) then
|
|
if(hosts_stats[key]["vlan"] ~= vlan) then
|
|
skip = true
|
|
end
|
|
end
|
|
end
|
|
|
|
if(skip == false) then
|
|
if((mode == "recv") or (mode == "sent")) then
|
|
v = hosts_stats[key][unit.."."..mode]
|
|
else
|
|
v = hosts_stats[key][unit..".sent"] + hosts_stats[key][unit..".rcvd"]
|
|
end
|
|
|
|
updateKey(hosts, hosts_stats[key]["asn"], v)
|
|
end
|
|
end
|
|
|
|
sliceHash(hosts, max_num_entries, sort_direction, "https://www.robtex.com/as/as", ".html")
|
|
end
|
|
|
|
-- host, vlan, unit (bytes/packets), mode (rcvd/sent/both), max_num_entries (10), sort(desc/asc)
|
|
function getFlowTalkers(ifname, vlan, unit, mode, max_num_entries, sort_direction)
|
|
interface.select(ifname)
|
|
flows_stats = interface.getFlowsInfo()
|
|
flows_stats = flows_stats["flows"]
|
|
|
|
hosts = { }
|
|
for key, value in ipairs(flows_stats) do
|
|
skip = false
|
|
|
|
if(vlan ~= 0) then
|
|
if(flows_stats[key]["vlan"] ~= vlan) then
|
|
skip = true
|
|
end
|
|
end
|
|
|
|
if(skip == false) then
|
|
if(mode == "recv") then
|
|
updateKey(hosts, flows_stats[key]["cli.ip"], flows_stats[key]["srv2cli."..unit])
|
|
updateKey(hosts, flows_stats[key]["srv.ip"], flows_stats[key]["cli2srv."..unit])
|
|
elseif(mode == "sent") then
|
|
updateKey(hosts, flows_stats[key]["cli.ip"], flows_stats[key]["cli2srv."..unit])
|
|
updateKey(hosts, flows_stats[key]["srv.ip"], flows_stats[key]["srv2cli."..unit])
|
|
else
|
|
v = flows_stats[key]["cli2srv."..unit] + flows_stats[key]["srv2cli."..unit]
|
|
updateKey(hosts, flows_stats[key]["cli.ip"], v)
|
|
updateKey(hosts, flows_stats[key]["srv.ip"], v)
|
|
end
|
|
end
|
|
end
|
|
|
|
for k, v in pairs(hosts) do
|
|
io.write(k.."="..v.."\n")
|
|
end
|
|
|
|
end
|
|
|