fix hardware notifications

This commit is contained in:
Alex Holliday committed 2025-06-16 13:53:08 +08:00
1 parent 4fd2a29ab3
commit 3df6b451e7
1 file changed
+51 -9
+51 -9
View File
@@ -95,6 +95,19 @@ class NotificationService {
return MESSAGE_FORMATTERS[platform](messageText, chatId); 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) => { sendTestWebhookNotification = async (notification) => {
const config = notification.config; const config = notification.config;
const response = await this.networkService.requestWebhook( const response = await this.networkService.requestWebhook(
@@ -121,7 +134,7 @@ class NotificationService {
* @returns {Promise<boolean>} A promise that resolves to true if the notification was sent successfully, otherwise false. * @returns {Promise<boolean>} 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 { monitor, status, code } = networkResponse;
const { webhookUrl, platform, botToken, chatId } = notification.config; const { webhookUrl, platform, botToken, chatId } = notification.config;
@@ -152,7 +165,19 @@ class NotificationService {
url = `${TELEGRAM_API_BASE_URL}${botToken}/sendMessage`; url = `${TELEGRAM_API_BASE_URL}${botToken}/sendMessage`;
} }
const message = this.formatNotificationMessage( let message;
if (monitor.type === "hardware") {
message = this.formatHardwareNotificationMessage(
monitor,
status,
platform,
chatId,
code,
networkResponse.timestamp,
alerts
);
} else {
message = this.formatNotificationMessage(
monitor, monitor,
status, status,
platform, platform,
@@ -160,6 +185,7 @@ class NotificationService {
code, // Pass the code field directly code, // Pass the code field directly
networkResponse.timestamp networkResponse.timestamp
); );
}
try { try {
const response = await this.networkService.requestWebhook(platform, url, message); const response = await this.networkService.requestWebhook(platform, url, message);
@@ -247,11 +273,23 @@ class NotificationService {
return response; return response;
} }
async sendPagerDutyNotification(networkResponse, notification) { async sendPagerDutyNotification(networkResponse, notification, alerts = []) {
const { monitor, status, code } = networkResponse; const { monitor, status, code } = networkResponse;
const { routingKey, platform } = notification.config; const { routingKey, platform } = notification.config;
const message = this.formatNotificationMessage( let message;
if (monitor.type === "hardware") {
message = this.formatHardwareNotificationMessage(
monitor,
status,
platform,
null,
code,
networkResponse.timestamp,
alerts
);
} else {
message = this.formatNotificationMessage(
monitor, monitor,
status, status,
platform, platform,
@@ -259,7 +297,7 @@ class NotificationService {
code, // Pass the code field directly code, // Pass the code field directly
networkResponse.timestamp networkResponse.timestamp
); );
}
try { try {
const response = await this.networkService.requestPagerDuty({ const response = await this.networkService.requestPagerDuty({
message, message,
@@ -350,9 +388,9 @@ class NotificationService {
) ?? false, ) ?? false,
}; };
const notifications = await this.db.getNotificationsByMonitorId( const notificationIDs = networkResponse.monitor?.notifications ?? [];
networkResponse.monitorId const notifications = await this.db.getNotificationsByIds(notificationIDs);
);
for (const notification of notifications) { for (const notification of notifications) {
const alertsToSend = []; const alertsToSend = [];
const alertTypes = ["cpu", "memory", "disk"]; const alertTypes = ["cpu", "memory", "disk"];
@@ -388,7 +426,11 @@ class NotificationService {
if (alertsToSend.length === 0) continue; // No alerts to send, we're done if (alertsToSend.length === 0) continue; // No alerts to send, we're done
if (notification.type === "email") { 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; return true;