Add initial project files including package.json, tsconfig.json, and index.ts for uptime monitoring

This commit is contained in:
2026-03-09 08:18:34 +01:00
parent a799e1996c
commit 1d81eb1e0b
4 changed files with 322 additions and 0 deletions

43
src/index.ts Normal file
View File

@@ -0,0 +1,43 @@
import ping from "ping";
class Uptime {
private startTime: Date = new Date();
private events: { up: boolean, time: Date }[] = [];
private lastUp: boolean = true;
private totalUp: number = 1;
private totalDown: number = 0;
private totalIte: number = 1;
constructor() {
console.log('Starting...');
console.log(`Starting at ${this.startTime.toLocaleDateString()} ${this.startTime.toLocaleTimeString()}`);
this.handle();
setInterval(() => this.handle(), 1000 * 30);
}
async handle() {
console.log('Fetch status...')
await ping.sys.probe('node.under-scape.com', (isAlive) => {
const now = new Date();
this.totalIte++;
if (isAlive.alive) {
this.totalUp++;
} else {
this.totalDown++;
}
if (isAlive.alive != this.lastUp) {
this.lastUp = isAlive.alive;
this.events.push({
up: isAlive.alive,
time: this.startTime
});
}
console.log(`[${now.toLocaleDateString()} ${now.toLocaleTimeString()}] Total: ${this.totalIte} Up: ${this.totalUp} Down: ${this.totalDown} Uptime: ${Math.round((this.totalUp / this.totalIte) * 100)}%`)
}, {timeout: 10});
}
}
new Uptime();