Spaces:
Paused
Paused
Deploy server dashboard
Browse files- Dockerfile +11 -0
- package.json +16 -0
- public/index.html +209 -0
- server.js +122 -0
Dockerfile
ADDED
|
@@ -0,0 +1,11 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
FROM node:lts-slim
|
| 2 |
+
|
| 3 |
+
WORKDIR /app
|
| 4 |
+
COPY node_modules/ node_modules/
|
| 5 |
+
COPY server.js server.js
|
| 6 |
+
COPY public/ public/
|
| 7 |
+
COPY package.json package.json
|
| 8 |
+
|
| 9 |
+
EXPOSE 7860
|
| 10 |
+
|
| 11 |
+
CMD ["node", "server.js"]
|
package.json
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"name": "dashboard",
|
| 3 |
+
"version": "1.0.0",
|
| 4 |
+
"description": "",
|
| 5 |
+
"main": "index.js",
|
| 6 |
+
"scripts": {
|
| 7 |
+
"test": "echo \"Error: no test specified\" && exit 1"
|
| 8 |
+
},
|
| 9 |
+
"keywords": [],
|
| 10 |
+
"author": "",
|
| 11 |
+
"license": "ISC",
|
| 12 |
+
"type": "commonjs",
|
| 13 |
+
"dependencies": {
|
| 14 |
+
"express": "^5.2.1"
|
| 15 |
+
}
|
| 16 |
+
}
|
public/index.html
ADDED
|
@@ -0,0 +1,209 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
<!DOCTYPE html>
|
| 2 |
+
<html lang="id">
|
| 3 |
+
<head>
|
| 4 |
+
<meta charset="UTF-8">
|
| 5 |
+
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
| 6 |
+
<title>Server Dashboard — Hedd</title>
|
| 7 |
+
<style>
|
| 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: 1000px; margin: 0 auto; padding: 1.5rem 1rem; }
|
| 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.6rem; font-weight: 700;
|
| 23 |
+
background: linear-gradient(135deg, #22d3ee, #818cf8);
|
| 24 |
+
-webkit-background-clip: text; -webkit-text-fill-color: transparent; background-clip: text;
|
| 25 |
+
}
|
| 26 |
+
.header-right { display: flex; align-items: center; gap: 0.75rem; }
|
| 27 |
+
.status-dot {
|
| 28 |
+
width: 10px; height: 10px; background: #22c55e; border-radius: 50%;
|
| 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.85rem; color: #22c55e; }
|
| 36 |
+
.refresh-info { font-size: 0.7rem; color: #444; }
|
| 37 |
+
|
| 38 |
+
.stats {
|
| 39 |
+
display: grid; grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
|
| 40 |
+
gap: 1rem; margin-bottom: 2rem;
|
| 41 |
+
}
|
| 42 |
+
.stat-card {
|
| 43 |
+
background: linear-gradient(135deg, #0d0d1a, #111128);
|
| 44 |
+
border: 1px solid #1a1a3a; border-radius: 12px; padding: 1.25rem;
|
| 45 |
+
transition: border-color 0.3s; position: relative;
|
| 46 |
+
}
|
| 47 |
+
.stat-card:hover { border-color: #6366f1; }
|
| 48 |
+
.stat-label { font-size: 0.75rem; color: #666; text-transform: uppercase; letter-spacing: 0.05em; margin-bottom: 0.5rem; }
|
| 49 |
+
.stat-value { font-size: 1.5rem; font-weight: 700; }
|
| 50 |
+
.stat-sub { font-size: 0.8rem; color: #555; margin-top: 0.25rem; }
|
| 51 |
+
.progress-bar { height: 6px; background: #1a1a2e; border-radius: 3px; margin-top: 0.75rem; overflow: hidden; }
|
| 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: 1rem; color: #818cf8; margin-bottom: 0.75rem; }
|
| 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.75rem 1rem; border-bottom: 1px solid #121225; font-size: 0.9rem;
|
| 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.75rem;
|
| 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.25rem; }
|
| 82 |
+
}
|
| 83 |
+
</style>
|
| 84 |
+
</head>
|
| 85 |
+
<body>
|
| 86 |
+
<div class="container">
|
| 87 |
+
<div class="header">
|
| 88 |
+
<div>
|
| 89 |
+
<h1>⚡ Server Dashboard</h1>
|
| 90 |
+
<p style="color:#555;font-size:0.85rem;" id="hostname">Loading...</p>
|
| 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">Updating...</span>
|
| 96 |
+
</div>
|
| 97 |
+
</div>
|
| 98 |
+
|
| 99 |
+
<div class="stats">
|
| 100 |
+
<div class="stat-card">
|
| 101 |
+
<div class="stat-label">CPU Usage</div>
|
| 102 |
+
<div class="stat-value" id="cpuValue">--%</div>
|
| 103 |
+
<div class="stat-sub" id="cpuSub">Loading...</div>
|
| 104 |
+
<div class="progress-bar"><div class="progress-fill fill-green" id="cpuBar" style="width:0%"></div></div>
|
| 105 |
+
</div>
|
| 106 |
+
<div class="stat-card">
|
| 107 |
+
<div class="stat-label">Uptime</div>
|
| 108 |
+
<div class="stat-value" id="uptimeValue">--</div>
|
| 109 |
+
<div class="stat-sub" id="uptimeSub">Loading...</div>
|
| 110 |
+
</div>
|
| 111 |
+
<div class="stat-card">
|
| 112 |
+
<div class="stat-label">RAM Usage</div>
|
| 113 |
+
<div class="stat-value" id="memValue">-- GB</div>
|
| 114 |
+
<div class="stat-sub" id="memSub">Loading...</div>
|
| 115 |
+
<div class="progress-bar"><div class="progress-fill fill-green" id="memBar" style="width:0%"></div></div>
|
| 116 |
+
</div>
|
| 117 |
+
<div class="stat-card">
|
| 118 |
+
<div class="stat-label">Disk Usage</div>
|
| 119 |
+
<div class="stat-value" id="diskValue">-- GB</div>
|
| 120 |
+
<div class="stat-sub" id="diskSub">Loading...</div>
|
| 121 |
+
<div class="progress-bar"><div class="progress-fill fill-green" id="diskBar" style="width:0%"></div></div>
|
| 122 |
+
</div>
|
| 123 |
+
</div>
|
| 124 |
+
|
| 125 |
+
<div class="section">
|
| 126 |
+
<div class="section-title">📋 Server Info</div>
|
| 127 |
+
<div class="info-list loading" id="infoList">
|
| 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>
|
| 131 |
+
<div class="info-row"><span class="info-key">Processes</span><span class="info-val" id="infoProcs">...</span></div>
|
| 132 |
+
<div class="info-row"><span class="info-key">Node.js</span><span class="info-val" id="infoNode">...</span></div>
|
| 133 |
+
<div class="info-row"><span class="info-key">Last Update</span><span class="info-val" id="infoTime">...</span></div>
|
| 134 |
+
</div>
|
| 135 |
+
</div>
|
| 136 |
+
|
| 137 |
+
<div class="footer">
|
| 138 |
+
<p>Powered by <a href="https://github.com/openclaw/openclaw" target="_blank">OpenClaw</a> · Dashboard by Hedd</p>
|
| 139 |
+
</div>
|
| 140 |
+
</div>
|
| 141 |
+
|
| 142 |
+
<script>
|
| 143 |
+
const cpuColors = { green: 60, yellow: 80 };
|
| 144 |
+
|
| 145 |
+
function getBarColor(percent) {
|
| 146 |
+
if (percent < cpuColors.green) return 'fill-green';
|
| 147 |
+
if (percent < cpuColors.yellow) return 'fill-yellow';
|
| 148 |
+
return 'fill-red';
|
| 149 |
+
}
|
| 150 |
+
|
| 151 |
+
function updateUI(data) {
|
| 152 |
+
document.getElementById('hostname').textContent = data.hostname + ' — ' + data.os;
|
| 153 |
+
|
| 154 |
+
// CPU
|
| 155 |
+
document.getElementById('cpuValue').textContent = data.cpu.usage + '%';
|
| 156 |
+
document.getElementById('cpuSub').textContent = `${data.cpu.cpuCount} core · ${data.cpu.freq} · Load: ${data.cpu.loadAvg[0]} ${data.cpu.loadAvg[1]} ${data.cpu.loadAvg[2]}`;
|
| 157 |
+
const cpuBar = document.getElementById('cpuBar');
|
| 158 |
+
cpuBar.style.width = data.cpu.usage + '%';
|
| 159 |
+
cpuBar.className = 'progress-fill ' + getBarColor(data.cpu.usage);
|
| 160 |
+
|
| 161 |
+
// Uptime
|
| 162 |
+
document.getElementById('uptimeValue').textContent = data.uptime;
|
| 163 |
+
document.getElementById('uptimeSub').textContent = data.kernel;
|
| 164 |
+
|
| 165 |
+
// Memory
|
| 166 |
+
document.getElementById('memValue').textContent = data.mem.used + ' GB';
|
| 167 |
+
document.getElementById('memSub').textContent = `${data.mem.available} GB available / ${data.mem.total} GB`;
|
| 168 |
+
const memBar = document.getElementById('memBar');
|
| 169 |
+
memBar.style.width = data.mem.percent + '%';
|
| 170 |
+
memBar.className = 'progress-fill ' + getBarColor(data.mem.percent);
|
| 171 |
+
|
| 172 |
+
// Disk
|
| 173 |
+
document.getElementById('diskValue').textContent = data.disk.used + ' GB';
|
| 174 |
+
document.getElementById('diskSub').textContent = `${data.disk.free} GB free / ${data.disk.total} GB`;
|
| 175 |
+
const diskBar = document.getElementById('diskBar');
|
| 176 |
+
diskBar.style.width = data.disk.percent + '%';
|
| 177 |
+
diskBar.className = 'progress-fill ' + getBarColor(data.disk.percent);
|
| 178 |
+
|
| 179 |
+
// Info
|
| 180 |
+
document.getElementById('infoHostname').textContent = data.hostname;
|
| 181 |
+
document.getElementById('infoOS').textContent = data.os;
|
| 182 |
+
document.getElementById('infoKernel').textContent = data.kernel;
|
| 183 |
+
document.getElementById('infoProcs').textContent = data.processes;
|
| 184 |
+
document.getElementById('infoNode').textContent = data.node;
|
| 185 |
+
|
| 186 |
+
const now = new Date(data.timestamp).toLocaleString('id-ID', { timeZone: 'Asia/Jakarta' });
|
| 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 |
+
updateUI(data);
|
| 199 |
+
} catch (err) {
|
| 200 |
+
console.error('Failed to fetch:', err);
|
| 201 |
+
document.getElementById('lastUpdate').textContent = 'Error fetching';
|
| 202 |
+
}
|
| 203 |
+
}
|
| 204 |
+
|
| 205 |
+
fetchStats();
|
| 206 |
+
setInterval(fetchStats, 5000);
|
| 207 |
+
</script>
|
| 208 |
+
</body>
|
| 209 |
+
</html>
|
server.js
ADDED
|
@@ -0,0 +1,122 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
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 stat = fs.readFileSync('/proc/stat', 'utf-8');
|
| 15 |
+
const line = stat.split('\n').find(l => l.startsWith('cpu '));
|
| 16 |
+
if (!line) return { usage: 0, freq: 'N/A' };
|
| 17 |
+
const parts = line.trim().split(/\s+/).map(Number);
|
| 18 |
+
// [user, nice, system, idle, iowait, irq, softirq, steal, guest, guest_nice]
|
| 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 |
+
const loadAvg = fs.readFileSync('/proc/loadavg', 'utf-8').trim().split(/\s+/).slice(0, 3).map(Number);
|
| 24 |
+
const cpuCount = os.cpus().length;
|
| 25 |
+
return {
|
| 26 |
+
usage: Math.round(((total - idle - iowait) / total) * 100),
|
| 27 |
+
loadAvg,
|
| 28 |
+
cpuCount,
|
| 29 |
+
freq: os.cpus()[0]?.speed?.toFixed(1) + ' GHz' || 'N/A'
|
| 30 |
+
};
|
| 31 |
+
} catch {
|
| 32 |
+
return { usage: 0, loadAvg: [0, 0, 0], cpuCount: 1, freq: 'N/A' };
|
| 33 |
+
}
|
| 34 |
+
}
|
| 35 |
+
|
| 36 |
+
function getMemUsage() {
|
| 37 |
+
try {
|
| 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; // convert kB to bytes
|
| 42 |
+
};
|
| 43 |
+
const total = getVal('MemTotal:');
|
| 44 |
+
const available = getVal('MemAvailable:');
|
| 45 |
+
const used = total - available;
|
| 46 |
+
return {
|
| 47 |
+
total: Math.round(total / (1024 * 1024)),
|
| 48 |
+
used: Math.round(used / (1024 * 1024)),
|
| 49 |
+
available: Math.round(available / (1024 * 1024)),
|
| 50 |
+
percent: Math.round((used / total) * 100)
|
| 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');
|
| 60 |
+
const seconds = parseFloat(raw.split(' ')[0]);
|
| 61 |
+
const days = Math.floor(seconds / 86400);
|
| 62 |
+
const hours = Math.floor((seconds % 86400) / 3600);
|
| 63 |
+
const mins = Math.floor((seconds % 3600) / 60);
|
| 64 |
+
return `${days}d ${hours}h ${mins}m`;
|
| 65 |
+
} catch {
|
| 66 |
+
return 'N/A';
|
| 67 |
+
}
|
| 68 |
+
}
|
| 69 |
+
|
| 70 |
+
function getKernel() {
|
| 71 |
+
try {
|
| 72 |
+
return fs.readFileSync('/proc/version', 'utf-8').trim();
|
| 73 |
+
} catch {
|
| 74 |
+
return os.release();
|
| 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(),
|
| 108 |
+
mem: getMemUsage(),
|
| 109 |
+
disk: getDiskUsage(),
|
| 110 |
+
uptime: getUptime(),
|
| 111 |
+
kernel: getKernel(),
|
| 112 |
+
hostname: os.hostname(),
|
| 113 |
+
processes: getProcessCount(),
|
| 114 |
+
os: `${os.type()} ${os.release()}`,
|
| 115 |
+
node: process.version,
|
| 116 |
+
timestamp: Date.now()
|
| 117 |
+
});
|
| 118 |
+
});
|
| 119 |
+
|
| 120 |
+
app.listen(PORT, () => {
|
| 121 |
+
console.log(`Dashboard running on port ${PORT}`);
|
| 122 |
+
});
|