ntopng/scripts/lua/modules/callback_utils.lua
2017-09-06 16:02:32 +02:00

122 lines
3.7 KiB
Lua

--
-- (C) 2017 - ntop.org
--
dirs = ntop.getDirs()
package.path = dirs.installdir .. "/scripts/lua/modules/?.lua;" .. package.path
require "lua_utils"
require "flow_aggregation_utils"
local callback_utils = {}
-- ########################################################
function callback_utils.print(file, line, message)
print("["..file.."]:["..line.."] "..message)
end
-- ########################################################
-- 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
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
-- ########################################################
-- Iterates each active host on the ifname interface.
-- Each host is passed to the callback with some more information.
function callback_utils.foreachHost(ifname, verbose, localHostsOnly, callback, deadline)
local hostbase
local hosts_stats
interface.select(ifname)
if(localHostsOnly) then
hosts_stats = interface.getLocalHostsInfo(false)
else
hosts_stats = interface.getHostsInfo(false)
end
if hosts_stats == nil then
hosts_stats = {hosts = {}}
end
hosts_stats = hosts_stats["hosts"]
for hostname, hoststats in pairs(hosts_stats) do
local host = interface.getHostInfo(hostname)
if ((deadline ~= nil) and (os.time() >= deadline)) then
-- Out of time
return false
end
if(host == nil) then
if(verbose) then print("\n["..__FILE__()..":"..__LINE__().."] NULL host "..hostname.." !!!!\n") end
else
if(verbose) then
print ("["..__FILE__()..":"..__LINE__().."] [" .. hostname .. "][local: ")
print(tostring(host["localhost"]))
print("]" .. (hoststats["bytes.sent"]+hoststats["bytes.rcvd"]) .. "]\n")
end
if(host.localhost) then
local keypath = getPathFromKey(hostname)
hostbase = fixPath(dirs.workingdir .. "/" .. getInterfaceId(ifname) .. "/rrd/" .. keypath)
if(not(ntop.exists(hostbase))) then
if(verbose) then print("\n["..__FILE__()..":"..__LINE__().."] Creating base directory ", hostbase, '\n') end
ntop.mkdir(hostbase)
end
else
hostbase = nil
end
if callback(hostname, host--[[hostinfo]], hostbase--[[base RRD host directory]], verbose) == false then
return false
end
end
end
return true
end
-- ########################################################
function callback_utils.harverstExpiredMySQLFlows(ifname, mysql_retention, verbose)
interface.select(ifname)
local dbtables = {"flowsv4", "flowsv6"}
if useAggregatedFlows() then
dbtables[#dbtables+1] = "aggrflowsv4"
dbtables[#dbtables+1] = "aggrflowsv6"
end
for _, tb in pairs(dbtables) do
local sql = "DELETE FROM "..tb.." where FIRST_SWITCHED < "..mysql_retention
sql = sql.." AND (INTERFACE_ID = "..getInterfaceId(ifname)..")"
sql = sql.." AND (NTOPNG_INSTANCE_NAME='"..ntop.getPrefs()["instance_name"].."' OR NTOPNG_INSTANCE_NAME IS NULL OR NTOPNG_INSTANCE_NAME='')"
interface.execSQLQuery(sql)
if(verbose) then io.write(sql.."\n") end
end
end
-- ########################################################
return callback_utils