Open-source status page, incident response and uptime monitoring
TypeScript alertingincident-managementmcpmcp-servermonitoringmonitoring-as-codeobservabilityon-callopen-sourcesrestatus-pagestatuspagesynthetic-monitoringterraformtinybirdtursouptimeuptime-checkeruptime-monitor
import { ntfyDataSchema } from "@openstatus/db/src/schema";
import type { NotificationContext } from "@openstatus/notification-base";
import { safeFetch } from "@openstatus/utils";
export const sendAlert = async ({
monitor,
notification,
statusCode,
message,
}: NotificationContext) => {
const notificationData = ntfyDataSchema.parse(JSON.parse(notification.data));
const { name } = monitor;
const body = `Your monitor ${name} / ${monitor.url} is down with ${
statusCode ? `status code ${statusCode}` : `error: ${message}`
}`;
const authorization = notificationData.ntfy.token
? { Authorization: `Bearer ${notificationData.ntfy.token}` }
: undefined;
const url = notificationData.ntfy.serverUrl
? `${notificationData.ntfy.serverUrl}/${notificationData.ntfy.topic}`
: `https://ntfy.sh/${notificationData.ntfy.topic}`;
const res = await safeFetch(url, {
method: "post",
body,
headers: {
...authorization,
},
});
if (!res.ok) {
throw new Error(
`Failed to send alert notification: ${res.status} ${res.statusText}`,
);
}
};
export const sendRecovery = async ({
monitor,
notification,
}: NotificationContext) => {
const notificationData = ntfyDataSchema.parse(JSON.parse(notification.data));
const { name } = monitor;
const body = `Your monitor ${name} / ${monitor.url} is up again`;
const authorization = notificationData.ntfy.token
? { Authorization: `Bearer ${notificationData.ntfy.token}` }
: undefined;
const url = notificationData.ntfy.serverUrl
? `${notificationData.ntfy.serverUrl}/${notificationData.ntfy.topic}`
: `https://ntfy.sh/${notificationData.ntfy.topic}`;
const res = await safeFetch(url, {
method: "post",
body,
headers: {
...authorization,
},
});
if (!res.ok) {
throw new Error(
`Failed to send recovery notification: ${res.status} ${res.statusText}`,
);
}
};
export const sendDegraded = async ({
monitor,
notification,
}: NotificationContext) => {
const notificationData = ntfyDataSchema.parse(JSON.parse(notification.data));
const { name } = monitor;
const body = `Your monitor ${name} / ${monitor.url} is degraded `;
const authorization = notificationData.ntfy.token
? { Authorization: `Bearer ${notificationData.ntfy.token}` }
: undefined;
const url = notificationData.ntfy.serverUrl
? `${notificationData.ntfy.serverUrl}/${notificationData.ntfy.topic}`
: `https://ntfy.sh/${notificationData.ntfy.topic}`;
const res = await safeFetch(url, {
method: "post",
body,
headers: {
...authorization,
},
});
if (!res.ok) {
throw new Error(
`Failed to send degraded notification: ${res.status} ${res.statusText}`,
);
}
};
export const sendTest = async ({
serverUrl,
topic,
token,
}: {
topic: string;
serverUrl?: string;
token?: string;
}) => {
const authorization = token
? { Authorization: `Bearer ${token}` }
: undefined;
const url = serverUrl ? `${serverUrl}/${topic}` : `https://ntfy.sh/${topic}`;
try {
const res = await safeFetch(url, {
method: "post",
body: "This is a test message from OpenStatus",
headers: {
...authorization,
},
});
if (!res.ok) {
throw new Error(`Failed to send test: ${res.status} ${res.statusText}`);
}
} catch (err) {
console.log(err);
return false;
}
return true;
};