Files
unpoller-unpoller-4/pkg/promunifi/port_anomalies.go
Cody Lee a81a6e6e16 feat: add port anomaly metrics (closes #929) (#982)
Collect port anomalies from the UniFi v2 API endpoint
/proxy/network/v2/api/site/{site}/ports/port-anomalies and export
them to all output plugins (Prometheus, InfluxDB, DataDog, OpenTelemetry).

Metrics exported per port:
- port_anomaly_count     – number of anomaly events
- port_anomaly_last_seen – unix timestamp of last event

Labels: site_name, source, device_mac, port_idx, anomaly_type

Bumps github.com/unpoller/unifi/v5 to v5.24.0 which adds GetPortAnomalies.

Co-authored-by: Claude Sonnet 4.6 (1M context) <noreply@anthropic.com>
2026-03-23 18:56:37 -05:00

40 lines
1006 B
Go

package promunifi
import (
"github.com/prometheus/client_golang/prometheus"
"github.com/unpoller/unifi/v5"
)
type portanomaly struct {
AnomalyCount *prometheus.Desc
AnomalyLastSeen *prometheus.Desc
}
func descPortAnomaly(ns string) *portanomaly {
labels := []string{"site_name", "source", "device_mac", "port_idx", "anomaly_type"}
nd := prometheus.NewDesc
return &portanomaly{
AnomalyCount: nd(ns+"port_anomaly_count", "Number of anomaly events on this port", labels, nil),
AnomalyLastSeen: nd(ns+"port_anomaly_last_seen", "Unix timestamp of the last anomaly event on this port", labels, nil),
}
}
func (u *promUnifi) exportPortAnomalies(r report, anomalies []*unifi.PortAnomaly) {
for _, a := range anomalies {
labels := []string{
a.SiteName,
a.SourceName,
a.DeviceMAC,
a.PortIdx.Txt,
a.AnomalyType,
}
r.send([]*metric{
{u.PortAnomaly.AnomalyCount, gauge, a.Count.Val, labels},
{u.PortAnomaly.AnomalyLastSeen, gauge, a.LastSeen.Val, labels},
})
}
}