From 6dfca0c16354431589f99d3fd312364c47ee4c18 Mon Sep 17 00:00:00 2001 From: "niclas.koegl" Date: Tue, 21 Mar 2023 18:07:19 +0100 Subject: [PATCH 1/3] Add Opsgenie notification provider --- server/notification-providers/opsgenie.js | 97 +++++++++++++++++++++++ server/notification.js | 2 + src/components/notifications/Opsgenie.vue | 24 ++++++ src/components/notifications/index.js | 2 + 4 files changed, 125 insertions(+) create mode 100644 server/notification-providers/opsgenie.js create mode 100644 src/components/notifications/Opsgenie.vue diff --git a/server/notification-providers/opsgenie.js b/server/notification-providers/opsgenie.js new file mode 100644 index 000000000..3ccbb756c --- /dev/null +++ b/server/notification-providers/opsgenie.js @@ -0,0 +1,97 @@ +const NotificationProvider = require("./notification-provider"); +const axios = require("axios"); +const { UP, DOWN } = require("../../src/util"); + +const opsgenieAlertsUrlEU = "https://api.eu.opsgenie.com/v2/alerts" +const opsgenieAlertsUrlUS = "https://api.opsgenie.com/v2/alerts" +let okMsg = "Sent Successfully."; + +class Opsgenie extends NotificationProvider { + + name = "Opsgenie"; + + /** + * @inheritdoc + */ + async send(notification, msg, monitorJSON = null, heartbeatJSON = null) { + let opsgenieAlertsUrl; + let priority = (notification.opsgeniePriority == "") ? 3 : notification.opsgeniePriority; + const textMsg = "Uptime Kuma Alert"; + + try { + switch (notification.opsgenieRegion) { + case "US": + opsgenieAlertsUrl = opsgenieAlertsUrlUS; + break; + case "EU": + opsgenieAlertsUrl = opsgenieAlertsUrlEU; + break; + default: + opsgenieAlertsUrl = opsgenieAlertsUrlUS; + }; + + if (heartbeatJSON == null) { + let notificationTestAlias = "uptime-kuma-notification-test"; + let data = { + "message": msg, + "alias": notificationTestAlias, + "source": "Uptime Kuma", + "priority": "P5" + }; + + return this.post(notification, opsgenieAlertsUrl, data) + }; + + if (heartbeatJSON.status === DOWN) { + let data = { + "message": monitorJSON ? textMsg + `: ${monitorJSON.name}` : textMsg, + "alias": monitorJSON.name, + "description": msg, + "source": "Uptime Kuma", + "priority": `P${priority}` + }; + + return this.post(notification, opsgenieAlertsUrl, data) + }; + + if (heartbeatJSON.status === UP) { + let opsgenieAlertsCloseUrl = `${opsgenieAlertsUrl}/${encodeURIComponent(monitorJSON.name)}/close?identifierType=alias`; + let data = { + "source": "Uptime Kuma", + }; + + return this.post(notification, opsgenieAlertsCloseUrl, data) + }; + } catch (error) { + this.throwGeneralAxiosError(error); + } + } + + /** + * + * @param {BeanModel} notification + * @param {string} url Request url + * @param {Object} data Request body + * @returns {Promise} + */ + async post(notification, url, data) { + let config = { + headers: { + "Content-Type": "application/json", + "Authorization": `GenieKey ${notification.opsgenieApiKey}`, + } + }; + + let res = await axios.post(url, data, config); + if (res.status == null) { + return "Opsgenie notification failed with invalid response!"; + }; + if (res.status < 200 || res.status >= 300) { + return `Opsgenie notification failed with status code ${res.status}`; + }; + + return okMsg + } +} + +module.exports = Opsgenie; diff --git a/server/notification.js b/server/notification.js index ad1c8705a..2ec2c752f 100644 --- a/server/notification.js +++ b/server/notification.js @@ -20,6 +20,7 @@ const Mattermost = require("./notification-providers/mattermost"); const Ntfy = require("./notification-providers/ntfy"); const Octopush = require("./notification-providers/octopush"); const OneBot = require("./notification-providers/onebot"); +const Opsgenie = require("./notification-providers/opsgenie"); const PagerDuty = require("./notification-providers/pagerduty"); const PromoSMS = require("./notification-providers/promosms"); const Pushbullet = require("./notification-providers/pushbullet"); @@ -69,6 +70,7 @@ class Notification { new Ntfy(), new Octopush(), new OneBot(), + new Opsgenie(), new PagerDuty(), new PromoSMS(), new Pushbullet(), diff --git a/src/components/notifications/Opsgenie.vue b/src/components/notifications/Opsgenie.vue new file mode 100644 index 000000000..6cdfa2a95 --- /dev/null +++ b/src/components/notifications/Opsgenie.vue @@ -0,0 +1,24 @@ + diff --git a/src/components/notifications/index.js b/src/components/notifications/index.js index c1b7da4aa..36dac993e 100644 --- a/src/components/notifications/index.js +++ b/src/components/notifications/index.js @@ -18,6 +18,7 @@ import Mattermost from "./Mattermost.vue"; import Ntfy from "./Ntfy.vue"; import Octopush from "./Octopush.vue"; import OneBot from "./OneBot.vue"; +import Opsgenie from "./Opsgenie.vue"; import PagerDuty from "./PagerDuty.vue"; import PromoSMS from "./PromoSMS.vue"; import Pushbullet from "./Pushbullet.vue"; @@ -62,6 +63,7 @@ const NotificationFormList = { "ntfy": Ntfy, "octopush": Octopush, "OneBot": OneBot, + "Opsgenie": Opsgenie, "PagerDuty": PagerDuty, "promosms": PromoSMS, "pushbullet": Pushbullet, From 776a482a1d8515dc0c1ada22d558c01e76c816b4 Mon Sep 17 00:00:00 2001 From: "niclas.koegl" Date: Tue, 21 Mar 2023 19:29:37 +0100 Subject: [PATCH 2/3] Add Opsgenie notification provider --- src/components/NotificationDialog.vue | 1 + src/components/notifications/Opsgenie.vue | 18 +++++++++++++++--- 2 files changed, 16 insertions(+), 3 deletions(-) diff --git a/src/components/NotificationDialog.vue b/src/components/NotificationDialog.vue index c3851b568..ce38c90ea 100644 --- a/src/components/NotificationDialog.vue +++ b/src/components/NotificationDialog.vue @@ -129,6 +129,7 @@ export default { "ntfy": "Ntfy", "octopush": "Octopush", "OneBot": "OneBot", + "Opsgenie": "Opsgenie", "PagerDuty": "PagerDuty", "pushbullet": "Pushbullet", "PushByTechulus": "Push by Techulus", diff --git a/src/components/notifications/Opsgenie.vue b/src/components/notifications/Opsgenie.vue index 6cdfa2a95..3dc96bc87 100644 --- a/src/components/notifications/Opsgenie.vue +++ b/src/components/notifications/Opsgenie.vue @@ -9,16 +9,28 @@ EU + +
- + +
+
- +
- + + From d9558833fcf12a89061602b537e9c3641dd6a95b Mon Sep 17 00:00:00 2001 From: "niclas.koegl" Date: Tue, 21 Mar 2023 19:45:44 +0100 Subject: [PATCH 3/3] Fix linting --- server/notification-providers/opsgenie.js | 38 +++++++++++------------ server/notification.js | 2 +- src/components/notifications/Opsgenie.vue | 12 +++---- 3 files changed, 26 insertions(+), 26 deletions(-) diff --git a/server/notification-providers/opsgenie.js b/server/notification-providers/opsgenie.js index 3ccbb756c..16bf9fc60 100644 --- a/server/notification-providers/opsgenie.js +++ b/server/notification-providers/opsgenie.js @@ -2,14 +2,14 @@ const NotificationProvider = require("./notification-provider"); const axios = require("axios"); const { UP, DOWN } = require("../../src/util"); -const opsgenieAlertsUrlEU = "https://api.eu.opsgenie.com/v2/alerts" -const opsgenieAlertsUrlUS = "https://api.opsgenie.com/v2/alerts" +const opsgenieAlertsUrlEU = "https://api.eu.opsgenie.com/v2/alerts"; +const opsgenieAlertsUrlUS = "https://api.opsgenie.com/v2/alerts"; let okMsg = "Sent Successfully."; class Opsgenie extends NotificationProvider { - + name = "Opsgenie"; - + /** * @inheritdoc */ @@ -17,7 +17,7 @@ class Opsgenie extends NotificationProvider { let opsgenieAlertsUrl; let priority = (notification.opsgeniePriority == "") ? 3 : notification.opsgeniePriority; const textMsg = "Uptime Kuma Alert"; - + try { switch (notification.opsgenieRegion) { case "US": @@ -28,7 +28,7 @@ class Opsgenie extends NotificationProvider { break; default: opsgenieAlertsUrl = opsgenieAlertsUrlUS; - }; + } if (heartbeatJSON == null) { let notificationTestAlias = "uptime-kuma-notification-test"; @@ -38,12 +38,12 @@ class Opsgenie extends NotificationProvider { "source": "Uptime Kuma", "priority": "P5" }; - - return this.post(notification, opsgenieAlertsUrl, data) - }; + + return this.post(notification, opsgenieAlertsUrl, data); + } if (heartbeatJSON.status === DOWN) { - let data = { + let data = { "message": monitorJSON ? textMsg + `: ${monitorJSON.name}` : textMsg, "alias": monitorJSON.name, "description": msg, @@ -51,24 +51,24 @@ class Opsgenie extends NotificationProvider { "priority": `P${priority}` }; - return this.post(notification, opsgenieAlertsUrl, data) - }; + return this.post(notification, opsgenieAlertsUrl, data); + } if (heartbeatJSON.status === UP) { let opsgenieAlertsCloseUrl = `${opsgenieAlertsUrl}/${encodeURIComponent(monitorJSON.name)}/close?identifierType=alias`; - let data = { + let data = { "source": "Uptime Kuma", }; - return this.post(notification, opsgenieAlertsCloseUrl, data) - }; + return this.post(notification, opsgenieAlertsCloseUrl, data); + } } catch (error) { this.throwGeneralAxiosError(error); } } /** - * + * * @param {BeanModel} notification * @param {string} url Request url * @param {Object} data Request body @@ -85,12 +85,12 @@ class Opsgenie extends NotificationProvider { let res = await axios.post(url, data, config); if (res.status == null) { return "Opsgenie notification failed with invalid response!"; - }; + } if (res.status < 200 || res.status >= 300) { return `Opsgenie notification failed with status code ${res.status}`; - }; + } - return okMsg + return okMsg; } } diff --git a/server/notification.js b/server/notification.js index 5d110f215..f69e0a384 100644 --- a/server/notification.js +++ b/server/notification.js @@ -84,7 +84,7 @@ class Notification { new Ntfy(), new Octopush(), new OneBot(), - new Opsgenie(), + new Opsgenie(), new PagerDuty(), new PagerTree(), new PromoSMS(), diff --git a/src/components/notifications/Opsgenie.vue b/src/components/notifications/Opsgenie.vue index 3dc96bc87..3f07d0528 100644 --- a/src/components/notifications/Opsgenie.vue +++ b/src/components/notifications/Opsgenie.vue @@ -18,12 +18,12 @@ -
- *{{ $t("Required") }} - - https://docs.opsgenie.com/docs/alert-api - -
+
+ *{{ $t("Required") }} + + https://docs.opsgenie.com/docs/alert-api + +