Skip to content

Commit

Permalink
Added REST parameters, mac, pool, pool_id (#8807)
Browse files Browse the repository at this point in the history
  • Loading branch information
MatteoBiscosi committed Nov 12, 2024
1 parent 2aa46f9 commit 35e0a4d
Showing 1 changed file with 77 additions and 72 deletions.
149 changes: 77 additions & 72 deletions scripts/lua/rest/v2/get/host/active.lua
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ package.path = dirs.installdir .. "/scripts/lua/modules/?.lua;" .. package.path

require "lua_utils"
require "flow_utils"
local format_utils = require("format_utils")
local json = require "dkjson"
local rest_utils = require("rest_utils")
local host_pools = require "host_pools"
local rest_utils = require("rest_utils")
local host_pools_instance = host_pools:create()

--
-- Read list of active hosts
Expand All @@ -18,22 +18,22 @@ local rest_utils = require("rest_utils")
-- NOTE: in case of invalid login, no error is returned but redirected to login
--

local rc = rest_utils.consts.success.ok
local res = {}
local rc = rest_utils.consts.success.ok
local res = {}

local ifid = _GET["ifid"]
local ifid = _GET["ifid"]

-- Pagination:
local all = _GET["all"]
local currentPage = _GET["currentPage"]
local perPage = _GET["perPage"]
local sortColumn = _GET["sortColumn"] -- ip, name, since, last, alerts, country, vlan, num_flows, traffic, thpt
local sortOrder = _GET["sortOrder"]
local all = _GET["all"]
local currentPage = _GET["currentPage"]
local perPage = _GET["perPage"]
local sortColumn = _GET["sortColumn"] -- ip, name, since, last, alerts, country, vlan, num_flows, traffic, thpt
local sortOrder = _GET["sortOrder"]

-- Filters
local mode = _GET["mode"] -- all local remote broadcast_domain filtered blacklisted dhcp
local ipversion = _GET["version"]
local protocol = _GET["protocol"]
local mode = _GET["mode"] -- all local remote broadcast_domain filtered blacklisted dhcp
local ipversion = _GET["version"]
local protocol = _GET["protocol"]
local traffic_type = _GET["traffic_type"]
local asn = _GET["asn"]
local vlan = _GET["vlan"]
Expand All @@ -55,20 +55,20 @@ interface.select(ifid)
if not isEmptyString(_GET["sortColumn"]) then
-- Backward compatibility
_GET["sortColumn"] = "column_" .. _GET["sortColumn"]
sortColumn = _GET["sortColumn"]
sortColumn = _GET["sortColumn"]
end

if(currentPage == nil) then
if (currentPage == nil) then
currentPage = 1
else
currentPage = tonumber(currentPage)
end

if(perPage == nil) then
if (perPage == nil) then
perPage = getDefaultTableSize()
else
perPage = tonumber(perPage)
tablePreferences("rows_number",perPage)
tablePreferences("rows_number", perPage)
end

local traffic_type_filter
Expand All @@ -85,9 +85,9 @@ end

interface.select(ifname)

local to_skip = (currentPage-1) * perPage
local to_skip = (currentPage - 1) * perPage

if(sortOrder == "desc") then sOrder = false else sOrder = true end
if (sortOrder == "desc") then sOrder = false else sOrder = true end

local filtered_hosts = false
local blacklisted = false
Expand Down Expand Up @@ -115,11 +115,11 @@ if all ~= nil then
end

local hosts_stats = hosts_retrv_function(false, sortColumn, perPage, to_skip, sOrder,
country, os_, tonumber(vlan), tonumber(asn),
tonumber(network), mac,
tonumber(pool), tonumber(ipversion),
tonumber(protocol), traffic_type_filter,
filtered_hosts, blacklisted_hosts, anomalous, dhcp_hosts, cidr)
country, os_, tonumber(vlan), tonumber(asn),
tonumber(network), mac,
tonumber(pool), tonumber(ipversion),
tonumber(protocol), traffic_type_filter,
filtered_hosts, blacklisted_hosts, anomalous, dhcp_hosts, cidr)

if hosts_stats == nil then
rest_utils.answer(rest_utils.consts.err.not_found)
Expand All @@ -141,7 +141,7 @@ function get_host_name(h)
h["name"] = h["mac"]
end
end
return(h["name"])
return (h["name"])
end

local vals = {}
Expand All @@ -150,35 +150,35 @@ for key, value in pairs(hosts_stats) do
num = num + 1
postfix = string.format("0.%04u", num)

