ntopng/scripts/lua/rest/v1/get/interface/data.lua
Simone Mainardi c7082a4647 Implements new alerts database and drilldown
alert store skeleton

Alert database type changes

Implement alert store for host alerts. All alert store skeletons.

Fix class method access

Enable tracing

Implements simple queries for host alerts

Implement flow alert store

Fixes escaping of INSERT queries

Flow alerts database schema fixes

Adds escaping for alert JSON in flows and hosts

Implements queries .select() for alerts store

Adds limit and offset to perform paginated queries

Adds new REST getter for flow alerts

Name changes alert_severity to severity, alert_json to json

Fixes alert message not shown

Implement active monitoring alerts store

Implements sort of queries in the new dataabase

Changes alert_type to alert_id

Implement mac alerts store

Fixes flow alert messages

Implement system alerts store

Implement snmp alert store

Add missing items to the flow alerts

Add missing items to the host alerts

Add missing items to the mac alerts

Implements COUNT aplerts api

Add device_name to snmp alerts

add flow alerts templates

updated gitignore

fix for missing order field

add families defined in `alert_store_schema.sql`

Reworks and simplifies alert store subclasses

Implements REST API to fetch alert timeseries

Fixes date in flow REST api

Host alert json fix

Add get/host/alert/list.lua

Move alert/list.lua to alert/past/list.lua

Add alert/past/list.lua for all alert families

Add entity_id to system table to identify the alert type based on <alert_id, entity_id>

Add missing field

Implements facilities to query engaged alerts via REST

Handle both historical and past alerts in alert/list.lua

Fix count

Update params of select_historical

Implement method to add family-specific filters

Add alert/ts.lua for all alert families

Implements facilities and REST endpoints to delete alerts

Implements ordering of alert queries

Fix add_order_by group_by

Rest API tests update

Tests output update

Remove debug trace

Use alert_id instead of type. Add more flow alert info.

Update http lint

Format obsolete tlv version alerts. Add more host info.

Add row_id to list of alerts

Fix selection of engaged alerts

Add test for mac alerts (bcast domains)

Removes attempt to format alerts as flow alerts

Fixes interface selection for active monitoring

Update test output with rest changes

Add more fields to be ignores

Set alert count to 1 for the time being

add bar timeseries chart

add apexcharts

improvements on timeseries bar chart

registered chart callbakcs

working on alert page

fix for date format

Fixes acrive monitoring REST API

Fixes alignment of grouped alert data

Additional fix for alert histogram

remove useless if

formatting alerts page

fixes on flows alert stats table

rename local networks to device

format host pagie in alert_stats

add tag support for hosts and flow

implemented single delete action

add release modal

Fixes format of threshold cross interface alerts

Fixes wrong increase of dropped alerts

Implements exclusion list for invalid dns queries

Reworks exclusions lists for hosts and flows

Addresses #5212
Addresses #5113

Adds host alert keys in host callbacks definitions

Adds alert ids to flow callbacks

