From cefe43800f521279d9327fd9b67fc561ea10671b Mon Sep 17 00:00:00 2001 From: Alvin Pergens Date: Wed, 26 Jan 2022 15:54:17 +0100 Subject: [PATCH 1/4] add alerta service --- server/notification-providers/alerta.js | 67 +++++++++++++++++++++++++ server/notification.js | 4 +- src/components/notifications/Alerta.vue | 14 ++++++ src/components/notifications/index.js | 4 +- src/languages/en.js | 6 +++ src/languages/fr-FR.js | 5 ++ 6 files changed, 98 insertions(+), 2 deletions(-) create mode 100644 server/notification-providers/alerta.js create mode 100644 src/components/notifications/Alerta.vue diff --git a/server/notification-providers/alerta.js b/server/notification-providers/alerta.js new file mode 100644 index 000000000..d3b1a23ca --- /dev/null +++ b/server/notification-providers/alerta.js @@ -0,0 +1,67 @@ +const NotificationProvider = require("./notification-provider"); +const { DOWN, UP } = require("../../src/util"); +const axios = require("axios"); + +class Alerta extends NotificationProvider { + + name = "alerta"; + + async send(notification, msg, monitorJSON = null, heartbeatJSON = null) { + let okMsg = "Sent Successfully."; + + try { + let alertaUrl = `${notification.alertaApiEndpoint}`; + let config = { + headers: { + "Content-Type": "application/json;charset=UTF-8", + "Authorization": "Key " + notification.alertaapiKey, + } + }; + let data = { + environment: notification.alertaEnvironment, + severity: "critical", + correlate: [], + service: [ "UptimeKuma" ], + value: "Timeout", + tags: [ "uptimekuma" ], + attributes: {}, + origin: "uptimekuma", + type: "exceptionAlert", + }; + + if (heartbeatJSON == null) { + let testdata = Object.assign( { + event: "test", + text: "Testing Successful.", + group: "uptimekuma-test", + resource: "Test", + }, data ); + await axios.post(alertaUrl, testdata, config); + } else { + let datadup = Object.assign( { + correlate: ["service_up", "service_down"], + group: "uptimekuma-" + monitorJSON["type"], + resource: monitorJSON["name"], + }, data ); + + if (heartbeatJSON["status"] == DOWN) { + datadup.severity = notification.alertaAlertState; // critical + datadup.event = "service_state"; + datadup.text = "Service is down."; + await axios.post(alertaUrl, datadup, config); + } else if (heartbeatJSON["status"] == UP) { + datadup.severity = notification.alertaRecoverState; // cleaner + datadup.event = "service_state"; + datadup.text = "Service is up."; + await axios.post(alertaUrl, datadup, config); + } + } + return okMsg; + } catch (error) { + this.throwGeneralAxiosError(error); + } + + } +} + +module.exports = Alerta; diff --git a/server/notification.js b/server/notification.js index 4d72c81c7..dc3ea7960 100644 --- a/server/notification.js +++ b/server/notification.js @@ -27,6 +27,7 @@ const SerwerSMS = require("./notification-providers/serwersms"); const Stackfield = require("./notification-providers/stackfield"); const WeCom = require("./notification-providers/wecom"); const GoogleChat = require("./notification-providers/google-chat"); +const Alerta = require("./notification-providers/alerta"); class Notification { @@ -65,7 +66,8 @@ class Notification { new SerwerSMS(), new Stackfield(), new WeCom(), - new GoogleChat() + new GoogleChat(), + new Alerta(), ]; for (let item of list) { diff --git a/src/components/notifications/Alerta.vue b/src/components/notifications/Alerta.vue new file mode 100644 index 000000000..962267fb6 --- /dev/null +++ b/src/components/notifications/Alerta.vue @@ -0,0 +1,14 @@ + diff --git a/src/components/notifications/index.js b/src/components/notifications/index.js index 03945f90e..599ea4db4 100644 --- a/src/components/notifications/index.js +++ b/src/components/notifications/index.js @@ -26,6 +26,7 @@ import SerwerSMS from "./SerwerSMS.vue"; import Stackfield from './Stackfield.vue'; import WeCom from "./WeCom.vue"; import GoogleChat from "./GoogleChat.vue"; +import Alerta from "./Alerta.vue"; /** * Manage all notification form. @@ -60,7 +61,8 @@ const NotificationFormList = { "serwersms": SerwerSMS, "stackfield": Stackfield, "WeCom": WeCom, - "GoogleChat": GoogleChat + "GoogleChat": GoogleChat, + "alerta": Alerta, }; export default NotificationFormList; diff --git a/src/languages/en.js b/src/languages/en.js index 47513466c..d4fe0a5fa 100644 --- a/src/languages/en.js +++ b/src/languages/en.js @@ -361,4 +361,10 @@ export default { smtpDkimHashAlgo: "Hash Algorithm (Optional)", smtpDkimheaderFieldNames: "Header Keys to sign (Optional)", smtpDkimskipFields: "Header Keys not to sign (Optional)", + alerta: 'Alerta', + alertaApiEndpoint: 'API Endpoint', + alertaEnvironment: 'Environment', + alertaApiKey: 'API Key', + alertaAlertState: 'Alert State', + alertaRecoverState: 'Recover State', }; diff --git a/src/languages/fr-FR.js b/src/languages/fr-FR.js index 04dede1b5..81d8f2dc6 100644 --- a/src/languages/fr-FR.js +++ b/src/languages/fr-FR.js @@ -304,4 +304,9 @@ export default { steamApiKeyDescription: "Pour surveiller un serveur Steam, vous avez besoin d'une clé Steam Web-API. Vous pouvez enregistrer votre clé ici : ", "Current User": "Utilisateur actuel", recent: "Récent", + alertaApiEndpoint: 'API Endpoint', + alertaEnvironment: 'Environement', + alertaApiKey: "Clé de l'API", + alertaAlertState: "État de l'Alerte", + alertaRecoverState: 'État de récupération', }; From 90f24975482068131258d672522047fc3c8ce7d4 Mon Sep 17 00:00:00 2001 From: Alvin Pergens Date: Fri, 28 Jan 2022 15:14:34 +0100 Subject: [PATCH 2/4] change data for Alerta --- server/notification-providers/alerta.js | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/server/notification-providers/alerta.js b/server/notification-providers/alerta.js index d3b1a23ca..3ee331ac1 100644 --- a/server/notification-providers/alerta.js +++ b/server/notification-providers/alerta.js @@ -40,19 +40,18 @@ class Alerta extends NotificationProvider { } else { let datadup = Object.assign( { correlate: ["service_up", "service_down"], + event: monitorJSON["type"], group: "uptimekuma-" + monitorJSON["type"], resource: monitorJSON["name"], }, data ); if (heartbeatJSON["status"] == DOWN) { datadup.severity = notification.alertaAlertState; // critical - datadup.event = "service_state"; - datadup.text = "Service is down."; + datadup.text = "Service " + monitorJSON["type"] + " is down."; await axios.post(alertaUrl, datadup, config); } else if (heartbeatJSON["status"] == UP) { datadup.severity = notification.alertaRecoverState; // cleaner - datadup.event = "service_state"; - datadup.text = "Service is up."; + datadup.text = "Service " + monitorJSON["type"] + " is up."; await axios.post(alertaUrl, datadup, config); } } From 8febff9282e3622b92bd420822ca3f9ec1f80d0e Mon Sep 17 00:00:00 2001 From: Alvin Pergens Date: Fri, 28 Jan 2022 15:35:33 +0100 Subject: [PATCH 3/4] fix comments --- server/notification-providers/alerta.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/server/notification-providers/alerta.js b/server/notification-providers/alerta.js index 3ee331ac1..f00d10980 100644 --- a/server/notification-providers/alerta.js +++ b/server/notification-providers/alerta.js @@ -50,7 +50,7 @@ class Alerta extends NotificationProvider { datadup.text = "Service " + monitorJSON["type"] + " is down."; await axios.post(alertaUrl, datadup, config); } else if (heartbeatJSON["status"] == UP) { - datadup.severity = notification.alertaRecoverState; // cleaner + datadup.severity = notification.alertaRecoverState; // cleaned datadup.text = "Service " + monitorJSON["type"] + " is up."; await axios.post(alertaUrl, datadup, config); } From fa490d0bf1fa64ad601d9d6b12ef9410b4d5966b Mon Sep 17 00:00:00 2001 From: Louis Lam Date: Fri, 4 Mar 2022 14:13:44 +0800 Subject: [PATCH 4/4] [Alerta] Handle general message --- server/notification-providers/alerta.js | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/server/notification-providers/alerta.js b/server/notification-providers/alerta.js index f00d10980..e692b57ba 100644 --- a/server/notification-providers/alerta.js +++ b/server/notification-providers/alerta.js @@ -30,13 +30,14 @@ class Alerta extends NotificationProvider { }; if (heartbeatJSON == null) { - let testdata = Object.assign( { - event: "test", - text: "Testing Successful.", - group: "uptimekuma-test", - resource: "Test", - }, data ); - await axios.post(alertaUrl, testdata, config); + let postData = Object.assign({ + event: "msg", + text: msg, + group: "uptimekuma-msg", + resource: "Message", + }, data); + + await axios.post(alertaUrl, postData, config); } else { let datadup = Object.assign( { correlate: ["service_up", "service_down"],