ntopng/scripts/plugins/syslog_alert_endpoint/alert_endpoints/syslog.lua
Simone Mainardi e487427aab Refactors alert_utils and enterprise_alert_utils
Addresses #3720

Alerts Refactor: alert_utils as module

Alerts Refactor: notify_ntopng_start and notify_ntopng_stop

Alerts Refactor: processAlertNotifications

Alerts Refactor: checkStoreAlertsFromC

Alerts Refactor: formatAlertNotification

Alerts Refactor: notification_timestamp_rev

Alerts Refactor: formatAlertMessage

Alerts Refactor: getConfigsetAlertLink

Alerts Refactor: alertNotificationActionToLabel

Alerts Refactor: flushAlertsData

Alerts Refactor: disableAlertsGeneration

Alerts Refactor: newAlertsWorkingStatus and other

Alerts Refactor: drawAlerts

Alerts Refactor: drawAlertTables

Alerts Refactor: printAlertTables

Alerts Refactor: checkDeleteStoredAlerts

Alerts Refactor: getUnpagedAlertOptions

Alerts Refactor: getTabParameters

Alerts Refactor: getAlerts

Alerts Refactor: getNumAlerts

Alerts Refactor: performAlertsQuery

Alerts Refactor: sec2granularity

Alerts Refactor: granularity2id

Alerts Refactor: granularity2sec

Alerts Refactor: alertEngineLabel

Alerts Refactor: alertEngine

Alerts Refactor: alertEngineRaw

Alerts Refactor: alertTypeDescription

Alerts Refactor: alertType

Alerts Refactor: alertTypeLabel

Alerts Refactor: alertTypeRaw

Alerts Refactor: alertSeverity

Alerts Refactor: alertSeverityLabel

Alerts Refactor: alertSeverityRaw

Alerts Refactor: get_make_room_keys

Alerts Refactor: enterprise_alert_utils
2020-04-10 14:03:20 +02:00

118 lines
3.6 KiB
Lua

--
-- (C) 2018 - ntop.org
--
require "lua_utils"
local json = require "dkjson"
local alert_utils = require "alert_utils"
local alert_consts = require "alert_consts"
local syslog = {}
syslog.DEFAULT_SEVERITY = "info"
syslog.EXPORT_FREQUENCY = 1 -- 1 second, i.e., as soon as possible
syslog.prio = 300
-- ##############################################
function syslog.isAvailable()
return(ntop.syslog ~= nil)
end
-- ##############################################
function syslog.dequeueAlerts(queue)
local notifications = ntop.lrangeCache(queue, 0, -1)
if not notifications then
return {success = true}
end
local syslog_format = ntop.getPref("ntopng.prefs.syslog_alert_format")
if isEmptyString(syslog_format) then
syslog_format = "plaintext"
end
-- Separate by severity and channel
local alerts_by_types = {}
for _, json_message in ipairs(notifications) do
local notif = json.decode(json_message)
if notif.alert_entity then
alerts_by_types[notif.alert_entity] = alerts_by_types[notif.alert_entity] or {}
alerts_by_types[notif.alert_entity][notif.alert_severity] = alerts_by_types[notif.alert_entity][notif.alert_severity] or {}
table.insert(alerts_by_types[notif.alert_entity][notif.alert_severity], notif)
end
end
for _, by_severity in pairs(alerts_by_types) do
for severity, notifications in pairs(by_severity) do
severity = alert_consts.alertSeverityRaw(severity)
-- Most recent notifications first
for _, notif in pairsByValues(notifications, alert_utils.notification_timestamp_rev) do
local syslog_severity = alert_consts.alertLevelToSyslogLevel(severity)
local msg
if syslog_format == "plaintext" then
-- prepare a plaintext message
msg = alert_utils.formatAlertNotification(notif, {nohtml = true,
show_severity = true,
show_entity = true})
else -- syslog_format == "json" then
-- send out the json message but prepare a nice
-- message
notif.message = alert_utils.formatAlertNotification(notif, {nohtml = true,
show_severity = false,
show_entity = false})
msg = json.encode(notif)
end
ntop.syslog(msg, syslog_severity)
end
end
end
-- Remove all the messages from queue on success
ntop.delCache(queue)
return {success = true}
end
-- ##############################################
function syslog.printPrefs(alert_endpoints, subpage_active, showElements)
print('<thead class="thead-light"><tr><th colspan="2" class="info">'..i18n("prefs.syslog_notification")..'</th></tr></thead>')
local alertsEnabled = showElements
local elementToSwitch = {"row_syslog_alert_format"}
prefsToggleButton(subpage_active, {
field = "toggle_alert_syslog",
pref = alert_endpoints.getAlertNotificationModuleEnableKey("syslog", true),
default = "0",
disabled = alertsEnabled == false,
to_switch = elementToSwitch,
})
local format_labels = {i18n("prefs.syslog_alert_format_plaintext"), i18n("prefs.syslog_alert_format_json")}
local format_values = {"plaintext", "json"}
if ntop.getPref(alert_endpoints.getAlertNotificationModuleEnableKey("syslog")) == "0" then
alertsEnabled = false
end
multipleTableButtonPrefs(subpage_active.entries["syslog_alert_format"].title,
subpage_active.entries["syslog_alert_format"].description,
format_labels, format_values,
"plaintext",
"primary",
"syslog_alert_format",
"ntopng.prefs.syslog_alert_format", nil,
nil, nil, nil, alertsEnabled)
end
-- ##############################################
return syslog