Improve email alert notification and add test button

This commit is contained in:
emanuele-f 2018-04-06 17:55:32 +02:00
parent 11517970ac
commit 07d89dfa8d
6 changed files with 91 additions and 20 deletions

View file

@ -19,22 +19,38 @@ local function getEmailMessage(now_ts, from, to, subject, body)
return table.concat(lines, "\r\n") .. "\r\n\r\n" .. body .. "\r\n"
end
function email.sendNotification(notif)
local email_address = ntop.getPref("ntopng.prefs.alerts.email_address")
function email.sendEmail(subject, message_body)
local smtp_server = ntop.getPref("ntopng.prefs.alerts.smtp_server")
local msg_prefix = alertNotificationActionToLabel(notif.action)
local from_addr = ntop.getPref("ntopng.prefs.alerts.email_sender")
local to_addr = ntop.getPref("ntopng.prefs.alerts.email_recipient")
if isEmptyString(email_address) or isEmptyString(smtp_server) then
if isEmptyString(from_addr) or isEmptyString(to_addr) or isEmptyString(smtp_server) then
return false
end
if not string.find(smtp_server, "://") then
smtp_server = "smtp://" .. smtp_server
end
local parts = string.split(to_addr, "@")
if #parts == 2 then
local sender_domain = parts[2]
smtp_server = smtp_server .. "/" .. sender_domain
end
local message = getEmailMessage(os.time(), from_addr, to_addr, subject, message_body)
return ntop.sendMail(from_addr, to_addr, message, smtp_server)
end
function email.sendNotification(notif)
local msg_prefix = alertNotificationActionToLabel(notif.action)
local product = ntop.getInfo(false).product
local subject = product .. " [" .. string.upper(notif.severity) .. "]: " .. alertTypeLabel(alertType(notif.type), true)
local message_body = noHtml(msg_prefix .. notif.message)
local from_to = email_address
local message = getEmailMessage(os.time(), from_to, from_to, subject, message_body)
return ntop.sendMail(from_to, from_to, message, smtp_server)
return email.sendEmail(subject, message_body)
end
return email