add interface stats and poe power monitoring via snmp

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
Ian Gulliver
2026-01-22 22:47:30 -08:00
parent 54fc941728
commit 5333d3e11e
2 changed files with 245 additions and 3 deletions

View File

@@ -11,9 +11,22 @@ import (
)
type Interface struct {
Name string
MAC net.HardwareAddr
IPs map[string]net.IP
Name string
MAC net.HardwareAddr
IPs map[string]net.IP
Stats *InterfaceStats
}
type InterfaceStats struct {
Speed uint64 // bits per second
InErrors uint64
OutErrors uint64
PoE *PoEStats
}
type PoEStats struct {
Power float64 // watts in use
MaxPower float64 // watts allocated/negotiated
}
func (i *Interface) String() string {
@@ -31,6 +44,9 @@ func (i *Interface) String() string {
if len(ips) > 0 {
parts = append(parts, fmt.Sprintf("%v", ips))
}
if i.Stats != nil {
parts = append(parts, i.Stats.String())
}
result := parts[0]
for _, p := range parts[1:] {
@@ -39,6 +55,45 @@ func (i *Interface) String() string {
return result
}
func (s *InterfaceStats) String() string {
var parts []string
if s.Speed > 0 {
if s.Speed >= 1000000000 {
parts = append(parts, fmt.Sprintf("%dG", s.Speed/1000000000))
} else if s.Speed >= 1000000 {
parts = append(parts, fmt.Sprintf("%dM", s.Speed/1000000))
} else {
parts = append(parts, fmt.Sprintf("%d", s.Speed))
}
}
if s.InErrors > 0 || s.OutErrors > 0 {
parts = append(parts, fmt.Sprintf("err:%d/%d", s.InErrors, s.OutErrors))
}
if s.PoE != nil {
if s.PoE.MaxPower > 0 {
parts = append(parts, fmt.Sprintf("poe:%.1f/%.1fW", s.PoE.Power, s.PoE.MaxPower))
} else {
parts = append(parts, fmt.Sprintf("poe:%.1fW", s.PoE.Power))
}
}
return "[" + fmt.Sprintf("%s", joinParts(parts)) + "]"
}
func joinParts(parts []string) string {
result := ""
for i, p := range parts {
if i > 0 {
result += " "
}
result += p
}
return result
}
type Node struct {
Name string
Interfaces map[string]*Interface