Add link speed and utilization tracking to interface stats

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
Ian Gulliver
2026-01-26 12:16:44 -08:00
parent 3255c1cfe5
commit 2af5ad51d7
3 changed files with 106 additions and 27 deletions

View File

@@ -12,6 +12,7 @@ const (
ErrorTypeStartup PortErrorType = "startup"
ErrorTypeNew PortErrorType = "new"
ErrorTypeUnreachable PortErrorType = "unreachable"
ErrorTypeHighUtilization PortErrorType = "high_utilization"
)
type PortError struct {
@@ -24,6 +25,7 @@ type PortError struct {
OutErrors uint64 `json:"out_errors"`
InDelta uint64 `json:"in_delta,omitempty"`
OutDelta uint64 `json:"out_delta,omitempty"`
Utilization float64 `json:"utilization,omitempty"`
FirstSeen time.Time `json:"first_seen"`
LastUpdated time.Time `json:"last_updated"`
}
@@ -65,6 +67,59 @@ func (e *ErrorTracker) CheckPort(node *Node, portName string, stats *InterfaceSt
}
}
func (e *ErrorTracker) CheckUtilization(node *Node, portName string, stats *InterfaceStats) {
if stats == nil || stats.Speed == 0 {
return
}
changed := e.checkUtilizationLocked(node, portName, stats)
if changed {
e.t.NotifyUpdate()
}
}
func (e *ErrorTracker) checkUtilizationLocked(node *Node, portName string, stats *InterfaceStats) bool {
e.mu.Lock()
defer e.mu.Unlock()
maxBytesRate := stats.InBytesRate
if stats.OutBytesRate > maxBytesRate {
maxBytesRate = stats.OutBytesRate
}
speedBytes := float64(stats.Speed) / 8.0
utilization := (maxBytesRate / speedBytes) * 100.0
key := "util:" + node.TypeID + ":" + portName
now := time.Now()
if utilization < 70.0 {
return false
}
if existing, ok := e.errors[key]; ok {
if utilization > existing.Utilization {
existing.Utilization = utilization
existing.LastUpdated = now
return true
}
return false
}
e.nextID++
e.errors[key] = &PortError{
ID: fmt.Sprintf("err-%d", e.nextID),
NodeTypeID: node.TypeID,
NodeName: node.DisplayName(),
PortName: portName,
ErrorType: ErrorTypeHighUtilization,
Utilization: utilization,
FirstSeen: now,
LastUpdated: now,
}
return true
}
func (e *ErrorTracker) checkPortLocked(node *Node, portName string, stats *InterfaceStats) bool {
e.mu.Lock()
defer e.mu.Unlock()

View File

@@ -286,6 +286,7 @@ func (t *Tendrils) queryInterfaceStats(snmp *gosnmp.GoSNMP, node *Node, ifNames
iface.Stats = stats
t.errors.CheckPort(node, name, stats)
t.errors.CheckUtilization(node, name, stats)
}
}

View File

