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

108 lines
2.9 KiB
TypeScript
Raw Normal View History

2026-01-19 22:16:33 +00:00
const SERVICE_NAME = "PagerDutyProvider";
2026-01-19 21:39:02 +00:00
import got from "got";
import type { Monitor, Notification, MonitorStatusResponse } from "@/types/index.js";
2026-02-15 18:35:54 +00:00
import type { MonitorActionDecision } from "@/service/infrastructure/SuperSimpleQueue/SuperSimpleQueueHelper.js";
2026-01-19 21:39:02 +00:00
import { INotificationProvider } from "@/service/index.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 21:39:02 +00:00
export class PagerDutyProvider 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 21:39:02 +00:00
return body;
};
private getContent = (monitor: Monitor, monitorStatusResponse: MonitorStatusResponse) => {
const body = buildWebhookBody(monitor, monitorStatusResponse);
return body;
};
2026-02-15 18:35:54 +00:00
async sendAlert(
notification: Notification,
monitor: Monitor,
monitorStatusResponse: MonitorStatusResponse,
decision: MonitorActionDecision,
clientHost: string
2026-02-15 18:35:54 +00:00
): Promise<boolean> {
2026-01-19 21:39:02 +00:00
let body;
if (monitor.type === "hardware") {
body = this.getHardwareContent(clientHost, monitor, monitorStatusResponse, decision);
2026-01-19 21:39:02 +00:00
} else {
body = this.getContent(monitor, monitorStatusResponse);
}
try {
await got.post("https://events.pagerduty.com/v2/enqueue", {
json: {
routing_key: notification.address,
event_action: "trigger",
payload: {
summary: body,
severity: "critical",
source: monitor.url,
timestamp: new Date().toISOString(),
},
},
});
return true;
} catch (error) {
2026-01-19 22:16:33 +00:00
const err = error as Error;
this.logger.warn({
message: "PagerDuty alert failed",
service: SERVICE_NAME,
method: "sendAlert",
stack: err?.stack,
});
2026-01-19 21:39:02 +00:00
return false;
}
}
async sendTestAlert(notification: Notification): Promise<boolean> {
2026-01-19 21:58:48 +00:00
try {
await got.post("https://events.pagerduty.com/v2/enqueue", {
json: {
routing_key: notification.address,
event_action: "trigger",
payload: {
summary: getTestMessage(),
severity: "info",
source: "checkmate",
timestamp: new Date().toISOString(),
},
},
responseType: "json",
});
return true;
} catch (error) {
2026-01-19 22:16:33 +00:00
const err = error as Error;
this.logger.warn({
message: "PagerDuty test alert failed",
service: SERVICE_NAME,
method: "sendTestAlert",
stack: err?.stack,
});
2026-01-19 21:58:48 +00:00
return false;
}
2026-01-19 21:39:02 +00:00
}
}