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
|
--
-- (C) 2021 - ntop.org
--
local dirs = ntop.getDirs()
package.path = dirs.installdir .. "/scripts/lua/modules/?.lua;" .. package.path
require "lua_utils"
local json = require "dkjson"
local discover = require "discover_utils"
sendHTTPContentTypeHeader('text/json')
-- Table parameters
local currentPage = _GET["currentPage"]
local perPage = _GET["perPage"]
local sortColumn = _GET["sortColumn"]
local sortOrder = _GET["sortOrder"]
local os_filter = tonumber(_GET["operating_system"])
local devtype_filter = tonumber(_GET["device_type"])
local manuf_filter = _GET["manufacturer"]
local sortPrefs = "discovery_sort_col"
-- ################################################
local doa_ox_fd = nil
local doa_ox = nil
local enable_doa_ox = false
if(enable_doa_ox) then
local doa_ox = require "doa_ox"
doa_ox_fd = doa_ox.init("/tmp/doa_ox.update")
end
-- ################################################
if isEmptyString(sortColumn) or sortColumn == "column_" then
sortColumn = getDefaultTableSort(sortPrefs)
else
if((sortColumn ~= "column_")
and (sortColumn ~= "")) then
tablePreferences("sort_"..sortPrefs, sortColumn)
end
end
if isEmptyString(_GET["sortColumn"]) then
sortOrder = getDefaultTableSortOrder(sortPrefs, true)
end
if((_GET["sortColumn"] ~= "column_")
and (_GET["sortColumn"] ~= "")) then
tablePreferences("sort_order_"..sortPrefs, sortOrder, true)
end
if(currentPage == nil) then
currentPage = 1
else
currentPage = tonumber(currentPage)
end
if(perPage == nil) then
perPage = 10
else
perPage = tonumber(perPage)
tablePreferences("rows_number_discovery", perPage)
end
-- ################################################
interface.select(ifname)
local to_skip = (currentPage-1) * perPage
if(sortOrder == "desc") then
if sortColumn == "column_ip" then
sOrder = ip_address_rev
else
sOrder = rev_insensitive
end
else
if sortColumn == "column_ip" then
sOrder = ip_address_asc
else
sOrder = asc_insensitive
end
end
local res = {data={}}
local discovered = discover.discover2table(ifname)
-- ################################################
if(enable_doa_ox) then
doa_ox.header(doa_ox_fd)
end
local sort_2_field = {
column_ip = "ip",
column_name = "name",
column_manufacturer = "manufacturer",
column_os = "os",
column_info = "info",
column_device = "device_type",
}
local sorted = {}
local sort_field = "mac"
if sort_2_field[sortColumn] then
sort_field = sort_2_field[sortColumn]
end
local tot_rows = 0
discovered["devices"] = discovered["devices"] or {}
for el_idx, el in pairs(discovered["devices"]) do
-- Manufacturer
local manufacturer = ""
if el["manufacturer"] then
manufacturer = el["manufacturer"]
else
manufacturer = get_manufacturer_mac(el["mac"])
end
local actual_manuf = manufacturer
if(el["modelName"] and (el["modelName"] ~= "")) then
manufacturer = manufacturer .. " ["..el["modelName"].."]"
end
el.manufacturer = manufacturer
-- Name
local name = ""
if el["sym"] then name = name .. el["sym"] end
if el["symIP"] then
if el["sym"] then
name = name .. " ["..el["symIP"].."]"
else
name = el["symIP"]
end
end
el.name = name
-- Retrieve information from L3 host
local host = interface.getHostInfo(el["ip"])
if(host ~= nil) then
el.os_type = host.os
end
el.os = discover.getOsIcon(el.os_type)
-- Device info
local devinfo = ""
if el["information"] then devinfo = devinfo .. table.concat(el["information"], "<br>") end
if el["url"] then
if el["information"] then
devinfo = devinfo .. "<br>"..el["url"]
else
devinfo = devinfo .. el["url"]
end
end
el.info = devinfo
if(enable_doa_ox) then
if el.os then
el.operatingSystem = discover.getOsName(el.os)
end
doa_ox.device2OX(doa_ox_fd, el)
end
-- Filter
if (os_filter ~= nil) and (el.os_type ~= os_filter) then
goto continue
end
if (manuf_filter ~= nil) and (actual_manuf ~= manuf_filter) then
goto continue
end
if (devtype_filter ~= nil) and (discover.devtype2id(el.device_type) ~= devtype_filter) then
goto continue
end
sorted[el_idx] = el[sort_field]
tot_rows = tot_rows + 1
::continue::
end
if(enable_doa_ox) then
doa_ox.term(doa_ox_fd)
end
-- ################################################
local cur_num = 0
-- Sort
for idx, _ in pairsByValues(sorted, sOrder) do
el = discovered["devices"][idx]
cur_num = cur_num + 1
if cur_num <= to_skip then
goto continue
elseif cur_num > to_skip + perPage then
break
end
local rec = {}
rec.column_ip = ip2detailshref(el["ip"], nil, nil, el["ip"])
..ternary(el["icon"], " " ..(el["icon"] or "").. " ", "")
..ternary(el["ghost"], " <font color=red>" ..(discover.ghost_icon or "").. "</font>", "")
rec.column_mac = [[<a href="]] ..ntop.getHttpPrefix().. [[/lua/mac_details.lua?host=]] ..el["mac"].. [[">]] ..get_symbolic_mac(el["mac"], true).. [[</a>]]
rec.column_name = el.name
rec.column_info = el.info
rec.column_device = el["device_label"]
rec.column_manufacturer = el.manufacturer
rec.column_os = el.os
res.data[#res.data + 1] = rec
::continue::
end
res["perPage"] = perPage
res["currentPage"] = currentPage
res["totalRows"] = tot_rows
res["sort"] = {{sortColumn, sortOrder}}
print(json.encode(res))
|