Files
Checkmate/server/src/service/infrastructure/notificationProviders/webhook.ts
T

194 lines
5.4 KiB
TypeScript
Raw Normal View History

2026-01-19 22:16:33 +00:00
const SERVICE_NAME = "WebhookProvider";
2026-01-18 16:59:38 +00:00
import type { Monitor, Alert, Notification, MonitorStatusResponse } from "@/types/index.js";
import { INotificationProvider } from "@/service/index.js";
2026-02-15 18:35:54 +00:00
import type { MonitorActionDecision } from "@/service/infrastructure/SuperSimpleQueue/SuperSimpleQueueHelper.js";
2026-02-16 20:55:00 +00:00
import type { NotificationMessage } from "@/types/notificationMessage.js";
2026-01-19 21:58:48 +00:00
import {
buildHardwareAlerts,
2026-02-16 00:17:51 +00:00
buildHardwareNotificationMessage,
2026-01-19 21:58:48 +00:00
buildWebhookBody,
getTestMessage,
} from "@/service/infrastructure/notificationProviders/utils.js";
2026-01-19 18:58:17 +00:00
import got from "got";
2026-01-18 16:59:38 +00:00
export class WebhookProvider implements INotificationProvider {
2026-01-19 22:16:33 +00:00
private logger: any;
constructor(logger: any) {
this.logger = logger;
}
2026-02-15 18:35:54 +00:00
private getHardwareContent = (
clientHost: string,
monitor: Monitor,
monitorStatusResponse: MonitorStatusResponse,
decision: MonitorActionDecision
) => {
// For status changes (recovery), use standard format
if (decision.notificationReason === "status_change") {
return buildWebhookBody(monitor, monitorStatusResponse);
}
// For threshold breaches, use hardware alert format
const { alertsToSend } = buildHardwareAlerts(clientHost, monitor, monitorStatusResponse);
2026-02-16 00:17:51 +00:00
const body = buildHardwareNotificationMessage(clientHost, alertsToSend, monitor);
2026-01-19 18:58:17 +00:00
return body;
2026-01-18 16:59:38 +00:00
};
2026-01-19 21:58:48 +00:00
private getContent = (monitor: Monitor, monitorStatusResponse: MonitorStatusResponse): string => {
2026-01-19 18:58:17 +00:00
const body = buildWebhookBody(monitor, monitorStatusResponse);
return body;
2026-01-18 16:59:38 +00:00
};
sendAlert = async (
notification: Notification,
monitor: Monitor,
monitorStatusResponse: MonitorStatusResponse,
decision: MonitorActionDecision,
clientHost: string
) => {
2026-01-19 18:58:17 +00:00
let body;
if (monitor.type === "hardware") {
body = this.getHardwareContent(clientHost, monitor, monitorStatusResponse, decision);
2026-01-19 18:58:17 +00:00
} else {
body = this.getContent(monitor, monitorStatusResponse);
}
if (!notification.address) {
return false;
}
try {
await got.post(notification.address, {
2026-01-19 21:58:48 +00:00
json: { text: body },
2026-01-19 18:58:17 +00:00
headers: {
"Content-Type": "application/json",
},
});
return true;
} catch (error) {
2026-01-19 22:16:33 +00:00
const err = error as Error;
this.logger.warn({
message: "Webhook alert failed",
service: SERVICE_NAME,
method: "sendAlert",
stack: err?.stack,
});
2026-01-19 18:58:17 +00:00
return false;
}
};
2026-02-16 20:55:00 +00:00
/**
* New unified message format - builds webhook payload from NotificationMessage
*/
sendMessage = async (notification: Notification, message: NotificationMessage): Promise<boolean> => {
if (!notification.address) {
return false;
}
// Build webhook payload from unified message
const payload = this.buildWebhookPayload(message);
try {
await got.post(notification.address, {
json: payload,
headers: {
"Content-Type": "application/json",
},
});
this.logger.info({
message: "[NEW] Webhook notification sent via sendMessage",
service: SERVICE_NAME,
method: "sendMessage",
});
return true;
} catch (error) {
const err = error as Error;
this.logger.warn({
message: "[NEW] Webhook alert failed via sendMessage",
service: SERVICE_NAME,
method: "sendMessage",
stack: err?.stack,
});
return false;
}
};
/**
* Build webhook payload from NotificationMessage
* Format: { text: string, severity: string, monitor: object, details: object }
*/
private buildWebhookPayload(message: NotificationMessage): object {
const lines: string[] = [];
// Title and summary
lines.push(`**${message.content.title}**`);
lines.push(message.content.summary);
lines.push("");
// Monitor information
lines.push("**Monitor Details:**");
lines.push(`- Name: ${message.monitor.name}`);
lines.push(`- URL: ${message.monitor.url}`);
lines.push(`- Type: ${message.monitor.type}`);
lines.push(`- Status: ${message.monitor.status}`);
lines.push("");
// Additional details
if (message.content.details && message.content.details.length > 0) {
lines.push("**Additional Information:**");
message.content.details.forEach((detail) => lines.push(`- ${detail}`));
lines.push("");
}
// Threshold breaches (for hardware monitors)
if (message.content.thresholds && message.content.thresholds.length > 0) {
lines.push("**Threshold Breaches:**");
message.content.thresholds.forEach((breach) => {
lines.push(`- ${breach.metric.toUpperCase()}: ${breach.formattedValue} (threshold: ${breach.threshold}${breach.unit})`);
});
lines.push("");
}
// Incident link
if (message.content.incident) {
lines.push(`[View Incident](${message.clientHost}/infrastructure/${message.monitor.id})`);
}
// Return webhook payload with both text and structured data
return {
text: lines.join("\n"),
severity: message.severity,
type: message.type,
monitor: {
id: message.monitor.id,
name: message.monitor.name,
url: message.monitor.url,
status: message.monitor.status,
},
timestamp: message.content.timestamp,
};
}
2026-01-19 18:58:17 +00:00
sendTestAlert = async (notification: Notification) => {
2026-01-19 21:58:48 +00:00
if (!notification.address) {
return false;
}
try {
await got.post(notification.address, {
json: { text: getTestMessage() },
headers: {
"Content-Type": "application/json",
},
});
return true;
} catch (error) {
2026-01-19 22:16:33 +00:00
const err = error as Error;
this.logger.warn({
message: "Webhook test alert failed",
service: SERVICE_NAME,
method: "sendTestAlert",
stack: err?.stack,
});
2026-01-19 21:58:48 +00:00
return false;
}
2026-01-19 18:58:17 +00:00
};
2026-01-18 16:59:38 +00:00
}