From ef54d9e3b648b7f7c538b512dcbe0b00b3a8a403 Mon Sep 17 00:00:00 2001 From: Austin Miller Date: Mon, 6 Feb 2023 11:33:14 -0700 Subject: [PATCH] Add PagerTree Notification Provider --- server/notification-providers/pagertree.js | 91 ++++++++++++++++++++++ server/notification.js | 2 + src/components/notifications/PagerTree.vue | 33 ++++++++ src/components/notifications/index.js | 2 + src/lang/en.json | 11 ++- 5 files changed, 138 insertions(+), 1 deletion(-) create mode 100644 server/notification-providers/pagertree.js create mode 100644 src/components/notifications/PagerTree.vue diff --git a/server/notification-providers/pagertree.js b/server/notification-providers/pagertree.js new file mode 100644 index 000000000..c39f56811 --- /dev/null +++ b/server/notification-providers/pagertree.js @@ -0,0 +1,91 @@ +const NotificationProvider = require("./notification-provider"); +const axios = require("axios"); +const { UP, DOWN, getMonitorRelativeURL } = require("../../src/util"); +const { setting } = require("../util-server"); +let successMessage = "Sent Successfully."; + +class PagerTree extends NotificationProvider { + name = "PagerTree"; + + /** + * @inheritdoc + */ + async send(notification, msg, monitorJSON = null, heartbeatJSON = null) { + try { + if (heartbeatJSON == null) { + const title = "[Test] Uptime Kuma Alert"; + return this.postNotification(notification, title, monitorJSON, heartbeatJSON); + } + + if (heartbeatJSON.status === UP && notification.pagertreeAutoResolve === "resolve") { + return this.postNotification(notification, null, monitorJSON, heartbeatJSON, notification.pagertreeAutoResolve); + } + + if (heartbeatJSON.status === DOWN) { + const title = `Uptime Kuma Monitor "${monitorJSON.name}" is DOWN`; + return this.postNotification(notification, title, monitorJSON, heartbeatJSON); + } + } catch (error) { + this.throwGeneralAxiosError(error); + } + } + + /** + * Check if result is successful, result code should be in range 2xx + * @param {Object} result Axios response object + * @throws {Error} The status code is not in range 2xx + */ + checkResult(result) { + if (result.status == null) { + throw new Error("PagerTree notification failed with invalid response!"); + } + if (result.status < 200 || result.status >= 300) { + throw new Error("PagerTree notification failed with status code " + result.status); + } + } + + /** + * Send the message + * @param {BeanModel} notification Message title + * @param {string} title Message title + * @param {Object} monitorJSON Monitor details (For Up/Down only) + * @param {?string} eventAction Action event for PagerTree (create, resolve) + * @returns {string} + */ + async postNotification(notification, title, monitorJSON, heartbeatJSON, eventAction = "create") { + + if (eventAction == null) { + return "No action required"; + } + + const options = { + method: "POST", + url: notification.pagertreeIntegrationUrl, + headers: { "Content-Type": "application/json" }, + data: { + event_type: eventAction, + id: heartbeatJSON?.monitorID || "uptime-kuma-test", + title: title, + urgency: notification.pagertreeUrgency, + heartbeat: heartbeatJSON, + monitor: monitorJSON + } + }; + + const baseURL = await setting("primaryBaseURL"); + if (baseURL && monitorJSON) { + options.client = "Uptime Kuma"; + options.client_url = baseURL + getMonitorRelativeURL(monitorJSON.id); + } + + let result = await axios.request(options); + this.checkResult(result); + if (result.statusText != null) { + return "PagerTree notification succeed: " + result.statusText; + } + + return successMessage; + } +} + +module.exports = PagerTree; diff --git a/server/notification.js b/server/notification.js index fd3491238..1897f5cc0 100644 --- a/server/notification.js +++ b/server/notification.js @@ -24,6 +24,7 @@ const Ntfy = require("./notification-providers/ntfy"); const Octopush = require("./notification-providers/octopush"); const OneBot = require("./notification-providers/onebot"); const PagerDuty = require("./notification-providers/pagerduty"); +const PagerTree = require("./notification-providers/pagertree"); const PromoSMS = require("./notification-providers/promosms"); const Pushbullet = require("./notification-providers/pushbullet"); const PushDeer = require("./notification-providers/pushdeer"); @@ -83,6 +84,7 @@ class Notification { new Octopush(), new OneBot(), new PagerDuty(), + new PagerTree(), new PromoSMS(), new Pushbullet(), new PushDeer(), diff --git a/src/components/notifications/PagerTree.vue b/src/components/notifications/PagerTree.vue new file mode 100644 index 000000000..823eb23b4 --- /dev/null +++ b/src/components/notifications/PagerTree.vue @@ -0,0 +1,33 @@ + + + diff --git a/src/components/notifications/index.js b/src/components/notifications/index.js index 3c8b26210..ed9dde0f1 100644 --- a/src/components/notifications/index.js +++ b/src/components/notifications/index.js @@ -22,6 +22,7 @@ import Ntfy from "./Ntfy.vue"; import Octopush from "./Octopush.vue"; import OneBot from "./OneBot.vue"; import PagerDuty from "./PagerDuty.vue"; +import PagerTree from "./PagerTree.vue"; import PromoSMS from "./PromoSMS.vue"; import Pushbullet from "./Pushbullet.vue"; import PushDeer from "./PushDeer.vue"; @@ -76,6 +77,7 @@ const NotificationFormList = { "octopush": Octopush, "OneBot": OneBot, "PagerDuty": PagerDuty, + "PagerTree": PagerTree, "promosms": PromoSMS, "pushbullet": Pushbullet, "PushByTechulus": TechulusPush, diff --git a/src/lang/en.json b/src/lang/en.json index d907f4e0c..4383aca91 100644 --- a/src/lang/en.json +++ b/src/lang/en.json @@ -695,5 +695,14 @@ "Google Analytics ID": "Google Analytics ID", "Edit Tag": "Edit Tag", "Server Address": "Server Address", - "Learn More": "Learn More" + "Learn More": "Learn More", + "pagertreeIntegrationUrl": "Integration URL", + "pagertreeUrgency": "Urgency", + "pagertreeSilent": "Silent", + "pagertreeLow": "Low", + "pagertreeMedium": "Medium", + "pagertreeHigh": "High", + "pagertreeCritical": "Critical", + "pagertreeResolve": "Auto Resolve", + "pagertreeDoNothing": "Do Nothing" }