Merge pull request #787 from unpoller/add-ubb-and-uci-support

Adds UBB & UCI support
This commit is contained in:
Cody Lee 2025-01-02 11:37:29 -06:00 committed by GitHub
commit 7a4ba7e4a2
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
16 changed files with 541 additions and 8 deletions

4
go.mod
View File

@ -13,7 +13,7 @@ require (
github.com/prometheus/common v0.61.0
github.com/spf13/pflag v1.0.6-0.20201009195203-85dd5c8bc61c
github.com/stretchr/testify v1.10.0
github.com/unpoller/unifi/v5 v5.0.4
github.com/unpoller/unifi/v5 v5.0.7
golang.org/x/crypto v0.31.0
golang.org/x/net v0.33.0
golang.org/x/term v0.27.0
@ -23,8 +23,6 @@ require (
gopkg.in/yaml.v3 v3.0.1
)
require github.com/unpoller/unifi v0.4.3 // indirect
require (
github.com/BurntSushi/toml v1.4.0 // indirect
github.com/Microsoft/go-winio v0.6.0 // indirect

6
go.sum
View File

@ -75,10 +75,8 @@ github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO
github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4=
github.com/stretchr/testify v1.10.0 h1:Xv5erBjTwe/5IxqUQTdXv5kgmIvbHo3QQyRwhJsOfJA=
github.com/stretchr/testify v1.10.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
github.com/unpoller/unifi v0.4.3 h1:MyX27nf/Nq9a+p/o5qIjNJDJSS+jvxGC7BbxDk09BRg=
github.com/unpoller/unifi v0.4.3/go.mod h1:TWzPB/1SVbvoweS3RcknQj3Ds+MclHzGGE2weqI+vO0=
github.com/unpoller/unifi/v5 v5.0.4 h1:JkmNgEyAGc6Oy6siWC8nwBiu08uLB4I5dz54jRmwLOY=
github.com/unpoller/unifi/v5 v5.0.4/go.mod h1:G45KRuSH9PFrIUFmDTzWEEM/E/e7GuyXp36AVOfhm7I=
github.com/unpoller/unifi/v5 v5.0.7 h1:Dj5HY2Nhdic4Ygvh2YYW6QKIZjXCSo9IBzVDKaj86Zg=
github.com/unpoller/unifi/v5 v5.0.7/go.mod h1:G45KRuSH9PFrIUFmDTzWEEM/E/e7GuyXp36AVOfhm7I=
github.com/yuin/goldmark v1.3.5/go.mod h1:mwnBkeHKe2W/ZEtQ+71ViKU8L12m81fl3OWwC1Zlc8k=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=

View File

@ -332,6 +332,10 @@ func (u *DatadogUnifi) switchExport(r report, v any) { //nolint:cyclop
u.batchUXG(r, v)
case *unifi.UDM:
u.batchUDM(r, v)
case *unifi.UBB:
u.batchUBB(r, v)
case *unifi.UCI:
u.batchUCI(r, v)
case *unifi.Site:
u.reportSite(r, v)
case *unifi.Client:

View File

@ -346,6 +346,26 @@ gauges:
- unifi.usw.tx_bytes
- unifi.usw.upgradeable
- unifi.usw.uptime
- unifi.ubb.loadavg_1
- unifi.ubb.mem_used
- unifi.ubb.cpu
- unifi.ubb.system_uptime
- unifi.ubb.probe
- unifi.ubb.mem_total
- unifi.ubb.mem_buffer
- unifi.ubb.bytes
- unifi.ubb.loadavg_5
- unifi.ubb.state
- unifi.ubb.tx_bytes
- unifi.ubb.loadavg_15
- unifi.ubb.user_num_sta
- unifi.ubb.memory
- unifi.ubb.rx_bytes
- unifi.ubb.last_seen
- unifi.ubb.sys
- unifi.ubb.uptime
- unifi.ubb.mem
- unifi.ubb.network
counts:
- unifi.collector.num_devices
- unifi.collector.num_errors

107
pkg/datadogunifi/ubb.go Normal file
View File

@ -0,0 +1,107 @@
package datadogunifi
import (
"github.com/unpoller/unifi/v5"
)
// ubbT is used as a name for printed/logged counters.
const ubbT = item("UBB")
// batchUBB generates UBB datapoints for Datadog.
// These points can be passed directly to datadog.
func (u *DatadogUnifi) batchUBB(r report, s *unifi.UBB) { // nolint: funlen
if !s.Adopted.Val || s.Locating.Val {
return
}
tags := cleanTags(map[string]string{
"source": s.SourceName,
"mac": s.Mac,
"site_name": s.SiteName,
"name": s.Name,
"version": s.Version,
"model": s.Model,
"serial": s.Serial,
"type": s.Type,
"ip": s.IP,
"license_state": s.LicenseState,
})
var sw *unifi.Bb
if s.Stat != nil {
sw = s.Stat.Bb
}
sysStats := unifi.SysStats{}
if s.SysStats != nil {
sysStats = *s.SysStats
}
systemStats := unifi.SystemStats{}
if s.SystemStats != nil {
systemStats = *s.SystemStats
}
data := CombineFloat64(
u.batchSysStats(sysStats, systemStats),
map[string]float64{
"bytes": s.Bytes.Val,
"last_seen": s.LastSeen.Val,
"rx_bytes": s.RxBytes.Val,
"tx_bytes": s.TxBytes.Val,
"uptime": s.Uptime.Val,
"state": s.State.Val,
"user_num_sta": s.UserNumSta.Val,
},
)
r.addCount(ubbT)
metricName := metricNamespace("ubb")
reportGaugeForFloat64Map(r, metricName, data, tags)
tags = cleanTags(map[string]string{
"mac": s.Mac,
"site_name": s.SiteName,
"source": s.SourceName,
"name": s.Name,
"version": s.Version,
"model": s.Model,
"serial": s.Serial,
"type": s.Type,
"ip": s.IP,
})
data = CombineFloat64(
u.batchUBBstat(sw),
map[string]float64{
"bytes": s.Bytes.Val,
"last_seen": s.LastSeen.Val,
"rx_bytes": s.RxBytes.Val,
"tx_bytes": s.TxBytes.Val,
"uptime": s.Uptime.Val,
})
metricName = metricNamespace("usw")
reportGaugeForFloat64Map(r, metricName, data, tags)
}
func (u *DatadogUnifi) batchUBBstat(sw *unifi.Bb) map[string]float64 {
if sw == nil {
return map[string]float64{}
}
return map[string]float64{
"stat_bytes": sw.Bytes.Val,
"stat_rx_bytes": sw.RxBytes.Val,
"stat_rx_crypts": sw.RxCrypts.Val,
"stat_rx_dropped": sw.RxDropped.Val,
"stat_rx_errors": sw.RxErrors.Val,
"stat_rx_frags": sw.RxFrags.Val,
"stat_rx_packets": sw.TxPackets.Val,
"stat_tx_bytes": sw.TxBytes.Val,
"stat_tx_dropped": sw.TxDropped.Val,
"stat_tx_errors": sw.TxErrors.Val,
"stat_tx_packets": sw.TxPackets.Val,
"stat_tx_retries": sw.TxRetries.Val,
}
}

85
pkg/datadogunifi/uci.go Normal file
View File

@ -0,0 +1,85 @@
package datadogunifi
import (
"github.com/unpoller/unifi/v5"
)
// uciT is used as a name for printed/logged counters.
const uciT = item("UCI")
// batchUCI generates UCI datapoints for Datadog.
// These points can be passed directly to datadog.
func (u *DatadogUnifi) batchUCI(r report, s *unifi.UCI) { // nolint: funlen
if !s.Adopted.Val || s.Locating.Val {
return
}
tags := cleanTags(map[string]string{
"source": s.SourceName,
"mac": s.Mac,
"site_name": s.SiteName,
"name": s.Name,
"version": s.Version,
"model": s.Model,
"serial": s.Serial,
"type": s.Type,
"ip": s.IP,
"license_state": s.LicenseState,
})
var sw *unifi.Sw
if s.Stat != nil {
sw = s.Stat.Sw
}
sysStats := unifi.SysStats{}
if s.SysStats != nil {
sysStats = *s.SysStats
}
systemStats := unifi.SystemStats{}
if s.SystemStats != nil {
systemStats = *s.SystemStats
}
data := CombineFloat64(
u.batchSysStats(sysStats, systemStats),
map[string]float64{
"bytes": s.Bytes.Val,
"last_seen": s.LastSeen.Val,
"rx_bytes": s.RxBytes.Val,
"tx_bytes": s.TxBytes.Val,
"uptime": s.Uptime.Val,
"state": s.State.Val,
},
)
r.addCount(uciT)
metricName := metricNamespace("usg")
reportGaugeForFloat64Map(r, metricName, data, tags)
tags = cleanTags(map[string]string{
"mac": s.Mac,
"site_name": s.SiteName,
"source": s.SourceName,
"name": s.Name,
"version": s.Version,
"model": s.Model,
"serial": s.Serial,
"type": s.Type,
"ip": s.IP,
})
data = CombineFloat64(
u.batchUSWstat(sw),
map[string]float64{
"bytes": s.Bytes.Val,
"last_seen": s.LastSeen.Val,
"rx_bytes": s.RxBytes.Val,
"tx_bytes": s.TxBytes.Val,
"uptime": s.Uptime.Val,
})
metricName = metricNamespace("usw")
reportGaugeForFloat64Map(r, metricName, data, tags)
}

View File

@ -443,6 +443,10 @@ func (u *InfluxUnifi) switchExport(r report, v any) { //nolint:cyclop
u.batchUSG(r, v)
case *unifi.UXG:
u.batchUXG(r, v)
case *unifi.UBB:
u.batchUBB(r, v)
case *unifi.UCI:
u.batchUCI(r, v)
case *unifi.UDM:
u.batchUDM(r, v)
case *unifi.Site:

View File

@ -390,6 +390,35 @@ points:
wifi_tx_latency_mov_max: float
wifi_tx_latency_mov_min: float
wifi_tx_latency_mov_total: float
ubb:
tags:
- mac
- model
- name
- serial
- site_name
- source
- type
- version
fields:
bytes: float
ip: string
last_seen: float
rx_bytes: float
stat_bytes: float
stat_rx_bytes: float
stat_rx_crypts: float
stat_rx_dropped: float
stat_rx_errors: float
stat_rx_frags: float
stat_rx_packets: float
stat_tx_bytes: float
stat_tx_dropped: float
stat_tx_errors: float
stat_tx_packets: float
stat_tx_retries: float
tx_bytes: float
uptime: float
unifi_alarm:
tags:
- action

View File

@ -117,7 +117,7 @@ func (r *Report) String() string {
"Gateways: %d, %s: %d, %s: %d, %s/%s/%s/%s: %d/%d/%d/%d, "+
"DPI Site/Client: %d/%d, %s: %d, %s: %d, Err: %d, Dur: %v",
len(m.Sites), len(m.Clients),
c[udmT]+c[usgT]+c[uxgT], uapT, c[uapT], uswT, c[uswT],
c[udmT]+c[usgT]+c[uxgT]+c[uciT]+c[ubbT], uapT, c[uapT], uswT, c[uswT],
idsT, eventT, alarmT, anomalyT, c[idsT], c[eventT], c[alarmT], c[anomalyT],
len(m.SitesDPI), len(m.ClientsDPI), pointT, c[pointT], fieldT, c[fieldT],
len(r.Errors), r.Elapsed.Round(time.Millisecond))

106
pkg/influxunifi/ubb.go Normal file
View File

@ -0,0 +1,106 @@
package influxunifi
import (
"github.com/unpoller/unifi/v5"
)
// ubbT is used as a name for printed/logged counters.
const ubbT = item("UBB")
// batchUXG generates UBB datapoints for InfluxDB.
// These points can be passed directly to influx.
func (u *InfluxUnifi) batchUBB(r report, s *unifi.UBB) { // nolint: funlen
if !s.Adopted.Val || s.Locating.Val {
return
}
tags := map[string]string{
"source": s.SourceName,
"mac": s.Mac,
"site_name": s.SiteName,
"name": s.Name,
"version": s.Version,
"model": s.Model,
"serial": s.Serial,
"type": s.Type,
}
var sw *unifi.Bb
if s.Stat != nil {
sw = s.Stat.Bb
}
sysStats := unifi.SysStats{}
if s.SysStats != nil {
sysStats = *s.SysStats
}
systemStats := unifi.SystemStats{}
if s.SystemStats != nil {
systemStats = *s.SystemStats
}
fields := Combine(
u.batchSysStats(sysStats, systemStats),
map[string]any{
"source": s.SourceName,
"ip": s.IP,
"bytes": s.Bytes.Val,
"last_seen": s.LastSeen.Val,
"license_state": s.LicenseState,
"rx_bytes": s.RxBytes.Val,
"tx_bytes": s.TxBytes.Val,
"uptime": s.Uptime.Val,
"state": s.State.Val,
"user-num_sta": s.UserNumSta.Val,
"version": s.Version,
},
)
r.addCount(ubbT)
r.send(&metric{Table: "usg", Tags: tags, Fields: fields})
tags = map[string]string{
"mac": s.Mac,
"site_name": s.SiteName,
"source": s.SourceName,
"name": s.Name,
"version": s.Version,
"model": s.Model,
"serial": s.Serial,
"type": s.Type,
}
fields = Combine(
u.batchUBBstat(sw),
map[string]any{
"ip": s.IP,
"bytes": s.Bytes.Val,
"last_seen": s.LastSeen.Val,
"rx_bytes": s.RxBytes.Val,
"tx_bytes": s.TxBytes.Val,
"uptime": s.Uptime.Val,
})
r.send(&metric{Table: "ubb", Tags: tags, Fields: fields})
}
func (u *InfluxUnifi) batchUBBstat(sw *unifi.Bb) map[string]any {
if sw == nil {
return map[string]any{}
}
return map[string]any{
"stat_bytes": sw.Bytes.Val,
"stat_rx_bytes": sw.RxBytes.Val,
"stat_rx_crypts": sw.RxCrypts.Val,
"stat_rx_dropped": sw.RxDropped.Val,
"stat_rx_errors": sw.RxErrors.Val,
"stat_rx_frags": sw.RxFrags.Val,
"stat_rx_packets": sw.TxPackets.Val,
"stat_tx_bytes": sw.TxBytes.Val,
"stat_tx_dropped": sw.TxDropped.Val,
"stat_tx_errors": sw.TxErrors.Val,
"stat_tx_packets": sw.TxPackets.Val,
"stat_tx_retries": sw.TxRetries.Val,
}
}

84
pkg/influxunifi/uci.go Normal file
View File

@ -0,0 +1,84 @@
package influxunifi
import (
"github.com/unpoller/unifi/v5"
)
// uciT is used as a name for printed/logged counters.
const uciT = item("UCI")
// batchUCI generates UCI datapoints for InfluxDB.
// These points can be passed directly to influx.
func (u *InfluxUnifi) batchUCI(r report, s *unifi.UCI) { // nolint: funlen
if !s.Adopted.Val || s.Locating.Val {
return
}
tags := map[string]string{
"source": s.SourceName,
"mac": s.Mac,
"site_name": s.SiteName,
"name": s.Name,
"version": s.Version,
"model": s.Model,
"serial": s.Serial,
"type": s.Type,
}
var sw *unifi.Sw
if s.Stat != nil {
sw = s.Stat.Sw
}
sysStats := unifi.SysStats{}
if s.SysStats != nil {
sysStats = *s.SysStats
}
systemStats := unifi.SystemStats{}
if s.SystemStats != nil {
systemStats = *s.SystemStats
}
fields := Combine(
u.batchSysStats(sysStats, systemStats),
map[string]any{
"source": s.SourceName,
"ip": s.IP,
"bytes": s.Bytes.Val,
"last_seen": s.LastSeen.Val,
"license_state": s.LicenseState,
"rx_bytes": s.RxBytes.Val,
"tx_bytes": s.TxBytes.Val,
"uptime": s.Uptime.Val,
"state": s.State.Val,
"version": s.Version,
},
)
r.addCount(uciT)
r.send(&metric{Table: "usg", Tags: tags, Fields: fields})
tags = map[string]string{
"mac": s.Mac,
"site_name": s.SiteName,
"source": s.SourceName,
"name": s.Name,
"version": s.Version,
"model": s.Model,
"serial": s.Serial,
"type": s.Type,
}
fields = Combine(
u.batchUSWstat(sw),
map[string]any{
"ip": s.IP,
"bytes": s.Bytes.Val,
"last_seen": s.LastSeen.Val,
"rx_bytes": s.RxBytes.Val,
"tx_bytes": s.TxBytes.Val,
"uptime": s.Uptime.Val,
})
r.send(&metric{Table: "usw", Tags: tags, Fields: fields})
}

View File

@ -218,6 +218,16 @@ func extractDevices(metrics *Metrics) (*poller.Metrics, map[string]string, map[s
m.Devices = append(m.Devices, r)
}
for _, r := range metrics.Devices.UBBs {
devices[r.Mac] = r.Name
m.Devices = append(m.Devices, r)
}
for _, r := range metrics.Devices.UCIs {
devices[r.Mac] = r.Name
m.Devices = append(m.Devices, r)
}
for _, r := range metrics.Devices.PDUs {
devices[r.Mac] = r.Name
m.Devices = append(m.Devices, r)

View File

@ -92,6 +92,8 @@ type Report struct {
UAP int // Total count of UAP devices.
UDM int // Total count of UDM devices.
UXG int // Total count of UXG devices.
UBB int // Total count of UBB devices.
UCI int // Total count of UCI devices.
Metrics *poller.Metrics // Metrics collected and recorded.
Elapsed time.Duration // Duration elapsed collecting and exporting.
Fetch time.Duration // Duration elapsed making controller requests.
@ -412,6 +414,12 @@ func (u *promUnifi) switchExport(r report, v any) {
case *unifi.UXG:
r.addUXG()
u.exportUXG(r, v)
case *unifi.UBB:
r.addUBB()
u.exportUBB(r, v)
case *unifi.UCI:
r.addUCI()
u.exportUCI(r, v)
case *unifi.UDM:
r.addUDM()
u.exportUDM(r, v)

View File

@ -21,6 +21,8 @@ type report interface {
error(ch chan<- prometheus.Metric, d *prometheus.Desc, v any)
addUDM()
addUXG()
addUBB()
addUCI()
addUSG()
addUAP()
addUSW()
@ -97,6 +99,14 @@ func (r *Report) addUXG() {
r.UXG++
}
func (r *Report) addUBB() {
r.UCI++
}
func (r *Report) addUCI() {
r.UCI++
}
// close is not part of the interface.
func (r *Report) close() {
r.wg.Wait()

36
pkg/promunifi/ubb.go Normal file
View File

@ -0,0 +1,36 @@
package promunifi
import (
"github.com/unpoller/unifi/v5"
)
// exportUBB is a collection of stats from UBB.
func (u *promUnifi) exportUBB(r report, d *unifi.UBB) {
if !d.Adopted.Val || d.Locating.Val {
return
}
//var sw *unifi.Bb
//if d.Stat != nil {
// sw = d.Stat.Bb
//}
// unsure of what to do with this yet.
labels := []string{d.Type, d.SiteName, d.Name, d.SourceName}
infoLabels := []string{d.Version, d.Model, d.Serial, d.Mac, d.IP, d.ID}
// Shared data (all devices do this).
u.exportBYTstats(r, labels, d.TxBytes, d.RxBytes)
if d.SysStats != nil && d.SystemStats != nil {
u.exportSYSstats(r, labels, *d.SysStats, *d.SystemStats)
}
// Dream Machine System Data.
r.send([]*metric{
{u.Device.Info, gauge, 1.0, append(labels, infoLabels...)},
{u.Device.Uptime, gauge, d.Uptime, labels},
})
// temperature
r.send([]*metric{{u.Device.Temperature, gauge, d.GeneralTemperature.Val, append(labels, d.Name, "general")}})
}

34
pkg/promunifi/uci.go Normal file
View File

@ -0,0 +1,34 @@
package promunifi
import (
"github.com/unpoller/unifi/v5"
)
// exportUCI is a collection of stats from UCI.
func (u *promUnifi) exportUCI(r report, d *unifi.UCI) {
if !d.Adopted.Val || d.Locating.Val {
return
}
var sw *unifi.Sw
if d.Stat != nil {
sw = d.Stat.Sw
}
labels := []string{d.Type, d.SiteName, d.Name, d.SourceName}
infoLabels := []string{d.Version, d.Model, d.Serial, d.Mac, d.IP, d.ID}
// Shared data (all devices do this).
u.exportBYTstats(r, labels, d.TxBytes, d.RxBytes)
if d.SysStats != nil && d.SystemStats != nil {
u.exportSYSstats(r, labels, *d.SysStats, *d.SystemStats)
}
// Switch Data
u.exportUSWstats(r, labels, sw)
// Dream Machine System Data.
r.send([]*metric{
{u.Device.Info, gauge, 1.0, append(labels, infoLabels...)},
{u.Device.Uptime, gauge, d.Uptime, labels},
})
}