const express = require('express'); const os = require('os'); const router = express.Router(); router.get('/', (req, res) => { // Get CPU usage const cpus = os.cpus(); let totalIdle = 0; let totalTick = 0; cpus.forEach(cpu => { // Calculate idle time and total time for each CPU core const idle = cpu.times.idle; const user = cpu.times.user; const nice = cpu.times.nice; const sys = cpu.times.sys; const irq = cpu.times.irq; // Calculate total time by summing all CPU states const total = user + nice + sys + idle + irq; // Add to running totals to get average across all cores totalTick += total; totalIdle += idle; }); const cpuUsage = 100 - (totalIdle / totalTick * 100); // Get memory usage const totalMemory = os.totalmem() / 1024 / 1024 / 1024; const freeMemory = os.freemem() / 1024 / 1024 / 1024; const usedMemory = totalMemory - freeMemory; const memoryUsage = (usedMemory / totalMemory * 100); res.json({ cpu: Math.round(cpuUsage * 100) / 100, memory: Math.round(memoryUsage * 100) / 100, totalMemory: totalMemory.toFixed(2), freeMemory: freeMemory.toFixed(2) }); }); module.exports = router;