diff --git a/server/notification-providers/alerta.js b/server/notification-providers/alerta.js new file mode 100644 index 000000000..e692b57ba --- /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 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"], + event: monitorJSON["type"], + group: "uptimekuma-" + monitorJSON["type"], + resource: monitorJSON["name"], + }, data ); + + if (heartbeatJSON["status"] == DOWN) { + datadup.severity = notification.alertaAlertState; // critical + datadup.text = "Service " + monitorJSON["type"] + " is down."; + await axios.post(alertaUrl, datadup, config); + } else if (heartbeatJSON["status"] == UP) { + datadup.severity = notification.alertaRecoverState; // cleaned + datadup.text = "Service " + monitorJSON["type"] + " 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 837828309..3b12fbcda 100644 --- a/server/notification.js +++ b/server/notification.js @@ -28,6 +28,7 @@ const Stackfield = require("./notification-providers/stackfield"); const WeCom = require("./notification-providers/wecom"); const GoogleChat = require("./notification-providers/google-chat"); const Gorush = require("./notification-providers/gorush"); +const Alerta = require("./notification-providers/alerta"); class Notification { @@ -67,7 +68,8 @@ class Notification { new Stackfield(), new WeCom(), new GoogleChat(), - new Gorush() + new Gorush(), + 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 7883890b4..9b6768cf4 100644 --- a/src/components/notifications/index.js +++ b/src/components/notifications/index.js @@ -27,6 +27,7 @@ import Stackfield from './Stackfield.vue'; import WeCom from "./WeCom.vue"; import GoogleChat from "./GoogleChat.vue"; import Gorush from "./Gorush.vue"; +import Alerta from "./Alerta.vue"; /** * Manage all notification form. @@ -62,7 +63,8 @@ const NotificationFormList = { "stackfield": Stackfield, "WeCom": WeCom, "GoogleChat": GoogleChat, - "gorush": Gorush + "gorush": Gorush, + "alerta": Alerta, }; export default NotificationFormList; diff --git a/src/languages/en.js b/src/languages/en.js index ebfc53b1f..9ab89717e 100644 --- a/src/languages/en.js +++ b/src/languages/en.js @@ -362,4 +362,10 @@ export default { smtpDkimheaderFieldNames: "Header Keys to sign (Optional)", smtpDkimskipFields: "Header Keys not to sign (Optional)", gorush: "Gorush", + 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 39199e4e3..054e16c11 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', };