diff --git a/integrations/inputunifi/poller/prometheus.go b/integrations/inputunifi/poller/prometheus.go index f0dea3ea..4fa134de 100644 --- a/integrations/inputunifi/poller/prometheus.go +++ b/integrations/inputunifi/poller/prometheus.go @@ -8,7 +8,7 @@ import ( ) // ExportMetrics updates the internal metrics provided via -// HTTP at /metrics for prometheus collection. This is run by Prometheus. +// HTTP at /metrics for prometheus collection. This is run by Prometheus CollectFn. func (u *UnifiPoller) ExportMetrics() (*metrics.Metrics, error) { if u.Config.ReAuth { u.LogDebugf("Re-authenticating to UniFi Controller") @@ -28,7 +28,7 @@ func (u *UnifiPoller) ExportMetrics() (*metrics.Metrics, error) { return m, nil } -// LogExportReport is called after prometheus exports metrics. This is run by Prometheus. +// LogExportReport is called after prometheus exports metrics. This is run by Prometheus as LoggingFn func (u *UnifiPoller) LogExportReport(m *metrics.Metrics, count int64) { idsMsg := "" if u.Config.CollectIDS { diff --git a/integrations/inputunifi/poller/start.go b/integrations/inputunifi/poller/start.go index 7c0f89a9..8acfe85b 100644 --- a/integrations/inputunifi/poller/start.go +++ b/integrations/inputunifi/poller/start.go @@ -103,7 +103,7 @@ func (u *UnifiPoller) Run() (err error) { prometheus.MustRegister(promunifi.NewUnifiCollector(promunifi.UnifiCollectorCnfg{ Namespace: defaultNamespace, // XXX: pass this in from config. CollectFn: u.ExportMetrics, - LoggerFn: u.LogExportReport, + LoggingFn: u.LogExportReport, CollectIDS: u.Config.CollectIDS, ReportErrors: true, // XXX: Does this need to be configurable? })) @@ -113,7 +113,6 @@ func (u *UnifiPoller) Run() (err error) { if err = u.GetInfluxDB(); err != nil { return err } - u.Logf("Logging Measurements to InfluxDB at %s as user %s", u.Config.InfluxURL, u.Config.InfluxUser) u.Config.Mode = "influx poller" return u.PollController() diff --git a/integrations/inputunifi/promunifi/collector.go b/integrations/inputunifi/promunifi/collector.go index 791e65c7..5df018e1 100644 --- a/integrations/inputunifi/promunifi/collector.go +++ b/integrations/inputunifi/promunifi/collector.go @@ -24,7 +24,7 @@ type UnifiCollectorCnfg struct { // function to retreive the latest UniFi CollectFn func() (*metrics.Metrics, error) // provide a logger function if you want to run a routine *after* prometheus checks in. - LoggerFn func(*metrics.Metrics, int64) + LoggingFn func(*metrics.Metrics, int64) // Setting this to true will enable IDS exports. CollectIDS bool } @@ -98,10 +98,15 @@ func (u *unifiCollector) Collect(ch chan<- prometheus.Metric) { var count int64 m, err := u.Config.CollectFn() if err != nil { - ch <- prometheus.NewInvalidMetric(prometheus.NewInvalidDesc(fmt.Errorf("metric fetch failed")), err) + ch <- prometheus.NewInvalidMetric( + prometheus.NewInvalidDesc(fmt.Errorf("metric fetch failed")), err) return } + if u.Config.LoggingFn != nil { + defer func() { u.Config.LoggingFn(m, count) }() + } + for _, asset := range m.Clients { count += u.export(ch, u.exportClient(asset), m.TS) } @@ -114,23 +119,21 @@ func (u *unifiCollector) Collect(ch chan<- prometheus.Metric) { } } - if m.Devices != nil { - for _, asset := range m.Devices.UAPs { - count += u.export(ch, u.exportUAP(asset), m.TS) - } - for _, asset := range m.Devices.USGs { - count += u.export(ch, u.exportUSG(asset), m.TS) - } - for _, asset := range m.Devices.USWs { - count += u.export(ch, u.exportUSW(asset), m.TS) - } - for _, asset := range m.Devices.UDMs { - count += u.export(ch, u.exportUDM(asset), m.TS) - } + if m.Devices == nil { + return } - if u.Config.LoggerFn != nil { - u.Config.LoggerFn(m, count) + for _, asset := range m.Devices.UAPs { + count += u.export(ch, u.exportUAP(asset), m.TS) + } + for _, asset := range m.Devices.USGs { + count += u.export(ch, u.exportUSG(asset), m.TS) + } + for _, asset := range m.Devices.USWs { + count += u.export(ch, u.exportUSW(asset), m.TS) + } + for _, asset := range m.Devices.UDMs { + count += u.export(ch, u.exportUDM(asset), m.TS) } } diff --git a/integrations/inputunifi/promunifi/ids.go b/integrations/inputunifi/promunifi/ids.go index badd23d8..35a27428 100644 --- a/integrations/inputunifi/promunifi/ids.go +++ b/integrations/inputunifi/promunifi/ids.go @@ -4,6 +4,8 @@ import ( "golift.io/unifi" ) +/* The IDS data goes into prometheus cleanly. This probably wont happen. */ + type ids struct { } diff --git a/integrations/inputunifi/promunifi/usw.go b/integrations/inputunifi/promunifi/usw.go index c91076e4..df8ac999 100644 --- a/integrations/inputunifi/promunifi/usw.go +++ b/integrations/inputunifi/promunifi/usw.go @@ -47,7 +47,7 @@ func descUSW(ns string) *usw { labels := []string{"site_name", "mac", "model", "name", "serial", "site_id", "type", "version", "device_id", "oid"} // Copy labels, and replace last four with different names. - labelP := append(append([]string(nil), labels[:6]...), + labelP := append(append([]string{}, labels[:6]...), "port_num", "port_name", "port_mac", "port_ip") return &usw{ @@ -107,7 +107,7 @@ func (u *unifiCollector) exportUSW(s *unifi.USW) []*metricExports { // Per-port data on the switch for _, p := range s.PortTable { // Copy labels, and replace last four with different data. - l := append(append([]string(nil), labels[:6]...), p.PortIdx.Txt, p.Name, p.Mac, p.IP) + l := append(append([]string{}, labels[:6]...), p.PortIdx.Txt, p.Name, p.Mac, p.IP) m = append(m, &metricExports{u.USW.PoeCurrent, prometheus.GaugeValue, p.PoeCurrent, l}) m = append(m, &metricExports{u.USW.PoePower, prometheus.GaugeValue, p.PoePower, l}) m = append(m, &metricExports{u.USW.PoeVoltage, prometheus.GaugeValue, p.PoeVoltage, l})