Spaces:
Paused
Paused
Fix CPU usage, disk usage, and UI
Browse files- public/index.html +71 -80
- server.js +89 -51
public/index.html
CHANGED
|
@@ -8,32 +8,31 @@
|
|
| 8 |
* { margin: 0; padding: 0; box-sizing: border-box; }
|
| 9 |
body {
|
| 10 |
font-family: 'Segoe UI', system-ui, -apple-system, sans-serif;
|
| 11 |
-
background: #06060e;
|
| 12 |
-
color: #e0e0e0;
|
| 13 |
-
min-height: 100vh;
|
| 14 |
}
|
| 15 |
-
.container { max-width:
|
| 16 |
|
| 17 |
.header {
|
| 18 |
display: flex; align-items: center; justify-content: space-between;
|
| 19 |
margin-bottom: 2rem; flex-wrap: wrap; gap: 1rem;
|
| 20 |
}
|
| 21 |
.header h1 {
|
| 22 |
-
font-size: 1.
|
| 23 |
background: linear-gradient(135deg, #22d3ee, #818cf8);
|
| 24 |
-
-webkit-background-clip: text; -webkit-text-fill-color: transparent;
|
|
|
|
| 25 |
}
|
| 26 |
-
.header-right { display: flex; align-items: center; gap: 0.
|
| 27 |
.status-dot {
|
| 28 |
-
width:
|
| 29 |
animation: pulse 2s infinite;
|
| 30 |
}
|
| 31 |
@keyframes pulse {
|
| 32 |
0%,100% { box-shadow: 0 0 0 0 rgba(34,197,94,0.5); }
|
| 33 |
50% { box-shadow: 0 0 0 8px rgba(34,197,94,0); }
|
| 34 |
}
|
| 35 |
-
.status-text { font-size: 0.
|
| 36 |
-
.refresh-info { font-size: 0.7rem; color: #444; }
|
| 37 |
|
| 38 |
.stats {
|
| 39 |
display: grid; grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
|
|
@@ -41,44 +40,40 @@
|
|
| 41 |
}
|
| 42 |
.stat-card {
|
| 43 |
background: linear-gradient(135deg, #0d0d1a, #111128);
|
| 44 |
-
border: 1px solid #1a1a3a; border-radius:
|
| 45 |
-
transition: border-color 0.3s;
|
| 46 |
}
|
| 47 |
.stat-card:hover { border-color: #6366f1; }
|
| 48 |
-
.stat-label { font-size: 0.
|
| 49 |
-
.stat-value { font-size: 1.
|
| 50 |
-
.stat-sub { font-size: 0.
|
| 51 |
-
.progress-bar { height:
|
| 52 |
.progress-fill { height: 100%; border-radius: 3px; transition: width 0.8s ease; }
|
| 53 |
.fill-green { background: linear-gradient(90deg, #22c55e, #16a34a); }
|
| 54 |
.fill-yellow { background: linear-gradient(90deg, #eab308, #ca8a04); }
|
| 55 |
.fill-red { background: linear-gradient(90deg, #ef4444, #dc2626); }
|
| 56 |
|
| 57 |
.section { margin-bottom: 2rem; }
|
| 58 |
-
.section-title { font-size:
|
| 59 |
-
|
| 60 |
.info-list { background: #0a0a15; border: 1px solid #1a1a30; border-radius: 10px; overflow: hidden; }
|
| 61 |
.info-row {
|
| 62 |
display: flex; justify-content: space-between; align-items: center;
|
| 63 |
-
padding: 0.
|
| 64 |
}
|
| 65 |
.info-row:last-child { border-bottom: none; }
|
| 66 |
.info-key { color: #777; }
|
| 67 |
-
.info-val { color: #c4c4e0; font-weight: 500; text-align: right; font-family: monospace; }
|
| 68 |
-
|
| 69 |
-
.loading .info-val { color: #22c55e; animation: blink 1s infinite; }
|
| 70 |
-
@keyframes blink { 0%,100% { opacity: 1; } 50% { opacity: 0.3; } }
|
| 71 |
|
| 72 |
.footer {
|
| 73 |
text-align: center; margin-top: 3rem; padding: 1.5rem 0;
|
| 74 |
-
border-top: 1px solid #111125; color: #333; font-size: 0.
|
| 75 |
}
|
| 76 |
.footer a { color: #6366f1; text-decoration: none; }
|
| 77 |
|
| 78 |
@media (max-width: 600px) {
|
| 79 |
.header { flex-direction: column; align-items: flex-start; }
|
| 80 |
.stats { grid-template-columns: 1fr 1fr; }
|
| 81 |
-
.info-row { flex-direction: column; align-items: flex-start; gap: 0.
|
| 82 |
}
|
| 83 |
</style>
|
| 84 |
</head>
|
|
@@ -87,12 +82,12 @@
|
|
| 87 |
<div class="header">
|
| 88 |
<div>
|
| 89 |
<h1>⚡ Server Dashboard</h1>
|
| 90 |
-
<p style="color:#555;font-size:0.
|
| 91 |
</div>
|
| 92 |
<div class="header-right">
|
| 93 |
<div class="status-dot"></div>
|
| 94 |
<span class="status-text">Online</span>
|
| 95 |
-
<span class="refresh-info" id="lastUpdate">
|
| 96 |
</div>
|
| 97 |
</div>
|
| 98 |
|
|
@@ -124,7 +119,7 @@
|
|
| 124 |
|
| 125 |
<div class="section">
|
| 126 |
<div class="section-title">📋 Server Info</div>
|
| 127 |
-
<div class="info-list
|
| 128 |
<div class="info-row"><span class="info-key">Hostname</span><span class="info-val" id="infoHostname">...</span></div>
|
| 129 |
<div class="info-row"><span class="info-key">OS</span><span class="info-val" id="infoOS">...</span></div>
|
| 130 |
<div class="info-row"><span class="info-key">Kernel</span><span class="info-val" id="infoKernel">...</span></div>
|
|
@@ -140,65 +135,61 @@
|
|
| 140 |
</div>
|
| 141 |
|
| 142 |
<script>
|
| 143 |
-
|
| 144 |
-
|
| 145 |
-
function
|
| 146 |
-
|
| 147 |
-
|
| 148 |
-
|
| 149 |
-
|
| 150 |
-
|
| 151 |
-
|
| 152 |
-
document.getElementById('
|
| 153 |
-
|
| 154 |
-
|
| 155 |
-
|
| 156 |
-
document.getElementById('
|
| 157 |
-
const
|
| 158 |
-
|
| 159 |
-
|
| 160 |
-
|
| 161 |
-
|
| 162 |
-
document.getElementById('
|
| 163 |
-
|
| 164 |
-
|
| 165 |
-
|
| 166 |
-
document.getElementById('
|
| 167 |
-
document.getElementById('
|
| 168 |
-
|
| 169 |
-
|
| 170 |
-
|
| 171 |
-
|
| 172 |
-
|
| 173 |
-
document.getElementById('
|
| 174 |
-
document.getElementById('
|
| 175 |
-
|
| 176 |
-
|
| 177 |
-
|
| 178 |
-
|
| 179 |
-
|
| 180 |
-
document.getElementById('
|
| 181 |
-
document.getElementById('
|
| 182 |
-
document.getElementById('
|
| 183 |
-
|
| 184 |
-
|
| 185 |
-
|
| 186 |
-
|
| 187 |
-
document.getElementById('infoTime').textContent = ' ' + now + ' WIB';
|
| 188 |
-
document.getElementById('lastUpdate').textContent = 'Updated: ' + now;
|
| 189 |
-
|
| 190 |
-
// Remove loading class
|
| 191 |
-
document.getElementById('infoList').classList.remove('loading');
|
| 192 |
}
|
| 193 |
|
| 194 |
async function fetchStats() {
|
| 195 |
try {
|
| 196 |
const res = await fetch('/api/stats');
|
|
|
|
| 197 |
const data = await res.json();
|
| 198 |
-
|
| 199 |
} catch (err) {
|
| 200 |
-
|
| 201 |
-
|
| 202 |
}
|
| 203 |
}
|
| 204 |
|
|
|
|
| 8 |
* { margin: 0; padding: 0; box-sizing: border-box; }
|
| 9 |
body {
|
| 10 |
font-family: 'Segoe UI', system-ui, -apple-system, sans-serif;
|
| 11 |
+
background: #06060e; color: #e0e0e0; min-height: 100vh;
|
|
|
|
|
|
|
| 12 |
}
|
| 13 |
+
.container { max-width: 960px; margin: 0 auto; padding: 1.5rem 1rem; }
|
| 14 |
|
| 15 |
.header {
|
| 16 |
display: flex; align-items: center; justify-content: space-between;
|
| 17 |
margin-bottom: 2rem; flex-wrap: wrap; gap: 1rem;
|
| 18 |
}
|
| 19 |
.header h1 {
|
| 20 |
+
font-size: 1.5rem; font-weight: 700;
|
| 21 |
background: linear-gradient(135deg, #22d3ee, #818cf8);
|
| 22 |
+
-webkit-background-clip: text; -webkit-text-fill-color: transparent;
|
| 23 |
+
background-clip: text;
|
| 24 |
}
|
| 25 |
+
.header-right { display: flex; align-items: center; gap: 0.5rem; }
|
| 26 |
.status-dot {
|
| 27 |
+
width: 8px; height: 8px; background: #22c55e; border-radius: 50%;
|
| 28 |
animation: pulse 2s infinite;
|
| 29 |
}
|
| 30 |
@keyframes pulse {
|
| 31 |
0%,100% { box-shadow: 0 0 0 0 rgba(34,197,94,0.5); }
|
| 32 |
50% { box-shadow: 0 0 0 8px rgba(34,197,94,0); }
|
| 33 |
}
|
| 34 |
+
.status-text { font-size: 0.8rem; color: #22c55e; }
|
| 35 |
+
.refresh-info { font-size: 0.7rem; color: #444; margin-left: 0.5rem; }
|
| 36 |
|
| 37 |
.stats {
|
| 38 |
display: grid; grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
|
|
|
|
| 40 |
}
|
| 41 |
.stat-card {
|
| 42 |
background: linear-gradient(135deg, #0d0d1a, #111128);
|
| 43 |
+
border: 1px solid #1a1a3a; border-radius: 10px; padding: 1.2rem;
|
| 44 |
+
transition: border-color 0.3s;
|
| 45 |
}
|
| 46 |
.stat-card:hover { border-color: #6366f1; }
|
| 47 |
+
.stat-label { font-size: 0.7rem; color: #666; text-transform: uppercase; letter-spacing: 0.05em; margin-bottom: 0.4rem; }
|
| 48 |
+
.stat-value { font-size: 1.8rem; font-weight: 700; }
|
| 49 |
+
.stat-sub { font-size: 0.75rem; color: #555; margin-top: 0.3rem; line-height: 1.4; }
|
| 50 |
+
.progress-bar { height: 5px; background: #1a1a2e; border-radius: 3px; margin-top: 0.6rem; overflow: hidden; }
|
| 51 |
.progress-fill { height: 100%; border-radius: 3px; transition: width 0.8s ease; }
|
| 52 |
.fill-green { background: linear-gradient(90deg, #22c55e, #16a34a); }
|
| 53 |
.fill-yellow { background: linear-gradient(90deg, #eab308, #ca8a04); }
|
| 54 |
.fill-red { background: linear-gradient(90deg, #ef4444, #dc2626); }
|
| 55 |
|
| 56 |
.section { margin-bottom: 2rem; }
|
| 57 |
+
.section-title { font-size: 0.95rem; color: #818cf8; margin-bottom: 0.6rem; }
|
|
|
|
| 58 |
.info-list { background: #0a0a15; border: 1px solid #1a1a30; border-radius: 10px; overflow: hidden; }
|
| 59 |
.info-row {
|
| 60 |
display: flex; justify-content: space-between; align-items: center;
|
| 61 |
+
padding: 0.65rem 1rem; border-bottom: 1px solid #121225; font-size: 0.85rem;
|
| 62 |
}
|
| 63 |
.info-row:last-child { border-bottom: none; }
|
| 64 |
.info-key { color: #777; }
|
| 65 |
+
.info-val { color: #c4c4e0; font-weight: 500; text-align: right; font-family: monospace; font-size: 0.8rem; }
|
|
|
|
|
|
|
|
|
|
| 66 |
|
| 67 |
.footer {
|
| 68 |
text-align: center; margin-top: 3rem; padding: 1.5rem 0;
|
| 69 |
+
border-top: 1px solid #111125; color: #333; font-size: 0.7rem;
|
| 70 |
}
|
| 71 |
.footer a { color: #6366f1; text-decoration: none; }
|
| 72 |
|
| 73 |
@media (max-width: 600px) {
|
| 74 |
.header { flex-direction: column; align-items: flex-start; }
|
| 75 |
.stats { grid-template-columns: 1fr 1fr; }
|
| 76 |
+
.info-row { flex-direction: column; align-items: flex-start; gap: 0.2rem; }
|
| 77 |
}
|
| 78 |
</style>
|
| 79 |
</head>
|
|
|
|
| 82 |
<div class="header">
|
| 83 |
<div>
|
| 84 |
<h1>⚡ Server Dashboard</h1>
|
| 85 |
+
<p style="color:#555;font-size:0.8rem;" id="hostname">Loading...</p>
|
| 86 |
</div>
|
| 87 |
<div class="header-right">
|
| 88 |
<div class="status-dot"></div>
|
| 89 |
<span class="status-text">Online</span>
|
| 90 |
+
<span class="refresh-info" id="lastUpdate">...</span>
|
| 91 |
</div>
|
| 92 |
</div>
|
| 93 |
|
|
|
|
| 119 |
|
| 120 |
<div class="section">
|
| 121 |
<div class="section-title">📋 Server Info</div>
|
| 122 |
+
<div class="info-list" id="infoList">
|
| 123 |
<div class="info-row"><span class="info-key">Hostname</span><span class="info-val" id="infoHostname">...</span></div>
|
| 124 |
<div class="info-row"><span class="info-key">OS</span><span class="info-val" id="infoOS">...</span></div>
|
| 125 |
<div class="info-row"><span class="info-key">Kernel</span><span class="info-val" id="infoKernel">...</span></div>
|
|
|
|
| 135 |
</div>
|
| 136 |
|
| 137 |
<script>
|
| 138 |
+
function getBarColor(p) { return p < 60 ? 'fill-green' : p < 80 ? 'fill-yellow' : 'fill-red'; }
|
| 139 |
+
|
| 140 |
+
function update(data) {
|
| 141 |
+
const hn = (data.hostname || '').split('-').slice(0, 3).join('-') || 'N/A';
|
| 142 |
+
document.getElementById('hostname').textContent = hn;
|
| 143 |
+
|
| 144 |
+
const c = data.cpu || {};
|
| 145 |
+
document.getElementById('cpuValue').textContent = c.usage !== undefined ? c.usage + '%' : 'N/A';
|
| 146 |
+
const loadText = (Array.isArray(c.loadAvg) ? c.loadAvg.join(' ') : '');
|
| 147 |
+
document.getElementById('cpuSub').textContent =
|
| 148 |
+
c.cpuCount + ' core' +
|
| 149 |
+
(c.loadPercent ? ' · Load: ' + c.loadPercent + '%' : '') +
|
| 150 |
+
(loadText ? ' · Avg(' + loadText + ')' : '');
|
| 151 |
+
const cpuB = document.getElementById('cpuBar');
|
| 152 |
+
const cpuPct = typeof c.usage === 'number' ? c.usage : 0;
|
| 153 |
+
cpuB.style.width = cpuPct + '%';
|
| 154 |
+
cpuB.className = 'progress-fill ' + getBarColor(cpuPct);
|
| 155 |
+
|
| 156 |
+
document.getElementById('uptimeValue').textContent = data.uptime || '--';
|
| 157 |
+
document.getElementById('uptimeSub').textContent = data.kernel || '';
|
| 158 |
+
|
| 159 |
+
const m = data.mem || {};
|
| 160 |
+
document.getElementById('memValue').textContent = m.used + ' GB';
|
| 161 |
+
document.getElementById('memSub').textContent = m.available + ' GB avail / ' + m.total + ' GB';
|
| 162 |
+
const memB = document.getElementById('memBar');
|
| 163 |
+
memB.style.width = m.percent + '%';
|
| 164 |
+
memB.className = 'progress-fill ' + getBarColor(typeof m.percent === 'number' ? m.percent : 0);
|
| 165 |
+
|
| 166 |
+
const d = data.disk || {};
|
| 167 |
+
document.getElementById('diskValue').textContent = d.used + ' GB';
|
| 168 |
+
document.getElementById('diskSub').textContent = d.free + ' GB free / ' + d.total + ' GB';
|
| 169 |
+
const diskB = document.getElementById('diskBar');
|
| 170 |
+
diskB.style.width = d.percent + '%';
|
| 171 |
+
diskB.className = 'progress-fill ' + getBarColor(typeof d.percent === 'number' ? d.percent : 0);
|
| 172 |
+
|
| 173 |
+
document.getElementById('infoHostname').textContent = data.hostname || 'N/A';
|
| 174 |
+
document.getElementById('infoOS').textContent = data.os || 'N/A';
|
| 175 |
+
document.getElementById('infoKernel').textContent = (data.kernel || '').split(' (')[0] || 'N/A';
|
| 176 |
+
document.getElementById('infoProcs').textContent = data.processes || 'N/A';
|
| 177 |
+
document.getElementById('infoNode').textContent = data.node || 'N/A';
|
| 178 |
+
|
| 179 |
+
const t = new Date(data.timestamp || Date.now()).toLocaleString('id-ID', { timeZone: 'Asia/Jakarta' });
|
| 180 |
+
document.getElementById('infoTime').textContent = ' ' + t + ' WIB';
|
| 181 |
+
document.getElementById('lastUpdate').textContent = 'Updated: ' + t;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 182 |
}
|
| 183 |
|
| 184 |
async function fetchStats() {
|
| 185 |
try {
|
| 186 |
const res = await fetch('/api/stats');
|
| 187 |
+
if (!res.ok) throw new Error('HTTP ' + res.status);
|
| 188 |
const data = await res.json();
|
| 189 |
+
update(data);
|
| 190 |
} catch (err) {
|
| 191 |
+
document.getElementById('lastUpdate').textContent = 'Error';
|
| 192 |
+
console.error(err);
|
| 193 |
}
|
| 194 |
}
|
| 195 |
|
server.js
CHANGED
|
@@ -2,34 +2,72 @@ const express = require('express');
|
|
| 2 |
const fs = require('fs');
|
| 3 |
const path = require('path');
|
| 4 |
const os = require('os');
|
|
|
|
| 5 |
|
| 6 |
const app = express();
|
| 7 |
const PORT = process.env.PORT || 7860;
|
| 8 |
|
| 9 |
-
// Static files
|
| 10 |
app.use(express.static(path.join(__dirname, 'public')));
|
| 11 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 12 |
function getCpuUsage() {
|
| 13 |
try {
|
| 14 |
-
const
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
const parts =
|
| 18 |
-
// [user, nice, system, idle, iowait, irq, softirq, steal
|
|
|
|
| 19 |
const total = parts.slice(0, 8).reduce((a, b) => a + b, 0);
|
| 20 |
-
const idle = parts[3];
|
| 21 |
-
const iowait = parts[4];
|
| 22 |
const active = total - idle;
|
| 23 |
-
|
|
|
|
|
|
|
| 24 |
const cpuCount = os.cpus().length;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 25 |
return {
|
| 26 |
-
usage:
|
| 27 |
-
loadAvg,
|
| 28 |
cpuCount,
|
| 29 |
-
|
| 30 |
};
|
| 31 |
-
} catch {
|
| 32 |
-
return { usage:
|
| 33 |
}
|
| 34 |
}
|
| 35 |
|
|
@@ -38,22 +76,49 @@ function getMemUsage() {
|
|
| 38 |
const meminfo = fs.readFileSync('/proc/meminfo', 'utf-8');
|
| 39 |
const getVal = (key) => {
|
| 40 |
const line = meminfo.split('\n').find(l => l.startsWith(key));
|
| 41 |
-
return line ? parseInt(line.split(/\s+/)[1]) * 1024 : 0;
|
| 42 |
};
|
| 43 |
const total = getVal('MemTotal:');
|
| 44 |
const available = getVal('MemAvailable:');
|
|
|
|
|
|
|
| 45 |
const used = total - available;
|
|
|
|
| 46 |
return {
|
| 47 |
-
total:
|
| 48 |
-
used:
|
| 49 |
-
available:
|
| 50 |
-
|
|
|
|
|
|
|
| 51 |
};
|
| 52 |
} catch {
|
| 53 |
return { total: 0, used: 0, available: 0, percent: 0 };
|
| 54 |
}
|
| 55 |
}
|
| 56 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 57 |
function getUptime() {
|
| 58 |
try {
|
| 59 |
const raw = fs.readFileSync('/proc/uptime', 'utf-8');
|
|
@@ -69,39 +134,12 @@ function getUptime() {
|
|
| 69 |
|
| 70 |
function getKernel() {
|
| 71 |
try {
|
| 72 |
-
return
|
| 73 |
} catch {
|
| 74 |
-
return
|
| 75 |
-
}
|
| 76 |
-
}
|
| 77 |
-
|
| 78 |
-
function getDiskUsage() {
|
| 79 |
-
try {
|
| 80 |
-
const stat = os.statfs('/');
|
| 81 |
-
const blockSize = typeof stat?.bsize === 'number' ? stat.bsize : 4096;
|
| 82 |
-
const total = stat?.blocks !== undefined ? stat.blocks * blockSize : 0;
|
| 83 |
-
const free = stat?.bfree !== undefined ? stat.bfree * blockSize : 0;
|
| 84 |
-
const used = total - free;
|
| 85 |
-
return {
|
| 86 |
-
total: Math.round(total / (1024 * 1024 * 1024)),
|
| 87 |
-
used: Math.round(used / (1024 * 1024 * 1024)),
|
| 88 |
-
free: Math.round(free / (1024 * 1024 * 1024)),
|
| 89 |
-
percent: total > 0 ? Math.round((used / total) * 100) : 0
|
| 90 |
-
};
|
| 91 |
-
} catch {
|
| 92 |
-
return { total: 0, used: 0, free: 0, percent: 0 };
|
| 93 |
-
}
|
| 94 |
-
}
|
| 95 |
-
|
| 96 |
-
function getProcessCount() {
|
| 97 |
-
try {
|
| 98 |
-
return fs.readdirSync('/proc').filter(f => /^\d+$/.test(f)).length;
|
| 99 |
-
} catch {
|
| 100 |
-
return 0;
|
| 101 |
}
|
| 102 |
}
|
| 103 |
|
| 104 |
-
// API endpoint
|
| 105 |
app.get('/api/stats', (req, res) => {
|
| 106 |
res.json({
|
| 107 |
cpu: getCpuUsage(),
|
|
@@ -110,13 +148,13 @@ app.get('/api/stats', (req, res) => {
|
|
| 110 |
uptime: getUptime(),
|
| 111 |
kernel: getKernel(),
|
| 112 |
hostname: os.hostname(),
|
| 113 |
-
processes:
|
| 114 |
-
os:
|
| 115 |
node: process.version,
|
| 116 |
timestamp: Date.now()
|
| 117 |
});
|
| 118 |
});
|
| 119 |
|
| 120 |
app.listen(PORT, () => {
|
| 121 |
-
console.log(`Dashboard running on port ${PORT}`);
|
| 122 |
});
|
|
|
|
| 2 |
const fs = require('fs');
|
| 3 |
const path = require('path');
|
| 4 |
const os = require('os');
|
| 5 |
+
const { execSync } = require('child_process');
|
| 6 |
|
| 7 |
const app = express();
|
| 8 |
const PORT = process.env.PORT || 7860;
|
| 9 |
|
|
|
|
| 10 |
app.use(express.static(path.join(__dirname, 'public')));
|
| 11 |
|
| 12 |
+
// Cache for CPU tracking
|
| 13 |
+
let lastCpu = null;
|
| 14 |
+
let lastCpuTime = null;
|
| 15 |
+
|
| 16 |
+
function getFirstLine(path) {
|
| 17 |
+
try {
|
| 18 |
+
const data = fs.readFileSync(path, 'utf-8');
|
| 19 |
+
const line = data.split('\n').find(l => l.trim().length > 0);
|
| 20 |
+
return line ? line.trim() : null;
|
| 21 |
+
} catch { return null; }
|
| 22 |
+
}
|
| 23 |
+
|
| 24 |
function getCpuUsage() {
|
| 25 |
try {
|
| 26 |
+
const statLine = getFirstLine('/proc/stat');
|
| 27 |
+
if (!statLine) return { usage: 'N/A', loadAvg: ['N/A'], cpuCount: os.cpus().length, freq: 'N/A' };
|
| 28 |
+
|
| 29 |
+
const parts = statLine.split(/\s+/).slice(1).map(Number);
|
| 30 |
+
// parts: [user, nice, system, idle, iowait, irq, softirq, steal]
|
| 31 |
+
const idle = parts[3] + (parts[4] || 0);
|
| 32 |
const total = parts.slice(0, 8).reduce((a, b) => a + b, 0);
|
|
|
|
|
|
|
| 33 |
const active = total - idle;
|
| 34 |
+
|
| 35 |
+
const loadAvg = getFirstLine('/proc/loadavg');
|
| 36 |
+
const loadArr = loadAvg ? loadAvg.split(/\s+/).slice(0, 3) : ['N/A', 'N/A', 'N/A'];
|
| 37 |
const cpuCount = os.cpus().length;
|
| 38 |
+
|
| 39 |
+
if (lastCpu && lastCpu.total && total > lastCpu.total) {
|
| 40 |
+
const diffTotal = total - lastCpu.total;
|
| 41 |
+
const diffActive = active - lastCpu.active;
|
| 42 |
+
const usage = diffTotal > 0 ? Math.round((diffActive / diffTotal) * 100) : 0;
|
| 43 |
+
|
| 44 |
+
lastCpu = { total, active, idle };
|
| 45 |
+
lastCpuTime = Date.now();
|
| 46 |
+
|
| 47 |
+
return {
|
| 48 |
+
usage,
|
| 49 |
+
loadAvg: loadArr,
|
| 50 |
+
cpuCount,
|
| 51 |
+
loadPercent: cpuCount > 0 ? ((parseFloat(loadAvg.split(/\s+/)[0]) / cpuCount) * 100).toFixed(0) : 'N/A'
|
| 52 |
+
};
|
| 53 |
+
}
|
| 54 |
+
|
| 55 |
+
// First call — store baseline, return load avg based
|
| 56 |
+
lastCpu = { total, active, idle };
|
| 57 |
+
lastCpuTime = Date.now();
|
| 58 |
+
|
| 59 |
+
// Use load average as instant estimate
|
| 60 |
+
const load = parseFloat(loadAvg.split(/\s+/)[0]);
|
| 61 |
+
const instantUsage = cpuCount > 0 ? Math.min(100, Math.round((load / cpuCount) * 100)) : 0;
|
| 62 |
+
|
| 63 |
return {
|
| 64 |
+
usage: instantUsage,
|
| 65 |
+
loadAvg: loadArr,
|
| 66 |
cpuCount,
|
| 67 |
+
loadPercent: cpuCount > 0 ? ((load / cpuCount) * 100).toFixed(0) : 'N/A'
|
| 68 |
};
|
| 69 |
+
} catch (e) {
|
| 70 |
+
return { usage: 'N/A', loadAvg: ['N/A'], cpuCount: 1, freq: 'N/A' };
|
| 71 |
}
|
| 72 |
}
|
| 73 |
|
|
|
|
| 76 |
const meminfo = fs.readFileSync('/proc/meminfo', 'utf-8');
|
| 77 |
const getVal = (key) => {
|
| 78 |
const line = meminfo.split('\n').find(l => l.startsWith(key));
|
| 79 |
+
return line ? parseInt(line.split(/\s+/)[1]) * 1024 : 0;
|
| 80 |
};
|
| 81 |
const total = getVal('MemTotal:');
|
| 82 |
const available = getVal('MemAvailable:');
|
| 83 |
+
const buffers = getVal('Buffers:');
|
| 84 |
+
const cached = getVal('Cached:');
|
| 85 |
const used = total - available;
|
| 86 |
+
|
| 87 |
return {
|
| 88 |
+
total: (total / (1024 * 1024 * 1024)).toFixed(1),
|
| 89 |
+
used: (used / (1024 * 1024 * 1024)).toFixed(1),
|
| 90 |
+
available: (available / (1024 * 1024 * 1024)).toFixed(1),
|
| 91 |
+
buffers: (buffers / (1024 * 1024 * 1024)).toFixed(1),
|
| 92 |
+
cached: (cached / (1024 * 1024 * 1024)).toFixed(1),
|
| 93 |
+
percent: total > 0 ? Math.round((used / total) * 100) : 0
|
| 94 |
};
|
| 95 |
} catch {
|
| 96 |
return { total: 0, used: 0, available: 0, percent: 0 };
|
| 97 |
}
|
| 98 |
}
|
| 99 |
|
| 100 |
+
function getDiskUsage() {
|
| 101 |
+
try {
|
| 102 |
+
// Parse df output since statfs is not available
|
| 103 |
+
const df = execSync('df -B1 /', { encoding: 'utf-8' });
|
| 104 |
+
const lines = df.trim().split('\n');
|
| 105 |
+
if (lines.length >= 2) {
|
| 106 |
+
const parts = lines[1].split(/\s+/);
|
| 107 |
+
const total = parseInt(parts[1]);
|
| 108 |
+
const used = parseInt(parts[2]);
|
| 109 |
+
const free = parseInt(parts[3]);
|
| 110 |
+
return {
|
| 111 |
+
total: (total / (1024 * 1024 * 1024)).toFixed(1),
|
| 112 |
+
used: (used / (1024 * 1024 * 1024)).toFixed(1),
|
| 113 |
+
free: (free / (1024 * 1024 * 1024)).toFixed(1),
|
| 114 |
+
percent: total > 0 ? Math.round((used / total) * 100) : 0,
|
| 115 |
+
device: parts[0]
|
| 116 |
+
};
|
| 117 |
+
}
|
| 118 |
+
} catch {}
|
| 119 |
+
return { total: 0, used: 0, free: 0, percent: 0 };
|
| 120 |
+
}
|
| 121 |
+
|
| 122 |
function getUptime() {
|
| 123 |
try {
|
| 124 |
const raw = fs.readFileSync('/proc/uptime', 'utf-8');
|
|
|
|
| 134 |
|
| 135 |
function getKernel() {
|
| 136 |
try {
|
| 137 |
+
return os.release() + ' (' + os.type() + ')';
|
| 138 |
} catch {
|
| 139 |
+
return 'N/A';
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 140 |
}
|
| 141 |
}
|
| 142 |
|
|
|
|
| 143 |
app.get('/api/stats', (req, res) => {
|
| 144 |
res.json({
|
| 145 |
cpu: getCpuUsage(),
|
|
|
|
| 148 |
uptime: getUptime(),
|
| 149 |
kernel: getKernel(),
|
| 150 |
hostname: os.hostname(),
|
| 151 |
+
processes: (() => { try { return fs.readdirSync('/proc').filter(f => /^\d+$/.test(f)).length; } catch { return 0; } })(),
|
| 152 |
+
os: os.type() + ' ' + os.release(),
|
| 153 |
node: process.version,
|
| 154 |
timestamp: Date.now()
|
| 155 |
});
|
| 156 |
});
|
| 157 |
|
| 158 |
app.listen(PORT, () => {
|
| 159 |
+
console.log(`Server Dashboard running on port ${PORT}`);
|
| 160 |
});
|