Files
Prototype0645 ca64173b35 feat(wan): label WAN interface state with its source
unpoller_wan_interface_state ships with site_name and nothing else to
identify where it came from. That is not enough: every UniFi OS console
names its only site "default", so site_name is "Default (default)" on all
of them. An instance polling several controllers emits WAN status that is
indistinguishable, and downstream attribution has to be guessed.

Observed in production before the fix: a scrape config guessing from
site_name filed a UDM's WAN state under the wrong customer. No error, no
missing series — just wrong data under someone else's name.

unifi/v6.1.0 (#244) added SourceName to WANStatus, stamped from the
controller URL in all three read paths. go.mod is already on v6.1.0, so
this only has to read the field.

Two lines, same shape as #1071 which did this for unpoller_wan_*.

Tests use the fakeReport already present in the package: every emitted
metric carries both site_name and source, the raw state is kept as a
label, and a nil status still produces nothing rather than panicking a
poll. TestExportWANStatusIsAttributed fails on master and passes here.
2026-08-31 23:42:30 +02:00

45 lines
1.0 KiB
Go

package promunifi
import (
"github.com/prometheus/client_golang/prometheus"
"github.com/unpoller/unifi/v6"
)
type wanStatus struct {
InterfaceState *prometheus.Desc
}
func descWANStatus(ns string) *wanStatus {
labels := []string{"site_name", "source", "wan_interface", "wan_networkgroup", "state"}
return &wanStatus{
InterfaceState: prometheus.NewDesc(ns+"wan_interface_state",
"WAN interface state: 1=ACTIVE, 0=other. The state label carries the raw "+
"controller value (ACTIVE, BACKUP, or DISCONNECTED) so BACKUP and DISCONNECTED "+
"can be distinguished.",
labels, nil),
}
}
func wanStateValue(state string) float64 {
if state == "ACTIVE" {
return 1
}
return 0
}
func (u *promUnifi) exportWANStatus(r report, ws *unifi.WANStatus) {
if ws == nil {
return
}
for _, iface := range ws.WANInterfaces {
labels := []string{ws.SiteName, ws.SourceName, iface.Name, iface.WANNetworkgroup, iface.State}
r.send([]*metric{
{u.WANStatus.InterfaceState, gauge, wanStateValue(iface.State), labels},
})
}
}