Added sflow usage timeserie statistics (#8112)

This commit is contained in:
Matteo Biscosi 2023-12-20 11:21:05 +00:00
parent 14ab264c1d
commit f2bb5923b0
3 changed files with 11 additions and 186 deletions

View file

@ -333,189 +333,6 @@ end
-- ########################################################
function ts_dump.sflow_device_update_rrds(when, ifstats, verbose)
local flowdevs = interface.getSFlowDevices()
-- Return in case of view interface, no timeseries for view interface,
-- already on the viewed interface.
if ifstats.isView then
return
end
local alerts_api = require "alerts_api"
for interface_id, device_list in pairs(flowdevs or {}) do
for flow_device_ip, _value in pairs(device_list, asc) do
local ports = interface.getSFlowDeviceInfo(flow_device_ip)
if (verbose) then
print("[" .. __FILE__() .. ":" .. __LINE__() .. "] Processing sFlow probe " .. flow_device_ip .. "\n")
end
for port_idx, port_value in pairs(ports) do
if ifstats.has_seen_ebpf_events then
-- This is actualy an event exporter
local dev_ifname = format_utils.formatExporterInterface(port_idx, port_value)
ts_utils.append("evexporter_iface:traffic", {
ifid = ifstats.id,
exporter = flow_device_ip,
ifname = dev_ifname,
bytes_sent = port_value.ifOutOctets,
bytes_rcvd = port_value.ifInOctets
}, when)
else
ts_utils.append("sflowdev_port:traffic", {
ifid = ifstats.id,
device = flow_device_ip,
port = port_idx,
bytes_sent = port_value.ifOutOctets,
bytes_rcvd = port_value.ifInOctets
}, when)
local exporter_config = getFlowDevInterfaceConfig(flow_device_ip, port_idx, ifstats.id)
local out_bytes_percentage = alerts_api.interface_delta_val(
"interface_usage_out_bytes_sflow" .. flow_device_ip .. '_' .. port_idx, "5mins",
port_value.ifOutOctets * 8, true)
-- Now divide the difference by the time, to have the right value
out_bytes_percentage = out_bytes_percentage / 300
-- Round the value correctly
out_bytes_percentage = math.floor((out_bytes_percentage / exporter_config.uplink_speed * 100) + 0.5)
-- Now for the in_bytes
local in_bytes_percentage = alerts_api.interface_delta_val(
"interface_usage_in_bytes_sflow" .. flow_device_ip .. '_' .. port_idx, "5mins",
port_value.ifInOctets * 8, true)
-- Now divide the difference by the time, to have the right value
in_bytes_percentage = in_bytes_percentage / 300
-- Round the value correctly
in_bytes_percentage = math.floor((in_bytes_percentage / exporter_config.downlink_speed * 100) + 0.5)
-- if debug then
-- traceError(TRACE_NORMAL, TRACE_CONSOLE,
-- "interface_usage_out_bytes" .. flow_device_ip .. '_' .. port_idx .. ', out bit perc.: ' ..
-- out_bytes_percentage .. ' | in bit perc.: ' .. in_bytes_percentage)
-- end
if in_bytes_percentage > 100 then
in_bytes_percentage = 100
end
if out_bytes_percentage > 100 then
out_bytes_percentage = 100
end
-- Traffic
ts_utils.append("sflowdev_port:usage", {
ifid = ifstats.id,
device = flow_device_ip,
port = port_idx,
uplink = out_bytes_percentage,
downlink = in_bytes_percentage
}, when)
end
end
end
end
end
-- ########################################################
function ts_dump.flow_device_update_rrds(when, ifstats, verbose)
local flowdevs = interface.getFlowDevices() -- Flow, not sFlow here
local debug = false
-- Return in case of view interface, no timeseries for view interface,
-- already on the viewed interface.
if ifstats.isView then
return
end
local alerts_api = require "alerts_api"
for interface_id, device_list in pairs(flowdevs or {}) do
for flow_device_ip, dev_resolved_name in pairsByValues(device_list, asc) do
local ports = interface.getFlowDeviceInfo(flow_device_ip)
local bytes_sent = 0
local bytes_rcvd = 0
if (verbose) then
print("[" .. __FILE__() .. ":" .. __LINE__() .. "] Processing flow probe " .. flow_device_ip .. "\n")
end
for port_idx, port_value in pairs(ports) do
-- Traffic
ts_utils.append("flowdev_port:traffic", {
ifid = ifstats.id,
device = flow_device_ip,
port = port_idx,
bytes_sent = port_value["bytes.out_bytes"],
bytes_rcvd = port_value["bytes.in_bytes"]
}, when)
bytes_sent = bytes_sent + port_value["bytes.out_bytes"]
bytes_rcvd = bytes_rcvd + port_value["bytes.in_bytes"]
-- nDPI
for proto_name, proto_stats in pairs(port_value["ndpi"]) do
ts_utils.append("flowdev_port:ndpi", {
ifid = ifstats.id,
device = flow_device_ip,
port = port_idx,
protocol = proto_name,
bytes_sent = proto_stats["bytes.sent"],
bytes_rcvd = proto_stats["bytes.rcvd"]
}, when)
end
local exporter_config = getFlowDevInterfaceConfig(flow_device_ip, port_idx, ifstats.id)
local out_bytes_percentage = alerts_api.interface_delta_val(
"interface_usage_out_bytes" .. flow_device_ip .. '_' .. port_idx, "5mins",
port_value["bytes.out_bytes"] * 8, true)
-- Now divide the difference by the time, to have the right value
out_bytes_percentage = out_bytes_percentage / 300
-- Round the value correctly
out_bytes_percentage = math.floor((out_bytes_percentage / exporter_config.uplink_speed * 100) + 0.5)
-- Now for the in_bytes
local in_bytes_percentage = alerts_api.interface_delta_val(
"interface_usage_in_bytes" .. flow_device_ip .. '_' .. port_idx, "5mins",
port_value["bytes.in_bytes"] * 8, true)
-- Now divide the difference by the time, to have the right value
in_bytes_percentage = in_bytes_percentage / 300
-- Round the value correctly
in_bytes_percentage = math.floor((in_bytes_percentage / exporter_config.downlink_speed * 100) + 0.5)
-- if debug then
-- traceError(TRACE_NORMAL, TRACE_CONSOLE,
-- "interface_usage_out_bytes" .. flow_device_ip .. '_' .. port_idx .. ', out bit perc.: ' ..
-- out_bytes_percentage .. ' | in bit perc.: ' .. in_bytes_percentage)
-- end
if in_bytes_percentage > 100 then
in_bytes_percentage = 100
end
if out_bytes_percentage > 100 then
out_bytes_percentage = 100
end
-- Traffic
ts_utils.append("flowdev_port:usage", {
ifid = ifstats.id,
device = flow_device_ip,
port = port_idx,
uplink = out_bytes_percentage,
downlink = in_bytes_percentage
}, when)
end
-- Total Traffic
ts_utils.append("flowdev:traffic", {
ifid = ifstats.id,
device = flow_device_ip,
bytes_sent = bytes_sent,
bytes_rcvd = bytes_rcvd
}, when)
end
end
end
-- ########################################################
function ts_dump.getConfig()
local config = {}
@ -1141,8 +958,11 @@ function ts_dump.run_5min_dump(_ifname, ifstats, config, when, verbose)
-- Create RRDs for flow and sFlow probes
if (config.flow_devices_rrd_creation == "1" and ntop.isEnterpriseM()) then
ts_dump.sflow_device_update_rrds(when, ifstats, verbose)
ts_dump.flow_device_update_rrds(when, ifstats, verbose)
package.path = dirs.installdir .. "/scripts/lua/pro/modules/timeseries/callbacks/?.lua;" .. package.path
local sflow_timeseries = require "sflow_timeseries"
local netflow_timeseries = require "netflow_timeseries"
sflow_timeseries.sflow_update(when, ifstats, verbose)
netflow_timeseries.netflow_update(when, ifstats, verbose)
end
-- Save Host Pools stats every 5 minutes