mirror of
https://github.com/louislam/uptime-kuma.git
synced 2024-10-01 01:25:45 -04:00
a9a1cf1353
- I unified where in file the name of `NotificationProvider.name` is placed - I made sure that all the providers adhere to the signature of `NotificationProvider.send()` - I made sure that all the providers use `okMsg` if returning success messages directly from the function. Here a discussion should be had: Should this be refactored into a constant of `NotificationProvider`? I could imagine that `NotificationProvider.SENDING_SUCCESSFULL` could be a suitable alternative. - I made sure all providers have the URL they `POST`/`GET` to be extraced into a variable. => refactored this way due to Nelsons suggestion
97 lines
3.2 KiB
JavaScript
97 lines
3.2 KiB
JavaScript
const NotificationProvider = require("./notification-provider");
|
|
const axios = require("axios");
|
|
const { UP, DOWN } = require("../../src/util");
|
|
|
|
const opsgenieAlertsUrlEU = "https://api.eu.opsgenie.com/v2/alerts";
|
|
const opsgenieAlertsUrlUS = "https://api.opsgenie.com/v2/alerts";
|
|
const okMsg = "Sent Successfully.";
|
|
|
|
class Opsgenie extends NotificationProvider {
|
|
name = "Opsgenie";
|
|
|
|
/**
|
|
* @inheritdoc
|
|
*/
|
|
async send(notification, msg, monitorJSON = null, heartbeatJSON = null) {
|
|
let opsgenieAlertsUrl;
|
|
let priority = (!notification.opsgeniePriority) ? 3 : notification.opsgeniePriority;
|
|
const textMsg = "Uptime Kuma Alert";
|
|
|
|
try {
|
|
switch (notification.opsgenieRegion) {
|
|
case "us":
|
|
opsgenieAlertsUrl = opsgenieAlertsUrlUS;
|
|
break;
|
|
case "eu":
|
|
opsgenieAlertsUrl = opsgenieAlertsUrlEU;
|
|
break;
|
|
default:
|
|
opsgenieAlertsUrl = opsgenieAlertsUrlUS;
|
|
}
|
|
|
|
if (heartbeatJSON == null) {
|
|
let notificationTestAlias = "uptime-kuma-notification-test";
|
|
let data = {
|
|
"message": msg,
|
|
"alias": notificationTestAlias,
|
|
"source": "Uptime Kuma",
|
|
"priority": "P5"
|
|
};
|
|
|
|
return this.post(notification, opsgenieAlertsUrl, data);
|
|
}
|
|
|
|
if (heartbeatJSON.status === DOWN) {
|
|
let data = {
|
|
"message": monitorJSON ? textMsg + `: ${monitorJSON.name}` : textMsg,
|
|
"alias": monitorJSON.name,
|
|
"description": msg,
|
|
"source": "Uptime Kuma",
|
|
"priority": `P${priority}`
|
|
};
|
|
|
|
return this.post(notification, opsgenieAlertsUrl, data);
|
|
}
|
|
|
|
if (heartbeatJSON.status === UP) {
|
|
let opsgenieAlertsCloseUrl = `${opsgenieAlertsUrl}/${encodeURIComponent(monitorJSON.name)}/close?identifierType=alias`;
|
|
let data = {
|
|
"source": "Uptime Kuma",
|
|
};
|
|
|
|
return this.post(notification, opsgenieAlertsCloseUrl, data);
|
|
}
|
|
} catch (error) {
|
|
this.throwGeneralAxiosError(error);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Make POST request to Opsgenie
|
|
* @param {BeanModel} notification Notification to send
|
|
* @param {string} url Request url
|
|
* @param {object} data Request body
|
|
* @returns {Promise<string>} Success message
|
|
*/
|
|
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!";
|
|
}
|
|
if (res.status < 200 || res.status >= 300) {
|
|
return `Opsgenie notification failed with status code ${res.status}`;
|
|
}
|
|
|
|
return okMsg;
|
|
}
|
|
}
|
|
|
|
module.exports = Opsgenie;
|