Implement ntopng plugins

Plugins are a convenient way to group together related lua scripts.
Their primary use case is to group user scripts and their alert/status
definition.
The builtin ntopng user scripts and definitions are now
packed into plugins directories. In future, we will support loading of
user created plugins.
Plugins are loaded at startup into some runtime directories and then
used. Other changes provided by this commit include:

- Add sample flow logger plugin
- Initial support for system user scripts
- Rename edge to threshold
- Migrate system probes to user scripts/plugins
- Migrate scripts to more explicit alerts_api.checkThresholdAlert api
This commit is contained in:
emanuele-f 2019-12-04 11:34:18 +01:00
parent df245fad3a
commit a3432e00e8
218 changed files with 2070 additions and 2097 deletions

View file

@ -0,0 +1,127 @@
--
-- (C) 2019 - ntop.org
--
local ts_utils = require("ts_utils_core")
local script = {
-- This module is enabled by default
default_enabled = true,
-- No default configuration is provided
default_value = {},
-- See below
hooks = {},
}
-- ##############################################
function script.setup()
-- This module is always enabled
return(true)
end
-- ##############################################
-- Defines an hook which is executed every minute
function script.hooks.min(params)
if params.ts_enabled then
local ifid = getSystemInterfaceId()
local stats = script.getStats()
local hits_key = "ntopng.cache.redis.stats"
local json = require("dkjson")
local hits_stats = ntop.getCacheStats()
local old_hits_stats = ntop.getCache(hits_key)
if(not isEmptyString(old_hits_stats)) then
old_hits_stats = json.decode(old_hits_stats) or {}
else
old_hits_stats = {}
end
ts_utils.append("redis:memory", {ifid = ifid, resident_bytes = stats["memory"]}, when)
ts_utils.append("redis:keys", {ifid = ifid, num_keys = stats["dbsize"]}, when)
for key, val in pairs(hits_stats) do
if(old_hits_stats[key] ~= nil) then
local delta = math.max(val - old_hits_stats[key], 0)
-- Dump the delta value as a gauge
ts_utils.append("redis:hits", {ifid = ifid, command = key, num_calls = delta}, when)
end
end
ntop.setCache(hits_key, json.encode(hits_stats))
end
end
-- ##############################################
function script.getRedisStatus()
local redis = ntop.getCacheStatus()
local redis_info = redis["info"]
local res = {}
for _, k in pairs(redis_info:split("\r\n")) do
local k = k:split(":")
if k then
local v_k = k[1]
local v = tonumber(k[2]) or k[2]
res[v_k] = v
end
end
if redis["dbsize"] then
res["dbsize"] = redis["dbsize"]
end
return res
end
-- ##############################################
local function getHealth(redis_status)
local health = "green"
if redis_status["aof_enabled"] and redis_status["aof_enabled"] ~= 0 then
-- If here the use of Redis Append Only File (AOF) is enabled
-- so we should check for its errors
if redis_stats["aof_last_bgrewrite_status"] ~= "ok" or redis_status["aof_last_write_status"] ~= "ok" then
health = "red"
end
end
if redis_status["rdb_last_bgsave_status"] ~= "ok" then
health = "red"
end
return health
end
-- ##############################################
function script.getStats()
local redis_status = script.getRedisStatus()
local res = {
-- used_memory_rss: Number of bytes that Redis allocated
-- as seen by the operating system (a.k.a resident set size).
-- This is the number reported by tools such as top(1) and ps(1)
memory = redis_status["used_memory_rss"],
-- The number of keys in the database
dbsize = redis_status["dbsize"],
-- Health
health = getHealth(redis_status)
}
return res
end
-- ##############################################
return(script)