diff --git a/server/src/service/infrastructure/notificationService.js b/server/src/service/infrastructure/notificationService.js index 96fc53592..6c5b8b25e 100644 --- a/server/src/service/infrastructure/notificationService.js +++ b/server/src/service/infrastructure/notificationService.js @@ -16,7 +16,7 @@ class NotificationService { 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; if (type === "email") { @@ -30,7 +30,9 @@ class NotificationService { if (type === "discord") { body = !discordContent ? { content } : discordContent; } - + if (type === "webhook") { + body = !webhookBody ? { content } : webhookBody; + } if (type === "slack" || type === "discord" || type === "webhook") { const response = await this.networkService.requestWebhook(type, address, body); return response.status; @@ -66,9 +68,9 @@ class NotificationService { if (alerts.length === 0) return false; const { subject, html } = await this.notificationUtils.buildHardwareEmail(networkResponse, alerts); - const content = await this.notificationUtils.buildHardwareNotificationMessage(alerts); - - const success = await this.notifyAll({ notificationIDs, subject, html, content, discordContent }); + 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, webhookBody }); return success; } @@ -79,12 +81,12 @@ class NotificationService { 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); // Map each notification to a test promise const promises = notifications.map(async (notification) => { try { - await this.sendNotification({ notification, subject, content, html, discordContent }); + await this.sendNotification({ notification, subject, content, html, discordContent, webhookBody }); return true; } catch (err) { return false; diff --git a/server/src/service/infrastructure/notificationUtils.js b/server/src/service/infrastructure/notificationUtils.js index b32886d52..d6b92cc0e 100644 --- a/server/src/service/infrastructure/notificationUtils.js +++ b/server/src/service/infrastructure/notificationUtils.js @@ -107,12 +107,16 @@ class NotificationUtils { const alertsToSend = []; const discordEmbeds = []; + const monitorInfoFields = [ + { name: "Monitor", value: monitor.name, inline: true }, + { name: "URL", value: monitor.url, inline: false }, + ]; const formatDiscordAlert = { cpu: () => ({ title: "CPU alert", description: `Your current CPU usage (${(cpuUsage * 100).toFixed(0)}%) is above your threshold (${(cpuThreshold * 100).toFixed(0)}%)`, color: 15548997, - + fields: monitorInfoFields, footer: { text: "Checkmate" }, }), @@ -120,7 +124,7 @@ class NotificationUtils { title: "Memory alert", description: `Your current memory usage (${(memoryUsage * 100).toFixed(0)}%) is above your threshold (${(memoryThreshold * 100).toFixed(0)}%)`, color: 15548997, - + fields: monitorInfoFields, footer: { text: "Checkmate" }, }), @@ -129,11 +133,14 @@ class NotificationUtils { description: `Your current disk usage is above your threshold (${(diskThreshold * 100).toFixed(0)}%)`, color: 15548997, footer: { text: "Checkmate" }, - fields: (Array.isArray(disk) ? disk : []).map((d, idx) => ({ - name: `Disk ${idx}`, - value: `${(d?.usage_percent * 100).toFixed(0)}%`, - inline: true, - })), + fields: [ + ...monitorInfoFields, + ...(Array.isArray(disk) ? disk : []).map((d, idx) => ({ + name: `Disk ${idx}`, + value: `${(d?.usage_percent * 100).toFixed(0)}%`, + inline: true, + })), + ], }), }; const alertTypes = ["cpu", "memory", "disk"]; @@ -174,8 +181,15 @@ class NotificationUtils { return { subject, html }; }; - buildHardwareNotificationMessage = (alerts) => { - return alerts.map((alert) => alert).join("\n"); + buildHardwareNotificationMessage = (alerts, monitor) => { + 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; }; }