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

77 lines
2.3 KiB
TypeScript
Raw Normal View History

2026-01-19 21:39:02 +00:00
import got from "got";
import type { Monitor, Notification, MonitorStatusResponse } from "@/types/index.js";
import { INotificationProvider } from "@/service/index.js";
2026-01-19 21:58:48 +00:00
import {
buildHardwareAlerts,
buildHardwareWebhookBody,
buildWebhookBody,
getTestMessage,
} from "@/service/infrastructure/notificationProviders/utils.js";
2026-01-19 21:39:02 +00:00
export class PagerDutyProvider implements INotificationProvider {
2026-01-19 21:58:48 +00:00
constructor(private logger: any) {}
2026-01-19 21:39:02 +00:00
private getHardwareContent = (monitor: Monitor, monitorStatusResponse: MonitorStatusResponse) => {
const { alertsToSend } = buildHardwareAlerts("HOST_PLACEHOLDER", monitor, monitorStatusResponse);
const body = buildHardwareWebhookBody(alertsToSend, monitor);
return body;
};
private getContent = (monitor: Monitor, monitorStatusResponse: MonitorStatusResponse) => {
const body = buildWebhookBody(monitor, monitorStatusResponse);
return body;
};
async sendAlert(notification: Notification, monitor: Monitor, monitorStatusResponse: MonitorStatusResponse): Promise<boolean> {
let body;
if (monitor.type === "hardware") {
body = this.getHardwareContent(monitor, monitorStatusResponse);
} else {
body = this.getContent(monitor, monitorStatusResponse);
}
try {
2026-01-19 21:58:48 +00:00
this.logger?.debug?.("Sending PagerDuty alert", { routingKey: notification.address });
2026-01-19 21:39:02 +00:00
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(),
},
},
responseType: "json",
});
return true;
} catch (error) {
2026-01-19 21:58:48 +00:00
this.logger?.error?.("PagerDuty alert failed", { error });
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 {
this.logger?.debug?.("Sending PagerDuty test alert", { routingKey: notification.address });
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) {
this.logger?.error?.("PagerDuty test alert failed", { error });
return false;
}
2026-01-19 21:39:02 +00:00
}
}