some cleanup

This commit is contained in:
davidnewhall2 2019-11-17 14:27:23 -08:00
parent 78d2ca3ba2
commit 88547d3ef8
5 changed files with 27 additions and 23 deletions

View File

@ -8,7 +8,7 @@ import (
) )
// ExportMetrics updates the internal metrics provided via // 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) { func (u *UnifiPoller) ExportMetrics() (*metrics.Metrics, error) {
if u.Config.ReAuth { if u.Config.ReAuth {
u.LogDebugf("Re-authenticating to UniFi Controller") u.LogDebugf("Re-authenticating to UniFi Controller")
@ -28,7 +28,7 @@ func (u *UnifiPoller) ExportMetrics() (*metrics.Metrics, error) {
return m, nil 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) { func (u *UnifiPoller) LogExportReport(m *metrics.Metrics, count int64) {
idsMsg := "" idsMsg := ""
if u.Config.CollectIDS { if u.Config.CollectIDS {

View File

@ -103,7 +103,7 @@ func (u *UnifiPoller) Run() (err error) {
prometheus.MustRegister(promunifi.NewUnifiCollector(promunifi.UnifiCollectorCnfg{ prometheus.MustRegister(promunifi.NewUnifiCollector(promunifi.UnifiCollectorCnfg{
Namespace: defaultNamespace, // XXX: pass this in from config. Namespace: defaultNamespace, // XXX: pass this in from config.
CollectFn: u.ExportMetrics, CollectFn: u.ExportMetrics,
LoggerFn: u.LogExportReport, LoggingFn: u.LogExportReport,
CollectIDS: u.Config.CollectIDS, CollectIDS: u.Config.CollectIDS,
ReportErrors: true, // XXX: Does this need to be configurable? 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 { if err = u.GetInfluxDB(); err != nil {
return err return err
} }
u.Logf("Logging Measurements to InfluxDB at %s as user %s", u.Config.InfluxURL, u.Config.InfluxUser) u.Logf("Logging Measurements to InfluxDB at %s as user %s", u.Config.InfluxURL, u.Config.InfluxUser)
u.Config.Mode = "influx poller" u.Config.Mode = "influx poller"
return u.PollController() return u.PollController()

View File

@ -24,7 +24,7 @@ type UnifiCollectorCnfg struct {
// function to retreive the latest UniFi // function to retreive the latest UniFi
CollectFn func() (*metrics.Metrics, error) CollectFn func() (*metrics.Metrics, error)
// provide a logger function if you want to run a routine *after* prometheus checks in. // 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. // Setting this to true will enable IDS exports.
CollectIDS bool CollectIDS bool
} }
@ -98,10 +98,15 @@ func (u *unifiCollector) Collect(ch chan<- prometheus.Metric) {
var count int64 var count int64
m, err := u.Config.CollectFn() m, err := u.Config.CollectFn()
if err != nil { 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 return
} }
if u.Config.LoggingFn != nil {
defer func() { u.Config.LoggingFn(m, count) }()
}
for _, asset := range m.Clients { for _, asset := range m.Clients {
count += u.export(ch, u.exportClient(asset), m.TS) count += u.export(ch, u.exportClient(asset), m.TS)
} }
@ -114,7 +119,10 @@ func (u *unifiCollector) Collect(ch chan<- prometheus.Metric) {
} }
} }
if m.Devices != nil { if m.Devices == nil {
return
}
for _, asset := range m.Devices.UAPs { for _, asset := range m.Devices.UAPs {
count += u.export(ch, u.exportUAP(asset), m.TS) count += u.export(ch, u.exportUAP(asset), m.TS)
} }
@ -129,11 +137,6 @@ func (u *unifiCollector) Collect(ch chan<- prometheus.Metric) {
} }
} }
if u.Config.LoggerFn != nil {
u.Config.LoggerFn(m, count)
}
}
func (u *unifiCollector) export(ch chan<- prometheus.Metric, exports []*metricExports, ts time.Time) (count int64) { func (u *unifiCollector) export(ch chan<- prometheus.Metric, exports []*metricExports, ts time.Time) (count int64) {
for _, e := range exports { for _, e := range exports {
var val float64 var val float64

View File

@ -4,6 +4,8 @@ import (
"golift.io/unifi" "golift.io/unifi"
) )
/* The IDS data goes into prometheus cleanly. This probably wont happen. */
type ids struct { type ids struct {
} }

View File

@ -47,7 +47,7 @@ func descUSW(ns string) *usw {
labels := []string{"site_name", "mac", "model", "name", "serial", "site_id", labels := []string{"site_name", "mac", "model", "name", "serial", "site_id",
"type", "version", "device_id", "oid"} "type", "version", "device_id", "oid"}
// Copy labels, and replace last four with different names. // 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") "port_num", "port_name", "port_mac", "port_ip")
return &usw{ return &usw{
@ -107,7 +107,7 @@ func (u *unifiCollector) exportUSW(s *unifi.USW) []*metricExports {
// Per-port data on the switch // Per-port data on the switch
for _, p := range s.PortTable { for _, p := range s.PortTable {
// Copy labels, and replace last four with different data. // 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.PoeCurrent, prometheus.GaugeValue, p.PoeCurrent, l})
m = append(m, &metricExports{u.USW.PoePower, prometheus.GaugeValue, p.PoePower, 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}) m = append(m, &metricExports{u.USW.PoeVoltage, prometheus.GaugeValue, p.PoeVoltage, l})