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-08-11 07:46:41 +00:00
|
|
|
* Make POST request to Opsgenie
|
|
|
|
* @param {BeanModel} notification Notification to send
|
2023-03-21 17:07:19 +00:00
|
|
|
* @param {string} url Request url
|
2023-08-11 07:46:41 +00:00
|
|
|
* @param {object} data Request body
|
|
|
|
* @returns {Promise<string>} Success message
|
2023-03-21 17:07:19 +00: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 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;
|