uptime-kuma/server/notification-providers/opsgenie.js

98 lines
3.2 KiB
JavaScript
Raw Normal View History

2023-03-21 10:07:19 -07:00
const NotificationProvider = require("./notification-provider");
const axios = require("axios");
const { UP, DOWN } = require("../../src/util");
2023-03-21 11:45:44 -07:00
const opsgenieAlertsUrlEU = "https://api.eu.opsgenie.com/v2/alerts";
const opsgenieAlertsUrlUS = "https://api.opsgenie.com/v2/alerts";
2023-03-21 10:07:19 -07:00
let okMsg = "Sent Successfully.";
class Opsgenie extends NotificationProvider {
2023-03-21 11:45:44 -07:00
2023-03-21 10:07:19 -07:00
name = "Opsgenie";
2023-03-21 11:45:44 -07:00
2023-03-21 10:07:19 -07:00
/**
* @inheritdoc
*/
async send(notification, msg, monitorJSON = null, heartbeatJSON = null) {
let opsgenieAlertsUrl;
2023-05-07 13:26:11 -07:00
let priority = (!notification.opsgeniePriority) ? 3 : notification.opsgeniePriority;
2023-03-21 10:07:19 -07:00
const textMsg = "Uptime Kuma Alert";
2023-03-21 11:45:44 -07:00
2023-03-21 10:07:19 -07:00
try {
switch (notification.opsgenieRegion) {
case "us":
2023-03-21 10:07:19 -07:00
opsgenieAlertsUrl = opsgenieAlertsUrlUS;
break;
case "eu":
2023-03-21 10:07:19 -07:00
opsgenieAlertsUrl = opsgenieAlertsUrlEU;
break;
default:
opsgenieAlertsUrl = opsgenieAlertsUrlUS;
2023-03-21 11:45:44 -07:00
}
2023-03-21 10:07:19 -07:00
if (heartbeatJSON == null) {
let notificationTestAlias = "uptime-kuma-notification-test";
let data = {
"message": msg,
"alias": notificationTestAlias,
"source": "Uptime Kuma",
"priority": "P5"
};
2023-03-21 11:45:44 -07:00
return this.post(notification, opsgenieAlertsUrl, data);
}
2023-03-21 10:07:19 -07:00
if (heartbeatJSON.status === DOWN) {
2023-03-21 11:45:44 -07:00
let data = {
2023-03-21 10:07:19 -07:00
"message": monitorJSON ? textMsg + `: ${monitorJSON.name}` : textMsg,
"alias": monitorJSON.name,
"description": msg,
"source": "Uptime Kuma",
"priority": `P${priority}`
};
2023-03-21 11:45:44 -07:00
return this.post(notification, opsgenieAlertsUrl, data);
}
2023-03-21 10:07:19 -07:00
if (heartbeatJSON.status === UP) {
let opsgenieAlertsCloseUrl = `${opsgenieAlertsUrl}/${encodeURIComponent(monitorJSON.name)}/close?identifierType=alias`;
2023-03-21 11:45:44 -07:00
let data = {
2023-03-21 10:07:19 -07:00
"source": "Uptime Kuma",
};
2023-03-21 11:45:44 -07:00
return this.post(notification, opsgenieAlertsCloseUrl, data);
}
2023-03-21 10:07:19 -07:00
} catch (error) {
this.throwGeneralAxiosError(error);
}
}
/**
* Make POST request to Opsgenie
* @param {BeanModel} notification Notification to send
2023-03-21 10:07:19 -07:00
* @param {string} url Request url
* @param {object} data Request body
* @returns {Promise<string>} Success message
2023-03-21 10:07:19 -07:00
*/
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!";
2023-03-21 11:45:44 -07:00
}
2023-03-21 10:07:19 -07:00
if (res.status < 200 || res.status >= 300) {
return `Opsgenie notification failed with status code ${res.status}`;
2023-03-21 11:45:44 -07:00
}
2023-03-21 10:07:19 -07:00
2023-03-21 11:45:44 -07:00
return okMsg;
2023-03-21 10:07:19 -07:00
}
}
module.exports = Opsgenie;