Add initial project files including package.json, tsconfig.json, and index.ts for uptime monitoring
This commit is contained in:
43
src/index.ts
Normal file
43
src/index.ts
Normal 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();
|
||||
Reference in New Issue
Block a user