mirror of
https://github.com/unpoller/unpoller.git
synced 2026-03-31 06:33:57 -04:00
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>
35 lines
1013 B
Go
35 lines
1013 B
Go
package otelunifi
|
|
|
|
import (
|
|
"context"
|
|
|
|
"go.opentelemetry.io/otel/attribute"
|
|
"go.opentelemetry.io/otel/metric"
|
|
|
|
"github.com/unpoller/unifi/v5"
|
|
"github.com/unpoller/unpoller/pkg/poller"
|
|
)
|
|
|
|
// exportPortAnomalies emits per-port anomaly metrics.
|
|
func (u *OtelOutput) exportPortAnomalies(ctx context.Context, meter metric.Meter, m *poller.Metrics, r *Report) {
|
|
for _, item := range m.PortAnomalies {
|
|
a, ok := item.(*unifi.PortAnomaly)
|
|
if !ok {
|
|
continue
|
|
}
|
|
|
|
attrs := attribute.NewSet(
|
|
attribute.String("site_name", a.SiteName),
|
|
attribute.String("source", a.SourceName),
|
|
attribute.String("device_mac", a.DeviceMAC),
|
|
attribute.String("port_idx", a.PortIdx.Txt),
|
|
attribute.String("anomaly_type", a.AnomalyType),
|
|
)
|
|
|
|
u.recordGauge(ctx, meter, r, "unifi_port_anomaly_count",
|
|
"Number of anomaly events on this port", a.Count.Val, attrs)
|
|
u.recordGauge(ctx, meter, r, "unifi_port_anomaly_last_seen",
|
|
"Unix timestamp of the last anomaly event on this port", a.LastSeen.Val, attrs)
|
|
}
|
|
}
|