Files
Admin_dash/monitor.js
Shivam Patel 032fdc1b12 Fix website URL and uptime check logic
- Website: www.akkolli.net has no DNS record, use akkolli.net instead
- Uptime: treat any response < 500 as up (was res.ok / 200-299 only).
  Nextcloud returns non-200 for untrusted Host headers but is still
  running. Only 5xx and network errors count as down.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-02-09 01:48:53 -05:00

82 lines
2.5 KiB
JavaScript

const sqlite3 = require('sqlite3');
const { open } = require('sqlite');
// Node 18+ has global fetch built-in
const SERVICES = [
{ name: 'Website', url: 'https://akkolli.net' },
{ name: 'Gitea', url: 'https://code.akkolli.net' },
{ name: 'Nextcloud', url: 'http://host.docker.internal:6060' },
];
async function monitor() {
console.log('Starting monitoring loop...');
const dbPath = process.env.DB_PATH || './dashboard.db';
const db = await open({
filename: dbPath,
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);
// Any HTTP response means the service is reachable (up).
// Only network errors/timeouts (caught below) count as down.
status = res.status < 500 ? '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();