Reworks plugin loading and structure

Implements #4358
This commit is contained in:
Simone Mainardi 2020-09-15 11:33:50 +02:00
parent 81f55a02a4
commit f7e1ea9709
164 changed files with 106 additions and 84 deletions

View file

@ -0,0 +1,10 @@
--
-- (C) 2019-20 - ntop.org
--
return {
title = "Redis Monitor",
description = "Monitors Redis health and performance",
author = "ntop",
dependencies = {},
}

View file

@ -0,0 +1,28 @@
local schema
local ts_utils = require("ts_utils_core")
schema = ts_utils.newSchema("redis:memory", {
metrics_type = ts_utils.metrics.gauge,
is_system_schema = true,
step = 60,
})
schema:addTag("ifid")
schema:addMetric("resident_bytes")
schema = ts_utils.newSchema("redis:keys", {
metrics_type = ts_utils.metrics.gauge,
is_system_schema = true,
step = 60,
})
schema:addTag("ifid")
schema:addMetric("num_keys")
-- Cache
schema = ts_utils.newSchema("redis:hits", {
metrics_type = ts_utils.metrics.gauge,
is_system_schema = true,
step = 60,
})
schema:addTag("ifid")
schema:addTag("command")
schema:addMetric("num_calls")

View file

@ -0,0 +1,144 @@
--
-- (C) 2019-20 - ntop.org
--
local ts_utils = require("ts_utils_core")
local user_scripts = require("user_scripts")
local script = {
-- Script category
category = user_scripts.script_categories.system,
-- This module is enabled by default
default_enabled = true,
-- No default configuration is provided
default_value = {},
-- See below
hooks = {},
gui = {
i18n_title = "system_stats.redis.redis_monitor",
i18n_description = "system_stats.redis.redis_monitor_description",
},
}
-- ##############################################
-- 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
if stats["memory"] then
ts_utils.append("redis:memory", {ifid = ifid, resident_bytes = stats["memory"]}, when)
end
if stats["dbsize"] then
ts_utils.append("redis:keys", {ifid = ifid, num_keys = stats["dbsize"]}, when)
end
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 ntop.isWindows() then
-- See Windows note in script.getStats()
return health
end
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_status["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
-- ##############################################
-- NOTE: on Windows, some stats are missing from script.getRedisStatus():
-- - aof_last_bgrewrite_status
-- - aof_last_write_status
-- - rdb_last_bgsave_status
-- - dbsize
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)

View file

@ -0,0 +1,18 @@
--
-- (C) 2019-20 - ntop.org
--
local dirs = ntop.getDirs()
package.path = dirs.installdir .. "/scripts/lua/modules/?.lua;" .. package.path
require "lua_utils"
local user_scripts = require("user_scripts")
local json = require "dkjson"
local redis = user_scripts.loadModule(getSystemInterfaceId(), user_scripts.script_types.system, "system", "redis_monitor")
sendHTTPContentTypeHeader('application/json')
local stats = redis.getStats()
print(json.encode(stats))

View file

@ -0,0 +1,126 @@
--
-- (C) 2019-20 - ntop.org
--
local dirs = ntop.getDirs()
package.path = dirs.installdir .. "/scripts/lua/modules/?.lua;" .. package.path
require "lua_utils"
local format_utils = require("format_utils")
local json = require("dkjson")
local plugins_utils = require("plugins_utils")
sendHTTPContentTypeHeader('application/json')
-- ################################################
local currentPage = _GET["currentPage"]
local perPage = _GET["perPage"]
local sortColumn = _GET["sortColumn"]
local sortOrder = _GET["sortOrder"]
local cmd_ids_filter = _GET["custom_hosts"]
local sortPrefs = "redis_commands_data"
-- ################################################
if isEmptyString(sortColumn) or sortColumn == "column_" then
sortColumn = getDefaultTableSort(sortPrefs)
else
if((sortColumn ~= "column_")
and (sortColumn ~= "")) then
tablePreferences("sort_"..sortPrefs, sortColumn)
end
end
if isEmptyString(_GET["sortColumn"]) then
sortOrder = getDefaultTableSortOrder(sortPrefs, true)
end
if((_GET["sortColumn"] ~= "column_")
and (_GET["sortColumn"] ~= "")) then
tablePreferences("sort_order_"..sortPrefs, sortOrder, true)
end
if(currentPage == nil) then
currentPage = 1
else
currentPage = tonumber(currentPage)
end
if(perPage == nil) then
perPage = getDefaultTableSize()
else
perPage = tonumber(perPage)
tablePreferences("rows_number", perPage)
end
local sOrder = ternary(sortOrder == "asc", asc_insensitive, rev_insensitive)
local to_skip = (currentPage-1) * perPage
-- ################################################
if(cmd_ids_filter) then
cmd_ids_filter = swapKeysValues(string.split(cmd_ids_filter, ",") or {cmd_ids_filter})
end
local commands_stats = ntop.getCacheStats() or {}
local totalRows = 0
local sort_to_key = {}
for command, hits in pairs(commands_stats) do
if(cmd_ids_filter and (cmd_ids_filter[command] == nil)) then
goto continue
end
if(sortColumn == "column_command") then
sort_to_key[command] = command
else
sort_to_key[command] = hits
end
totalRows = totalRows + 1
::continue::
end
-- ################################################
local res = {}
local i = 0
local sys_ifaceid = getSystemInterfaceId()
local charts_available = plugins_utils.timeseriesCreationEnabled()
for key in pairsByValues(sort_to_key, sOrder) do
if i >= to_skip + perPage then
break
end
if (i >= to_skip) then
local chart = ""
local value = commands_stats[key]
if(charts_available) then
chart = '<a href="?page=historical&redis_command='..key..'&ts_schema=redis:hits"><i class=\'fas fa-chart-area fa-lg\'></i></a>'
end
res[#res + 1] = {
column_key = key,
column_command = string.upper(string.sub(key, 5)),
column_chart = chart,
column_hits = value,
}
end
i = i + 1
end
-- ################################################
local result = {}
result["perPage"] = perPage
result["currentPage"] = currentPage
result["totalRows"] = totalRows
result["data"] = res
result["sort"] = {{sortColumn, sortOrder}}
print(json.encode(result))

