fix a bug and send a few more pieces into the mix

This commit is contained in:
davidnewhall2 2019-11-17 01:12:33 -08:00
parent e762929221
commit d63a91655e
9 changed files with 75 additions and 42 deletions

View File

@ -99,19 +99,14 @@ func (u *UnifiPoller) Run() (err error) {
case "prometheus", "exporter":
u.Logf("Exporting Measurements at https://%s/metrics for Prometheus", u.Config.HTTPListen)
u.Config.Mode = "http exporter"
http.Handle("/metrics", http.HandlerFunc(u.PromHandler))
http.Handle("/metrics", promhttp.Handler())
prometheus.MustRegister(promunifi.NewUnifiCollector(promunifi.UnifiCollectorOpts{
Namespace: "unifi",
CollectFn: u.ExportMetrics,
ReportErrors: true,
Namespace: "unifi",
CollectIDS: true,
}))
err = http.ListenAndServe(u.Config.HTTPListen, nil)
if err != http.ErrServerClosed {
return err
}
return nil
return http.ListenAndServe(u.Config.HTTPListen, nil)
default:
if err = u.GetInfluxDB(); err != nil {
@ -139,12 +134,6 @@ func (u *UnifiPoller) GetInfluxDB() (err error) {
return nil
}
// PromHandler logs /metrics requests and serves them with the prometheus handler.
func (u *UnifiPoller) PromHandler(w http.ResponseWriter, r *http.Request) {
u.LogDebugf("/metrics endpoint polled by %v", r.RemoteAddr)
promhttp.Handler().ServeHTTP(w, r)
}
// GetUnifi returns a UniFi controller interface.
func (u *UnifiPoller) GetUnifi() (err error) {
// Create an authenticated session to the Unifi Controller.

View File

@ -100,18 +100,14 @@ func (u *UnifiPoller) ExportMetrics() *metrics.Metrics {
return nil
}
}
metrics, err := u.CollectMetrics()
m, err := u.CollectMetrics()
if err != nil {
u.LogErrorf("collecting metrics: %v", err)
return nil
}
u.AugmentMetrics(metrics)
u.LogExportReport(metrics)
return metrics
}
u.AugmentMetrics(m)
// LogExportReport writes a log line after exporting metrics via HTTP.
func (u *UnifiPoller) LogExportReport(m *metrics.Metrics) {
idsMsg := ""
if u.Config.CollectIDS {
idsMsg = fmt.Sprintf(", IDS Events: %d, ", len(m.IDSList))
@ -120,6 +116,8 @@ func (u *UnifiPoller) LogExportReport(m *metrics.Metrics) {
"Wireless APs: %d, Gateways: %d, Switches: %d%s",
len(m.Sites), len(m.Clients), len(m.UAPs),
len(m.UDMs)+len(m.USGs), len(m.USWs), idsMsg)
return m
}
// CollectMetrics grabs all the measurements from a UniFi controller and returns them.

View File

@ -29,12 +29,12 @@ type UnifiCollectorOpts struct {
type unifiCollector struct {
opts UnifiCollectorOpts
Client *client
// UAP *UAP
// USG *USG
// USW *USW
// UDM *UDM
// IDS *IDS
// Site *Site
UAP *uap
USG *usg
USW *usw
UDM *udm
IDS *ids
Site *site
}
type metricExports struct {
@ -53,12 +53,12 @@ func NewUnifiCollector(opts UnifiCollectorOpts) prometheus.Collector {
return &unifiCollector{
opts: opts,
Client: descClient(opts.Namespace),
// UAP: descUAP(opts.Namespace),
// USG: descUSG(opts.Namespace),
// USW: descUSW(opts.Namespace),
// UDM: descUDM(opts.Namespace),
// Site: descSite(opts.Namespace),
// IDS: descIDS(opts.Namespace),
UAP: descUAP(opts.Namespace),
USG: descUSG(opts.Namespace),
USW: descUSW(opts.Namespace),
UDM: descUDM(opts.Namespace),
Site: descSite(opts.Namespace),
IDS: descIDS(opts.Namespace),
}
}
@ -66,7 +66,8 @@ func NewUnifiCollector(opts UnifiCollectorOpts) prometheus.Collector {
// metric descriptions that this packages produces.
func (u *unifiCollector) Describe(ch chan<- *prometheus.Desc) {
describe := func(from interface{}) {
v := reflect.ValueOf(from)
v := reflect.Indirect(reflect.ValueOf(from))
// Loop each struct member and send it to the provided channel.
for i := 0; i < v.NumField(); i++ {
desc, ok := v.Field(i).Interface().(*prometheus.Desc)
@ -76,20 +77,23 @@ func (u *unifiCollector) Describe(ch chan<- *prometheus.Desc) {
}
}
describe(u.Client)
// describe(u.UAP)
// describe(u.USG)
// describe(u.USW)
// describe(u.UDM)
// describe(u.Site)
// if u.opts.CollectIDS {
// describe(u.IDS)
// }
describe(u.UAP)
describe(u.USG)
describe(u.USW)
describe(u.UDM)
describe(u.Site)
if u.opts.CollectIDS {
describe(u.IDS)
}
}
// Collect satisifes the prometheus Collector. This runs the input method to get
// the current metrics (from another package) then exports them for prometheus.
func (u *unifiCollector) Collect(ch chan<- prometheus.Metric) {
m := u.opts.CollectFn()
if m == nil {
return
}
for _, asset := range m.Clients {
u.export(ch, u.exportClient(asset), m.TS)

View File

@ -4,6 +4,13 @@ import (
"golift.io/unifi"
)
type ids struct {
}
func descIDS(ns string) *ids {
return &ids{}
}
// exportIDS exports Intrusion Detection System Data
func (u *unifiCollector) exportIDS(i *unifi.IDS) []*metricExports {
return nil

View File

@ -4,6 +4,13 @@ import (
"golift.io/unifi"
)
type site struct {
}
func descSite(ns string) *site {
return &site{}
}
// exportSite exports Network Site Data
func (u *unifiCollector) exportSite(s *unifi.Site) []*metricExports {
return nil

View File

@ -4,6 +4,13 @@ import (
"golift.io/unifi"
)
type uap struct {
}
func descUAP(ns string) *uap {
return &uap{}
}
// exportUAP exports Access Point Data
func (u *unifiCollector) exportUAP(a *unifi.UAP) []*metricExports {
return nil

View File

@ -4,6 +4,13 @@ import (
"golift.io/unifi"
)
type udm struct {
}
func descUDM(ns string) *udm {
return &udm{}
}
// exportUDM exports UniFi Dream Machine (and Pro) Data
func (u *unifiCollector) exportUDM(d *unifi.UDM) []*metricExports {
return nil

View File

@ -4,6 +4,13 @@ import (
"golift.io/unifi"
)
type usg struct {
}
func descUSG(ns string) *usg {
return &usg{}
}
// exportUSG Exports Security Gateway Data
func (u *unifiCollector) exportUSG(s *unifi.USG) []*metricExports {
return nil

View File

@ -4,6 +4,13 @@ import (
"golift.io/unifi"
)
type usw struct {
}
func descUSW(ns string) *usw {
return &usw{}
}
// exportUSW exports Network Switch Data
func (u *unifiCollector) exportUSW(s *unifi.USW) []*metricExports {
return nil