Merge branch 'develop' into fix-skip-maintenance-notifications
This commit is contained in:
commit
bf53f107e3
2 files changed
+32
-16
No files matched your search
@@ -16,7 +16,7 @@ class NotificationService {
|
|||||||
return NotificationService.SERVICE_NAME;
|
return NotificationService.SERVICE_NAME;
|
||||||
}
|
}
|
||||||
|
|
||||||
sendNotification = async ({ notification, subject, content, html, discordContent = null }) => {
|
sendNotification = async ({ notification, subject, content, html, discordContent = null, webhookBody = null }) => {
|
||||||
const { type, address } = notification;
|
const { type, address } = notification;
|
||||||
|
|
||||||
if (type === "email") {
|
if (type === "email") {
|
||||||
@@ -30,7 +30,9 @@ class NotificationService {
|
|||||||
if (type === "discord") {
|
if (type === "discord") {
|
||||||
body = !discordContent ? { content } : discordContent;
|
body = !discordContent ? { content } : discordContent;
|
||||||
}
|
}
|
||||||
|
if (type === "webhook") {
|
||||||
|
body = !webhookBody ? { content } : webhookBody;
|
||||||
|
}
|
||||||
if (type === "slack" || type === "discord" || type === "webhook") {
|
if (type === "slack" || type === "discord" || type === "webhook") {
|
||||||
const response = await this.networkService.requestWebhook(type, address, body);
|
const response = await this.networkService.requestWebhook(type, address, body);
|
||||||
return response.status;
|
return response.status;
|
||||||
@@ -66,9 +68,9 @@ class NotificationService {
|
|||||||
if (alerts.length === 0) return false;
|
if (alerts.length === 0) return false;
|
||||||
|
|
||||||
const { subject, html } = await this.notificationUtils.buildHardwareEmail(networkResponse, alerts);
|
const { subject, html } = await this.notificationUtils.buildHardwareEmail(networkResponse, alerts);
|
||||||
const content = await this.notificationUtils.buildHardwareNotificationMessage(alerts);
|
const content = await this.notificationUtils.buildHardwareNotificationMessage(alerts, monitor);
|
||||||
|
const webhookBody = await this.notificationUtils.buildHardwareWebhookBody(alerts, monitor);
|
||||||
const success = await this.notifyAll({ notificationIDs, subject, html, content, discordContent });
|
const success = await this.notifyAll({ notificationIDs, subject, html, content, discordContent, webhookBody });
|
||||||
return success;
|
return success;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -79,12 +81,12 @@ class NotificationService {
|
|||||||
return success;
|
return success;
|
||||||
}
|
}
|
||||||
|
|
||||||
async notifyAll({ notificationIDs, subject, html, content, discordContent = null }) {
|
async notifyAll({ notificationIDs, subject, html, content, discordContent = null, webhookBody = null }) {
|
||||||
const notifications = await this.db.notificationModule.getNotificationsByIds(notificationIDs);
|
const notifications = await this.db.notificationModule.getNotificationsByIds(notificationIDs);
|
||||||
// Map each notification to a test promise
|
// Map each notification to a test promise
|
||||||
const promises = notifications.map(async (notification) => {
|
const promises = notifications.map(async (notification) => {
|
||||||
try {
|
try {
|
||||||
await this.sendNotification({ notification, subject, content, html, discordContent });
|
await this.sendNotification({ notification, subject, content, html, discordContent, webhookBody });
|
||||||
return true;
|
return true;
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
return false;
|
return false;
|
||||||
|
|||||||
@@ -107,12 +107,16 @@ class NotificationUtils {
|
|||||||
|
|
||||||
const alertsToSend = [];
|
const alertsToSend = [];
|
||||||
const discordEmbeds = [];
|
const discordEmbeds = [];
|
||||||
|
const monitorInfoFields = [
|
||||||
|
{ name: "Monitor", value: monitor.name, inline: true },
|
||||||
|
{ name: "URL", value: monitor.url, inline: false },
|
||||||
|
];
|
||||||
const formatDiscordAlert = {
|
const formatDiscordAlert = {
|
||||||
cpu: () => ({
|
cpu: () => ({
|
||||||
title: "CPU alert",
|
title: "CPU alert",
|
||||||
description: `Your current CPU usage (${(cpuUsage * 100).toFixed(0)}%) is above your threshold (${(cpuThreshold * 100).toFixed(0)}%)`,
|
description: `Your current CPU usage (${(cpuUsage * 100).toFixed(0)}%) is above your threshold (${(cpuThreshold * 100).toFixed(0)}%)`,
|
||||||
color: 15548997,
|
color: 15548997,
|
||||||
|
fields: monitorInfoFields,
|
||||||
footer: { text: "Checkmate" },
|
footer: { text: "Checkmate" },
|
||||||
}),
|
}),
|
||||||
|
|
||||||
@@ -120,7 +124,7 @@ class NotificationUtils {
|
|||||||
title: "Memory alert",
|
title: "Memory alert",
|
||||||
description: `Your current memory usage (${(memoryUsage * 100).toFixed(0)}%) is above your threshold (${(memoryThreshold * 100).toFixed(0)}%)`,
|
description: `Your current memory usage (${(memoryUsage * 100).toFixed(0)}%) is above your threshold (${(memoryThreshold * 100).toFixed(0)}%)`,
|
||||||
color: 15548997,
|
color: 15548997,
|
||||||
|
fields: monitorInfoFields,
|
||||||
footer: { text: "Checkmate" },
|
footer: { text: "Checkmate" },
|
||||||
}),
|
}),
|
||||||
|
|
||||||
@@ -129,11 +133,14 @@ class NotificationUtils {
|
|||||||
description: `Your current disk usage is above your threshold (${(diskThreshold * 100).toFixed(0)}%)`,
|
description: `Your current disk usage is above your threshold (${(diskThreshold * 100).toFixed(0)}%)`,
|
||||||
color: 15548997,
|
color: 15548997,
|
||||||
footer: { text: "Checkmate" },
|
footer: { text: "Checkmate" },
|
||||||
fields: (Array.isArray(disk) ? disk : []).map((d, idx) => ({
|
fields: [
|
||||||
name: `Disk ${idx}`,
|
...monitorInfoFields,
|
||||||
value: `${(d?.usage_percent * 100).toFixed(0)}%`,
|
...(Array.isArray(disk) ? disk : []).map((d, idx) => ({
|
||||||
inline: true,
|
name: `Disk ${idx}`,
|
||||||
})),
|
value: `${(d?.usage_percent * 100).toFixed(0)}%`,
|
||||||
|
inline: true,
|
||||||
|
})),
|
||||||
|
],
|
||||||
}),
|
}),
|
||||||
};
|
};
|
||||||
const alertTypes = ["cpu", "memory", "disk"];
|
const alertTypes = ["cpu", "memory", "disk"];
|
||||||
@@ -174,8 +181,15 @@ class NotificationUtils {
|
|||||||
return { subject, html };
|
return { subject, html };
|
||||||
};
|
};
|
||||||
|
|
||||||
buildHardwareNotificationMessage = (alerts) => {
|
buildHardwareNotificationMessage = (alerts, monitor) => {
|
||||||
return alerts.map((alert) => alert).join("\n");
|
const alertsHeader = [`Monitor: ${monitor.name}`, `URL: ${monitor.url}`];
|
||||||
|
const alertText = alerts.length > 0 ? [...alertsHeader, ...alerts] : [];
|
||||||
|
return alertText.map((alert) => alert).join("\n");
|
||||||
|
};
|
||||||
|
buildHardwareWebhookBody = (alerts, monitor) => {
|
||||||
|
const content = alerts.map((alert) => alert).join("\n");
|
||||||
|
const body = { text: content, name: monitor.name, url: monitor.url };
|
||||||
|
return body;
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in new issue
Block a user