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 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315
|
--
-- (C) 2014-22 - ntop.org
--
local callback_utils = require "callback_utils"
require "lua_utils" -- TODO: remove
local json = require "dkjson"
local top_talkers_utils = {}
top_talkers_utils.MAX_NUM_ENTRIES = 10
top_talkers_utils.THRESHOLD_LOW = .05
top_talkers_utils.TOP_ENABLED_KEY = "ntopng.prefs.ifid_%i.interface_top_talkers_creation"
local vlan_totals = {}
local asname_cache = {}
local hostname_cache = {}
local localhost_cache = {}
local ipaddr_cache = {}
-- ########################################################
local function getTopEnabledKey(ifid)
return string.format(top_talkers_utils.TOP_ENABLED_KEY, ifid)
end
-- ########################################################
local function updateCache(cache, key, val)
if cache[key] == nil then
cache[key] = val
end
end
-- ########################################################
local function getCache(cache, key)
return cache[key]
end
-- ########################################################
local function updateRes(res, vlan, what_key, what_value, direction, delta)
if res == nil then res = {} end
if res[vlan] == nil then res[vlan] = {} end
if res[vlan][what_key] == nil then res[vlan][what_key] = {} end
if res[vlan][what_key][direction] == nil then res[vlan][what_key][direction] = {} end
if res[vlan][what_key][direction][what_value] == nil then res[vlan][what_key][direction][what_value] = 0 end
res[vlan][what_key][direction][what_value] = res[vlan][what_key][direction][what_value] + delta
end
-- ########################################################
local function sortRes(res)
for vlan_k, vlan_val in pairs(res) do
for what_key_k, what_key_val in pairs(vlan_val) do
for direction_key, direction_val in pairs(what_key_val) do
local total, count, other = 0, 0, 0
for what_val_k, delta in pairsByValues(direction_val, rev) do
count = count + 1
total = total + delta
if delta <= 0 or count > top_talkers_utils.MAX_NUM_ENTRIES then
if delta > 0 then other = other + delta end
direction_val[what_val_k] = nil
end
end
count = 0
for what_val_k, delta in pairs(direction_val) do
count = count + 1
-- at least 5
if (count > top_talkers_utils.MAX_NUM_ENTRIES / 2
and delta / total < top_talkers_utils.THRESHOLD_LOW) then
if delta > 0 then other = other + delta end
direction_val[what_val_k] = nil
end
end
if other > 0 then
direction_val["Other"] = other
end
end
end
end
end
-- ########################################################
local function finalizeRes(res)
for vlan_k, vlan_val in pairs(res) do
for what_key_k, what_key_val in pairs(vlan_val) do
for direction_key, direction_val in pairs(what_key_val) do
for what_val_k, delta in pairs(direction_val) do
local label = what_val_k
if (what_val_k ~= "Other") and (what_val_k ~= "Hidden Hosts") then
if what_key_k == "hosts" then
label = getCache(hostname_cache, what_val_k)
elseif what_key_k == "asn" then
label = getCache(asname_cache, what_val_k)
end
end
if label == what_val_k then
-- Skip this label, as it is the same as the address
label = nil
end
direction_val[what_val_k] = {address = what_val_k..'', value = delta, label = label}
if what_key_k == "hosts" then
local ip_addr = getCache(ipaddr_cache, what_val_k)
direction_val[what_val_k]["local"] = tostring(getCache(localhost_cache, what_val_k) or "false")
if ip_addr ~= what_val_k then
-- storing the IP address is necessary for MAC based keys to create the
-- correct URL
direction_val[what_val_k]["ipaddr"] = ip_addr
end
end
end
end
end
end
-- Convert to the previous format
local p = {}
for vlan_k, vlan_val in pairs(res) do
p[#p + 1] = vlan_val
for what_key_k, what_key_val in pairs(vlan_val) do
local s = {}
for _, sender in pairs(what_key_val["sent"]) do
s[#s + 1] = sender
end
local r = {}
for _, receiver in pairs(what_key_val["rcvd"]) do
r[#r + 1] = receiver
end
vlan_val[what_key_k] = {{senders = s, receivers = r}}
end
vlan_val["value"] = vlan_totals[vlan_k]
vlan_val["address"] = vlan_k..""
end
return {vlan = p}
end
-- ########################################################
function top_talkers_utils.areTopEnabled(ifid)
local k = getTopEnabledKey(ifid)
return not (ntop.getPref(k) == "false")
end
-- ########################################################
function top_talkers_utils.disableTop(ifid)
local k = getTopEnabledKey(ifid)
ntop.setPref(k, "false")
end
-- ########################################################
function top_talkers_utils.enableTop(ifid)
local k = getTopEnabledKey(ifid)
ntop.delCache(k)
end
-- ########################################################
function top_talkers_utils.makeTopJson(_ifname)
local ifid = getInterfaceId(_ifname)
local res = {}
if not top_talkers_utils.areTopEnabled(ifid) then
return nil
end
local in_time = callback_utils.foreachHost(_ifname, function (hostname, hoststats)
local checkpoint = interface.checkpointHostTalker(ifid, hostname)
local tskey = hoststats["tskey"]
local vlan = hoststats["vlan"]
updateCache(hostname_cache, tskey, hoststats["name"])
updateCache(localhost_cache, tskey, hoststats["localhost"])
updateCache(ipaddr_cache, tskey, hostname)
updateCache(asname_cache, hoststats["asn"], hoststats["asname"])
if((checkpoint == nil) or (checkpoint["delta"] == nil)) then
goto continue
end
for _, direction in pairs({"sent", "rcvd"}) do
local delta = checkpoint["delta"][direction]
vlan_totals[vlan] = (vlan_totals[vlan] or 0) + delta
local os_key = "non-local os"
if hoststats["localhost"] then
os_key = "local os"
end
local country = interface.getHostCountry(hostname)
for what_key, what_value in pairs({
["hosts"] = tskey, ["asn"] = hoststats["asn"], [os_key] = hoststats["os"],
["countries"] = ternary(not isEmptyString(country), country, nil),
["networks"] = hoststats["local_network_id"],
}) do
if hoststats.hiddenFromTop then
what_value = "Hidden Hosts"
end
updateRes(res, vlan, what_key, what_value, direction, delta)
end
end
::continue::
end)
if not in_time then
print("[".. _ifname .."] ERROR: Cannot complete top talkers generation in 1 minute. Is there a huge number of hosts in the system?")
end
sortRes(res)
return json.encode(finalizeRes(res))
end
-- ########################################################
-- Computes label and url during visualization
function top_talkers_utils.enrichRecordInformation(class_key, rec, show_vlan)
local url = ""
local label = rec.label or rec.address
local url_target = rec.address
if (rec.address ~= "Other") and (rec.address ~= "Hidden Hosts") then
if class_key == "hosts" then
local address = rec.address
if ends(address, "_v4") or ends(address, "_v6") then
rec.visual_addr = visualTsKey(address)
address = string.sub(address, 1, string.len(address)-3)
if(rec.ipaddr ~= nil) then
url_target = rec.ipaddr
end
end
url_target = url_target .. "&tskey=" .. rec.address
url = ntop.getHttpPrefix()..'/lua/host_details.lua?always_show_hist=true&host='
-- Use the host alias as label, if set
local alt_name = ip2label(address)
if not isEmptyString(alt_name) and (alt_name ~= address) then
label = alt_name
else
local hinfo = hostkey2hostinfo(address)
if not show_vlan then hinfo.vlan = 0 end
alt_name = hostinfo2label(hinfo)
if not isEmptyString(alt_name) and (alt_name ~= address) then
label = alt_name
else
label = address
end
end
elseif class_key == "asn" then
url = ntop.getHttpPrefix()..'/lua/hosts_stats.lua?asn='
elseif class_key == "networks" then
url = ntop.getHttpPrefix()..'/lua/hosts_stats.lua?network='
local network_name = nil
if rec.address == "-1" then
network_name = i18n("remote_networks")
else
network_name = getFullLocalNetworkName(ntop.getNetworkNameById(tonumber(rec.address)))
end
if not isEmptyString(network_name) then
label = network_name
end
elseif class_key:contains("os") then
url = ntop.getHttpPrefix()..'/lua/hosts_stats.lua?os='
end
if not isEmptyString(url) then
url = url .. url_target
end
end
-- Update record information
rec.url = url
rec.label = label
end
-- ########################################################
function top_talkers_utils.enrichVlanInformation(vlan_tbl)
local vlan_id = ternary(vlan_tbl.address, tostring(vlan_tbl.address), "0")
vlan_tbl["label"] = vlan_id
vlan_tbl["name"] = vlan_id
vlan_tbl["url"] = ntop.getHttpPrefix()..'/lua/hosts_stats.lua?vlan='..vlan_id
end
-- ########################################################
return top_talkers_utils
|