mirror of
https://github.com/louislam/uptime-kuma.git
synced 2024-11-09 23:24:07 -08:00
eca90a2b00
Some checks are pending
Auto Test / auto-test (18, ARM64) (push) Blocked by required conditions
Auto Test / auto-test (18, macos-latest) (push) Blocked by required conditions
Auto Test / auto-test (18, ubuntu-latest) (push) Blocked by required conditions
Auto Test / auto-test (18, windows-latest) (push) Blocked by required conditions
Auto Test / auto-test (20, ARM64) (push) Blocked by required conditions
Auto Test / auto-test (20, macos-latest) (push) Blocked by required conditions
Auto Test / auto-test (20, ubuntu-latest) (push) Blocked by required conditions
Auto Test / auto-test (20, windows-latest) (push) Blocked by required conditions
Auto Test / armv7-simple-test (18, ARMv7) (push) Waiting to run
Auto Test / armv7-simple-test (20, ARMv7) (push) Waiting to run
Auto Test / check-linters (push) Waiting to run
Auto Test / e2e-test (push) Waiting to run
CodeQL / Analyze (go) (push) Waiting to run
CodeQL / Analyze (javascript-typescript) (push) Waiting to run
Merge Conflict Labeler / Labeling (push) Waiting to run
json-yaml-validate / json-yaml-validate (push) Waiting to run
115 lines
4 KiB
JavaScript
115 lines
4 KiB
JavaScript
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 PagerDuty extends NotificationProvider {
|
|
name = "PagerDuty";
|
|
|
|
/**
|
|
* @inheritdoc
|
|
*/
|
|
async send(notification, msg, monitorJSON = null, heartbeatJSON = null) {
|
|
try {
|
|
if (heartbeatJSON == null) {
|
|
const title = "Uptime Kuma Alert";
|
|
const monitor = {
|
|
type: "ping",
|
|
url: "Uptime Kuma Test Button",
|
|
};
|
|
return this.postNotification(notification, title, msg, monitor);
|
|
}
|
|
|
|
if (heartbeatJSON.status === UP) {
|
|
const title = "Uptime Kuma Monitor ✅ Up";
|
|
const eventAction = notification.pagerdutyAutoResolve || null;
|
|
|
|
return this.postNotification(notification, title, heartbeatJSON.msg, monitorJSON, eventAction);
|
|
}
|
|
|
|
if (heartbeatJSON.status === DOWN) {
|
|
const title = "Uptime Kuma Monitor 🔴 Down";
|
|
return this.postNotification(notification, title, heartbeatJSON.msg, monitorJSON, "trigger");
|
|
}
|
|
} catch (error) {
|
|
this.throwGeneralAxiosError(error);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Check if result is successful, result code should be in range 2xx
|
|
* @param {object} result Axios response object
|
|
* @returns {void}
|
|
* @throws {Error} The status code is not in range 2xx
|
|
*/
|
|
checkResult(result) {
|
|
if (result.status == null) {
|
|
throw new Error("PagerDuty notification failed with invalid response!");
|
|
}
|
|
if (result.status < 200 || result.status >= 300) {
|
|
throw new Error("PagerDuty notification failed with status code " + result.status);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Send the message
|
|
* @param {BeanModel} notification Message title
|
|
* @param {string} title Message title
|
|
* @param {string} body Message
|
|
* @param {object} monitorInfo Monitor details (For Up/Down only)
|
|
* @param {?string} eventAction Action event for PagerDuty (trigger, acknowledge, resolve)
|
|
* @returns {Promise<string>} Success message
|
|
*/
|
|
async postNotification(notification, title, body, monitorInfo, eventAction = "trigger") {
|
|
|
|
if (eventAction == null) {
|
|
return "No action required";
|
|
}
|
|
|
|
let monitorUrl;
|
|
if (monitorInfo.type === "port") {
|
|
monitorUrl = monitorInfo.hostname;
|
|
if (monitorInfo.port) {
|
|
monitorUrl += ":" + monitorInfo.port;
|
|
}
|
|
} else if (monitorInfo.hostname != null) {
|
|
monitorUrl = monitorInfo.hostname;
|
|
} else {
|
|
monitorUrl = monitorInfo.url;
|
|
}
|
|
|
|
const options = {
|
|
method: "POST",
|
|
url: notification.pagerdutyIntegrationUrl,
|
|
headers: { "Content-Type": "application/json" },
|
|
data: {
|
|
payload: {
|
|
summary: `[${title}] [${monitorInfo.name}] ${body}`,
|
|
severity: notification.pagerdutyPriority || "warning",
|
|
source: monitorUrl,
|
|
},
|
|
routing_key: notification.pagerdutyIntegrationKey,
|
|
event_action: eventAction,
|
|
dedup_key: "Uptime Kuma/" + monitorInfo.id,
|
|
}
|
|
};
|
|
|
|
const baseURL = await setting("primaryBaseURL");
|
|
if (baseURL && monitorInfo) {
|
|
options.client = "Uptime Kuma";
|
|
options.client_url = baseURL + getMonitorRelativeURL(monitorInfo.id);
|
|
}
|
|
|
|
let result = await axios.request(options);
|
|
this.checkResult(result);
|
|
if (result.statusText != null) {
|
|
return "PagerDuty notification succeed: " + result.statusText;
|
|
}
|
|
|
|
return successMessage;
|
|
}
|
|
}
|
|
|
|
module.exports = PagerDuty;
|