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
|
--
-- (C) 2013-22 - ntop.org
--
local dirs = ntop.getDirs()
package.path = dirs.installdir .. "/scripts/lua/modules/?.lua;" .. package.path
package.path = dirs.installdir .. "/scripts/lua/modules/pools/?.lua;" .. package.path
require "lua_utils"
require "mac_utils"
local host_pools = require "host_pools"
local json = require("dkjson")
sendHTTPContentTypeHeader('text/html')
-- Instantiate host pools
local host_pools_instance = host_pools:create()
interface.select(ifname)
local ifstats = interface.getStats()
-- Table parameters
local currentPage = _GET["currentPage"]
local perPage = _GET["perPage"]
local sortColumn = _GET["sortColumn"]
local sortOrder = _GET["sortOrder"]
local vlan = _GET["vlan"]
local devices_mode = _GET["unassigned_devices"]
local sortPrefs = "unknown_devices"
if((sortColumn == nil) or (sortColumn == "column_"))then
sortColumn = getDefaultTableSort(sortPrefs)
else
if((sortColumn ~= "column_")
and (sortColumn ~= "")) then
tablePreferences("sort_"..sortPrefs,sortColumn)
end
end
if(sortOrder == nil) then
sortOrder = getDefaultTableSortOrder(sortPrefs)
else
if((sortColumn ~= "column_")
and (sortColumn ~= "")) then
tablePreferences("sort_order_"..sortPrefs,sortOrder)
end
end
if(currentPage == nil) then
currentPage = 1
else
currentPage = tonumber(currentPage)
end
if(perPage == nil) then
perPage = getDefaultTableSize()
else
perPage = tonumber(perPage)
tablePreferences("rows_number", perPage)
end
interface.select(ifname)
to_skip = (currentPage-1) * perPage
if(isEmptyString(vlan)) then vlan = 0 end
if(sortOrder == "desc") then sOrder = false else sOrder = true end
local total_rows = 0
local mac_to_device = {}
local mac_to_sort = {}
local now = os.time()
local res = {}
local sortField
if sortColumn == "column_first_seen" then
sortField = "seen.first"
elseif sortColumn == "column_last_seen" then
sortField = "seen.last"
elseif sortColumn == "column_name" then
sortField = "name"
elseif sortColumn == "column_mac" then
sortField = "mac"
else
-- sort in memory MACs first, off memory after
sortField = "in_memory_mac"
sortOrder = "desc"
end
-- First data source: memory
-- NB: we must fetch this data even if mode is "inactive_only", to properly filter redis data
local macs_stats = interface.getMacsInfo(nil, nil, nil, nil,
true --[[ sourceMacsOnly ]], nil--[[manufacturer]],
tonumber(host_pools_instance.DEFAULT_POOL_ID), nil, nil, nil)
if (macs_stats ~= nil) then
macs_stats = macs_stats.macs
for key, device in pairs(macs_stats) do
device.in_memory_mac = "1" .. device["mac"]
mac_to_device[device["mac"]] = device
if sortField == "name" then
mac_to_device[device["mac"]]["name"] = getDeviceName(device["mac"])
end
mac_to_sort[device["mac"]] = device[sortField]
if devices_mode ~= "inactive_only" then
total_rows = total_rows + 1
end
end
else
macs_stats = {}
end
macs_stats = nil
-- Second data source: redis
if devices_mode ~= "active_only" then
local keys = ntop.getKeysCache("ntopng.serialized_macs.ifid_"..(ifstats.id).."__*")
for key in pairs(keys or {}) do
local device = json.decode(ntop.getCache(key))
if (device ~= nil) and (not mac_to_device[device["mac"]]) and (tostring(interface.findMacPool(device["mac"]) or 0) == host_pools_instance.DEFAULT_POOL_ID) then
device.in_memory_mac = "0" .. device["mac"]
mac_to_device[device["mac"]] = device
if sortField == "name" then
mac_to_device[device["mac"]]["name"] = getDeviceName(device["mac"])
end
mac_to_sort[device["mac"]] = device[sortField]
total_rows = total_rows + 1
end
end
end
-- Visualize data
local i = 0
local num = 0
local sort_function
if sortOrder == "asc" then
sort_function = asc
else
sort_function = rev
end
for mac, _ in pairsByValues(mac_to_sort, sort_function) do
i = i + 1
local device = mac_to_device[mac]
local in_memory = (device["manufacturer"] ~= nil)
local filter_out = ((not in_memory) and devices_mode == "active_only") or (in_memory and devices_mode == "inactive_only")
if (i > to_skip) and (not filter_out) then
local record = {}
if in_memory then
record["column_mac"] = mac2link(device)
else
record["column_mac"] = macAddIcon(device["mac"])
end
record["key"] = device["mac"]
record["column_name"] = device["name"] or getDeviceName(device["mac"])
record["column_first_seen"] = formatEpoch(device["seen.first"]) .. " [" .. secondsToTime(now - device["seen.first"]) .. " " .. i18n("details.ago").."]"
record["column_last_seen"] = formatEpoch(device["seen.last"]) .. " [" .. secondsToTime(now - device["seen.last"]) .. " " .. i18n("details.ago").."]"
-- record["manufacturer"] = device["manufacturer"] or ntop.getMacManufacturer(device["mac"])
res[#res + 1] = record
num = num + 1
if num >= perPage then
break
end
end
end
local result = {}
result["perPage"] = perPage
result["currentPage"] = currentPage
result["totalRows"] = total_rows
result["data"] = res
result["sort"] = {{sortColumn, sortOrder}}
print(json.encode(result, nil))
|