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

98 lines
3.1 KiB
JavaScript
Raw Normal View History

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