Additional work on the alerts manager

This commit is contained in:
Simone Mainardi 2016-08-31 13:08:20 +02:00
parent 19091e2a79
commit f73ffc65ec
7 changed files with 414 additions and 16 deletions

View file

@ -11,6 +11,8 @@ sendHTTPHeader('text/html; charset=iso-8859-1')
currentPage = _GET["currentPage"]
perPage = _GET["perPage"]
status = _GET["alert_status"]
alertsImpl = _GET["alerts_impl"]
if(currentPage == nil) then
currentPage = 1
@ -24,24 +26,51 @@ else
perPage = tonumber(perPage)
end
engaged = false
if status == "engaged" then
engaged = true
end
initial_idx = (currentPage-1)*perPage
interface.select(ifname)
alerts = interface.getQueuedAlerts(initial_idx, perPage)
if alertsImpl == "new" then
alerts = interface.getAlerts(initial_idx, perPage, engaged)
num_alerts = interface.getNumAlerts(engaged)
else
alerts = interface.getQueuedAlerts(initial_idx, perPage)
num_alerts = interface.getNumQueuedAlerts()
end
print ("{ \"currentPage\" : " .. currentPage .. ",\n \"data\" : [\n")
total = 0
if alerts == nil then alerts = {} end
for _key,_value in pairs(alerts) do
for _key,_value in ipairs(alerts) do
if(total > 0) then print(",\n") end
values = split(string.gsub(_value, "\n", ""), "|")
column_id = "<form class=form-inline style='margin-bottom: 0px;' method=get action='"..ntop.getHttpPrefix().."/lua/show_alerts.lua'><input type=hidden name=id_to_delete value="..(initial_idx+tonumber(_key)).."><input type=hidden name=currentPage value=".. currentPage .."><input type=hidden name=perPage value=".. perPage .."><button class='btn btn-default btn-xs' type='submit'><input id=csrf name=csrf type=hidden value='"..ntop.getRandomCSRFValue().."' /><i type='submit' class='fa fa-trash-o'></i></button></form>"
column_date = os.date("%c", values[1])
column_severity = alertSeverityLabel(tonumber(values[2]))
column_type = alertTypeLabel(tonumber(values[4]))
column_msg = values[5]
if alertsImpl ~= "new" then
values = split(string.gsub(_value, "\n", ""), "|")
alert_id = (initial_idx+tonumber(_key))
column_date = os.date("%c", values[1])
column_severity = alertSeverityLabel(tonumber(values[2]))
column_type = alertTypeLabel(tonumber(values[4]))
column_msg = values[5]
else
alert_id = _value["rowid"]
column_date = os.date("%c", _value["alert_tstamp"])
if tonumber(_value["alert_tstamp_end"]) ~= nil then
local duration = secondsToTime(tonumber(_value["alert_tstamp_end"]) - tonumber(_value["alert_tstamp"]))
column_date = duration.." ending on "..os.date("%c", _value["alert_tstamp_end"])
end
column_severity = alertSeverityLabel(tonumber(_value["alert_severity"]))
column_type = alertTypeLabel(tonumber(_value["alert_type"]))
column_msg = _value["alert_json"]
end
column_id = "<form class=form-inline style='margin-bottom: 0px;' method=get action='"..ntop.getHttpPrefix().."/lua/show_alerts.lua'><input type=hidden name=id_to_delete value="..alert_id.."><input type=hidden name=currentPage value=".. currentPage .."><input type=hidden name=perPage value=".. perPage .."><input type=hidden name=engaged value="..tostring(engaged).."><input type=hidden name=alerts_impl value="..tostring(alertsImpl).."><button class='btn btn-default btn-xs' type='submit'><input id=csrf name=csrf type=hidden value='"..ntop.getRandomCSRFValue().."' /><i type='submit' class='fa fa-trash-o'></i></button></form>"
print('{ "column_key" : "'..column_id..'", "column_date" : "'..column_date..'", "column_severity" : "'..column_severity..'", "column_type" : "'..column_type..'", "column_msg" : "'..column_msg..'" }')
@ -51,4 +80,4 @@ end -- for
print ("\n], \"perPage\" : " .. perPage .. ",\n")
print ("\"sort\" : [ [ \"\", \"\" ] ],\n")
print ("\"totalRows\" : " .. interface.getNumQueuedAlerts() .. " \n}")
print ("\"totalRows\" : " ..num_alerts .. " \n}")

View file

@ -1564,7 +1564,6 @@ print [[
</table>
]]
elseif(page == "snmp") then
if(ntop.isPro()) then
print_snmp_report(host_info["host"], true, ifId)
end

View file

@ -16,9 +16,26 @@ if(_GET["csrf"] ~= nil) then
if(_GET["id_to_delete"] ~= nil) then
if(_GET["id_to_delete"] == "__all__") then
interface.flushAllQueuedAlerts()
if _GET["alerts_impl"] == "new" then
if _GET["engaged"] == "true" then
interface.deleteAlerts(true)
else
interface.deleteAlerts(false)
end
end
print("")
else
interface.deleteQueuedAlert(tonumber(_GET["id_to_delete"]))
local id_to_delete = tonumber(_GET["id_to_delete"])
if id_to_delete ~= nil then
interface.deleteQueuedAlert(id_to_delete)
if _GET["alerts_impl"] == "new" then
if _GET["engaged"] == "true" then
interface.deleteAlerts(true, id_to_delete)
else
interface.deleteAlerts(false, id_to_delete)
end
end
end
end
end
end
@ -88,9 +105,82 @@ print [[
}
]
});
</script>
]]
local alert_items = {}
if interface.getNumAlerts(true --[[ engaged --]]) > 0 then
alert_items[#alert_items + 1] = {["label"] = "Currently Engaged Alerts", ["div-id"] = "table-engaged-alerts", ["status"] = "engaged", ["date"] = "First Seen"}
end
if interface.getNumAlerts(false --[[ NOT engaged --]]) > 0 then
alert_items[#alert_items +1] = {["label"] = "Alerts History", ["div-id"] = "table-alerts-history", ["status"] = "historical", ["date"] = "Time"}
end
alert_items = {} --[[ TEMPORARILY DISABLED --]]
for k, t in ipairs(alert_items) do
print [[
<div id="]] print(t["div-id"]) print[["></div>
<script>
$("#]] print(t["div-id"]) print[[").datatable({
url: "]]
print (ntop.getHttpPrefix())
print [[/lua/get_alerts_data.lua?alerts_impl=new&alert_status=]] print(t["status"]) print[[",
showPagination: true,
]]
if(_GET["currentPage"] ~= nil) then print("currentPage: ".._GET["currentPage"]..",\n") end
if(_GET["perPage"] ~= nil) then print("perPage: ".._GET["perPage"]..",\n") end
print [[
title: "]] print(t["label"]) print[[",
columns: [
{
title: "Action",
field: "column_key",
css: {
textAlign: 'center'
}
},
{
title: "]] print(t["date"]) print[[",
field: "column_date",
css: {
textAlign: 'center'
}
},
{
title: "Severity",
field: "column_severity",
css: {
textAlign: 'center'
}
},
{
title: "Type",
field: "column_type",
css: {
textAlign: 'center'
}
},
{
title: "Description",
field: "column_msg",
css: {
textAlign: 'left'
}
}
]
});
</script>
]]
end
if(interface.getNumQueuedAlerts() > 0) then
print [[