fix for not working button (#5215)

Fixes reported timeseries name

removed any additional button inside chart's toolbar (#5200)

Add tables for interfaces, networks, users to the schema. Skeleton alert_store classes

fixed broken range picker layout in firefox (#5199)

Alert insert fixes

Add more info to network alerts

Add rest endpoint for interface, network, user alerts

Fix endpoint selection in alerts_stats

Unifies columns between engaged and past alerts

Fixes Missing mandatory 'alert_granularity'

Minor fixes for missing alert_severity

Fixes arithmetic on a nil value (field 'last_seen')

Fixes get/system/alert/list.lua

use tstamp for column names (#5221)

Implements host alert formatter

Add alerts_store format_record_common

Use common format_record for am, system alerts

Use common format_record for all alerts

Fixes formatting of alerts of all types

Fixes nil in function 'hostinfo2label'

fixes on disable modal

add pages for network, user and interface endpoint (#5224)

Set alert_entity in all classes

Unifies influxdb alerts into system alerts

Addresses #5224

Unifies process alerts into system alerts

Addresses #5224

Cleanup unused periodicActivityEntity

Unifies category lists alerts into system alerts

Addresses #5224

Aligns new alert enums

Addresses #5224

Fixes alert page links

Fixes insertion of interface alerts

Implement filters for Host alerts

Fixes active monitoring alerts not triggering

Implement filters on flow alerts

Fixes for internal alerts timestamp and subtype

implements disable for the alerts

formatted alert disable label

Add address and device type to mac alert records

fix for delete alert toggle

Fix access to entity_val in alert_unexpected_new_device

add mac address and device type inside table

Fixes for new alert fields not handled

Fixes alert_definitions to handle new fields

Add ip/port to snmp alert records

Implements deletion of stored flow alerts

Add alert_name to all alert records via rest. Fix duration.

fixes for snmp tab

Implements delete of past host alerts

Add name to snmp alert records

fixes on system tab

Fix Date column

fixes link

Update menu

Fixes bad argument #3 to 'format' in snmp alerts

updated interface link

new alerts url for host (#5228)

Fixes sort of engaged alert

Minor cleanup

Fixes data returned for local network alerts

Fix duration for one shot. Note.

Fix duration override

Fixes interface selection for system alerts

Move host alert page

fixes for local network tab

Minor fix

Fix engaged host alerts

fixes on user tab

Fix alert_user_activity message
2021-04-26 19:41:34 +02:00

273 lines
10 KiB
Lua

--
-- (C) 2013-21 - ntop.org
--
local dirs = ntop.getDirs()
package.path = dirs.installdir .. "/scripts/lua/modules/?.lua;" .. package.path
require "lua_utils"
local alert_utils = require "alert_utils"
local json = require("dkjson")
local ts_utils = require("ts_utils_core")
local plugins_utils = require("plugins_utils")
local periodic_activities_utils = require "periodic_activities_utils"
local cpu_utils = require("cpu_utils")
local callback_utils = require("callback_utils")
local recording_utils = require("recording_utils")
local alert_consts = require("alert_consts")
local rest_utils = require("rest_utils")
--
-- Read information about an interface
-- Example: curl -u admin:admin -H "Content-Type: application/json" -d '{"ifid": "1"}' http://localhost:3000/lua/rest/v1/get/interface/data.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 iffilter = _GET["iffilter"]
if isEmptyString(ifid) and isEmptyString(iffilter) then
rc = rest_utils.consts.err.invalid_interface
rest_utils.answer(rc)
return
end
local function userHasRestrictions()
local allowed_nets = ntop.getPref("ntopng.user." .. (_SESSION["user"] or "") .. ".allowed_nets")
for _, net in pairs(split(allowed_nets, ",")) do
if not isEmptyString(net) and net ~= "0.0.0.0/0" and net ~= "::/0" then
return true
end
end
return false
end
local function countHosts()
local res = {
local_hosts = 0,
hosts = 0,
}
for host, info in callback_utils.getHostsIterator(false --[[no details]]) do
if info.localhost then
res.local_hosts = res.local_hosts + 1
end
res.hosts = res.hosts + 1
end
return res
end
function dumpInterfaceStats(ifid)
local interface_name = getInterfaceName(ifid)
interface.select(ifid..'')
local ifstats = interface.getStats()
local res = {}
if(ifstats ~= nil) then
local uptime = ntop.getUptime()
local prefs = ntop.getPrefs()
-- Round up
local hosts_pctg = math.floor(1+((ifstats.stats.hosts*100)/prefs.max_num_hosts))
local flows_pctg = math.floor(1+((ifstats.stats.flows*100)/prefs.max_num_flows))
local macs_pctg = math.floor(1+((ifstats.stats.current_macs*100)/prefs.max_num_hosts))
res["ifid"] = ifid
res["ifname"] = interface_name
res["speed"] = getInterfaceSpeed(ifstats.id)
res["periodic_stats_update_frequency_secs"] = ifstats.periodic_stats_update_frequency_secs
-- network load is used by web pages that are shown to the user
-- so we must return statistics since the latest (possible) reset
res["packets"] = ifstats.stats_since_reset.packets
res["bytes"] = ifstats.stats_since_reset.bytes
res["drops"] = ifstats.stats_since_reset.drops
if ifstats.stats_since_reset.discarded_probing_packets then
res["discarded_probing_packets"] = ifstats.stats_since_reset.discarded_probing_packets
res["discarded_probing_bytes"] = ifstats.stats_since_reset.discarded_probing_bytes
end
res["throughput_bps"] = ifstats.stats.throughput_bps;
res["throughput_pps"] = ifstats.stats.throughput_pps;
if prefs.is_dump_flows_enabled == true then
res["flow_export_drops"] = ifstats.stats_since_reset.flow_export_drops
res["flow_export_rate"] = ifstats.stats_since_reset.flow_export_rate
res["flow_export_count"] = ifstats.stats_since_reset.flow_export_count
end
if prefs.are_alerts_enabled == true then
res["engaged_alerts"] = ifstats["num_alerts_engaged"] or 0
res["dropped_alerts"] = ifstats["num_dropped_alerts"] or 0
-- Active flow alerts: total
res["alerted_flows"] = ifstats["num_alerted_flows"] or 0
-- Active flow alerts: breakdown
res["alerted_flows_notice"] = ifstats["num_alerted_flows_notice"] or 0
res["alerted_flows_warning"] = ifstats["num_alerted_flows_warning"] or 0
res["alerted_flows_error"] = ifstats["num_alerted_flows_error"] or 0
res["has_alerts"] = ifstats["has_alerts"]
end
if periodic_activities_utils.have_degraded_performance() then
res["degraded_performance"] = true
end
if not userHasRestrictions() then
res["num_flows"] = ifstats.stats.flows
res["num_hosts"] = ifstats.stats.hosts
res["num_local_hosts"] = ifstats.stats.local_hosts
res["num_devices"] = ifstats.stats.devices
else
local num_hosts = countHosts()
res["num_hosts"] = num_hosts.hosts
res["num_local_hosts"] = num_hosts.local_hosts
end
res["epoch"] = os.time()
res["localtime"] = os.date("%H:%M:%S %z", res["epoch"])
res["uptime"] = secondsToTime(uptime)
if ntop.isPro() then
local product_info = ntop.getInfo(true)
if product_info["pro.out_of_maintenance"] then
res["out_of_maintenance"] = true
end
end
res["system_host_stats"] = cpu_utils.systemHostStats()
res["hosts_pctg"] = hosts_pctg
res["flows_pctg"] = flows_pctg
res["macs_pctg"] = macs_pctg
res["remote_pps"] = ifstats.remote_pps
res["remote_bps"] = ifstats.remote_bps
res["is_view"] = ifstats.isView
if isAdministrator() then
res["num_live_captures"] = ifstats.stats.num_live_captures
end
res["local2remote"] = ifstats["localstats"]["bytes"]["local2remote"]
res["remote2local"] = ifstats["localstats"]["bytes"]["remote2local"]
res["bytes_upload"] = ifstats["eth"]["egress"]["bytes"]
res["bytes_download"] = ifstats["eth"]["ingress"]["bytes"]
res["packets_upload"] = ifstats["eth"]["egress"]["packets"]
res["packets_download"] = ifstats["eth"]["ingress"]["packets"]
res["num_local_hosts_anomalies"] = ifstats.anomalies.num_local_hosts_anomalies
res["num_remote_hosts_anomalies"] = ifstats.anomalies.num_remote_hosts_anomalies
local ingress_thpt = ifstats["eth"]["ingress"]["throughput"]
local egress_thpt = ifstats["eth"]["egress"]["throughput"]
res["throughput"] = {
download = {
bps = ingress_thpt["bps"], bps_trend = ingress_thpt["bps_trend"],
pps = ingress_thpt["pps"], pps_trend = ingress_thpt["pps_trend"]
},
upload = {
bps = egress_thpt["bps"], bps_trend = egress_thpt["bps_trend"],
pps = egress_thpt["pps"], pps_trend = egress_thpt["pps_trend"]
},
}
if ntop.isnEdge() and ifstats.type == "netfilter" and ifstats.netfilter then
res["netfilter"] = ifstats.netfilter
end
if(ifstats.zmqRecvStats ~= nil) then
if ifstats.zmqRecvStats_since_reset then
-- override stats with the values calculated from the latest user reset
-- for consistency with if_stats.lua
for k, v in pairs(ifstats.zmqRecvStats_since_reset) do
ifstats.zmqRecvStats[k] = v
end
end
res["zmqRecvStats"] = {}
res["zmqRecvStats"]["flows"] = ifstats.zmqRecvStats.flows
res["zmqRecvStats"]["dropped_flows"] = ifstats.zmqRecvStats.dropped_flows
res["zmqRecvStats"]["events"] = ifstats.zmqRecvStats.events
res["zmqRecvStats"]["counters"] = ifstats.zmqRecvStats.counters
res["zmqRecvStats"]["zmq_msg_rcvd"] = ifstats.zmqRecvStats.zmq_msg_rcvd
res["zmqRecvStats"]["zmq_msg_drops"] = ifstats.zmqRecvStats.zmq_msg_drops
res["zmqRecvStats"]["zmq_avg_msg_flows"] = math.max(1, ifstats.zmqRecvStats.flows / (ifstats.zmqRecvStats.zmq_msg_rcvd + 1))
res["zmq.num_flow_exports"] = ifstats["zmq.num_flow_exports"] or 0
res["zmq.num_exporters"] = ifstats["zmq.num_exporters"] or 0
res["zmq.drops.export_queue_full"] = ifstats["zmq.drops.export_queue_full"] or 0
res["zmq.drops.flow_collection_drops"] = ifstats["zmq.drops.flow_collection_drops"] or 0
res["zmq.drops.flow_collection_udp_socket_drops"] = ifstats["zmq.drops.flow_collection_udp_socket_drops"] or 0
end
res["tcpPacketStats"] = {}
res["tcpPacketStats"]["retransmissions"] = ifstats.tcpPacketStats.retransmissions
res["tcpPacketStats"]["out_of_order"] = ifstats.tcpPacketStats.out_of_order
res["tcpPacketStats"]["lost"] = ifstats.tcpPacketStats.lost
if interface.isSyslogInterface() then
res["syslog"] = {}
res["syslog"]["tot_events"] = ifstats.syslog.tot_events
res["syslog"]["malformed"] = ifstats.syslog.malformed
res["syslog"]["dispatched"] = ifstats.syslog.dispatched
res["syslog"]["unhandled"] = ifstats.syslog.unhandled
res["syslog"]["alerts"] = ifstats.syslog.alerts
res["syslog"]["host_correlations"] = ifstats.syslog.host_correlations
res["syslog"]["flows"] = ifstats.syslog.flows
end
if(ifstats["profiles"] ~= nil) then
res["profiles"] = ifstats["profiles"]
end
if recording_utils.isAvailable() then
if recording_utils.isEnabled(ifstats.id) then
if recording_utils.isActive(ifstats.id) then
res["traffic_recording"] = "recording"
else
res["traffic_recording"] = "failed"
end
end
if recording_utils.isEnabled(ifstats.id) then
local jobs_info = recording_utils.extractionJobsInfo(ifstats.id)
if jobs_info.ready > 0 then
res["traffic_extraction"] = "ready"
elseif jobs_info.total > 0 then
res["traffic_extraction"] = jobs_info.total
end
res["traffic_extraction_num_tasks"] = jobs_info.total
end
end
end
return res
end
-- ###############################
if(iffilter == "all") then
for cur_ifid, ifname in pairs(interface.getIfNames()) do
-- ifid in the key must be a string or json.encode will think
-- its a lua array and will look for integers starting at one
res[cur_ifid..""] = dumpInterfaceStats(cur_ifid)
end
elseif not isEmptyString(iffilter) then
res = dumpInterfaceStats(iffilter)
else
res = dumpInterfaceStats(ifid)
end
rest_utils.answer(rc, res)