diff --git a/go.mod b/go.mod index f09101fe..5a84e229 100644 --- a/go.mod +++ b/go.mod @@ -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.5 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 @@ -50,4 +48,4 @@ require ( ) // for local iterative development only -// replace github.com/unpoller/unifi/v5 => ../unifi +replace github.com/unpoller/unifi/v5 => ../unifi diff --git a/go.sum b/go.sum index f7cb8649..dcb5c902 100644 --- a/go.sum +++ b/go.sum @@ -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.5 h1:4u3Dd3CrEH9t0BMCy/jjBMFN2xk67Zm3UKOT//Oq/ZQ= +github.com/unpoller/unifi/v5 v5.0.5/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= diff --git a/pkg/datadogunifi/datadog.go b/pkg/datadogunifi/datadog.go index c88fb58a..891a9455 100644 --- a/pkg/datadogunifi/datadog.go +++ b/pkg/datadogunifi/datadog.go @@ -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: diff --git a/pkg/datadogunifi/ubb.go b/pkg/datadogunifi/ubb.go new file mode 100644 index 00000000..05e167e8 --- /dev/null +++ b/pkg/datadogunifi/ubb.go @@ -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, + } +} diff --git a/pkg/datadogunifi/uci.go b/pkg/datadogunifi/uci.go new file mode 100644 index 00000000..02ac89ab --- /dev/null +++ b/pkg/datadogunifi/uci.go @@ -0,0 +1,84 @@ +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(uxgT) + + 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) +} diff --git a/pkg/influxunifi/influxdb.go b/pkg/influxunifi/influxdb.go index 9017b90e..5c11de08 100644 --- a/pkg/influxunifi/influxdb.go +++ b/pkg/influxunifi/influxdb.go @@ -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: diff --git a/pkg/influxunifi/report.go b/pkg/influxunifi/report.go index 8cd45569..b8fcfcbb 100644 --- a/pkg/influxunifi/report.go +++ b/pkg/influxunifi/report.go @@ -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)) diff --git a/pkg/influxunifi/ubb.go b/pkg/influxunifi/ubb.go new file mode 100644 index 00000000..57333b0a --- /dev/null +++ b/pkg/influxunifi/ubb.go @@ -0,0 +1,104 @@ +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, + } +} diff --git a/pkg/influxunifi/uci.go b/pkg/influxunifi/uci.go new file mode 100644 index 00000000..5ccf8442 --- /dev/null +++ b/pkg/influxunifi/uci.go @@ -0,0 +1,83 @@ +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}) +} diff --git a/pkg/inputunifi/collector.go b/pkg/inputunifi/collector.go index 14fde812..9642964c 100644 --- a/pkg/inputunifi/collector.go +++ b/pkg/inputunifi/collector.go @@ -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) diff --git a/pkg/promunifi/collector.go b/pkg/promunifi/collector.go index 62ce3a56..4f2cd225 100644 --- a/pkg/promunifi/collector.go +++ b/pkg/promunifi/collector.go @@ -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) diff --git a/pkg/promunifi/report.go b/pkg/promunifi/report.go index c3543b4b..c31d18f2 100644 --- a/pkg/promunifi/report.go +++ b/pkg/promunifi/report.go @@ -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() diff --git a/pkg/promunifi/ubb.go b/pkg/promunifi/ubb.go new file mode 100644 index 00000000..f7857d21 --- /dev/null +++ b/pkg/promunifi/ubb.go @@ -0,0 +1,35 @@ +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")}}) +} diff --git a/pkg/promunifi/uci.go b/pkg/promunifi/uci.go new file mode 100644 index 00000000..bfc51caa --- /dev/null +++ b/pkg/promunifi/uci.go @@ -0,0 +1,32 @@ +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}, + }) +}