some cleanup
This commit is contained in:
parent
78d2ca3ba2
commit
88547d3ef8
|
|
@ -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 {
|
||||
|
|
|
|||
|
|
@ -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()
|
||||
|
|
|
|||
|
|
@ -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,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 {
|
||||
count += u.export(ch, u.exportUAP(asset), m.TS)
|
||||
}
|
||||
|
|
@ -127,11 +135,6 @@ func (u *unifiCollector) Collect(ch chan<- prometheus.Metric) {
|
|||
for _, asset := range m.Devices.UDMs {
|
||||
count += u.export(ch, u.exportUDM(asset), m.TS)
|
||||
}
|
||||
}
|
||||
|
||||
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) {
|
||||
|
|
|
|||
|
|
@ -4,6 +4,8 @@ import (
|
|||
"golift.io/unifi"
|
||||
)
|
||||
|
||||
/* The IDS data goes into prometheus cleanly. This probably wont happen. */
|
||||
|
||||
type ids struct {
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -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})
|
||||
|
|
|
|||
Loading…
Reference in New Issue