Readme

Uptime Checker & Status Page

This is a free, hackable uptime/downtime monitor that sends you an email when the site doesn't return a 200. It also stores historical uptime and latency data in your Val Town SQLite, which is used to power a status page. It supports multiple URLs in the same database and status page.

Installation

  1. Fork this val
  2. Edit the list of URLs to what you want to check
  3. For the status page, fork this val: @stevekrouse/status
Runs every 15 min
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
import { email } from "https://esm.town/v/std/email?v=11";
import { sqlite } from "https://esm.town/v/std/sqlite?v=6";
await sqlite.execute(
"CREATE TABLE IF NOT EXISTS uptime (id INTEGER PRIMARY KEY AUTOINCREMENT, url TEXT, ok INTEGER, reason TEXT, status INTEGER, duration INTEGER, timestamp DATETIME DEFAULT CURRENT_TIMESTAMP);",
);
export async function uptimeCheck(url: string) {
let reason: string, status: number, end: number;
let ok = true;
const start = performance.now();
try {
const res = await fetch(url);
end = performance.now();
status = res.status;
if (res.status === 200) {
console.log(`Website up (${url}): ${res.status} (${end - start}ms)`);
} else {
ok = false;
console.log(`Website down (${url}): ${res.status} (${end - start}ms)`);
}
} catch (e) {
end = performance.now();
reason = `couldn't fetch: ${e}`;
ok = false;
console.log(`Website down (${url}): ${reason} (${end - start}ms)`);
}
if (!ok) {
email({ subject: `Website down (${url})` });
}
await sqlite.execute(
{
sql: "INSERT INTO uptime (url, ok, reason, status, duration) VALUES (?, ?, ?, ?, ?)",
args: [url, ok ? 1 : 0, reason, status, end - start],
},
);
}
export default async () => {
["https://jonnie.com", "https://faith.tools/"].forEach(uptimeCheck);
};
Val Town is a social website to write and deploy JavaScript.
Build APIs and schedule functions from your browser.
Comments
Nobody has commented on this val yet: be the first!
v18
July 1, 2024