ntopng/scripts/lua/modules/callback_utils.lua
Emanuele Faranda 4cbe45a948
New pro charts features and timeseries api (#1865)
* Integrate modified nvd3 library with zoom and multicharts working support

* Fix count in RRD driver

* Initial chart ajax migration

* Update nvd3 with zoom stack support

* Fix RRD listSeries with directories

* Work in progress graphs

* Compatibility fix

* Initial support for schema based api

* Add missing script

* Fix listSeries for existance check

* Implement topk timseries api

* Implement topk queries and fix labels and formats

* Migrate interface top sender and receivers

* Migrate charts to new API

* Move timeseries list for menu inside respective scripts

* Add support for extended labels

* Fix missing fields while chaning graph resolution

Also Rename drawRRD in drawGraphs

* Fix historical tabs

* Add missing time fence in influx topk

* Add graphs support for custom statistics visualization

* Initial support for graphs statistics footer

* Implement statistics in single graphs for RRD driver

* Move sampling function into the driver and fix graph statistics

* Fix max/min value offset

* Implement influxdb sampling use built-in FILL

* Implement stats and total serie in influxdb driver

* Update nvd3 with multiChart fixes

* Update nvd3 with new multiChart fixes

* RRD driver fixes

* Move metrics type from single metric to schema

* Handle ajax errors and empty data in charts

* Fix flow device interfaces graphs

* Use timeserie label as timeseries dropdown text

* Implement topk aggregation into one data serie

* ts_utils module now provides all the schemas

* Migrate ntop.exist(rrd) to ts_utils.exist

* Fix timeseries dropdown label

* L4 protos fixes for charts

* Migrate getProtoVolume to new API

* Integrate nvd3 fix for tooltip position

* Initial community graphs migration to timseries API

* Fix community timeseries dropdown and historical tabs

* Hide total serie by default

* Remove l4 protos from topk charts
2018-07-18 15:09:19 +02:00

260 lines
7.3 KiB
Lua

--
-- (C) 2017-18 - ntop.org
--
dirs = ntop.getDirs()
package.path = dirs.installdir .. "/scripts/lua/modules/?.lua;" .. package.path
local os_utils = require "os_utils"
local callback_utils = {}
-- ########################################################
-- Iterates available interfaces, excluding PCAP interfaces.
-- Each valid interface is select-ed and passed to the callback.
function callback_utils.foreachInterface(ifnames, condition, callback)
for _,_ifname in pairs(ifnames) do
if(ntop.isShutdown()) then return true end
interface.select(_ifname)
local ifstats = interface.getStats()
if condition == nil or condition(ifstats.id) then
if((ifstats.type ~= "pcap dump") and (ifstats.type ~= "unknown")) then
if callback(_ifname, ifstats, false) == false then
return false
end
end
end
end
return true
end
-- ########################################################
-- An iterator on the C batched API
--
-- batched_function: the function to call
-- field: a string to get the value from a slot
-- function_params : parameters to pass to the function
--
local function getBatchedIterator(batched_function, field, function_params)
local debug_enabled = false
local loaded_elems = nil
local nextSlot = 0
local iterator_finished = false
local first_iteration = true
function_params = function_params or {}
return function()
if (loaded_elems == nil) or table.empty(loaded_elems) then
if loaded_elems ~= nil then
-- that was the first iteration
first_iteration = false
end
if ((nextSlot == 0) or (nextSlot == nil)) and not first_iteration then
iterator_finished = true
end
if iterator_finished then return nil end
-- we need to load new slots from C
if(debug_enabled) then
io.write("getBatchedIterator["..field.."](curSlot=".. nextSlot ..")\n")
end
-- Assumption: nextSlot is always the first parameter
local slot = batched_function(nextSlot, table.unpack(function_params))
if slot == nil then
iterator_finished = true
return nil
end
nextSlot = slot.nextSlot
loaded_elems = slot[field]
if(debug_enabled) then
io.write("getBatchedIterator["..field.."](numElems=".. table.len(loaded_elems) ..", nextSlot=".. nextSlot ..")\n")
end
end
for key, value in pairs(loaded_elems) do
loaded_elems[key] = nil -- pop
return key, value
end
end
end
-- A batched iterator over the local hosts
function callback_utils.getLocalHostsIterator(...)
return getBatchedIterator(interface.getBatchedLocalHostsInfo, "hosts", { ... })
end
-- A batched iterator over the hosts (both local and remote)
function callback_utils.getHostsIterator(...)
return getBatchedIterator(interface.getBatchedHostsInfo, "hosts", { ... })
end
-- A batched iterator over the l2 devices
function callback_utils.getDevicesIterator(...)
return getBatchedIterator(interface.getBatchedMacsInfo, "macs", { ... })
end
-- ########################################################
-- Iterates each active host on the ifname interface for RRD creation.
-- Each host is passed to the callback with some more information.
function callback_utils.foreachLocalRRDHost(ifname, deadline, callback)
interface.select(ifname)
local iterator = callback_utils.getLocalHostsIterator(false --[[ no details ]])
for hostname, hoststats in iterator do
-- Note: this is expensive
local host = interface.getHostInfo(hostname)
if(ntop.isShutdown()) then return true end
if ((deadline ~= nil) and (os.time() >= deadline)) then
-- Out of time
return false
end
if host ~= nil then
if callback(hostname, host--[[hostinfo]]) == false then
return false
end
end
end
return true
end
-- ########################################################
-- Iterates each active host on the ifname interface.
-- Each host is passed to the callback with some more information.
function callback_utils.foreachHost(ifname, deadline, callback)
interface.select(ifname)
local iterator = callback_utils.getHostsIterator(false --[[ no details ]])
for hostname, hoststats in iterator do
if(ntop.isShutdown()) then return true end
if ((deadline ~= nil) and (os.time() >= deadline)) then
-- Out of time
return false
end
if callback(hostname, hoststats) == false then
return false
end
end
return true
end
-- ########################################################
-- Iterates each active host on the ifname interface.
-- Each host is passed to the callback with some more information.
function callback_utils.foreachLocalHost(ifname, deadline, callback)
interface.select(ifname)
local iterator = callback_utils.getLocalHostsIterator(false --[[ no details ]])
for hostname, hoststats in iterator do
if(ntop.isShutdown()) then return true end
if ((deadline ~= nil) and (os.time() >= deadline)) then
-- Out of time
return false
end
if callback(hostname, hoststats) == false then
return false
end
end
return true
end
-- Iterates each device on the ifname interface.
-- Each device is passed to the callback with some more information.
function callback_utils.foreachDevice(ifname, deadline, callback)
interface.select(ifname)
local devices_stats = callback_utils.getDevicesIterator()
for devicename, devicestats in devices_stats do
if(ntop.isShutdown()) then return true end
devicename = hostinfo2hostkey(devicestats) -- make devicename the combination of mac address and vlan
if ((deadline ~= nil) and (os.time() >= deadline)) then
-- Out of time
return false
end
if callback(devicename, devicestats) == false then
return false
end
end
return true
end
-- ########################################################
function callback_utils.harverstOldRRDFiles(ifname)
-- currently this is only implemented for old devices files. It should actually be implemented for other rrds as well
local rrd_max_days = ntop.getPref("ntopng.prefs.rrd_files_retention")
if isEmptyString(rrd_max_days) then rrd_max_days = 30 end
ntop.deleteOldRRDs(getInterfaceId(ifname), tonumber(rrd_max_days) * 60 * 60 * 24)
end
-- ########################################################
function callback_utils.uploadTSdata()
local url = ntop.getCache("ntopng.prefs.ts_post_data_url")
if((url == nil) or (url == "")) then
return
end
while(true) do
local name_id = ntop.lpopCache("ntopng.influx_file_queue")
local ret
if((name_id == nil) or (name_id == "")) then
break
end
if(tonumber(name_id) == nil) then
print("[ERROR] Invalid name "..name_id.."\n")
break
end
local fname = os_utils.fixPath(dirs.workingdir .. "/" .. getInterfaceId(ifname) .. "/ts_export/" .. name_id)
-- Delete the file after POST
local delete_file_after_post = true
ret = ntop.postHTTPTextFile("", "", url .. "/write?db=ntopng", fname, delete_file_after_post)
--print(fname .. "\n")
if(ret ~= true) then
print("[ERROR] POST of "..fname.." failed\n")
-- delete the file manually
os.remove(fname)
end
end
end
-- ########################################################
return callback_utils