mirror of
https://github.com/ntop/ntopng.git
synced 2026-05-06 03:45:26 +00:00
Reworked lua code
This commit is contained in:
parent
74f03c3a38
commit
49bdd32ce0
7 changed files with 2173 additions and 1953 deletions
|
|
@ -2,13 +2,13 @@
|
|||
-- (C) 2013-22 - ntop.org
|
||||
--
|
||||
|
||||
local clock_start = os.clock()
|
||||
|
||||
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
|
||||
package.path = dirs.installdir .. "/scripts/lua/modules/notifications/?.lua;" .. package.path
|
||||
|
||||
local clock_start = os.clock()
|
||||
|
||||
local json = require("dkjson")
|
||||
local alert_severities = require "alert_severities"
|
||||
local alert_consts = require("alert_consts")
|
||||
|
|
|
|||
|
|
@ -1,6 +1,9 @@
|
|||
--
|
||||
-- (C) 2017-22 - ntop.org
|
||||
--
|
||||
|
||||
local clock_start = os.clock()
|
||||
|
||||
local dirs = ntop.getDirs()
|
||||
package.path = dirs.installdir .. "/scripts/lua/modules/?.lua;" .. package.path
|
||||
|
||||
|
|
@ -874,7 +877,15 @@ local function validateFilters(other_validation)
|
|||
end
|
||||
end
|
||||
http_lint.validateFilters = validateFilters
|
||||
|
||||
|
||||
local L4_PROTO_KEYS = {
|
||||
tcp=6,
|
||||
udp=17,
|
||||
icmp=1,
|
||||
eigrp=88,
|
||||
other_ip=-1
|
||||
}
|
||||
|
||||
local function validateProtocolIdOrName(p)
|
||||
-- Lower used because TCP instead of tcp wasn't seen as a l4proto
|
||||
local tmp = string.lower(p)
|
||||
|
|
@ -2512,4 +2523,8 @@ if(pragma_once) then
|
|||
pragma_once = 0
|
||||
end
|
||||
|
||||
if(trace_script_duration ~= nil) then
|
||||
io.write(debug.getinfo(1,'S').source .." executed in ".. (os.clock()-clock_start)*1000 .. " ms\n")
|
||||
end
|
||||
|
||||
return http_lint
|
||||
|
|
|
|||
File diff suppressed because it is too large
Load diff
386
scripts/lua/modules/lua_utils_generic.lua
Normal file
386
scripts/lua/modules/lua_utils_generic.lua
Normal file
|
|
@ -0,0 +1,386 @@
|
|||
--
|
||||
-- (C) 2014-22 - ntop.org
|
||||
--
|
||||
|
||||
local clock_start = os.clock()
|
||||
|
||||
-- GENERIC UTILS
|
||||
|
||||
-- split
|
||||
function split(s, delimiter)
|
||||
result = {};
|
||||
if(s ~= nil) then
|
||||
if delimiter == nil then
|
||||
-- No delimiter, split all characters
|
||||
for match in s:gmatch"." do
|
||||
table.insert(result, match);
|
||||
end
|
||||
else
|
||||
-- Split by delimiter
|
||||
for match in (s..delimiter):gmatch("(.-)"..delimiter) do
|
||||
table.insert(result, match);
|
||||
end
|
||||
end
|
||||
end
|
||||
return result;
|
||||
end
|
||||
|
||||
-- startswith
|
||||
function startswith(s, char)
|
||||
return string.sub(s, 1, string.len(s)) == char
|
||||
end
|
||||
|
||||
-- strsplit
|
||||
|
||||
function strsplit(s, delimiter)
|
||||
result = {};
|
||||
for match in (s..delimiter):gmatch("(.-)"..delimiter) do
|
||||
if(match ~= "") then result[match] = true end
|
||||
end
|
||||
return result;
|
||||
end
|
||||
|
||||
-- isempty
|
||||
function isempty(array)
|
||||
local count = 0
|
||||
for _,__ in pairs(array) do
|
||||
count = count + 1
|
||||
end
|
||||
return (count == 0)
|
||||
end
|
||||
|
||||
-- isin
|
||||
function isin(s, array)
|
||||
if (s == nil or s == "" or array == nil or isempty(array)) then return false end
|
||||
for _, v in pairs(array) do
|
||||
if (s == v) then return true end
|
||||
end
|
||||
return false
|
||||
end
|
||||
|
||||
-- hasKey
|
||||
function hasKey(key, theTable)
|
||||
if((theTable == nil) or (theTable[key] == nil)) then
|
||||
return(false)
|
||||
else
|
||||
return(true)
|
||||
end
|
||||
end
|
||||
|
||||
-- ###############################################
|
||||
|
||||
-- removes trailing/leading spaces
|
||||
function trimString(s)
|
||||
return (s:gsub("^%s*(.-)%s*$", "%1"))
|
||||
end
|
||||
|
||||
-- ###############################################
|
||||
|
||||
-- removes all spaces
|
||||
function trimSpace(what)
|
||||
if(what == nil) then return("") end
|
||||
return(string.gsub(string.gsub(what, "%s+", ""), "+%s", ""))
|
||||
end
|
||||
|
||||
-- ###############################################
|
||||
|
||||
-- TODO: improve this function
|
||||
function jsonencode(what)
|
||||
what = string.gsub(what, '"', "'")
|
||||
-- everything but all ASCII characters from the space to the tilde
|
||||
what = string.gsub(what, "[^ -~]", " ")
|
||||
-- cleanup line feeds and carriage returns
|
||||
what = string.gsub(what, "\n", " ")
|
||||
what = string.gsub(what, "\r", " ")
|
||||
-- escape all the remaining backslashes
|
||||
what = string.gsub(what, "\\", "\\\\")
|
||||
-- max 1 sequential whitespace
|
||||
what = string.gsub(what, " +"," ")
|
||||
return(what)
|
||||
end
|
||||
|
||||
-- ###########################################
|
||||
|
||||
-- Merges table a and table b into a new table. If some elements are presents in
|
||||
-- both a and b, b elements will have precedence.
|
||||
-- NOTE: this does *not* perform a deep merge. Only first level is merged.
|
||||
function table.merge(a, b)
|
||||
local merged = {}
|
||||
a = a or {}
|
||||
b = b or {}
|
||||
|
||||
if((a[1] ~= nil) and (b[1] ~= nil)) then
|
||||
-- index based tables
|
||||
for _, t in ipairs({a, b}) do
|
||||
for _,v in pairs(t) do
|
||||
merged[#merged + 1] = v
|
||||
end
|
||||
end
|
||||
else
|
||||
-- key based tables
|
||||
for _, t in ipairs({a, b}) do
|
||||
for k,v in pairs(t) do
|
||||
merged[k] = v
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
return merged
|
||||
end
|
||||
|
||||
-- ###########################################
|
||||
|
||||
-- Performs a deep copy of the table.
|
||||
function table.clone(orig)
|
||||
local orig_type = type(orig)
|
||||
local copy
|
||||
|
||||
if orig_type == 'table' then
|
||||
copy = {}
|
||||
for orig_key, orig_value in next, orig, nil do
|
||||
copy[table.clone(orig_key)] = table.clone(orig_value)
|
||||
end
|
||||
setmetatable(copy, table.clone(getmetatable(orig)))
|
||||
else -- number, string, boolean, etc
|
||||
copy = orig
|
||||
end
|
||||
|
||||
return copy
|
||||
end
|
||||
|
||||
-- ###########################################
|
||||
|
||||
-- From http://lua-users.org/lists/lua-l/2014-09/msg00421.html
|
||||
-- Returns true if tables are equal
|
||||
function table.compare(t1, t2, ignore_mt)
|
||||
local ty1 = type(t1)
|
||||
local ty2 = type(t2)
|
||||
|
||||
if ty1 ~= ty2 then return false end
|
||||
if ty1 ~= 'table' and ty2 ~= 'table' then return t1 == t2 end
|
||||
local mt = getmetatable(t1)
|
||||
if not ignore_mt and mt and mt.__eq then return t1 == t2 end
|
||||
|
||||
for k1,v1 in pairs(t1) do
|
||||
local v2 = t2[k1]
|
||||
if v2 == nil or not table.compare(v1, v2) then return false end
|
||||
end
|
||||
|
||||
for k2,v2 in pairs(t2) do
|
||||
local v1 = t1[k2]
|
||||
if v1 == nil or not table.compare(v1, v2) then return false end
|
||||
end
|
||||
|
||||
return true
|
||||
end
|
||||
|
||||
-- ###########################################
|
||||
|
||||
function toboolean(s)
|
||||
if((s == "true") or (s == true)) then
|
||||
return true
|
||||
elseif((s == "false") or (s == false)) then
|
||||
return false
|
||||
else
|
||||
return nil
|
||||
end
|
||||
end
|
||||
|
||||
-- ###########################################
|
||||
|
||||
--
|
||||
-- Find the highest divisor which divides input value.
|
||||
-- val_idx can be used to index divisors values.
|
||||
-- Returns the highest_idx
|
||||
--
|
||||
function highestDivisor(divisors, value, val_idx, iterator_fn)
|
||||
local highest_idx = nil
|
||||
local highest_val = nil
|
||||
iterator_fn = iterator_fn or ipairs
|
||||
|
||||
for i, v in iterator_fn(divisors) do
|
||||
local cmp_v
|
||||
if val_idx ~= nil then
|
||||
v = v[val_idx]
|
||||
end
|
||||
|
||||
if((highest_val == nil) or ((v > highest_val) and (value % v == 0))) then
|
||||
highest_val = v
|
||||
highest_idx = i
|
||||
end
|
||||
end
|
||||
|
||||
return highest_idx
|
||||
end
|
||||
|
||||
-- ###########################################
|
||||
|
||||
--- Test if each element inside the table t satisfies the predicate function
|
||||
--- @param t table The table containing values to test
|
||||
--- @param predicate function The function that return a boolean value (true|false)
|
||||
--- @return boolean
|
||||
function table.all(t, predicate)
|
||||
|
||||
if type(t) ~= 'table' then
|
||||
traceError(TRACE_DEBUG, TRACE_CONSOLE, "the first paramater is not a table!")
|
||||
return false
|
||||
end
|
||||
if type(predicate) ~= 'function' then
|
||||
traceError(TRACE_DEBUG, TRACE_CONSOLE, "the passed predicate is not a function!")
|
||||
return false
|
||||
end
|
||||
|
||||
if t == nil then return false end
|
||||
|
||||
for _, value in pairs(t) do
|
||||
|
||||
-- check if the value satisfies the boolean predicate
|
||||
local term = predicate(value)
|
||||
|
||||
-- if the return value is valid and true then do nothing
|
||||
-- otherwise stop the loop and return false
|
||||
if term == nil then
|
||||
-- inform the client about the nil value
|
||||
traceError(TRACE_DEBUG, TRACE_CONSOLE, "a null term has been returned from the predicate function!")
|
||||
return false
|
||||
elseif not term then
|
||||
return false
|
||||
end
|
||||
end
|
||||
|
||||
-- each entry satisfies the predicate
|
||||
return true
|
||||
end
|
||||
|
||||
-- ###########################################
|
||||
|
||||
--- Perform a linear search to check if an element is inside a table
|
||||
--- @param t table The table to scan
|
||||
--- @param needle any The element to search
|
||||
--- @param comp function The compare function used to compare the searched element with others
|
||||
--- @return boolean True if the element is insie the table, False otherwise
|
||||
function table.contains(t, needle, comp)
|
||||
|
||||
if (t == nil) then return false end
|
||||
if (type(t) ~= "table") then return false end
|
||||
if (#t == 0) then return false end
|
||||
|
||||
local default_compare = (function(e) return e == needle end)
|
||||
comp = comp or default_compare
|
||||
|
||||
for _, element in ipairs(t) do
|
||||
if comp(element) then return true end
|
||||
end
|
||||
|
||||
return false
|
||||
end
|
||||
|
||||
-- ###########################################
|
||||
|
||||
--- Insert an element inside the table if is not present
|
||||
function table.insertIfNotPresent(t, element, comp)
|
||||
if table.contains(t, element, comp) then return end
|
||||
t[#t+1] = element
|
||||
end
|
||||
|
||||
-- ###########################################
|
||||
|
||||
--- Fold right table with a custom function
|
||||
--- @param t table Table to fold
|
||||
--- @param func function Function to execute on table values
|
||||
--- @param val any The returned default value
|
||||
function table.foldr(t, func, val)
|
||||
for i,v in pairs(t) do
|
||||
val = func(val, v)
|
||||
end
|
||||
return val
|
||||
end
|
||||
|
||||
-- ###########################################
|
||||
|
||||
function table.has_key(table, key)
|
||||
return table[key] ~= nil
|
||||
end
|
||||
|
||||
function table.slice(t, start_table, end_table)
|
||||
if t == nil then
|
||||
error("The array to slice cannot be nil!")
|
||||
end
|
||||
|
||||
if end_table > #t then
|
||||
end_table = #t
|
||||
end
|
||||
|
||||
if start_table < 1 then
|
||||
error("Invalid bounds!")
|
||||
end
|
||||
|
||||
local res = {}
|
||||
for i = start_table, end_table, 1 do
|
||||
res[#res + 1] = t[i]
|
||||
end
|
||||
|
||||
return res
|
||||
end
|
||||
|
||||
if(trace_script_duration ~= nil) then
|
||||
io.write(debug.getinfo(1,'S').source .." executed in ".. (os.clock()-clock_start)*1000 .. " ms\n")
|
||||
end
|
||||
|
||||
-- ##############################################
|
||||
|
||||
-- Note: Regexs are applied by default. Pass plain=true to disable them.
|
||||
function string.contains(str, start, is_plain)
|
||||
if type(str) ~= 'string' or type(start) ~= 'string' or isEmptyString(str) or isEmptyString(start) then
|
||||
return false
|
||||
end
|
||||
|
||||
local i, _ = string.find(str, start, 1, is_plain)
|
||||
|
||||
return(i ~= nil)
|
||||
end
|
||||
|
||||
-- ##############################################
|
||||
|
||||
function string.containsIgnoreCase(str, start, is_plain)
|
||||
return string.contains(string.lower(str), string.lower(start), is_plain)
|
||||
end
|
||||
|
||||
-- ##############################################
|
||||
|
||||
function shortenString(name, max_len)
|
||||
local ellipsis = "\u{2026}" -- The unicode ellipsis (takes less space than three separate dots)
|
||||
if(name == nil) then return("") end
|
||||
|
||||
if max_len == nil then
|
||||
max_len = ntop.getPref("ntopng.prefs.max_ui_strlen")
|
||||
max_len = tonumber(max_len)
|
||||
if(max_len == nil) then max_len = 24 end
|
||||
end
|
||||
|
||||
if(string.len(name) < max_len + 1 --[[ The space taken by the ellipsis --]]) then
|
||||
return(name)
|
||||
else
|
||||
return(string.sub(name, 1, max_len)..ellipsis)
|
||||
end
|
||||
end
|
||||
|
||||
-- ##############################################
|
||||
|
||||
function convertDate(vardate)
|
||||
local m,d,y,h,i,s = string.match(vardate, '(%d+)/(%d+)/(%d+) (%d+):(%d+):(%d+)')
|
||||
local key = ntop.getPref('ntopng.user.' .. _SESSION["user"] .. '.date_format')
|
||||
|
||||
if(key == "little_endian") then
|
||||
return string.format('%s/%s/%s %s:%s:%s', d,m,y,h,i,s)
|
||||
elseif( key == "middle_endian") then
|
||||
return string.format('%s/%s/%s %s:%s:%s', m,d,y,h,i,s)
|
||||
else
|
||||
return string.format('%s/%s/%s %s:%s:%s', y,m,d,h,i,s)
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
if(trace_script_duration ~= nil) then
|
||||
io.write(debug.getinfo(1,'S').source .." executed in ".. (os.clock()-clock_start)*1000 .. " ms\n")
|
||||
end
|
||||
1260
scripts/lua/modules/lua_utils_get.lua
Normal file
1260
scripts/lua/modules/lua_utils_get.lua
Normal file
File diff suppressed because it is too large
Load diff
383
scripts/lua/modules/lua_utils_print.lua
Normal file
383
scripts/lua/modules/lua_utils_print.lua
Normal file
|
|
@ -0,0 +1,383 @@
|
|||
--
|
||||
-- (C) 2014-22 - ntop.org
|
||||
--
|
||||
|
||||
local clock_start = os.clock()
|
||||
|
||||
local dscp_consts = require "dscp_consts"
|
||||
|
||||
-- ##############################################
|
||||
|
||||
function printGETParameters(get)
|
||||
for key, value in pairs(get) do
|
||||
io.write(key.."="..value.."\n")
|
||||
end
|
||||
end
|
||||
|
||||
-- ##############################################
|
||||
|
||||
function printASN(asn, asname)
|
||||
asname = asname:gsub('"','')
|
||||
if(asn > 0) then
|
||||
return("<A class='ntopng-external-link' href='http://as.robtex.com/as"..asn..".html'>"..asname.." <i class='fas fa-external-link-alt fa-lg'></i></A>")
|
||||
else
|
||||
return(asname)
|
||||
end
|
||||
end
|
||||
|
||||
-- ##############################################
|
||||
|
||||
function printIpVersionDropdown(base_url, page_params)
|
||||
local ipversion = _GET["version"]
|
||||
local ipversion_filter
|
||||
if not isEmptyString(ipversion) then
|
||||
ipversion_filter = '<span class="fas fa-filter"></span>'
|
||||
else
|
||||
ipversion_filter = ''
|
||||
end
|
||||
|
||||
-- table.clone needed to modify some parameters while keeping the original unchanged
|
||||
local ipversion_params = table.clone(page_params)
|
||||
ipversion_params["version"] = nil
|
||||
|
||||
print[[\
|
||||
<button class="btn btn-link dropdown-toggle" data-bs-toggle="dropdown">]] print(i18n("flows_page.ip_version")) print[[]] print(ipversion_filter) print[[<span class="caret"></span></button>\
|
||||
<ul class="dropdown-menu scrollable-dropdown" role="menu" id="flow_dropdown">\
|
||||
<li><a class="dropdown-item" href="]] print(getPageUrl(base_url, ipversion_params)) print[[">]] print(i18n("flows_page.all_ip_versions")) print[[</a></li>\
|
||||
<li><a class="dropdown-item ]] if ipversion == "4" then print('active') end print[[" href="]] ipversion_params["version"] = "4"; print(getPageUrl(base_url, ipversion_params)); print[[">]] print(i18n("flows_page.ipv4_only")) print[[</a></li>\
|
||||
<li><a class="dropdown-item ]] if ipversion == "6" then print('active') end print[[" href="]] ipversion_params["version"] = "6"; print(getPageUrl(base_url, ipversion_params)); print[[">]] print(i18n("flows_page.ipv6_only")) print[[</a></li>\
|
||||
</ul>]]
|
||||
end
|
||||
|
||||
-- ##############################################
|
||||
|
||||
function printVLANFilterDropdown(base_url, page_params)
|
||||
local vlans = interface.getVLANsList()
|
||||
|
||||
if vlans == nil then vlans = {VLANs={}} end
|
||||
vlans = vlans["VLANs"]
|
||||
|
||||
local ids = {}
|
||||
for _, vlan in ipairs(vlans) do
|
||||
ids[#ids + 1] = vlan["vlan_id"]
|
||||
end
|
||||
|
||||
local vlan_id = _GET["vlan"]
|
||||
local vlan_id_filter = ''
|
||||
if not isEmptyString(vlan_id) then
|
||||
vlan_id_filter = '<span class="fas fa-filter"></span>'
|
||||
end
|
||||
|
||||
-- table.clone needed to modify some parameters while keeping the original unchanged
|
||||
local vlan_id_params = table.clone(page_params)
|
||||
vlan_id_params["vlan"] = nil
|
||||
|
||||
print[[\
|
||||
<button class="btn btn-link dropdown-toggle" data-bs-toggle="dropdown">]] print(i18n("flows_page.vlan")) print[[]] print(vlan_id_filter) print[[<span class="caret"></span></button>\
|
||||
<ul class="dropdown-menu scrollable-dropdown" role="menu" id="flow_dropdown">\
|
||||
<li><a class="dropdown-item" href="]] print(getPageUrl(base_url, vlan_id_params)) print[[">]] print(i18n("flows_page.all_vlan_ids")) print[[</a></li>\]]
|
||||
for _, vid in ipairs(ids) do
|
||||
vlan_id_params["vlan"] = vid
|
||||
print[[
|
||||
<li>\
|
||||
<a class="dropdown-item ]] print(vlan_id == tostring(vid) and 'active' or '') print[[" href="]] print(getPageUrl(base_url, vlan_id_params)) print[[">VLAN ]] print(tostring(getFullVlanName(vid))) print[[</a></li>\]]
|
||||
end
|
||||
print[[
|
||||
|
||||
</ul>]]
|
||||
end
|
||||
|
||||
-- ##############################################
|
||||
|
||||
function printDSCPDropdown(base_url, page_params, dscp_list)
|
||||
local dscp = _GET["dscp"]
|
||||
local dscp_filter
|
||||
if not isEmptyString(dscp) then
|
||||
dscp_filter = '<span class="fas fa-filter"></span>'
|
||||
else
|
||||
dscp_filter = ''
|
||||
end
|
||||
|
||||
-- table.clone needed to modify some parameters while keeping the original unchanged
|
||||
local dscp_params = table.clone(page_params)
|
||||
dscp_params["dscp"] = nil
|
||||
-- Used to possibly remove tcp state filters when selecting a non-TCP l4 protocol
|
||||
local dscp_params_non_filter = table.clone(dscp_params)
|
||||
if dscp_params_non_filter["dscp"] then
|
||||
dscp_params_non_filter["dscp"] = nil
|
||||
end
|
||||
|
||||
local ordered_dscp_list = {}
|
||||
|
||||
for key, value in pairs(dscp_list) do
|
||||
local name = dscp_consts.dscp_descr(key)
|
||||
ordered_dscp_list[name] = {}
|
||||
ordered_dscp_list[name]["id"] = key
|
||||
ordered_dscp_list[name]["count"] = value.count
|
||||
end
|
||||
|
||||
print[[\
|
||||
<button class="btn btn-link dropdown-toggle" data-bs-toggle="dropdown">]] print(i18n("flows_page.dscp")) print[[]] print(dscp_filter) print[[<span class="caret"></span></button>\
|
||||
<ul class="dropdown-menu dropdown-menu-end scrollable-dropdown" role="menu" id="flow_dropdown">\
|
||||
<li><a class="dropdown-item" href="]] print(getPageUrl(base_url, dscp_params_non_filter)) print[[">]] print(i18n("flows_page.all_dscp")) print[[</a></li>]]
|
||||
|
||||
for key, value in pairsByKeys(ordered_dscp_list, asc) do
|
||||
print[[<li]]
|
||||
|
||||
print([[><a class="dropdown-item ]].. (tonumber(dscp) == value.id and 'active' or '') ..[[" href="]])
|
||||
|
||||
local dscp_table = ternary(key ~= 6, dscp_params_non_filter, dscp_params)
|
||||
|
||||
dscp_table["dscp"] = value.id
|
||||
print(getPageUrl(base_url, dscp_table))
|
||||
|
||||
print[[">]] print(key) print [[ (]] print(string.format("%d", value.count)) print [[)</a></li>]]
|
||||
end
|
||||
|
||||
print[[</ul>]]
|
||||
end
|
||||
|
||||
-- ###################################
|
||||
|
||||
function printHostPoolDropdown(base_url, page_params, host_pool_list)
|
||||
local host_pools = require "host_pools"
|
||||
|
||||
local host_pools_instance = host_pools:create()
|
||||
local host_pool = _GET["host_pool_id"]
|
||||
local host_pool_filter
|
||||
if not isEmptyString(host_pool) then
|
||||
host_pool_filter = '<span class="fas fa-filter"></span>'
|
||||
else
|
||||
host_pool_filter = ''
|
||||
end
|
||||
|
||||
-- table.clone needed to modify some parameters while keeping the original unchanged
|
||||
local host_pool_params = table.clone(page_params)
|
||||
host_pool_params["host_pool_id"] = nil
|
||||
-- Used to possibly remove tcp state filters when selecting a non-TCP l4 protocol
|
||||
local host_pool_params_non_filter = table.clone(host_pool_params)
|
||||
if host_pool_params_non_filter["host_pool_id"] then
|
||||
host_pool_params_non_filter["host_pool_id"] = nil
|
||||
end
|
||||
|
||||
local ordered_host_pool_list = {}
|
||||
|
||||
if host_pool then
|
||||
local id = tonumber(host_pool)
|
||||
ordered_host_pool_list[id] = {}
|
||||
ordered_host_pool_list[id]["count"] = host_pool_list[id]["count"]
|
||||
else
|
||||
for key, value in pairs(host_pool_list) do
|
||||
ordered_host_pool_list[key] = {}
|
||||
ordered_host_pool_list[key]["count"] = value.count
|
||||
end
|
||||
end
|
||||
|
||||
print[[\
|
||||
<button class="btn btn-link dropdown-toggle" data-bs-toggle="dropdown">]] print(i18n("details.host_pool")) print[[]] print(host_pool_filter) print[[<span class="caret"></span></button>\
|
||||
<ul class="dropdown-menu dropdown-menu-end scrollable-dropdown" role="menu" id="flow_dropdown">\
|
||||
<li><a class="dropdown-item" href="]] print(getPageUrl(base_url, host_pool_params_non_filter)) print[[">]] print(i18n("flows_page.all_host_pool")) print[[</a></li>]]
|
||||
|
||||
for key, value in pairsByKeys(ordered_host_pool_list, asc) do
|
||||
print[[<li]]
|
||||
|
||||
print([[><a class="dropdown-item ]].. (tonumber(host_pool) == key and 'active' or '') ..[[" href="]])
|
||||
|
||||
local host_pool_table = ternary(key ~= 6, host_pool_params_non_filter, host_pool_params)
|
||||
|
||||
host_pool_table["host_pool_id"] = key
|
||||
print(getPageUrl(base_url, host_pool_table))
|
||||
|
||||
print[[">]] print(host_pools_instance:get_pool_name(key)) print [[ (]] print(string.format("%d", value.count)) print [[)</a></li>]]
|
||||
end
|
||||
|
||||
print[[</ul>]]
|
||||
end
|
||||
|
||||
-- ###################################
|
||||
|
||||
function printLocalNetworksDropdown(base_url, page_params)
|
||||
local networks_stats = interface.getNetworksStats()
|
||||
|
||||
local ids = {}
|
||||
for n, local_network in pairs(networks_stats) do
|
||||
local network_name = getFullLocalNetworkName(local_network["network_key"])
|
||||
ids[network_name] = local_network
|
||||
end
|
||||
|
||||
local local_network_id = _GET["network"]
|
||||
local local_network_id_filter = ''
|
||||
if not isEmptyString(local_network_id) then
|
||||
local_network_id_filter = '<span class="fas fa-filter"></span>'
|
||||
end
|
||||
|
||||
-- table.clone needed to modify some parameters while keeping the original unchanged
|
||||
local local_network_id_params = table.clone(page_params)
|
||||
local_network_id_params["network"] = nil
|
||||
|
||||
print[[\
|
||||
<button class="btn btn-link dropdown-toggle" data-bs-toggle="dropdown">]] print(i18n("flows_page.networks")) print[[]] print(local_network_id_filter) print[[<span class="caret"></span></button>\
|
||||
<ul class="dropdown-menu scrollable-dropdown" role="menu" id="flow_dropdown">\
|
||||
<li><a class="dropdown-item" href="]] print(getPageUrl(base_url, local_network_id_params)) print[[">]] print(i18n("flows_page.all_networks")) print[[</a></li>\]]
|
||||
|
||||
for local_network_name, local_network in pairsByKeys(ids) do
|
||||
local cur_id = local_network["network_id"]
|
||||
local_network_id_params["network"] = cur_id
|
||||
print[[
|
||||
<li>\
|
||||
<a class="dropdown-item ]] print(local_network_id == tostring(cur_id) and 'active' or '') print[[" href="]] print(getPageUrl(base_url, local_network_id_params)) print[[">]] print(local_network_name) print[[</a></li>\]]
|
||||
end
|
||||
print[[
|
||||
|
||||
</ul>]]
|
||||
end
|
||||
|
||||
-- ##############################################
|
||||
|
||||
function printTrafficTypeFilterDropdown(base_url, page_params)
|
||||
local traffic_type = _GET["traffic_type"]
|
||||
local traffic_type_filter = ''
|
||||
if not isEmptyString(traffic_type) then
|
||||
traffic_type_filter = '<span class="fas fa-filter"></span>'
|
||||
end
|
||||
|
||||
-- table.clone needed to modify some parameters while keeping the original unchanged
|
||||
local traffic_type_params = table.clone(page_params)
|
||||
traffic_type_params["traffic_type"] = nil
|
||||
|
||||
print[[\
|
||||
<button class="btn btn-link dropdown-toggle" data-bs-toggle="dropdown">]] print(i18n("flows_page.direction")) print[[]] print(traffic_type_filter) print[[<span class="caret"></span></button>\
|
||||
<ul class="dropdown-menu scrollable-dropdown" role="menu" id="flow_dropdown">\
|
||||
<li><a class="dropdown-item" href="]] print(getPageUrl(base_url, traffic_type_params)) print[[">]] print(i18n("hosts_stats.traffic_type_all")) print[[</a></li>\]]
|
||||
|
||||
-- now forthe one-way
|
||||
traffic_type_params["traffic_type"] = "one_way"
|
||||
print[[
|
||||
<li>\
|
||||
<a class="dropdown-item ]] if traffic_type == "one_way" then print('active') end print[[" href="]] print(getPageUrl(base_url, traffic_type_params)) print[[">]] print(i18n("hosts_stats.traffic_type_one_way")) print[[</a></li>\]]
|
||||
traffic_type_params["traffic_type"] = "bidirectional"
|
||||
print[[
|
||||
<li>\
|
||||
<a class="dropdown-item ]] if traffic_type == "bidirectional" then print('active') end print[[" href="]] print(getPageUrl(base_url, traffic_type_params)) print[[">]] print(i18n("hosts_stats.traffic_type_two_ways")) print[[</a></li>\]]
|
||||
print[[
|
||||
</ul>]]
|
||||
end
|
||||
|
||||
-- ##############################################
|
||||
|
||||
function printHostsDeviceFilterDropdown(base_url, page_params)
|
||||
-- Getting probes
|
||||
local flowdevs = interface.getFlowDevices() or {}
|
||||
local ordering_fun = pairsByKeys
|
||||
local cur_dev = _GET["deviceIP"]
|
||||
local cur_dev_filter = ''
|
||||
-- table.clone needed to modify some parameters while keeping the original unchanged
|
||||
local dev_params = table.clone(page_params)
|
||||
local devips = getProbesName(flowdevs)
|
||||
local devips_order = ntop.getPref("ntopng.prefs.flow_table_probe_order") == "1" -- Order by Probe Name
|
||||
|
||||
if devips_order then
|
||||
ordering_fun = pairsByValues
|
||||
end
|
||||
|
||||
if not isEmptyString(cur_dev) then
|
||||
cur_dev_filter = '<span class="fas fa-filter"></span>'
|
||||
end
|
||||
|
||||
dev_params["deviceIP"] = nil
|
||||
|
||||
if table.len(devips) > 0 then
|
||||
print[[, '<div class="btn-group float-right">]]
|
||||
|
||||
print[[
|
||||
<button class="btn btn-link dropdown-toggle" data-bs-toggle="dropdown">]] print(i18n("flows_page.device_ip")) print[[]] print(cur_dev_filter) print[[<span class="caret"></span></button>\
|
||||
<ul class="dropdown-menu dropdown-menu-end scrollable-dropdown" role="menu" id="flow_dropdown">\
|
||||
<li><a class="dropdown-item" href="]] print(getPageUrl(base_url, dev_params)) print[[">]] print(i18n("flows_page.all_devices")) print[[</a></li>\]]
|
||||
|
||||
for dev_ip, dev_resolved_name in ordering_fun(devips, asc) do
|
||||
local dev_name = dev_ip
|
||||
|
||||
dev_params["deviceIP"] = dev_name
|
||||
|
||||
if not isEmptyString(dev_resolved_name) and dev_resolved_name ~= dev_name then
|
||||
dev_name = dev_name .. " ["..shortenString(dev_resolved_name).."]"
|
||||
end
|
||||
|
||||
print[[
|
||||
<li><a class="dropdown-item ]] print(dev_ip == cur_dev and 'active' or '') print[[" href="]] print(getPageUrl(base_url, dev_params)) print[[">]] print(i18n("flows_page.device_ip").." "..dev_name) print[[</a></li>\]]
|
||||
end
|
||||
|
||||
print[[
|
||||
</ul>\
|
||||
]]
|
||||
|
||||
print[[</div>']]
|
||||
end
|
||||
end
|
||||
|
||||
function processColor(proc)
|
||||
if(proc == nil) then
|
||||
return("")
|
||||
elseif(proc["average_cpu_load"] < 33) then
|
||||
return("<font color=green>"..proc["name"].."</font>")
|
||||
elseif(proc["average_cpu_load"] < 66) then
|
||||
return("<font color=orange>"..proc["name"].."</font>")
|
||||
else
|
||||
return("<font color=red>"..proc["name"].."</font>")
|
||||
end
|
||||
end
|
||||
|
||||
-- print TCP flags
|
||||
function printTCPFlags(flags)
|
||||
print(formatTCPFlags(flags))
|
||||
end
|
||||
|
||||
|
||||
-- ###########################################
|
||||
|
||||
function printWarningAlert(message)
|
||||
print[[<div class="alert alert-warning alert-dismissable" role="alert">]]
|
||||
print[[<i class="fas fa-exclamation-triangle fa-sm"></i> ]]
|
||||
print[[<strong>]] print(i18n("warning")) print[[</strong> ]]
|
||||
print(message)
|
||||
print[[<button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close"></button>]]
|
||||
print[[</div>]]
|
||||
end
|
||||
|
||||
-- ###########################################
|
||||
|
||||
-- Banner format: {type="success|warning|danger", text="..."}
|
||||
function printMessageBanners(banners)
|
||||
for _, msg in ipairs(banners) do
|
||||
print[[
|
||||
<div class="alert alert-]] print(msg.type) print([[ alert-dismissible" style="margin-top:2em; margin-bottom:0em;">
|
||||
]])
|
||||
|
||||
if (msg.type == "warning") then
|
||||
print("<b>".. i18n("warning") .. "</b>: ")
|
||||
elseif (msg.type == "danger") then
|
||||
print("<b>".. i18n("error") .. "</b>: ")
|
||||
end
|
||||
|
||||
print(msg.text)
|
||||
|
||||
print[[
|
||||
<button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close"></button>
|
||||
</div>]]
|
||||
end
|
||||
end
|
||||
|
||||
-- ##############################################
|
||||
|
||||
function print_copy_button(id, data)
|
||||
print('<button style="" class="btn btn-sm border ms-1" data-placement="bottom" id="btn-copy-' .. id ..'" data="' .. data .. '"><i class="fas fa-copy"></i></button>')
|
||||
print("<script>$('#btn-copy-" .. id .. "').click(function(e) { NtopUtils.copyToClipboard($(this).attr('data'), '" .. i18n('copied') .. "', '" .. i18n('request_failed_message') .. "', $(this));});</script>")
|
||||
end
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
if(trace_script_duration ~= nil) then
|
||||
io.write(debug.getinfo(1,'S').source .." executed in ".. (os.clock()-clock_start)*1000 .. " ms\n")
|
||||
end
|
||||
|
|
@ -11,7 +11,6 @@ local tracker = require "tracker"
|
|||
local os_utils = {}
|
||||
|
||||
local is_windows = ntop.isWindows()
|
||||
local is_freebsd = ntop.isFreeBSD()
|
||||
|
||||
local dirs = ntop.getDirs()
|
||||
|
||||
|
|
@ -65,6 +64,7 @@ function os_utils.execWithOutput(c, ret_code_success)
|
|||
local debug = false
|
||||
local f_name = nil
|
||||
local f
|
||||
local is_freebsd = ntop.isFreeBSD()
|
||||
|
||||
ret_code_success = ret_code_success or 0
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue