Implements host lambda functions

This commit is contained in:
Simone Mainardi 2019-07-08 12:21:37 +02:00
parent 5d99489d70
commit 8ad9e6b336
2 changed files with 100 additions and 87 deletions

View file

@ -18,92 +18,10 @@ function setup(str_granularity)
print("alert.lua:setup("..str_granularity..") called\n")
ifname = interface.setActiveInterfaceId(tonumber(interface.getId()))
config_alerts = getHostsConfiguredAlertThresholds(ifname, str_granularity)
end
-- #################################################################
local function cached_val_key(metric_name, granularity)
return string.format("%s:%s", metric_name, granularity)
end
-- #################################################################
local function delta_val(metric_name, granularity, curr_val)
local key = cached_val_key(metric_name, granularity)
-- Read cached value and purify it
local prev_val = host.getCachedAlertValue(key)
prev_val = tonumber(prev_val) or 0
-- Save the value for the next round
host.setCachedAlertValue(key, tostring(curr_val))
-- Compute the delta
return curr_val - prev_val
end
-- #################################################################
local function application_bytes(info, application_name)
local curr_val = 0
if info["ndpi"] and info["ndpi"][application_name] then
curr_val = info["ndpi"][application_name]["bytes.sent"] + info["ndpi"][application_name]["bytes.rcvd"]
end
return curr_val
end
-- #################################################################
function active(metric_name, info, granularity)
return delta_val(metric_name, granularity, info["total_activity_time"])
end
-- #################################################################
function bytes(metric_name, info, granularity)
return delta_val(metric_name, granularity, info["bytes.sent"] + info["bytes.rcvd"])
end
-- #################################################################
function packets(metric_name, info, granularity)
return delta_val(metric_name, granularity, info["packets.sent"] + info["packets.rcvd"])
end
-- #################################################################
function flows(metric_name, info, granularity)
return delta_val(metric_name, granularity, info["total_flows.as_client"] + info["total_flows.as_server"])
end
-- #################################################################
function idle(metric_name, info, granularity)
return delta_val(metric_name, granularity, os.time() - info["seen.last"])
end
-- #################################################################
function dns(metric_name, info, granularity)
return delta_val(metric_name, granularity, application_bytes(info, "DNS"))
end
-- #################################################################
function p2p(metric_name, info, granularity)
local tot_p2p = application_bytes(info, "eDonkey") + application_bytes(info, "BitTorrent") + application_bytes(info, "Skype")
return delta_val(metric_name, granularity, tot_p2p)
end
-- #################################################################
function throughput(metric_name, info, granularity)
local duration = granularity2sec(granularity)
return delta_val(metric_name, granularity, info["bytes.sent"] + info["bytes.rcvd"]) * 8 / duration
-- Load the threshold checking functions
package.path = dirs.installdir .. "/scripts/callbacks/interface/alerts/host/?.lua;" .. package.path
checks = require("check")
end
-- #################################################################
@ -126,9 +44,9 @@ local function checkHostAlertsThreshold(host_key, host_info, granularity, rules)
if(true) then
-- This is where magic happens: load() evaluates the string
local what = 'return('..function_name..'(metric_name, h_info, threshold_gran))'
local what = 'return checks.'..function_name..'(metric_name, h_info, threshold_gran)'
-- tprint(what)
local func, err = load(what)
local func, err = load(what, 't')
if func then
local ok, value = pcall(func)