fix hardware notifications
This commit is contained in:
1 parent
4fd2a29ab3
commit
3df6b451e7
1 file changed
+65
-23
@@ -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,14 +165,27 @@ class NotificationService {
|
|||||||
url = `${TELEGRAM_API_BASE_URL}${botToken}/sendMessage`;
|
url = `${TELEGRAM_API_BASE_URL}${botToken}/sendMessage`;
|
||||||
}
|
}
|
||||||
|
|
||||||
const message = this.formatNotificationMessage(
|
let message;
|
||||||
monitor,
|
if (monitor.type === "hardware") {
|
||||||
status,
|
message = this.formatHardwareNotificationMessage(
|
||||||
platform,
|
monitor,
|
||||||
chatId,
|
status,
|
||||||
code, // Pass the code field directly
|
platform,
|
||||||
networkResponse.timestamp
|
chatId,
|
||||||
);
|
code,
|
||||||
|
networkResponse.timestamp,
|
||||||
|
alerts
|
||||||
|
);
|
||||||
|
} else {
|
||||||
|
message = this.formatNotificationMessage(
|
||||||
|
monitor,
|
||||||
|
status,
|
||||||
|
platform,
|
||||||
|
chatId,
|
||||||
|
code, // Pass the code field directly
|
||||||
|
networkResponse.timestamp
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const response = await this.networkService.requestWebhook(platform, url, message);
|
const response = await this.networkService.requestWebhook(platform, url, message);
|
||||||
@@ -247,19 +273,31 @@ 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;
|
||||||
monitor,
|
if (monitor.type === "hardware") {
|
||||||
status,
|
message = this.formatHardwareNotificationMessage(
|
||||||
platform,
|
monitor,
|
||||||
null,
|
status,
|
||||||
code, // Pass the code field directly
|
platform,
|
||||||
networkResponse.timestamp
|
null,
|
||||||
);
|
code,
|
||||||
|
networkResponse.timestamp,
|
||||||
|
alerts
|
||||||
|
);
|
||||||
|
} else {
|
||||||
|
message = this.formatNotificationMessage(
|
||||||
|
monitor,
|
||||||
|
status,
|
||||||
|
platform,
|
||||||
|
null,
|
||||||
|
code, // Pass the code field directly
|
||||||
|
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;
|
||||||
|
|||||||
Reference in new issue
Block a user