From 3df6b451e7074e8ed05ba183eb35c0613a8e8943 Mon Sep 17 00:00:00 2001 From: Alex Holliday Date: Mon, 16 Jun 2025 13:53:08 +0800 Subject: [PATCH] fix hardware notifications --- server/service/notificationService.js | 88 ++++++++++++++++++++------- 1 file changed, 65 insertions(+), 23 deletions(-) diff --git a/server/service/notificationService.js b/server/service/notificationService.js index 6e12722c2..a8dfa4729 100755 --- a/server/service/notificationService.js +++ b/server/service/notificationService.js @@ -95,6 +95,19 @@ class NotificationService { return MESSAGE_FORMATTERS[platform](messageText, chatId); } + formatHardwareNotificationMessage = ( + monitor, + status, + platform, + chatId, + code, + timestamp, + alerts + ) => { + const messageText = alerts.map((alert) => alert).join("\n"); + return MESSAGE_FORMATTERS[platform](messageText, chatId); + }; + sendTestWebhookNotification = async (notification) => { const config = notification.config; const response = await this.networkService.requestWebhook( @@ -121,7 +134,7 @@ class NotificationService { * @returns {Promise} A promise that resolves to true if the notification was sent successfully, otherwise false. */ - async sendWebhookNotification(networkResponse, notification) { + async sendWebhookNotification(networkResponse, notification, alerts = []) { const { monitor, status, code } = networkResponse; const { webhookUrl, platform, botToken, chatId } = notification.config; @@ -152,14 +165,27 @@ class NotificationService { url = `${TELEGRAM_API_BASE_URL}${botToken}/sendMessage`; } - const message = this.formatNotificationMessage( - monitor, - status, - platform, - chatId, - code, // Pass the code field directly - networkResponse.timestamp - ); + let message; + if (monitor.type === "hardware") { + message = this.formatHardwareNotificationMessage( + monitor, + status, + platform, + chatId, + code, + networkResponse.timestamp, + alerts + ); + } else { + message = this.formatNotificationMessage( + monitor, + status, + platform, + chatId, + code, // Pass the code field directly + networkResponse.timestamp + ); + } try { const response = await this.networkService.requestWebhook(platform, url, message); @@ -247,19 +273,31 @@ class NotificationService { return response; } - async sendPagerDutyNotification(networkResponse, notification) { + async sendPagerDutyNotification(networkResponse, notification, alerts = []) { const { monitor, status, code } = networkResponse; const { routingKey, platform } = notification.config; - const message = this.formatNotificationMessage( - monitor, - status, - platform, - null, - code, // Pass the code field directly - networkResponse.timestamp - ); - + let message; + if (monitor.type === "hardware") { + message = this.formatHardwareNotificationMessage( + monitor, + status, + platform, + null, + code, + networkResponse.timestamp, + alerts + ); + } else { + message = this.formatNotificationMessage( + monitor, + status, + platform, + null, + code, // Pass the code field directly + networkResponse.timestamp + ); + } try { const response = await this.networkService.requestPagerDuty({ message, @@ -350,9 +388,9 @@ class NotificationService { ) ?? false, }; - const notifications = await this.db.getNotificationsByMonitorId( - networkResponse.monitorId - ); + const notificationIDs = networkResponse.monitor?.notifications ?? []; + const notifications = await this.db.getNotificationsByIds(notificationIDs); + for (const notification of notifications) { const alertsToSend = []; const alertTypes = ["cpu", "memory", "disk"]; @@ -388,7 +426,11 @@ class NotificationService { if (alertsToSend.length === 0) continue; // No alerts to send, we're done if (notification.type === "email") { - this.sendHardwareEmail(networkResponse, notification.address, alertsToSend); + await this.sendHardwareEmail(networkResponse, notification.address, alertsToSend); + } else if (notification.type === "webhook") { + await this.sendWebhookNotification(networkResponse, notification, alertsToSend); + } else if (notification.type === "pager_duty") { + await this.sendPagerDutyNotification(networkResponse, notification, alertsToSend); } } return true;