@@ -127,7 +127,8 @@
left: 50%;
transform: translateX(-50%);
font-size: 9px;
background: #555;
font-weight: normal;
background: #444;
color: #fff;
padding: 1px 6px;
border-radius: 8px;
@@ -167,7 +168,8 @@
left: 50%;
transform: translateX(-50%);
font-size: 9px;
background: #555;
font-weight: normal;
background: #444;
color: #fff;
padding: 1px 6px;
border-radius: 8px;
@@ -180,7 +182,8 @@
left: 50%;
transform: translateX(-50%);
font-size: 9px;
background: #864;
font-weight: normal;
background: #753;
color: #fff;
padding: 1px 6px;
border-radius: 8px;
@@ -189,22 +192,22 @@
.node .switch-port.speed-10g,
.node .uplink.speed-10g {
background: #468;
background: #357;
}
.node .switch-port.speed-1g,
.node .uplink.speed-1g {
background: #486;
background: #375;
}
.node .switch-port.speed-100m,
.node .uplink.speed-100m {
background: #864;
background: #753;
}
.node .switch-port.speed-slow,
.node .uplink.speed-slow {
background: #844;
background: #733;
}
.node:hover {
@@ -304,6 +307,7 @@
left: 50%;
transform: translateX(-50%);
font-size: 9px;
font-weight: normal;
padding: 1px 6px;
border-radius: 8px;
white-space: nowrap;
@@ -327,12 +331,12 @@
}
.node .dante-info.tx-info {
background: #864;
background: #753;
color: #fff;
}
.node .dante-info.rx-info {
background: #468;
background: #357;
color: #fff;
}
@@ -828,17 +832,19 @@
};
}
function formatRate(bps) {
if (bps < 1024) return bps.toFixed(0) + ' B/s';
if (bps < 1024 * 1024) return (bps / 1024).toFixed(1) + ' KB/s';
if (bps < 1024 * 1024 * 1024) return (bps / (1024 * 1024)).toFixed(1) + ' MB/s';
return (bps / (1024 * 1024 * 1024)).toFixed(1) + ' GB/s';
function formatMbps(bytesPerSec) {
const mbps = (bytesPerSec * 8) / 1000000;
return Math.round(mbps).toLocaleString() + ' Mbit/s';
}
function formatPps(pps) {
if (pps < 1000) return pps.toFixed(0) + ' pps';
if (pps < 1000000) return (pps / 1000).toFixed(1) + 'K pps';
return (pps / 1000000).toFixed(1) + 'M pps';
return Math.round(pps).toLocaleString() + ' pps';
}
function formatLinkSpeed(bps) {
if (!bps) return '?';
const mbps = bps / 1000000;
return mbps.toLocaleString() + ' Mbit/s';
}
let anonCounter = 0;
@@ -940,11 +946,12 @@
const errOut = switchConnection.errors?.out || 0;
const statsInfo = document.createElement('div');
statsInfo.className = 'error-info';
let statsText = 'err: ' + errIn + '/' + errOut;
let statsText = 'link: ' + formatLinkSpeed(switchConnection.speed);
statsText += '\nerr: rx ' + errIn + ' / tx ' + errOut;
if (switchConnection.rates) {
const r = switchConnection.rates;
statsText += '\nrx: ' + formatRate(r.outBytes) + ' (' + formatPps(r.outPkts) + ')';
statsText += '\ntx: ' + formatRate(r.inBytes) + ' (' + formatPps(r.inPkts) + ')';
statsText += '\nrx: ' + formatMbps(r.outBytes) + ' (' + formatPps(r.outPkts) + ')';
statsText += '\ntx: ' + formatMbps(r.inBytes) + ' (' + formatPps(r.inPkts) + ')';
}
statsInfo.textContent = statsText;
portEl.appendChild(statsInfo);
@@ -1022,11 +1029,12 @@
const errOut = uplinkInfo.errors?.out || 0;
const statsInfo = document.createElement('div');
statsInfo.className = 'error-info';
let statsText = 'err: ' + errIn + '/' + errOut;
let statsText = 'link: ' + formatLinkSpeed(uplinkInfo.speed);
statsText += '\nerr: rx ' + errIn + ' / tx ' + errOut;
if (uplinkInfo.rates) {
const r = uplinkInfo.rates;
statsText += '\nrx: ' + formatRate(r.inBytes) + ' (' + formatPps(r.inPkts) + ')';
statsText += '\ntx: ' + formatRate(r.outBytes) + ' (' + formatPps(r.outPkts) + ')';
statsText += '\nrx: ' + formatMbps(r.inBytes) + ' (' + formatPps(r.inPkts) + ')';
statsText += '\ntx: ' + formatMbps(r.outBytes) + ' (' + formatPps(r.outPkts) + ')';
}
statsInfo.textContent = statsText;
uplinkEl.appendChild(statsInfo);
@@ -1153,6 +1161,21 @@
typeEl.className = 'error-type';
typeEl.textContent = 'Unreachable';
item.appendChild(typeEl);
} else if (err.error_type === 'high_utilization') {
const portEl = document.createElement('div');
portEl.className = 'error-port';
portEl.textContent = 'Port: ' + err.port_name;
item.appendChild(portEl);
const countsEl = document.createElement('div');
countsEl.className = 'error-counts';
countsEl.textContent = 'Utilization: ' + (err.utilization || 0).toFixed(0) + '%';
item.appendChild(countsEl);
const typeEl = document.createElement('div');
typeEl.className = 'error-type';
typeEl.textContent = 'High link utilization';
item.appendChild(typeEl);
} else {
const portEl = document.createElement('div');
portEl.className = 'error-port';
@@ -1161,7 +1184,7 @@
const countsEl = document.createElement('div');
countsEl.className = 'error-counts';
countsEl.textContent = 'In: ' + err.in_errors + ' (+' + (err.in_delta || 0) + ') / Out: ' + err.out_errors + ' (+' + (err.out_delta || 0) + ')';
countsEl.textContent = 'rx: ' + err.in_errors + ' (+' + (err.in_delta || 0) + ') / tx: ' + err.out_errors + ' (+' + (err.out_delta || 0) + ')';
item.appendChild(countsEl);
const typeEl = document.createElement('div');