Here is the php code Ive been playing with on the lan network that reads from the API, maybe someone will find it useful.
<?php
/**
* IPFire WUI Dashboard
*/
// CONFIGURATION
$api_url = 'https://IPFIRE IP ADDRESS:444/cgi-bin/NAME_OF_API.cgi';
$username = 'admin';
$password = '';
function fetch_ipfire_data(string $url, string $user, string $pass): string
{
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_USERPWD, "$user:$pass");
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($ch, CURLOPT_TIMEOUT, 6);
$response = curl_exec($ch);
$http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
if ($http_code !== 200 || !$response) {
return json_encode(['error' => "HTTP {$http_code} - Unable to connect"]);
}
return $response;
}
function format_bytes($bytes, $precision = 2) {
$units = ['B', 'KiB', 'MiB', 'GiB', 'TiB', 'PiB'];
$bytes = max($bytes, 0);
$pow = floor(($bytes ? log($bytes) : 0) / log(1024));
$pow = min($pow, count($units) - 1);
return round($bytes / pow(1024, $pow), $precision) . ' ' . $units[$pow];
}
// Handle async live polling requests
if (isset($_GET['action']) && $_GET['action'] === 'poll') {
header('Content-Type: application/json');
echo fetch_ipfire_data($api_url, $username, $password);
exit;
}
$raw_json = fetch_ipfire_data($api_url, $username, $password);
$data = json_decode($raw_json, true);
$has_error = isset($data['error']) || !$data;
$sys = $data['system'] ?? [];
$conn = $data['connection'] ?? [];
$traffic = $data['traffic'] ?? [];
$net = $data['network']['interfaces'] ?? [];
$neighbors = $data['network']['neighbors'] ?? [];
$routes = $data['network']['routes'] ?? [];
$dhcp_leases = $data['dhcp']['leases'] ?? [];
$services = $data['services'] ?? [];
$addons = $data['addons'] ?? [];
$certs = $data['certificates'] ?? [];
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title><?= htmlspecialchars($conn['external_hostname'] ?? 'IPFire') ?> - Status</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link href="https://fonts.googleapis.com/css2?family=Open+Sans:ital,wght@0,300..800;1,300..800&family=Source+Code+Pro:wght@400;600&display=swap" rel="stylesheet">
<script src="https://cdn.jsdelivr.net/npm/chart.js@4.4.0"></script>
<style>
:root {
--ipf-bg: #eceff1;
--ipf-card: #ffffff;
--ipf-text: #263238;
--ipf-muted: #607d8b;
--ipf-border: #cfd8dc;
--ipf-primary: #bf360c;
--ipf-header-bg: #37474f;
--ipf-nav-bg: #263238;
--ipf-green: #2e7d32;
--ipf-green-bg: #e8f5e9;
--ipf-red: #c62828;
--ipf-red-bg: #ffebee;
--ipf-blue: #1565c0;
}
* { box-sizing: border-box; }
body {
margin: 0;
padding: 0;
background-color: var(--ipf-bg);
color: var(--ipf-text);
font-family: 'Open Sans', sans-serif;
font-size: 13px;
line-height: 1.5;
}
#header {
background-color: var(--ipf-header-bg);
color: #ffffff;
padding: 10px 24px;
display: flex;
align-items: center;
justify-content: space-between;
box-shadow: 0 2px 4px rgba(0,0,0,0.2);
}
#header h1 {
font-size: 16px;
margin: 0;
font-weight: 600;
letter-spacing: 0.5px;
display: flex;
align-items: center;
gap: 12px;
}
#header .logo-badge {
background-color: var(--ipf-primary);
color: #fff;
padding: 2px 8px;
border-radius: 3px;
font-size: 12px;
font-weight: 800;
letter-spacing: 1px;
}
#traffic-bar {
background-color: var(--ipf-nav-bg);
color: #cfd8dc;
padding: 6px 24px;
font-size: 12px;
display: flex;
justify-content: flex-end;
gap: 16px;
border-bottom: 2px solid var(--ipf-primary);
}
#traffic-bar span { font-weight: 600; color: #fff; font-family: 'Source Code Pro', monospace; }
.wrapper {
max-width: 1440px;
margin: 20px auto;
padding: 0 16px;
}
.alert-box {
background-color: var(--ipf-red-bg);
border-left: 4px solid var(--ipf-red);
padding: 12px 16px;
margin-bottom: 20px;
border-radius: 2px;
color: var(--ipf-red);
font-weight: 600;
}
.dashboard-grid {
display: grid;
grid-template-columns: repeat(auto-fit, minmax(440px, 1fr));
gap: 16px;
}
.ipf-box {
background: var(--ipf-card);
border: 1px solid var(--ipf-border);
border-radius: 8px;
box-shadow: 0 1px 3px rgba(0,0,0,0.05);
overflow: hidden;
display: flex;
flex-direction: column;
margin-bottom: 16px;
}
.ipf-box header {
background: #f7f9fa;
border-bottom: 1px solid var(--ipf-border);
padding: 8px 14px;
}
.ipf-box header h2 {
margin: 0;
font-size: 13px;
font-weight: 700;
text-transform: uppercase;
letter-spacing: 0.5px;
color: var(--ipf-header-bg);
}
.ipf-box-body {
padding: 10px;
flex-grow: 1;
}
table.ipf-table {
width: 100%;
border-collapse: collapse;
font-size: 12px;
}
table.ipf-table tr {
border-bottom: 1px solid #eceff1;
}
table.ipf-table tr:nth-child(even) {
background-color: #fafbfc;
}
table.ipf-table tr:last-child {
border-bottom: none;
}
table.ipf-table td,
table.ipf-table th {
padding: 6px 12px;
text-align: left;
}
table.ipf-table td.key {
color: var(--ipf-muted);
font-weight: 600;
width: 38%;
border-right: 1px solid #eceff1;
}
table.ipf-table td.val {
text-align: right;
font-family: 'Source Code Pro', monospace;
font-size: 11.5px;
word-break: break-all;
}
.badge {
display: inline-block;
padding: 1px 6px;
border-radius: 2px;
font-size: 11px;
font-weight: 700;
text-transform: uppercase;
}
.badge-online { background-color: var(--ipf-green-bg); color: var(--ipf-green); border: 1px solid #a5d6a7; }
.badge-offline { background-color: var(--ipf-red-bg); color: var(--ipf-red); border: 1px solid #ef9a9a; }
.metric-cards {
display: grid;
grid-template-columns: repeat(3, 1fr);
border-bottom: 1px solid var(--ipf-border);
background: #fafbfc;
}
.metric-cell {
padding: 10px;
text-align: center;
border-right: 1px solid var(--ipf-border);
}
.metric-cell:last-child { border-right: none; }
.metric-cell .label { font-size: 10px; font-weight: 700; text-transform: uppercase; color: var(--ipf-muted); }
.metric-cell .value { font-size: 14px; font-weight: 700; color: var(--ipf-text); margin-top: 2px; }
#footer {
max-width: 1440px;
margin: 20px auto;
padding: 10px 16px;
font-size: 11px;
color: var(--ipf-muted);
border-top: 1px solid var(--ipf-border);
display: flex;
justify-content: space-between;
}
#footer a { color: var(--ipf-muted); text-decoration: none; }
#footer a:hover { text-decoration: underline; }
.scrollable {
max-height: 300px;
overflow-y: auto;
border: 1px solid var(--ipf-border);
border-radius: 4px;
}
.scrollable th {
text-align: center;
font-weight: 600;
background-color: #f0f4f5;
}
.neighbor-box, .dhcp-box {
grid-column: 1 / -1;
}
@media (max-width: 768px) {
.dashboard-grid {
grid-template-columns: 1fr;
}
}
@media (max-width: 768px) {
/* 1. Layout Grid: Allow boxes to be full width */
.dashboard-grid {
grid-template-columns: 1fr !important;
gap: 12px;
}
.wrapper { padding: 0 8px; margin: 12px auto; }
#header, #traffic-bar { padding: 8px 12px; }
#header { flex-direction: column; align-items: flex-start; gap: 4px; }
#header h1 { font-size: 15px; flex-wrap: wrap; }
#header small { font-size: 11px; opacity: 0.8; }
#traffic-bar {
flex-direction: column;
align-items: flex-start;
gap: 4px;
font-size: 11px;
}
#traffic-bar div { width: 100%; display: flex; flex-wrap: wrap; gap: 8px; justify-content: flex-start; }
#traffic-bar span { font-size: 11px; }
.metric-cards {
grid-template-columns: 1fr !important;
}
.metric-cell { border-right: none; border-bottom: 1px solid var(--ipf-border); padding: 8px; }
.metric-cell:last-child { border-bottom: none; }
.ipf-box-body {
overflow-x: auto; /* Enable scroll on container */
-webkit-overflow-scrolling: touch;
}
table.ipf-table {
min-width: 600px; /* Force layout width, let container scroll */
white-space: nowrap; /* Prevent cell wrapping */
}
table.ipf-table td, table.ipf-table th {
padding: 8px 10px;
font-size: 11px;
}
#trafficChart {
width: 100% !important;
height: auto !important;
max-width: 100%;
display: block;
}
.badge { white-space: nowrap; font-size: 10px; padding: 1px 5px; }
#footer { flex-direction: column; gap: 6px; text-align: center; padding: 12px 8px; }
}
@media (max-width: 480px) {
html { font-size: 12px; } /* Base rem drop */
.ipf-box header h2 { font-size: 11px; }
table.ipf-table td.key { width: 35%; min-width: 80px; }
table.ipf-table td.val { font-size: 10px; }
}
</style>
</head>
<body>
<header id="header">
<h1>
<span class="logo-badge">IPFire_</span>
<?= htmlspecialchars($conn['external_hostname'] ?? 'IPFire Appliance') ?>
</h1>
<div><small><?= htmlspecialchars($sys['version'] ?? '') ?></small></div>
</header>
<div id="traffic-bar">
<div>RED Traffic: In <span id="red-rx-rate">--</span> • Out <span id="red-tx-rate">--</span></div>
</div>
<div class="wrapper">
<?php if ($has_error): ?>
<div class="alert-box">
Failed to communicate with IPFire device at <code><?= htmlspecialchars($api_url) ?></code>. Check authentication credentials and network connectivity.
</div>
<?php endif; ?>
<div class="dashboard-grid">
<!-- System Information -->
<div class="ipf-box">
<header><h2>System Information</h2></header>
<div class="ipf-box-body">
<table class="ipf-table">
<tr><td class="key">Core Release</td><td class="val"><?= htmlspecialchars($sys['version'] ?? 'N/A') ?></td></tr>
<tr><td class="key">Pakfire Version</td><td class="val"><?= htmlspecialchars($sys['pakfire_version'] ?? 'N/A') ?></td></tr>
<tr><td class="key">Kernel Release</td><td class="val"><?= htmlspecialchars($sys['kernel_version'] ?? 'N/A') ?></td></tr>
<tr><td class="key">Architecture</td><td class="val"><?= htmlspecialchars($sys['architecture'] ?? 'N/A') ?></td></tr>
<tr><td class="key">Hardware</td><td class="val"><?= htmlspecialchars(($sys['vendor'] ?? '') . ' ' . ($sys['model'] ?? '')) ?></td></tr>
<tr><td class="key">CPU</td><td class="val"><?= htmlspecialchars($sys['cpu_model'] ?? 'N/A') ?> (<?= $sys['cpu_count'] ?? 1 ?> cores)</td></tr>
<tr><td class="key">Virtual</td><td class="val"><?= $sys['virtual'] ? 'Yes' : 'No' ?></td></tr>
<tr><td class="key">System Memory</td><td class="val"><?= isset($sys['memory']['total']) ? format_bytes($sys['memory']['total']) : 'N/A' ?></td></tr>
<tr><td class="key">Root Filesystem</td><td class="val"><?= isset($sys['disk']['root']['total']) ? format_bytes($sys['disk']['root']['total']) : 'N/A' ?> (<?= $sys['disk']['root']['use_percent'] ?? 0 ?>% used)</td></tr>
<tr><td class="key">Reboot Required</td><td class="val"><?= $sys['reboot_required'] ? '<strong style="color:#c62828;">YES</strong>' : 'No' ?></td></tr>
<tr><td class="key">Updates Available</td><td class="val"><?= ($sys['package_updates'] ?? 0) . ' packages' ?></td></tr>
</table>
</div>
</div>
<!-- Connection Status -->
<div class="ipf-box">
<header><h2>External Connection (RED)</h2></header>
<div class="ipf-box-body">
<table class="ipf-table">
<tr>
<td class="key">Status</td>
<td class="val">
<span class="badge <?= ($conn['state'] ?? '') === 'connected' ? 'badge-online' : 'badge-offline' ?>">
<?= htmlspecialchars(ucfirst($conn['state'] ?? 'down')) ?>
</span>
</td>
</tr>
<tr><td class="key">Internet IP</td><td class="val"><?= htmlspecialchars($conn['external_ip'] ?? '0.0.0.0') ?></td></tr>
<tr><td class="key">Hostname</td><td class="val"><?= htmlspecialchars($conn['external_hostname'] ?? 'N/A') ?></td></tr>
<tr><td class="key">Connected Since</td><td class="val"><?= htmlspecialchars($conn['duration_text'] ?? '0s') ?></td></tr>
<tr>
<td class="key">Fireinfo Profile</td>
<td class="val">
<?php if (!empty($conn['profile'])): ?>
<a href="https://fireinfo.ipfire.org/profile/<?= htmlspecialchars($conn['profile']) ?>" target="_blank" style="color:var(--ipf-blue);">View Profile</a>
<?php else: ?>
Disabled
<?php endif; ?>
</td>
</tr>
</table>
</div>
</div>
<!-- Network Interfaces -->
<div class="ipf-box">
<header><h2>Network Interfaces</h2></header>
<div class="ipf-box-body">
<table class="ipf-table">
<?php foreach(['red' => 'RED', 'green' => 'GREEN', 'blue' => 'BLUE', 'orange' => 'ORANGE'] as $iface => $label):
if (!isset($net[$iface]) || $net[$iface] === null || $net[$iface] === false):
continue;
endif;
?>
<tr><td class="key"><?= $label ?> Driver</td><td class="val"><?= htmlspecialchars($net[$iface]['driver'] ?? 'N/A') ?> (<?= htmlspecialchars($net[$iface]['driver_version'] ?? '') ?>)</td></tr>
<tr><td class="key"><?= $label ?> Device</td><td class="val"><?= htmlspecialchars($net[$iface]['device'] ?? 'N/A') ?></td></tr>
<tr><td class="key"><?= $label ?> Speed</td><td class="val"><?= $net[$iface]['speed_mbps'] ?? 0 ?> Mbps</td></tr>
<tr><td class="key"><?= $label ?> Duplex</td><td class="val"><?= htmlspecialchars($net[$iface]['duplex'] ?? 'N/A') ?></td></tr>
<tr><td class="key"><?= $label ?> Link Detected</td><td class="val"><?= $net[$iface]['link_detected'] ? 'Yes' : 'No' ?></td></tr>
<tr><td class="key"><?= $label ?> Firmware</td><td class="val"><?= htmlspecialchars($net[$iface]['firmware_version'] ?? 'N/A') ?></td></tr>
<tr><td class="key"><?= $label ?> RX Bytes</td><td class="val"><?= number_format($net[$iface]['rx_bytes'] ?? 0) ?></td></tr>
<tr><td class="key"><?= $label ?> TX Bytes</td><td class="val"><?= number_format($net[$iface]['tx_bytes'] ?? 0) ?></td></tr>
<?php endforeach; ?>
</table>
</div>
</div>
<!-- Traffic Metrics -->
<div class="ipf-box">
<header><h2>Traffic Metrics</h2></header>
<div class="ipf-box-body">
<div class="metric-cards">
<div class="metric-cell">
<div class="label">Incoming</div>
<div class="value"><?= format_bytes($traffic['rx_bytes'] ?? 0) ?></div>
</div>
<div class="metric-cell">
<div class="label">Outgoing</div>
<div class="value"><?= format_bytes($traffic['tx_bytes'] ?? 0) ?></div>
</div>
<div class="metric-cell">
<div class="label">Total</div>
<div class="value"><?= format_bytes(($traffic['rx_bytes'] ?? 0) + ($traffic['tx_bytes'] ?? 0)) ?></div>
</div>
</div>
<!-- Traffic Graph Canvas (added after metric-cards) -->
<div style="width:100%;max-width:100%;background:#fafbfc;border:1px solid var(--ipf-border);border-radius:6px;padding:12px;margin-top:12px;">
<canvas id="trafficChart" style="background:#fafbfc;border-radius:6px;display:block;margin:auto;"></canvas>
</div>
<table class="ipf-table">
<tr><td class="key">RX Bytes</td><td class="val"><?= number_format($traffic['rx_bytes'] ?? 0) ?></td></tr>
<tr><td class="key">TX Bytes</td><td class="val"><?= number_format($traffic['tx_bytes'] ?? 0) ?></td></tr>
</table>
</div>
</div>
<!-- Core Services -->
<div class="ipf-box">
<header><h2>Core Services</h2></header>
<div class="ipf-box-body">
<table class="ipf-table">
<?php foreach ($services as $srv => $active): ?>
<tr>
<td class="key"><?= strtoupper(str_replace('_', ' ', $srv)) ?></td>
<td class="val">
<span class="badge <?= $active ? 'badge-online' : 'badge-offline' ?>">
<?= $active ? 'Running' : 'Stopped' ?>
</span>
</td>
</tr>
<?php endforeach; ?>
</table>
</div>
</div>
<!-- Add-on Services -->
<div class="ipf-box">
<header><h2>Add-on Services</h2></header>
<div class="ipf-box-body">
<table class="ipf-table">
<?php foreach ($addons as $addon => $info):
$isRunning = !empty($info['running']);
?>
<tr>
<td class="key"><?= htmlspecialchars($addon) ?></td>
<td class="val">
<span class="badge <?= $isRunning ? 'badge-online' : 'badge-offline' ?>">
<?= $isRunning ? 'Running' : 'Stopped' ?>
</span>
</td>
</tr>
<?php endforeach; ?>
</table>
</div>
</div>
<!-- Certificates -->
<div class="ipf-box">
<header><h2>Certificates</h2></header>
<div class="ipf-box-body">
<table class="ipf-table">
<?php if (isset($certs['apache'])): ?>
<tr><td class="key">Apache Cert Status</td><td class="val"><?= htmlspecialchars($certs['apache']['status'] ?? 'Unknown') ?></td></tr>
<tr><td class="key">Subject</td><td class="val"><?= htmlspecialchars($certs['apache']['subject'] ?? 'N/A') ?></td></tr>
<tr><td class="key">Issuer</td><td class="val"><?= htmlspecialchars($certs['apache']['issuer'] ?? 'N/A') ?></td></tr>
<tr><td class="key">Valid From</td><td class="val"><?= htmlspecialchars($certs['apache']['not_before'] ?? 'N/A') ?></td></tr>
<tr><td class="key">Valid To</td><td class="val"><?= htmlspecialchars($certs['apache']['not_after'] ?? 'N/A') ?></td></tr>
<tr><td class="key">Days Remaining</td><td class="val"><?= $certs['apache']['remaining_days'] ?? 'N/A' ?></td></tr>
<?php if (isset($certs['apache']['remaining'])): ?>
<tr><td class="key">Years</td><td class="val"><?= $certs['apache']['remaining']['years'] ?? '' ?></td></tr>
<tr><td class="key">Months</td><td class="val"><?= $certs['apache']['remaining']['months'] ?? '' ?></td></tr>
<tr><td class="key">Days</td><td class="val"><?= $certs['apache']['remaining']['days'] ?? '' ?></td></tr>
<?php endif; ?>
<?php endif; ?>
<?php if (isset($certs['openvpn']) && isset($certs['openvpn']['clients']) && count($certs['openvpn']['clients'])): ?>
<tr><td class="key">OpenVPN Clients</td><td class="val"><?= count($certs['openvpn']['clients']) ?></td></tr>
<?php endif; ?>
</table>
</div>
</div>
<!-- Network Routes -->
<div class="ipf-box">
<header><h2>Network Routes</h2></header>
<div class="ipf-box-body">
<div class="scrollable">
<table class="ipf-table">
<thead>
<tr>
<th>Destination</th>
<th>Via</th>
<th>Device</th>
<th>Raw</th>
</tr>
</thead>
<tbody>
<?php foreach ($routes as $route): ?>
<tr>
<td><?= htmlspecialchars($route['destination'] ?? '-') ?></td>
<td><?= htmlspecialchars($route['via'] ?? '-') ?></td>
<td><?= htmlspecialchars($route['device'] ?? '-') ?></td>
<td style="font-family:'Source Code Pro',monospace;font-size:11px;"><?= htmlspecialchars($route['raw'] ?? '-') ?></td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
</div>
</div>
</div>
<!-- Neighbor Peers -->
<div class="ipf-box">
<header><h2>Neighbor Peers</h2></header>
<div class="ipf-box-body">
<div class="scrollable">
<table class="ipf-table">
<thead>
<tr>
<th>Device</th>
<th>Address</th>
<th>State</th>
</tr>
</thead>
<tbody>
<?php foreach ($neighbors as $neighbor): ?>
<tr>
<td><?= htmlspecialchars($neighbor['device'] ?? '-') ?></td>
<td><?= htmlspecialchars($neighbor['address'] ?? '-') ?></td>
<td class="<?= $neighbor['state'] === 'failed' ? 'badge-offline' : ($neighbor['state'] === 'delay' ? '' : 'badge-online') ?>">
<?= htmlspecialchars(ucfirst($neighbor['state'] ?? 'unknown')) ?>
</td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
</div>
</div>
</div>
<!-- DHCP Leases -->
<div class="ipf-box">
<header><h2>DHCP Leases</h2></header>
<div class="ipf-box-body">
<div class="scrollable">
<table class="ipf-table">
<thead>
<tr>
<th>MAC Address</th>
<th>IP Address</th>
<th>Hostname</th>
<th>Status</th>
<th>Expires</th>
</tr>
</thead>
<tbody>
<?php foreach ($dhcp_leases as $lease): ?>
<tr>
<td><?= htmlspecialchars($lease['mac_address'] ?? '-') ?></td>
<td><?= htmlspecialchars($lease['ip_address'] ?? '-') ?></td>
<td><?= htmlspecialchars($lease['hostname'] ?? '-') ?></td>
<td>
<span class="badge <?= $lease['expired'] ? 'badge-offline' : 'badge-online' ?>">
<?= $lease['expired'] ? 'Expired' : 'Active' ?>
</span>
</td>
<td><?= htmlspecialchars($lease['expires_at'] ?? '-') ?></td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
</div>
</div>
</div>
</div>
</div>
<footer id="footer">
<div><strong>IPFire Appliance Dashboard</strong></div>
<div>
<a href="https://www.ipfire.org/" target="_blank">IPFire.org</a> •
<a href="https://www.ipfire.org/donate" target="_blank">Donate</a>
</div>
</footer>
<script>
(function() {
// Init values from PHP
let lastRx = <?= (float)($traffic['rx_bytes'] ?? 0) ?>;
let lastTx = <?= (float)($traffic['tx_bytes'] ?? 0) ?>;
let lastTime = Date.now();
// Chart.js init
const ctx = document.getElementById('trafficChart').getContext('2d');
const trafficChart = new Chart(ctx, {
type: 'line',
data: {
labels: [],
datasets: [
{
label: 'RX',
borderColor: '#2e7d32',
backgroundColor: 'rgba(46,125,50,0.2)',
fill: true,
tension: 0.2,
data: []
},
{
label: 'TX',
borderColor: '#1565c0',
backgroundColor: 'rgba(21,101,192,0.2)',
fill: true,
tension: 0.2,
data: []
}
]
},
options: {
responsive: true,
maintainAspectRatio: false,
animation: { duration: 0 },
scales: {
y: {
title: { display: true, text: 'Bytes' },
ticks: { stepSize: 1000000 }
}
},
plugins: {
legend: { position: 'top' }
}
}
});
// Keep only last 30 data points
const maxPoints = 30;
let pointCount = 0;
async function updateBandwidth() {
try {
const response = await fetch('?action=poll', { cache: 'no-store' });
if (!response.ok) return;
const json = await response.json();
if (!json.traffic) return;
const now = Date.now();
const curRx = json.traffic.rx_bytes;
const curTx = json.traffic.tx_bytes;
const elapsed = (now - lastTime) / 1000;
if (elapsed > 0) {
const diffRx = Math.max(0, curRx - lastRx);
const diffTx = Math.max(0, curTx - lastTx);
// Update real-time text (Mbit/s)
const rxMbit = ((diffRx * 8) / (elapsed * 1000000)).toFixed(2);
const txMbit = ((diffTx * 8) / (elapsed * 1000000)).toFixed(2);
document.getElementById('red-rx-rate').textContent = rxMbit + ' Mbit/s';
document.getElementById('red-tx-rate').textContent = txMbit + ' Mbit/s';
// Feed Chart.js
const label = 't' + pointCount;
trafficChart.data.labels.push(label);
trafficChart.data.datasets[0].data.push(diffRx);
trafficChart.data.datasets[1].data.push(diffTx);
if (trafficChart.data.labels.length > maxPoints) {
trafficChart.data.labels.shift();
trafficChart.data.datasets.forEach(ds => ds.data.shift());
}
trafficChart.update('quiet'); // 'quiet' avoids redraw animation
}
lastRx = curRx;
lastTx = curTx;
lastTime = now;
} catch (err) {
console.warn('Real-time metric poller failed', err);
}
}
// Poll every 2.5 seconds
setInterval(updateBandwidth, 2500);
})();
</script>
</script>
</body>
</html>