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