Port email, slack, syslog, webhook plugins to the new alert API

This commit is contained in:
Alfredo Cardigliano 2020-07-28 10:28:35 +02:00
parent 5f4b7d0c02
commit 9acf38efa9
5 changed files with 67 additions and 571 deletions

View file

@ -62,56 +62,6 @@ end
-- ##############################################
function webhook.dequeueAlerts(queue)
local start_time = os.time()
local settings = {
url = ntop.getPref("ntopng.prefs.alerts.webhook_url"),
sharedsecret = ntop.getPref("ntopng.prefs.alerts.webhook_sharedsecret"),
username = ntop.getPref("ntopng.prefs.alerts.webhook_username"),
password = ntop.getPref("ntopng.prefs.alerts.webhook_password"),
}
local alerts = {}
while true do
local diff = os.time() - start_time
if diff >= webhook.ITERATION_TIMEOUT then
break
end
local json_alert = ntop.lpopCache(queue)
if not json_alert then
break
end
local alert = json.decode(json_alert)
table.insert(alerts, alert)
if #alerts >= MAX_ALERTS_PER_REQUEST then
if not webhook.sendMessage(alerts, settings) then
ntop.delCache(queue)
return {success=false, error_message="Unable to send alerts to the webhook"}
end
alerts = {}
end
end
if #alerts > 0 then
if not webhook.sendMessage(alerts, settings) then
ntop.delCache(queue)
return {success=false, error_message="Unable to send alerts to the webhook"}
end
end
return {success=true}
end
-- ##############################################
function webhook.dequeueRecipientAlerts(recipient, budget)
local start_time = os.time()
local sent = 0
@ -164,27 +114,25 @@ end
-- ##############################################
function webhook.handlePost()
function webhook.runTest()
local message_info, message_severity
if(_POST["send_test_webhook"] ~= nil) then
local settings = {
-- TODO
url = ntop.getPref("ntopng.prefs.alerts.webhook_url"),
sharedsecret = ntop.getPref("ntopng.prefs.alerts.webhook_sharedsecret"),
username = ntop.getPref("ntopng.prefs.alerts.webhook_username"),
password = ntop.getPref("ntopng.prefs.alerts.webhook_password"),
}
local settings = {
url = ntop.getPref("ntopng.prefs.alerts.webhook_url"),
sharedsecret = ntop.getPref("ntopng.prefs.alerts.webhook_sharedsecret"),
username = ntop.getPref("ntopng.prefs.alerts.webhook_username"),
password = ntop.getPref("ntopng.prefs.alerts.webhook_password"),
}
local success = webhook.sendMessage({}, settings)
local success = webhook.sendMessage({}, settings)
if success then
message_info = i18n("prefs.webhook_sent_successfully")
message_severity = "alert-success"
else
message_info = i18n("prefs.webhook_send_error", {product=product})
message_severity = "alert-danger"
end
if success then
message_info = i18n("prefs.webhook_sent_successfully")
message_severity = "alert-success"
else
message_info = i18n("prefs.webhook_send_error", {product=product})
message_severity = "alert-danger"
end
return message_info, message_severity
@ -192,62 +140,5 @@ end
-- ##############################################
function webhook.printPrefs(alert_endpoints, subpage_active, showElements)
print('<thead class="thead-light"><tr><th colspan="2" class="info">'..i18n("prefs.webhook_notification")..'</th></tr></thead>')
local elementToSwitchWebhook = {"row_webhook_notification_severity_preference", "webhook_url", "webhook_sharedsecret", "webhook_test", "webhook_username", "webhook_password"}
prefsToggleButton(subpage_active, {
field = "toggle_webhook_notification",
pref = alert_endpoints.getAlertNotificationModuleEnableKey("webhook", true),
default = "0",
disabled = showElements==false,
to_switch = elementToSwitchWebhook,
})
local showWebhookNotificationPrefs = false
if ntop.getPref(alert_endpoints.getAlertNotificationModuleEnableKey("webhook")) == "1" then
showWebhookNotificationPrefs = true
else
showWebhookNotificationPrefs = false
end
multipleTableButtonPrefs(subpage_active.entries["webhook_notification_severity_preference"].title, subpage_active.entries["webhook_notification_severity_preference"].description,
alert_endpoints.getSeverityLabels(), alert_endpoints.getSeverityValues(), "error", "primary", "webhook_notification_severity_preference",
alert_endpoints.getAlertNotificationModuleSeverityKey("webhook"), nil, nil, nil, nil, showElements and showWebhookNotificationPrefs)
prefsInputFieldPrefs(subpage_active.entries["webhook_url"].title, subpage_active.entries["webhook_url"].description,
"ntopng.prefs.alerts.", "webhook_url",
"", nil, showElements and showWebhookNotificationPrefs, true, true, {attributes={spellcheck="false"}, style={width="43em"}, required=true, pattern=getURLPattern()})
prefsInputFieldPrefs(subpage_active.entries["webhook_sharedsecret"].title, subpage_active.entries["webhook_sharedsecret"].description,
"ntopng.prefs.alerts.", "webhook_sharedsecret",
"", nil, showElements and showWebhookNotificationPrefs, false, nil, {attributes={spellcheck="false"}, required=false})
prefsInputFieldPrefs(subpage_active.entries["webhook_username"].title, subpage_active.entries["webhook_username"].description,
"ntopng.prefs.alerts.", "webhook_username",
"", false, showElements and showWebhookNotificationPrefs, nil, nil, {attributes={spellcheck="false"}, pattern="[^\\s]+", required=false})
prefsInputFieldPrefs(subpage_active.entries["webhook_password"].title, subpage_active.entries["webhook_password"].description,
"ntopng.prefs.alerts.", "webhook_password",
"", "password", showElements and showWebhookNotificationPrefs, nil, nil, {attributes={spellcheck="false"}, pattern="[^\\s]+", required=false})
print('<tr id="webhook_test" style="' .. ternary(showWebhookNotificationPrefs, "", "display:none;").. '"><td><button class="btn btn-secondary disable-on-dirty" type="button" onclick="sendTestWebhook();" style="width:230px; float:left;">'..i18n("prefs.send_test_webhook")..'</button></td></tr>')
print[[<script>
function sendTestWebhook() {
var params = {};
params.send_test_webhook = "";
params.csrf = "]] print(ntop.getRandomCSRFValue()) print[[";
var form = paramsToForm('<form method="post"></form>', params);
form.appendTo('body').submit();
}
</script>]]
end
-- ##############################################
return webhook