diff --git a/server/notification-providers/alerta.js b/server/notification-providers/alerta.js new file mode 100644 index 00000000..d3b1a23c --- /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 4d72c81c..dc3ea796 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 00000000..962267fb --- /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 03945f90..599ea4db 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 47513466..d4fe0a5f 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 04dede1b..81d8f2dc 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', };