View file

@ -0,0 +1,12 @@
return {
label = "Redis",
script = "redis_stats.lua",
sort_order = 1700,
menu_entry = {key = "redis_monitor", i18n_title = "Redis", section = "system_stats"},
is_shown = function()
local user_scripts = require("user_scripts")
return(user_scripts.isSystemScriptEnabled("redis_monitor"))
end
}

View file

@ -0,0 +1,210 @@
--
-- (C) 2013-20 - ntop.org
--
local dirs = ntop.getDirs()
package.path = dirs.installdir .. "/scripts/lua/modules/?.lua;" .. package.path
if((dirs.scriptdir ~= nil) and (dirs.scriptdir ~= "")) then package.path = dirs.scriptdir .. "/lua/modules/?.lua;" .. package.path end
require "lua_utils"
local page_utils = require("page_utils")
local alert_consts = require("alert_consts")
local plugins_utils = require("plugins_utils")
local graph_utils = require("graph_utils")
local alert_utils = require("alert_utils")
local charts_available = plugins_utils.timeseriesCreationEnabled()
if not isAllowedSystemInterface() then
return
end
sendHTTPContentTypeHeader('text/html')
page_utils.set_active_menu_entry(page_utils.menu_entries.redis_monitor)
dofile(dirs.installdir .. "/scripts/lua/inc/menu.lua")
local page = _GET["page"] or "overview"
local url = plugins_utils.getUrl("redis_stats.lua") .. "?ifid=" .. getInterfaceId(ifname)
page_utils.print_navbar("Redis", url,
{
{
active = page == "overview" or not page,
page_name = "overview",
label = "<i class=\"fas fa-lg fa-home\"></i>",
},
{
active = page == "stats",
page_name = "stats",
label = "<i class=\"fas fa-lg fa-wrench\"></i>",
},
{
hidden = not charts_available,
active = page == "historical",
page_name = "historical",
label = "<i class='fas fa-lg fa-chart-area'></i>",
},
}
)
-- #######################################################
if(page == "overview") then
local fa_external = "<i class='fas fa-external-link-alt'></i>"
local tags = {ifid=getSystemInterfaceId()}
print("<table class=\"table table-bordered table-striped\">\n")
if not ntop.isWindows() then
-- NOTE: on Windows, some stats are missing from script.getRedisStatus()
print("<tr><td nowrap width='30%'><b>".. i18n("system_stats.health") .."</b><br><small>"..i18n("system_stats.redis.short_desc_redis_health").."</small></td><td></td><td><span id='throbber' class='spinner-border redis-info-load spinner-border-sm text-primary' role='status'><span class='sr-only'>Loading...</span></span> <span id=\"redis-health\"></span></td></tr>\n")
end
print("<tr><td nowrap width='30%'><b>".. i18n("about.ram_memory") .."</b><br><small>"..i18n("system_stats.redis.short_desc_redis_ram_memory").."</small></td>")
print("<td class='text-center' width=5%>")
print(ternary(charts_available, "<A HREF='"..url.."&page=historical&ts_schema=redis:memory'><i class='fas fa-lg fa-chart-area'></i></A>", ""))
print("</td><td><span id='throbber' class='spinner-border redis-info-load spinner-border-sm text-primary' role='status'><span class='sr-only'>Loading...</span></span> <span id=\"redis-info-memory\"></span></td></tr>\n")
if not ntop.isWindows() then
print("<tr><td nowrap width='30%'><b>".. i18n("system_stats.redis.redis_keys") .."</b><br><small>"..i18n("system_stats.redis.short_desc_redis_keys").."</small></td>")
print("<td class='text-center' width=5%>")
print(ternary(charts_available, "<A HREF='"..url.."&page=historical&ts_schema=redis:keys'><i class='fas fa-chart-area fa-lg'></i></A>", ""))
print("</td><td><span id='throbber' class='spinner-border redis-info-load spinner-border-sm text-primary' role='status'><span class='sr-only'>Loading...</span></span> <span id=\"redis-info-keys\"></span></td></tr>\n")
end
print[[<script>
var last_keys, last_memory
var health_descr = {
]]
print('"green" : {"status" : "<span class=\'badge badge-success\'>'..i18n("system_stats.redis.redis_health_green")..'</span>", "descr" : "<small>'..i18n("system_stats.redis.redis_health_green_descr")..'</small>"},')
print('"red" : {"status" : "<span class=\'badge badge-danger\'>'..i18n("system_stats.redis.redis_health_red")..'</span>", "descr" : "<small>'..i18n("system_stats.redis.redis_health_red_descr", {product = ntop.getInfo()["product"]})..'</small>"},')
print[[
};
function refreshRedisStats() {
$.get("]] print(plugins_utils.getUrl("get_redis_info.lua")) print[[", function(info) {
$(".redis-info-load").hide();
if(typeof info.health !== "undefined" && health_descr[info.health]) {
$("#redis-health").html(health_descr[info.health]["status"] + "<br>" + health_descr[info.health]["descr"]);
}
if(typeof info.dbsize !== "undefined") {
$("#redis-info-keys").html(NtopUtils.formatValue(info.dbsize) + " ");
if(typeof last_keys !== "undefined")
$("#redis-info-keys").append(NtopUtils.drawTrend(info.dbsize, last_keys));
last_keys = info.dbsize;
}
if(typeof info.memory !== "undefined") {
$("#redis-info-memory").html(NtopUtils.bytesToVolume(info.memory) + " ");
if(typeof last_memory !== "undefined")
$("#redis-info-memory").append(NtopUtils.drawTrend(info.memory, last_memory));
last_memory = info.memory;
}
}).fail(function() {
$(".redis-info-load").hide();
});
}
setInterval(refreshRedisStats, 5000);
refreshRedisStats();
</script>
]]
print("</table>\n")
elseif(page == "stats") then
print [[
<div id="table-redis-stats"></div>
<script type='text/javascript'>
$("#table-redis-stats").datatable({
title: "",
perPage: 100,
hidePerPage: true,
url: "]] print(plugins_utils.getUrl("get_redis_stats.lua")) print(ntop.getHttpPrefix()) print[[",
columns: [
{
field: "column_key",
hidden: true,
css: {
width: '15%',
}
}, {
field: "column_command",
sortable: true,
title: "]] print(i18n("please_wait_page.command")) print[[",
css: {
width: '15%',
}
}, {
title: "]] print(i18n("chart")) print[[",
field: "column_chart",
hidden: ]] if not charts_available then print("true") else print("false") end print[[,
sortable: false,
css: {
textAlign: 'center',
width: '5%',
}
}, {
title: "]] print(i18n("system_stats.redis.tot_calls")) print[[",
field: "column_hits",
sortable: true,
css: {
textAlign: 'right'
}
}
], tableCallback: function() {
datatableInitRefreshRows($("#table-redis-stats"), "column_key", 5000, {"column_hits": addCommas});
}
});
</script>
]]
elseif(page == "historical" and charts_available) then
local ts_utils = require("ts_utils")
local schema = _GET["ts_schema"] or "redis:memory"
local selected_epoch = _GET["epoch"] or ""
local tags = {ifid = getSystemInterfaceId(), command = _GET["redis_command"]}
url = url.."&page=historical"
local timeseries = {
{schema = "redis:memory", label = i18n("about.ram_memory")},
{schema = "redis:keys", label = i18n("system_stats.redis.redis_keys")},
{separator=1, label=i18n("system_stats.redis.commands")},
}
-- Populate individual commands timeseries
local series = ts_utils.listSeries("redis:hits", {ifid = getSystemInterfaceId()}, 0)
if(series) then
for _, serie in pairsByField(series, "command", asc) do
timeseries[#timeseries + 1] = {
schema = "redis:hits",
label = i18n("system_stats.redis.command_hits", {cmd = string.upper(string.sub(serie.command, 5))}),
extra_params = {redis_command = serie.command},
metrics_labels = {i18n("graphs.num_calls")},
}
end
end
graph_utils.drawGraphs(getSystemInterfaceId(), schema, tags, _GET["zoom"], url, selected_epoch, {
top_redis_hits = "top:redis:hits",
timeseries = timeseries,
})
elseif((page == "alerts") and isAdministrator()) then
local old_ifname = ifname
interface.select(getSystemInterfaceId())
_GET["ifid"] = getSystemInterfaceId()
-- _GET["entity"] = alert_consts.alertEntity("redis")
alert_utils.drawAlerts()
interface.select(old_ifname)
end
-- #######################################################
dofile(dirs.installdir .. "/scripts/lua/inc/footer.lua")