diff --git a/promunifi/clients.go b/promunifi/clients.go index 174ce35f..10c32859 100644 --- a/promunifi/clients.go +++ b/promunifi/clients.go @@ -78,7 +78,7 @@ func (u *unifiCollector) exportClient(r report, c *unifi.Client) { if c.IsWired.Val { labels[len(labels)-1] = "true" labelW[len(labelW)-1] = "true" - r.send([]*metricExports{ + r.send([]*metric{ {u.Client.RxBytes, prometheus.CounterValue, c.WiredRxBytes, labels}, {u.Client.RxBytesR, prometheus.GaugeValue, c.WiredRxBytesR, labels}, {u.Client.RxPackets, prometheus.CounterValue, c.WiredRxPackets, labels}, @@ -89,7 +89,7 @@ func (u *unifiCollector) exportClient(r report, c *unifi.Client) { } else { labels[len(labels)-1] = "false" labelW[len(labelW)-1] = "false" - r.send([]*metricExports{ + r.send([]*metric{ {u.Client.Anomalies, prometheus.CounterValue, c.Anomalies, labelW}, {u.Client.CCQ, prometheus.GaugeValue, c.Ccq / 10, labelW}, {u.Client.Noise, prometheus.GaugeValue, c.Noise, labelW}, @@ -109,7 +109,7 @@ func (u *unifiCollector) exportClient(r report, c *unifi.Client) { {u.Client.BytesR, prometheus.GaugeValue, c.BytesR, labelW}, }) } - r.send([]*metricExports{ + r.send([]*metric{ {u.Client.Uptime, prometheus.GaugeValue, c.Uptime, labelW}, /* needs more "looking into" {u.Client.DpiStatsApp, prometheus.GaugeValue, c.DpiStats.App, labels}, diff --git a/promunifi/collector.go b/promunifi/collector.go index 6ea0964d..6433ce13 100644 --- a/promunifi/collector.go +++ b/promunifi/collector.go @@ -43,7 +43,7 @@ type unifiCollector struct { Site *site } -type metricExports struct { +type metric struct { Desc *prometheus.Desc ValueType prometheus.ValueType Value interface{} @@ -52,15 +52,15 @@ type metricExports struct { // Report is passed into LoggingFn to log the export metrics to stdout (outside this package). type Report struct { - Total int - Errors int - Zeros int - Descs int - Metrics *metrics.Metrics - Elapsed time.Duration - Fetch time.Duration - Start time.Time - ch chan []*metricExports + Total int // Total count of metrics recorded. + Errors int // Total count of errors recording metrics. + Zeros int // Total count of metrics equal to zero. + Descs int // Total count of unique metrics descriptions. + Metrics *metrics.Metrics // Metrics collected and recorded. + Elapsed time.Duration // Duration elapsed collecting and exporting. + Fetch time.Duration // Duration elapsed making controller requests. + Start time.Time // Time collection began. + ch chan []*metric wg sync.WaitGroup cf UnifiCollectorCnfg } @@ -104,10 +104,9 @@ func (u *unifiCollector) Describe(ch chan<- *prometheus.Desc) { // the current metrics (from another package) then exports them for prometheus. func (u *unifiCollector) Collect(ch chan<- prometheus.Metric) { var err error - r := &Report{cf: u.Config, ch: make(chan []*metricExports, buffer)} - defer r.finish() + r := &Report{cf: u.Config, ch: make(chan []*metric, buffer), Start: time.Now()} + defer r.close() - r.Start = time.Now() if r.Metrics, err = r.cf.CollectFn(); err != nil { r.error(ch, prometheus.NewInvalidDesc(fmt.Errorf("metric fetch failed")), err) return @@ -119,10 +118,9 @@ func (u *unifiCollector) Collect(ch chan<- prometheus.Metric) { // Pass Report interface into our collecting and reporting methods. go u.exportMetrics(r, ch) - // in loops.go. for _, f := range []func(report){u.loopClients, u.loopSites, u.loopUAPs, u.loopUSWs, u.loopUSGs, u.loopUDMs} { r.add() - go f(r) + go f(r) // in loops.go. } } diff --git a/promunifi/report.go b/promunifi/report.go index 92550984..016d3db8 100644 --- a/promunifi/report.go +++ b/promunifi/report.go @@ -6,7 +6,6 @@ import ( "github.com/davidnewhall/unifi-poller/metrics" "github.com/prometheus/client_golang/prometheus" - "golift.io/unifi" ) // This file contains the report interface. @@ -16,11 +15,11 @@ import ( type report interface { add() done() - send([]*metricExports) + send([]*metric) metrics() *metrics.Metrics - channel() chan []*metricExports + channel() chan []*metric report(descs map[*prometheus.Desc]bool) - export(m *metricExports, v float64) prometheus.Metric + export(m *metric, v float64) prometheus.Metric error(ch chan<- prometheus.Metric, d *prometheus.Desc, v interface{}) } @@ -35,19 +34,16 @@ func (r *Report) done() { r.wg.Add(-one) } -func (r *Report) send(m []*metricExports) { +func (r *Report) send(m []*metric) { r.wg.Add(one) r.ch <- m } func (r *Report) metrics() *metrics.Metrics { - if r.Metrics == nil { - return &metrics.Metrics{Devices: &unifi.Devices{}} - } return r.Metrics } -func (r *Report) channel() chan []*metricExports { +func (r *Report) channel() chan []*metric { return r.ch } @@ -59,7 +55,7 @@ func (r *Report) report(descs map[*prometheus.Desc]bool) { r.cf.LoggingFn(r) } -func (r *Report) export(m *metricExports, v float64) prometheus.Metric { +func (r *Report) export(m *metric, v float64) prometheus.Metric { r.Total++ if v == 0 { r.Zeros++ @@ -74,8 +70,8 @@ func (r *Report) error(ch chan<- prometheus.Metric, d *prometheus.Desc, v interf } } -// finish is not part of the interface. -func (r *Report) finish() { +// close is not part of the interface. +func (r *Report) close() { r.wg.Wait() r.Elapsed = time.Since(r.Start) close(r.ch) diff --git a/promunifi/site.go b/promunifi/site.go index 40cf5237..73248553 100644 --- a/promunifi/site.go +++ b/promunifi/site.go @@ -69,7 +69,7 @@ func (u *unifiCollector) exportSite(r report, s *unifi.Site) { labels := []string{h.Subsystem, h.Status, s.SiteName} switch h.Subsystem { case "www": - r.send([]*metricExports{ + r.send([]*metric{ {u.Site.TxBytesR, prometheus.GaugeValue, h.TxBytesR, labels}, {u.Site.RxBytesR, prometheus.GaugeValue, h.RxBytesR, labels}, {u.Site.Uptime, prometheus.GaugeValue, h.Latency, labels}, @@ -81,7 +81,7 @@ func (u *unifiCollector) exportSite(r report, s *unifi.Site) { }) case "wlan": - r.send([]*metricExports{ + r.send([]*metric{ {u.Site.TxBytesR, prometheus.GaugeValue, h.TxBytesR, labels}, {u.Site.RxBytesR, prometheus.GaugeValue, h.RxBytesR, labels}, {u.Site.NumAdopted, prometheus.GaugeValue, h.NumAdopted, labels}, @@ -95,7 +95,7 @@ func (u *unifiCollector) exportSite(r report, s *unifi.Site) { }) case "wan": - r.send([]*metricExports{ + r.send([]*metric{ {u.Site.TxBytesR, prometheus.GaugeValue, h.TxBytesR, labels}, {u.Site.RxBytesR, prometheus.GaugeValue, h.RxBytesR, labels}, {u.Site.NumAdopted, prometheus.GaugeValue, h.NumAdopted, labels}, @@ -106,7 +106,7 @@ func (u *unifiCollector) exportSite(r report, s *unifi.Site) { }) case "lan": - r.send([]*metricExports{ + r.send([]*metric{ {u.Site.TxBytesR, prometheus.GaugeValue, h.TxBytesR, labels}, {u.Site.RxBytesR, prometheus.GaugeValue, h.RxBytesR, labels}, {u.Site.NumAdopted, prometheus.GaugeValue, h.NumAdopted, labels}, @@ -119,7 +119,7 @@ func (u *unifiCollector) exportSite(r report, s *unifi.Site) { }) case "vpn": - r.send([]*metricExports{ + r.send([]*metric{ {u.Site.RemoteUserNumActive, prometheus.GaugeValue, h.RemoteUserNumActive, labels}, {u.Site.RemoteUserNumInactive, prometheus.GaugeValue, h.RemoteUserNumInactive, labels}, {u.Site.RemoteUserRxBytes, prometheus.CounterValue, h.RemoteUserRxBytes, labels}, diff --git a/promunifi/uap.go b/promunifi/uap.go index 0b14a840..8a1f0eac 100644 --- a/promunifi/uap.go +++ b/promunifi/uap.go @@ -168,7 +168,7 @@ func descUAP(ns string) *uap { func (u *unifiCollector) exportUAP(r report, d *unifi.UAP) { labels := []string{d.IP, d.Version, d.Model, d.Serial, d.Type, d.Mac, d.SiteName, d.Name} // Wireless System Data. - r.send([]*metricExports{ + r.send([]*metric{ {u.Device.Uptime, prometheus.GaugeValue, d.Uptime, labels}, {u.Device.TotalTxBytes, prometheus.CounterValue, d.TxBytes, labels}, {u.Device.TotalRxBytes, prometheus.CounterValue, d.RxBytes, labels}, @@ -199,7 +199,7 @@ func (u *unifiCollector) exportUAPstats(r report, labels []string, ap *unifi.Ap) // labelA := append([]string{"all"}, labels[2:]...) labelU := append([]string{"user"}, labels[6:]...) labelG := append([]string{"guest"}, labels[6:]...) - r.send([]*metricExports{ + r.send([]*metric{ /* // all {u.UAP.ApWifiTxDropped, prometheus.CounterValue, ap.WifiTxDropped, labelA}, {u.UAP.ApRxErrors, prometheus.CounterValue, ap.RxErrors, labelA}, @@ -257,7 +257,7 @@ func (u *unifiCollector) exportVAPtable(r report, labels []string, vt unifi.VapT } labelV := append([]string{v.Name, v.Bssid, v.Radio, v.RadioName, v.Essid, v.Usage}, labels[6:]...) - r.send([]*metricExports{ + r.send([]*metric{ {u.UAP.VAPCcq, prometheus.GaugeValue, v.Ccq / 10, labelV}, {u.UAP.VAPMacFilterRejections, prometheus.CounterValue, v.MacFilterRejections, labelV}, {u.UAP.VAPNumSatisfactionSta, prometheus.GaugeValue, v.NumSatisfactionSta, labelV}, @@ -303,7 +303,7 @@ func (u *unifiCollector) exportRadtable(r report, labels []string, rt unifi.Radi // radio table for _, p := range rt { labelR := append([]string{p.Name, p.Radio}, labels[6:]...) - r.send([]*metricExports{ + r.send([]*metric{ {u.UAP.RadioCurrentAntennaGain, prometheus.GaugeValue, p.CurrentAntennaGain, labelR}, {u.UAP.RadioHt, prometheus.GaugeValue, p.Ht, labelR}, {u.UAP.RadioMaxTxpower, prometheus.GaugeValue, p.MaxTxpower, labelR}, @@ -317,7 +317,7 @@ func (u *unifiCollector) exportRadtable(r report, labels []string, rt unifi.Radi if t.Name != p.Name { continue } - r.send([]*metricExports{ + r.send([]*metric{ {u.UAP.RadioTxPower, prometheus.GaugeValue, t.TxPower, labelR}, {u.UAP.RadioAstBeXmit, prometheus.GaugeValue, t.AstBeXmit, labelR}, {u.UAP.RadioChannel, prometheus.GaugeValue, t.Channel, labelR}, diff --git a/promunifi/udm.go b/promunifi/udm.go index 9779cfad..09206540 100644 --- a/promunifi/udm.go +++ b/promunifi/udm.go @@ -71,7 +71,7 @@ func descDevice(ns string) *unifiDevice { func (u *unifiCollector) exportUDM(r report, d *unifi.UDM) { labels := []string{d.IP, d.Version, d.Model, d.Serial, d.Type, d.Mac, d.SiteName, d.Name} // Dream Machine System Data. - r.send([]*metricExports{ + r.send([]*metric{ {u.Device.Uptime, prometheus.GaugeValue, d.Uptime, labels}, {u.Device.TotalTxBytes, prometheus.CounterValue, d.TxBytes, labels}, {u.Device.TotalRxBytes, prometheus.CounterValue, d.RxBytes, labels}, diff --git a/promunifi/usg.go b/promunifi/usg.go index 5cfef6c1..8392d4a3 100644 --- a/promunifi/usg.go +++ b/promunifi/usg.go @@ -69,7 +69,7 @@ func descUSG(ns string) *usg { func (u *unifiCollector) exportUSG(r report, d *unifi.USG) { labels := []string{d.IP, d.Version, d.Model, d.Serial, d.Type, d.Mac, d.SiteName, d.Name} // Gateway System Data. - r.send([]*metricExports{ + r.send([]*metric{ {u.Device.Uptime, prometheus.GaugeValue, d.Uptime, labels}, {u.Device.TotalTxBytes, prometheus.CounterValue, d.TxBytes, labels}, {u.Device.TotalRxBytes, prometheus.CounterValue, d.RxBytes, labels}, @@ -96,7 +96,7 @@ func (u *unifiCollector) exportUSG(r report, d *unifi.USG) { func (u *unifiCollector) exportUSGstats(r report, labels []string, gw *unifi.Gw, st unifi.SpeedtestStatus) { labelLan := []string{"lan", labels[6], labels[7]} labelWan := []string{"all", labels[6], labels[7]} - r.send([]*metricExports{ + r.send([]*metric{ /* // Combined Port Stats - not really needed. sum() the others instead. {u.USG.WanRxPackets, prometheus.CounterValue, gw.WanRxPackets, labelWan}, {u.USG.WanRxBytes, prometheus.CounterValue, gw.WanRxBytes, labelWan}, @@ -124,7 +124,7 @@ func (u *unifiCollector) exportWANPorts(r report, labels []string, wans ...unifi continue // only record UP interfaces. } labelWan := []string{wan.Name, labels[6], labels[7]} - r.send([]*metricExports{ + r.send([]*metric{ {u.USG.WanRxPackets, prometheus.CounterValue, wan.RxPackets, labelWan}, {u.USG.WanRxBytes, prometheus.CounterValue, wan.RxBytes, labelWan}, {u.USG.WanRxDropped, prometheus.CounterValue, wan.RxDropped, labelWan}, diff --git a/promunifi/usw.go b/promunifi/usw.go index fccb65bf..2c25271e 100644 --- a/promunifi/usw.go +++ b/promunifi/usw.go @@ -93,14 +93,14 @@ func descUSW(ns string) *usw { func (u *unifiCollector) exportUSW(r report, d *unifi.USW) { labels := []string{d.IP, d.Version, d.Model, d.Serial, d.Type, d.Mac, d.SiteName, d.Name} if d.HasTemperature.Val { - r.send([]*metricExports{{u.Device.Temperature, prometheus.GaugeValue, d.GeneralTemperature, labels}}) + r.send([]*metric{{u.Device.Temperature, prometheus.GaugeValue, d.GeneralTemperature, labels}}) } if d.HasFan.Val { - r.send([]*metricExports{{u.Device.FanLevel, prometheus.GaugeValue, d.FanLevel, labels}}) + r.send([]*metric{{u.Device.FanLevel, prometheus.GaugeValue, d.FanLevel, labels}}) } // Switch System Data. - r.send([]*metricExports{ + r.send([]*metric{ {u.Device.Uptime, prometheus.GaugeValue, d.Uptime, labels}, {u.Device.TotalMaxPower, prometheus.GaugeValue, d.TotalMaxPower, labels}, {u.Device.TotalTxBytes, prometheus.CounterValue, d.TxBytes, labels}, @@ -124,7 +124,7 @@ func (u *unifiCollector) exportUSW(r report, d *unifi.USW) { func (u *unifiCollector) exportUSWstats(r report, labels []string, sw *unifi.Sw) { labelS := labels[6:] - r.send([]*metricExports{ + r.send([]*metric{ {u.USW.SwRxPackets, prometheus.CounterValue, sw.RxPackets, labelS}, {u.USW.SwRxBytes, prometheus.CounterValue, sw.RxBytes, labelS}, {u.USW.SwRxErrors, prometheus.CounterValue, sw.RxErrors, labelS}, @@ -153,14 +153,14 @@ func (u *unifiCollector) exportPortTable(r report, labels []string, pt []unifi.P // Copy labels, and add four new ones. labelP := []string{p.PortIdx.Txt, p.Name, p.Mac, p.IP, labels[6], labels[7]} if p.PoeEnable.Val && p.PortPoe.Val { - r.send([]*metricExports{ + r.send([]*metric{ {u.USW.PoeCurrent, prometheus.GaugeValue, p.PoeCurrent, labelP}, {u.USW.PoePower, prometheus.GaugeValue, p.PoePower, labelP}, {u.USW.PoeVoltage, prometheus.GaugeValue, p.PoeVoltage, labelP}, }) } - r.send([]*metricExports{ + r.send([]*metric{ {u.USW.RxBroadcast, prometheus.CounterValue, p.RxBroadcast, labelP}, {u.USW.RxBytes, prometheus.CounterValue, p.RxBytes, labelP}, {u.USW.RxBytesR, prometheus.GaugeValue, p.RxBytesR, labelP},