1
0
mirror of https://github.com/louislam/uptime-kuma.git synced 2024-12-24 22:14:47 +02:00
uptime-kuma/server/notification-providers/opsgenie.js

97 lines
3.2 KiB
JavaScript
Raw Normal View History

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