Files
Admin_dash/monitor.js

79 lines
2.5 KiB
JavaScript
Raw Normal View History

2026-02-08 03:03:53 -05:00
const sqlite3 = require('sqlite3');
const { open } = require('sqlite');
const fetch = require('node-fetch'); // Native fetch in Node 18, but let's be safe or use global fetch
// Node 18 has global fetch, so we don't need node-fetch if running with node 18
const SERVICES = [
{ name: 'Website', url: 'https://www.akkolli.net' },
{ name: 'Gitea', url: 'https://code.akkolli.net' },
{ name: 'Nextcloud', url: 'http://localhost:6060' },
];
async function monitor() {
console.log('Starting monitoring loop...');
const db = await open({
filename: './dashboard.db',
driver: sqlite3.Database
});
// Ensure table exists (in case monitor runs before app)
await db.exec(`
CREATE TABLE IF NOT EXISTS uptime_logs (
id INTEGER PRIMARY KEY AUTOINCREMENT,
service_name TEXT NOT NULL,
url TEXT NOT NULL,
status TEXT NOT NULL,
latency INTEGER,
timestamp DATETIME DEFAULT CURRENT_TIMESTAMP
);
`);
setInterval(async () => {
console.log('Running checks...');
const now = new Date().toISOString();
for (const service of SERVICES) {
const start = performance.now();
let status = 'down';
let latency = 0;
try {
const controller = new AbortController();
const timeout = setTimeout(() => controller.abort(), 5000);
const res = await fetch(service.url, {
method: 'HEAD',
signal: controller.signal
});
clearTimeout(timeout);
status = res.ok ? 'up' : 'down';
const end = performance.now();
latency = Math.round(end - start);
} catch (err) {
status = 'down';
latency = 0;
// console.error(`Failed to reach ${service.name}:`, err.message);
}
try {
await db.run(
`INSERT INTO uptime_logs (service_name, url, status, latency, timestamp) VALUES (?, ?, ?, ?, ?)`,
service.name, service.url, status, latency, now
);
} catch (dbErr) {
console.error('DB Write Error:', dbErr);
}
}
// Prune old logs (keep 7 days)
try {
await db.run(`DELETE FROM uptime_logs WHERE timestamp < datetime('now', '-7 days')`);
} catch (e) { }
}, 60000); // Run every minute
}
monitor();