Initial commit - Resource Monitor with Ubuntu installer
This commit is contained in:
commit
17c366dcae
16 files changed
+3865
No files matched your search
@@ -0,0 +1,55 @@
|
||||
const cron = require('node-cron');
|
||||
const db = require('./database');
|
||||
const monitorService = require('./monitorService');
|
||||
const notificationService = require('./notificationService');
|
||||
|
||||
class Scheduler {
|
||||
constructor() {
|
||||
this.jobs = new Map();
|
||||
}
|
||||
|
||||
start() {
|
||||
// Run checks every minute
|
||||
cron.schedule('* * * * *', async () => {
|
||||
await this.runChecks();
|
||||
});
|
||||
|
||||
// Clean old checks every hour (keep last 7 days)
|
||||
cron.schedule('0 * * * *', () => {
|
||||
db.prepare(`
|
||||
DELETE FROM checks
|
||||
WHERE checked_at < datetime('now', '-7 days')
|
||||
`).run();
|
||||
});
|
||||
|
||||
console.log('Scheduler started');
|
||||
}
|
||||
|
||||
async runChecks() {
|
||||
const resources = db.prepare(`
|
||||
SELECT * FROM resources WHERE enabled = 1
|
||||
`).all();
|
||||
|
||||
for (const resource of resources) {
|
||||
try {
|
||||
const result = await monitorService.checkResource(resource);
|
||||
monitorService.saveCheck(result);
|
||||
|
||||
const incident = monitorService.handleIncident(
|
||||
resource.id,
|
||||
result.status === 'down'
|
||||
);
|
||||
|
||||
if (incident.type !== 'none') {
|
||||
await notificationService.sendAlert(resource, incident);
|
||||
}
|
||||
|
||||
console.log(`Checked ${resource.name}: ${result.status} (${result.response_time}ms)`);
|
||||
} catch (error) {
|
||||
console.error(`Error checking ${resource.name}:`, error.message);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = new Scheduler();
|
||||
Reference in new issue
Block a user