- client 模式抓取水星 MW305R DHCP 列表,记录上下线,本地看板 - server 模式收上报,按站点分组持久化,多站点看板,7天滚动 - ESP8266 低功耗固件抓原始 HTML 上报 /api/raw,服务器端解析 - 部署脚本+systemd 单元+文档,交叉编译 Linux 二进制
152 lines
6.8 KiB
HTML
152 lines
6.8 KiB
HTML
<!DOCTYPE html>
|
|
<html lang="zh-CN">
|
|
<head>
|
|
<meta charset="utf-8">
|
|
<meta name="viewport" content="width=device-width, initial-scale=1">
|
|
<title>DHCP 客户端监控</title>
|
|
<style>
|
|
:root {
|
|
--bg: #0f172a; --card: #1e293b; --border: #334155;
|
|
--text: #e2e8f0; --muted: #94a3b8;
|
|
--green: #22c55e; --red: #ef4444; --blue: #3b82f6; --amber: #f59e0b;
|
|
}
|
|
* { box-sizing: border-box; margin: 0; padding: 0; }
|
|
body { background: var(--bg); color: var(--text); font-family: -apple-system, "PingFang SC", "Microsoft YaHei", sans-serif; padding: 20px; }
|
|
h1 { font-size: 22px; margin-bottom: 4px; }
|
|
.sub { color: var(--muted); font-size: 13px; margin-bottom: 20px; }
|
|
.stats { display: grid; grid-template-columns: repeat(auto-fit, minmax(150px, 1fr)); gap: 12px; margin-bottom: 24px; }
|
|
.stat { background: var(--card); border: 1px solid var(--border); border-radius: 10px; padding: 14px 16px; }
|
|
.stat .num { font-size: 26px; font-weight: 700; }
|
|
.stat .lbl { color: var(--muted); font-size: 12px; margin-top: 2px; }
|
|
.num.online { color: var(--green); } .num.offline { color: var(--red); } .num.total { color: var(--blue); } .num.events { color: var(--amber); }
|
|
.card { background: var(--card); border: 1px solid var(--border); border-radius: 10px; padding: 16px; margin-bottom: 24px; }
|
|
.card h2 { font-size: 15px; margin-bottom: 12px; color: var(--muted); font-weight: 600; }
|
|
table { width: 100%; border-collapse: collapse; font-size: 13px; }
|
|
th { text-align: left; color: var(--muted); font-weight: 500; padding: 8px 10px; border-bottom: 1px solid var(--border); white-space: nowrap; }
|
|
td { padding: 8px 10px; border-bottom: 1px solid rgba(51,65,85,.4); white-space: nowrap; }
|
|
tr:hover td { background: rgba(59,130,246,.06); }
|
|
.badge { display: inline-block; padding: 2px 10px; border-radius: 999px; font-size: 12px; font-weight: 600; }
|
|
.badge.online { background: rgba(34,197,94,.15); color: var(--green); }
|
|
.badge.offline { background: rgba(239,68,68,.15); color: var(--red); }
|
|
.mac { font-family: ui-monospace, SFMono-Regular, Menlo, monospace; }
|
|
.muted { color: var(--muted); }
|
|
.evt { display: flex; gap: 10px; align-items: center; padding: 7px 0; border-bottom: 1px solid rgba(51,65,85,.3); font-size: 13px; }
|
|
.evt:last-child { border-bottom: none; }
|
|
.dot { width: 8px; height: 8px; border-radius: 50%; flex-shrink: 0; }
|
|
.dot.up { background: var(--green); } .dot.down { background: var(--red); }
|
|
.evt .time { color: var(--muted); white-space: nowrap; }
|
|
.evt .name { font-weight: 600; }
|
|
.evt .detail { color: var(--muted); font-size: 12px; }
|
|
.footer { color: var(--muted); font-size: 12px; text-align: center; padding: 10px 0 30px; }
|
|
.scroll { max-height: 480px; overflow-y: auto; }
|
|
@media (max-width: 640px) { body { padding: 10px; } table { font-size: 12px; } }
|
|
</style>
|
|
</head>
|
|
<body>
|
|
<h1>📡 DHCP 客户端监控</h1>
|
|
<div class="sub" id="sub"></div>
|
|
|
|
<div class="stats">
|
|
<div class="stat"><div class="num online" id="st-online">-</div><div class="lbl">在线设备</div></div>
|
|
<div class="stat"><div class="num offline" id="st-offline">-</div><div class="lbl">离线设备</div></div>
|
|
<div class="stat"><div class="num total" id="st-total">-</div><div class="lbl">设备总数</div></div>
|
|
<div class="stat"><div class="num events" id="st-events">-</div><div class="lbl">今日事件</div></div>
|
|
</div>
|
|
|
|
<div class="card">
|
|
<h2>设备列表</h2>
|
|
<table>
|
|
<thead><tr>
|
|
<th>状态</th><th>设备名</th><th>MAC 地址</th><th>IP 地址</th>
|
|
<th>首次发现</th><th>最近上线</th><th>最近下线</th><th>在线次数</th><th>累计在线</th>
|
|
</tr></thead>
|
|
<tbody id="devices"></tbody>
|
|
</table>
|
|
</div>
|
|
|
|
<div class="card">
|
|
<h2>最近事件</h2>
|
|
<div class="scroll" id="events"></div>
|
|
</div>
|
|
|
|
<div class="footer" id="footer"></div>
|
|
|
|
<script>
|
|
const $ = id => document.getElementById(id);
|
|
const fmtTime = ts => {
|
|
if (!ts) return '-';
|
|
const d = new Date(ts);
|
|
const p = n => String(n).padStart(2, '0');
|
|
return `${d.getMonth()+1}-${p(d.getDate())} ${p(d.getHours())}:${p(d.getMinutes())}:${p(d.getSeconds())}`;
|
|
};
|
|
const fmtDur = sec => {
|
|
sec = Number(sec) || 0;
|
|
const h = Math.floor(sec/3600), m = Math.floor(sec%3600/60);
|
|
return h > 0 ? `${h}小时${m}分` : `${m}分钟`;
|
|
};
|
|
const esc = s => String(s ?? '').replace(/[&<>"']/g, c => ({'&':'&','<':'<','>':'>','"':'"',"'":'''}[c]));
|
|
|
|
let lastEventTs = null;
|
|
|
|
async function load() {
|
|
try {
|
|
const [devs, evts] = await Promise.all([
|
|
fetch('/api/devices').then(r => r.json()),
|
|
fetch('/api/events?limit=100').then(r => r.json()),
|
|
]);
|
|
|
|
// 统计
|
|
let online = 0, today = 0;
|
|
const now = new Date();
|
|
const today0 = new Date(now.getFullYear(), now.getMonth(), now.getDate()).getTime();
|
|
(devs.devices || []).forEach(d => { if (d.status === 'online') online++; });
|
|
(evts.events || []).forEach(e => { if (new Date(e.timestamp).getTime() >= today0) today++; });
|
|
|
|
$('st-online').textContent = online;
|
|
$('st-offline').textContent = devs.devices.length - online;
|
|
$('st-total').textContent = devs.devices.length;
|
|
$('st-events').textContent = today;
|
|
|
|
// 设备表
|
|
const rows = (devs.devices || []).map(d => `
|
|
<tr>
|
|
<td><span class="badge ${d.status === 'online' ? 'online' : 'offline'}">${d.status === 'online' ? '在线' : '离线'}</span></td>
|
|
<td>${esc(d.name || '-')}</td>
|
|
<td class="mac">${esc(d.mac)}</td>
|
|
<td class="mac">${esc(d.last_ip || '-')}</td>
|
|
<td>${fmtTime(d.first_seen)}</td>
|
|
<td>${fmtTime(d.last_online)}</td>
|
|
<td>${fmtTime(d.last_offline)}</td>
|
|
<td>${d.online_count ?? 0}</td>
|
|
<td>${fmtDur(d.total_online_seconds)}</td>
|
|
</tr>`).join('');
|
|
$('devices').innerHTML = rows || '<tr><td colspan="9" class="muted">暂无数据</td></tr>';
|
|
|
|
// 事件列表
|
|
const evtHtml = (evts.events || []).map(e => {
|
|
const up = e.event === 'online';
|
|
return `<div class="evt">
|
|
<span class="dot ${up ? 'up' : 'down'}"></span>
|
|
<span class="time">${fmtTime(e.timestamp)}</span>
|
|
<span class="name">${esc(e.name || '-')}</span>
|
|
<span class="mac muted">${esc(e.mac)}</span>
|
|
<span class="mac muted">${esc(e.ip || '')}</span>
|
|
<span class="detail">${esc(e.detail || '')}</span>
|
|
</div>`;
|
|
}).join('');
|
|
$('events').innerHTML = evtHtml || '<div class="muted">暂无事件</div>';
|
|
|
|
if (evts.events && evts.events.length) lastEventTs = evts.events[0].timestamp;
|
|
$('sub').textContent = `抓取间隔 ${devs.interval || '-'} · 最近抓取 ${fmtTime(devs.last_poll_ok)} · 数据保留 7 天`;
|
|
$('footer').textContent = `页面自动刷新 · ${new Date().toLocaleString()}`;
|
|
} catch (err) {
|
|
$('footer').textContent = '加载失败: ' + err.message;
|
|
}
|
|
}
|
|
|
|
load();
|
|
setInterval(load, 5000);
|
|
</script>
|
|
</body>
|
|
</html>
|