Adds new REST API v2/

Addresses #5269
This commit is contained in:
Simone Mainardi 2021-07-08 09:57:46 +02:00
parent 1f24dec3e3
commit b117e8a23a
178 changed files with 6639 additions and 124 deletions

View file

@ -0,0 +1,184 @@
--
-- (C) 2013-21 - ntop.org
--
local dirs = ntop.getDirs()
package.path = dirs.installdir .. "/scripts/lua/modules/?.lua;" .. package.path
require "lua_utils"
require "flow_utils"
local format_utils = require("format_utils")
local flow_utils = require "flow_utils"
local icmp_utils = require "icmp_utils"
local json = require "dkjson"
local rest_utils = require("rest_utils")
--
-- Read list of active flows
-- Example: curl -u admin:admin -H "Content-Type: application/json" -d '{"ifid": "1"}' http://localhost:3000/lua/rest/v2/get/flow/active.lua
--
-- NOTE: in case of invalid login, no error is returned but redirected to login
--
local rc = rest_utils.consts.success.ok
local res = {}
local ifid = _GET["ifid"]
local verbose = (_GET["verbose"] == "true")
if isEmptyString(ifid) then
rc = rest_utils.consts.err.invalid_interface
rest_utils.answer(rc)
return
end
interface.select(ifid)
if not isEmptyString(_GET["sortColumn"]) then
-- Backward compatibility
_GET["sortColumn"] = "column_" .. _GET["sortColumn"]
end
-- This is using GET parameters to handle:
--
-- Pagination:
-- - sortColumn
-- - sortOrder
-- - currentPage
-- - perPage
--
-- Filtering, including:
-- - application
-- - l4proto
-- - host
-- - vlan
--
local flows_filter = getFlowsFilter()
local flows_stats = interface.getFlowsInfo(flows_filter["hostFilter"], flows_filter)
if flows_stats == nil then
rest_utils.answer(rest_utils.consts.err.not_found)
return
end
local total = flows_stats["numFlows"]
flows_stats = flows_stats["flows"]
if flows_stats == nil then
rest_utils.answer(rest_utils.consts.err.internal_error)
return
end
local data = {}
for _key, value in ipairs(flows_stats) do
local record = {}
local key = value["ntopng.key"]
record["key"] = string.format("%u", value["ntopng.key"])
record["hash_id"] = string.format("%u", value["hash_entry_id"])
record["first_seen"] = value["seen.first"]
record["last_seen"] = value["seen.last"]
local client = {}
local cli_name = flowinfo2hostname(value, "cli")
client["name"] = stripVlan(cli_name)
client["ip"] = value["cli.ip"]
client["port"] = value["cli.port"]
local info = interface.getHostInfo(value["cli.ip"], value["cli.vlan"])
if info then
client["is_broadcast_domain"] = info.broadcast_domain_host
client["is_dhcp"] = info.dhcpHost
client["is_blacklisted"] = info.is_blacklisted
end
record["client"] = client
local server = {}
local srv_name = flowinfo2hostname(value, "srv")
server["name"] = stripVlan(srv_name)
server["ip"] = value["srv.ip"]
server["port"] = value["srv.port"]
info = interface.getHostInfo(value["srv.ip"], value["srv.vlan"])
local info = interface.getHostInfo(value["cli.ip"], value["cli.vlan"])
if info then
server["is_broadcast"] = info.broadcast_domain_host
server["is_dhcp"] = info.dhcpHost
server["is_blacklisted"] = info.is_blacklisted
end
record["server"] = server
record["vlan"] = value["vlan"]
record["protocol"] = {}
record["protocol"]["l4"] = value["proto.l4"]
record["protocol"]["l7"] = value["proto.ndpi"]
record["duration"] = value["duration"]
record["bytes"] = value["bytes"]
record["thpt"] = {}
record["thpt"]["pps"] = value["throughput_pps"]
record["thpt"]["bps"] = value["throughput_bps"]*8
local cli2srv = round((value["cli2srv.bytes"] * 100) / value["bytes"], 0)
record["breakdown"] = {}
record["breakdown"]["cli2srv"] = cli2srv
record["breakdown"]["srv2cli"] = (100-cli2srv)
if isScoreEnabled() then
record["score"] = format_utils.formatValue(value["score"]["flow_score"])
end
if verbose then
record["packets"] = value["cli2srv.packets"] + value["srv2cli.packets"]
record["tcp"] = {}
record["tcp"]["appl_latency"] = value["tcp.appl_latency"]
record["tcp"]["nw_latency"] = {}
record["tcp"]["nw_latency"]["cli"] = value["tcp.nw_latency.client"]
record["tcp"]["nw_latency"]["srv"] = value["tcp.nw_latency.server"]
record["tcp"]["retransmissions"] = {}
record["tcp"]["retransmissions"]["cli2srv"] = value["cli2srv.retransmissions"]
record["tcp"]["retransmissions"]["srv2cli"] = value["srv2cli.retransmissions"]
record["tcp"]["out_of_order"] = {}
record["tcp"]["out_of_order"]["cli2srv"] = value["cli2srv.out_of_order"]
record["tcp"]["out_of_order"]["srv2cli"] = value["srv2cli.out_of_order"]
record["tcp"]["lost"] = {}
record["tcp"]["lost"]["cli2srv"] = value["cli2srv.lost"]
record["tcp"]["lost"]["srv2cli"] = value["srv2cli.lost"]
end
data[#data + 1] = record
end -- for
res = {
perPage = flows_filter["perPage"],
currentPage = flows_filter["currentPage"],
totalRows = total,
data = data,
sort = {
{
flows_filter["sortColumn"],
flows_filter["sortOrder"]
}
},
}
rest_utils.answer(rc, res)

View file

@ -0,0 +1,10 @@
--
-- (C) 2019-21 - ntop.org
--
local dirs = ntop.getDirs()
package.path = dirs.installdir .. "/scripts/lua/modules/?.lua;" .. package.path
local alert_rest_utils = require "alert_rest_utils"
alert_rest_utils.get_alert_exclusions("flow", _GET["host"])

View file

@ -0,0 +1,56 @@
--
-- (C) 2013-21 - ntop.org
--
local dirs = ntop.getDirs()
package.path = dirs.installdir .. "/scripts/lua/modules/?.lua;" .. package.path
package.path = dirs.installdir .. "/scripts/lua/modules/alert_store/?.lua;" .. package.path
local rest_utils = require("rest_utils")
local flow_alert_store = require "flow_alert_store".new()
local auth = require "auth"
--
-- Read alerts data
-- Example: curl -u admin:admin -H "Content-Type: application/json" -d '{"ifid": "1"}' http://localhost:3000/lua/rest/v2/get/flow/alert/list.lua
--
-- NOTE: in case of invalid login, no error is returned but redirected to login
--
local rc = rest_utils.consts.success.ok
local res = {}
local ifid = _GET["ifid"]
local format = _GET["format"] or "json"
local no_html = (format == "txt")
if not auth.has_capability(auth.capabilities.alerts) then
rest_utils.answer(rest_utils.consts.err.not_granted)
return
end
if isEmptyString(ifid) then
rc = rest_utils.consts.err.invalid_interface
rest_utils.answer(rc)
return
end
interface.select(ifid)
-- Fetch the results
local alerts, recordsFiltered = flow_alert_store:select_request(nil, "*, hex(alerts_map) alerts_map")
for _, _value in ipairs(alerts or {}) do
res[#res + 1] = flow_alert_store:format_record(_value, no_html)
end
if no_html then
res = flow_alert_store:to_csv(res)
rest_utils.vanilla_payload_response(rc, res, "text/csv")
else
rest_utils.extended_answer(rc, {records = res}, {
["draw"] = tonumber(_GET["draw"]),
["recordsFiltered"] = recordsFiltered,
["recordsTotal"] = #res
}, format)
end

View file

@ -0,0 +1,43 @@
--
-- (C) 2013-21 - ntop.org
--
local dirs = ntop.getDirs()
package.path = dirs.installdir .. "/scripts/lua/modules/?.lua;" .. package.path
package.path = dirs.installdir .. "/scripts/lua/modules/alert_store/?.lua;" .. package.path
local alert_utils = require "alert_utils"
local alert_consts = require "alert_consts"
local alert_entities = require "alert_entities"
local rest_utils = require("rest_utils")
local flow_alert_store = require "flow_alert_store".new()
local alert_severities = require "alert_severities"
local auth = require "auth"
--
-- Read alerts data
-- Example: curl -u admin:admin -H "Content-Type: application/json" -d '{"ifid": "1"}' http://localhost:3000/lua/rest/v2/get/flow/alert/ts.lua
--
-- NOTE: in case of invalid login, no error is returned but redirected to login
--
local rc = rest_utils.consts.success.ok
local ifid = _GET["ifid"]
if not auth.has_capability(auth.capabilities.alerts) then
rest_utils.answer(rest_utils.consts.err.not_granted)
return
end
if isEmptyString(ifid) then
rc = rest_utils.consts.err.invalid_interface
rest_utils.answer(rc)
return
end
interface.select(ifid)
local res = flow_alert_store:count_by_severity_and_time_request(true)
rest_utils.answer(rc, res)

View file

@ -0,0 +1,43 @@
--
-- (C) 2013-21 - ntop.org
--
local dirs = ntop.getDirs()
package.path = dirs.installdir .. "/scripts/lua/modules/?.lua;" .. package.path
require "lua_utils"
local json = require ("dkjson")
local tracker = require("tracker")
local rest_utils = require("rest_utils")
--
-- Read number of active flows per protocol
-- Example: curl -u admin:admin -H "Content-Type: application/json" -d '{"ifid": "1"}' http://localhost:3000/lua/rest/v2/get/flow/l4/counters.lua
--
-- NOTE: in case of invalid login, no error is returned but redirected to login
--
local rc = rest_utils.consts.success.ok
local res = {}
local ifid = _GET["ifid"]
if isEmptyString(ifid) then
rest_utils.answer(rest_utils.consts.err.invalid_interface)
return
end
interface.select(ifid)
local flowstats = interface.getActiveFlowsStats()
local l4_proto = flowstats["l4_protocols"]
for k,v in pairs(l4_proto, asc) do
res[#res + 1] = {
id = k,
count = v.count,
}
end
rest_utils.answer(rc, res)

View file

@ -0,0 +1,43 @@
--
-- (C) 2013-21 - ntop.org
--
local dirs = ntop.getDirs()
package.path = dirs.installdir .. "/scripts/lua/modules/?.lua;" .. package.path
require "lua_utils"
local json = require ("dkjson")
local tracker = require("tracker")
local rest_utils = require("rest_utils")
--
-- Read number of active flows per protocol
-- Example: curl -u admin:admin -H "Content-Type: application/json" -d '{"ifid": "1"}' http://localhost:3000/lua/rest/v2/get/flow/l7/counters.lua
--
-- NOTE: in case of invalid login, no error is returned but redirected to login
--
local rc = rest_utils.consts.success.ok
local res = {}
local ifid = _GET["ifid"]
if isEmptyString(ifid) then
rest_utils.answer(rest_utils.consts.err.invalid_interface)
return
end
interface.select(ifid)
local flowstats = interface.getActiveFlowsStats()
local l7_proto = flowstats["ndpi"]
for k,v in pairsByKeys(l7_proto, asc) do
res[#res + 1] = {
name = k,
count = v.num_flows,
}
end
rest_utils.answer(rc, res)

View file

@ -0,0 +1,12 @@
--
-- (C) 2013-21 - 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
local flow_pools = require "flow_pools"
local pools_rest_utils = require "pools_rest_utils"
pools_rest_utils.get_pools(flow_pools)

View file

@ -0,0 +1,35 @@
--
-- (C) 2013-21 - ntop.org
--
local dirs = ntop.getDirs()
package.path = dirs.installdir .. "/scripts/lua/modules/?.lua;" .. package.path
require "flow_utils"
require "lua_utils"
local rest_utils = require("rest_utils")
--
-- Read list of active flows
-- Example: curl -u admin:admin -H "Content-Type: application/json" -d '{"ifid": "1"}' http://localhost:3000/lua/rest/v2/get/flow/traffic_stats.lua
--
-- NOTE: in case of invalid login, no error is returned but redirected to login
--
local flows_filter = getFlowsFilter()
local rc = rest_utils.consts.success.ok
local res
local ifid = _GET["ifid"]
if isEmptyString(ifid) then
rc = rest_utils.consts.err.invalid_interface
rest_utils.answer(rc)
return
end
interface.select(ifid)
-- This is used to get the current bytes rcvd and sent by these specific filters
res = interface.getActiveFlowsStats(flows_filter["hostFilter"], flows_filter, true)
rest_utils.answer(rc, res)