diff options
author | Daniel Baumann <daniel@debian.org> | 2024-11-26 09:28:28 +0100 |
---|---|---|
committer | Daniel Baumann <daniel@debian.org> | 2024-11-26 12:25:58 +0100 |
commit | a1882b67c41fe9901a0cd8059b5cc78a5beadec0 (patch) | |
tree | 2a24507c67aa99a15416707b2f7e645142230ed8 /server/notification-providers/opsgenie.js | |
parent | Initial commit. (diff) | |
download | uptime-kuma-upstream.tar.xz uptime-kuma-upstream.zip |
Adding upstream version 2.0.0~beta.0+dfsg.upstream/2.0.0_beta.0+dfsgupstream
Signed-off-by: Daniel Baumann <daniel@debian.org>
Diffstat (limited to 'server/notification-providers/opsgenie.js')
-rw-r--r-- | server/notification-providers/opsgenie.js | 96 |
1 files changed, 96 insertions, 0 deletions
diff --git a/server/notification-providers/opsgenie.js b/server/notification-providers/opsgenie.js new file mode 100644 index 0000000..59a7970 --- /dev/null +++ b/server/notification-providers/opsgenie.js @@ -0,0 +1,96 @@ +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 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); + } + } + + /** + * Make POST request to Opsgenie + * @param {BeanModel} notification Notification to send + * @param {string} url Request url + * @param {object} data Request body + * @returns {Promise<string>} Success message + */ + 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; |