if(isEmptyString(sortColumn)) then
if (isEmptyString(sortColumn)) then
vals[key] = key
elseif(sortColumn == "column_name") then
elseif (sortColumn == "column_name") then
hosts_stats[key]["name"] = get_host_name(hosts_stats[key])
vals[hosts_stats[key]["name"]..postfix] = key
elseif(sortColumn == "column_since") then
vals[hosts_stats[key]["seen.first"]+postfix] = key
elseif(sortColumn == "column_alerts") then
vals[hosts_stats[key]["num_alerts"]+postfix] = key
elseif(sortColumn == "column_last") then
vals[hosts_stats[key]["seen.last"]+postfix] = key
elseif(sortColumn == "column_country") then
vals[hosts_stats[key]["country"]..postfix] = key
elseif(sortColumn == "column_vlan") then
vals[hosts_stats[key]["vlan"]..postfix] = key
elseif(sortColumn == "column_num_flows") then
local t = hosts_stats[key]["active_flows.as_client"]+hosts_stats[key]["active_flows.as_server"]
vals[t+postfix] = key
elseif(sortColumn == "column_num_dropped_flows") then
vals[hosts_stats[key]["name"] .. postfix] = key
elseif (sortColumn == "column_since") then
vals[hosts_stats[key]["seen.first"] + postfix] = key
elseif (sortColumn == "column_alerts") then
vals[hosts_stats[key]["num_alerts"] + postfix] = key
elseif (sortColumn == "column_last") then
vals[hosts_stats[key]["seen.last"] + postfix] = key
elseif (sortColumn == "column_country") then
vals[hosts_stats[key]["country"] .. postfix] = key
elseif (sortColumn == "column_vlan") then
vals[hosts_stats[key]["vlan"] .. postfix] = key
elseif (sortColumn == "column_num_flows") then
local t = hosts_stats[key]["active_flows.as_client"] + hosts_stats[key]["active_flows.as_server"]
vals[t + postfix] = key
elseif (sortColumn == "column_num_dropped_flows") then
local t = hosts_stats[key]["flows.dropped"] or 0
vals[t+postfix] = key
elseif(sortColumn == "column_traffic") then
vals[hosts_stats[key]["bytes.sent"]+hosts_stats[key]["bytes.rcvd"]+postfix] = key
elseif(sortColumn == "column_thpt") then
vals[hosts_stats[key]["throughput_bps"]+postfix] = key
elseif(sortColumn == "column_queries") then
vals[hosts_stats[key]["queries.rcvd"]+postfix] = key
elseif(sortColumn == "column_ip") then
vals[hosts_stats[key]["ipkey"]+postfix] = key
vals[t + postfix] = key
elseif (sortColumn == "column_traffic") then
vals[hosts_stats[key]["bytes.sent"] + hosts_stats[key]["bytes.rcvd"] + postfix] = key
elseif (sortColumn == "column_thpt") then
vals[hosts_stats[key]["throughput_bps"] + postfix] = key
elseif (sortColumn == "column_queries") then
vals[hosts_stats[key]["queries.rcvd"] + postfix] = key
elseif (sortColumn == "column_ip") then
vals[hosts_stats[key]["ipkey"] + postfix] = key
else
vals[key] = key
end
Expand All @@ -202,14 +202,19 @@ for _key, _value in pairsByKeys(vals, funct) do
record["first_seen"] = value["seen.first"]
record["last_seen"] = value["seen.last"]
record["vlan"] = value["vlan"]
record["ip"] = stripVlan(key)
record["ip"] = stripVlan(key)
record["os"] = value["os"]
record["num_alerts"] = value["num_alerts"]

local host = interface.getHostInfo(hosts_stats[key].ip, hosts_stats[key].vlan)
if host ~= nil then
record["country"] = host["country"]
record["is_blacklisted"] = host["is_blacklisted"]
record["mac"] = host["mac"]
record["pool"] = {
id = host["host_pool_id"],
name = host_pools_instance:get_pool_name(host["host_pool_id"])
}
end

local name = value["name"]
Expand All @@ -223,36 +228,36 @@ for _key, _value in pairsByKeys(vals, funct) do
if value["ip"] ~= nil then
local label = hostinfo2label(value)
if label ~= value["ip"] and name ~= label then
name = name .. " ["..label.."]"
name = name .. " [" .. label .. "]"
end
end

record["name"] = name
record["name"] = name

record["score"] = {}
record["score"]["total"] = value["score"]
record["score"]["as_client"] = value["score.as_client"]
record["score"]["as_server"] = value["score.as_server"]
record["thpt"] = {}
record["thpt"]["pps"] = value["throughput_pps"]
record["thpt"]["bps"] = value["throughput_bps"]*8
record["score"] = {}
record["score"]["total"] = value["score"]
record["score"]["as_client"] = value["score.as_client"]
record["score"]["as_server"] = value["score.as_server"]
record["thpt"] = {}
record["thpt"]["pps"] = value["throughput_pps"]
record["thpt"]["bps"] = value["throughput_bps"] * 8

record["bytes"] = {}
record["bytes"]["total"] = (value["bytes.sent"]+value["bytes.rcvd"])
record["bytes"]["sent"] = value["bytes.sent"]
record["bytes"]["recvd"] = value["bytes.rcvd"]
record["bytes"] = {}
record["bytes"]["total"] = (value["bytes.sent"] + value["bytes.rcvd"])
record["bytes"]["sent"] = value["bytes.sent"]
record["bytes"]["recvd"] = value["bytes.rcvd"]

record["is_localhost"] = value["localhost"]
record["is_multicast"] = value["is_multicast"]
record["is_broadcast"] = value["is_broadcast"]
record["is_broadcast_domain"] = value["broadcast_domain_host"]
record["is_localhost"] = value["localhost"]
record["is_multicast"] = value["is_multicast"]
record["is_broadcast"] = value["is_broadcast"]
record["is_broadcast_domain"] = value["broadcast_domain_host"]

record["num_flows"] = {}
record["num_flows"] = {}
record["num_flows"]["total"] = (value["active_flows.as_client"] + value["active_flows.as_server"])
record["num_flows"]["as_client"] = (value["active_flows.as_client"])
record["num_flows"]["as_server"] = (value["active_flows.as_server"])

data[#data + 1] = record
data[#data + 1] = record
end -- for

res = {
Expand Down

0 comments on commit 35e0a4d

Please sign in to comment.