add prom config and logs to webserver

This commit is contained in:
davidnewhall2 2020-06-28 05:48:11 -07:00
parent 2e135412f8
commit f9eb30763b
7 changed files with 66 additions and 17 deletions

View File

@ -75,7 +75,7 @@ func descClient(ns string) *uclient {
func (u *promUnifi) exportClientDPI(r report, v interface{}, appTotal, catTotal totalsDPImap) {
s, ok := v.(*unifi.DPITable)
if !ok {
u.Collector.LogErrorf("invalid type given to ClientsDPI: %T", v)
u.LogErrorf("invalid type given to ClientsDPI: %T", v)
return
}

View File

@ -14,8 +14,12 @@ import (
"github.com/prometheus/common/version"
"github.com/unifi-poller/poller"
"github.com/unifi-poller/unifi"
"github.com/unifi-poller/webserver"
)
// PluginName is the name of this plugin.
const PluginName = "prometheus"
const (
// channel buffer, fits at least one batch.
defaultBuffer = 50
@ -96,7 +100,7 @@ func init() { // nolint: gochecknoinits
u := &promUnifi{Config: &Config{}}
poller.NewOutput(&poller.Output{
Name: "prometheus",
Name: PluginName,
Config: u,
Method: u.Run,
})
@ -105,8 +109,8 @@ func init() { // nolint: gochecknoinits
// Run creates the collectors and starts the web server up.
// Should be run in a Go routine. Returns nil if not configured.
func (u *promUnifi) Run(c poller.Collect) error {
if u.Config == nil || u.Disable {
c.Logf("Prometheus config missing (or disabled), Prometheus HTTP listener disabled!")
if u.Collector = c; u.Config == nil || u.Disable {
u.Logf("Prometheus config missing (or disabled), Prometheus HTTP listener disabled!")
return nil
}
@ -123,8 +127,6 @@ func (u *promUnifi) Run(c poller.Collect) error {
u.Buffer = defaultBuffer
}
// Later can pass this in from poller by adding a method to the interface.
u.Collector = c
u.Client = descClient(u.Namespace + "_client_")
u.Device = descDevice(u.Namespace + "_device_") // stats for all device types.
u.UAP = descUAP(u.Namespace + "_device_")
@ -133,9 +135,10 @@ func (u *promUnifi) Run(c poller.Collect) error {
u.Site = descSite(u.Namespace + "_site_")
mux := http.NewServeMux()
webserver.UpdateOutput(&webserver.Output{Name: PluginName, Config: u.Config})
prometheus.MustRegister(version.NewCollector(u.Namespace))
prometheus.MustRegister(u)
c.Logf("Prometheus exported at https://%s/ - namespace: %s", u.HTTPListen, u.Namespace)
u.Logf("Prometheus exported at https://%s/ - namespace: %s", u.HTTPListen, u.Namespace)
mux.Handle("/metrics", promhttp.HandlerFor(prometheus.DefaultGatherer,
promhttp.HandlerOpts{ErrorHandling: promhttp.ContinueOnError},
))
@ -157,7 +160,7 @@ func (u *promUnifi) ScrapeHandler(w http.ResponseWriter, r *http.Request) {
}
if pathOld := r.URL.Query().Get("path"); pathOld != "" {
u.Collector.LogErrorf("deprecated 'path' parameter used; update your config to use 'target'")
u.LogErrorf("deprecated 'path' parameter used; update your config to use 'target'")
if t.Path == "" {
t.Path = pathOld
@ -165,7 +168,7 @@ func (u *promUnifi) ScrapeHandler(w http.ResponseWriter, r *http.Request) {
}
if roleOld := r.URL.Query().Get("role"); roleOld != "" {
u.Collector.LogErrorf("deprecated 'role' parameter used; update your config to use 'target'")
u.LogErrorf("deprecated 'role' parameter used; update your config to use 'target'")
if t.Path == "" {
t.Path = roleOld
@ -173,7 +176,7 @@ func (u *promUnifi) ScrapeHandler(w http.ResponseWriter, r *http.Request) {
}
if t.Path == "" {
u.Collector.LogErrorf("'target' parameter missing on scrape from %v", r.RemoteAddr)
u.LogErrorf("'target' parameter missing on scrape from %v", r.RemoteAddr)
http.Error(w, "'target' parameter must be specified: configured OR unconfigured url", 400)
return
@ -239,7 +242,7 @@ func (u *promUnifi) collect(ch chan<- prometheus.Metric, filter *poller.Filter)
if err != nil {
r.error(ch, prometheus.NewInvalidDesc(err), errMetricFetchFailed)
u.Collector.LogErrorf("metric fetch failed: %v", err)
u.LogErrorf("metric fetch failed: %v", err)
return
}
@ -254,7 +257,7 @@ func (u *promUnifi) collect(ch chan<- prometheus.Metric, filter *poller.Filter)
// This is where our channels connects to the prometheus channel.
func (u *promUnifi) exportMetrics(r report, ch chan<- prometheus.Metric, ourChan chan []*metric) {
descs := make(map[*prometheus.Desc]bool) // used as a counter
defer r.report(u.Collector, descs)
defer r.report(u, descs)
for newMetrics := range ourChan {
for _, m := range newMetrics {
@ -326,6 +329,6 @@ func (u *promUnifi) switchExport(r report, v interface{}) {
case *unifi.Client:
u.exportClient(r, v)
default:
u.Collector.LogErrorf("invalid type: %T", v)
u.LogErrorf("invalid type: %T", v)
}
}

View File

@ -6,6 +6,7 @@ require (
github.com/prometheus/client_golang v1.6.0
github.com/prometheus/common v0.10.0
github.com/prometheus/procfs v0.1.2 // indirect
github.com/unifi-poller/poller v0.0.8-0.20200621214016-5d1ed3324a46
github.com/unifi-poller/poller v0.0.8-0.20200626082958-a9a7092a5684
github.com/unifi-poller/unifi v0.0.5-0.20200620103801-b927287ea1cd
github.com/unifi-poller/webserver v0.0.0-20200628115531-e071827d7598
)

View File

@ -41,6 +41,8 @@ github.com/google/go-cmp v0.3.0/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMyw
github.com/google/go-cmp v0.3.1/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMywk6iLU=
github.com/google/go-cmp v0.4.0/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg=
github.com/gorilla/mux v1.7.4 h1:VuZ8uybHlWmqV03+zRzdwKL4tUnIp1MAQtp1mIFE1bc=
github.com/gorilla/mux v1.7.4/go.mod h1:DVbg23sWSpFRCP0SfiEN6jmj59UnW/n46BH5rLB71So=
github.com/json-iterator/go v1.1.6/go.mod h1:+SdeFBvtyEkXs7REEP0seUULqWtbJapLOCVDaaPEHmU=
github.com/json-iterator/go v1.1.9/go.mod h1:KdQUCv79m/52Kvf8AW2vK1V8akMuk1QjK/uOdHXbAo4=
github.com/julienschmidt/httprouter v1.2.0/go.mod h1:SYymIcj16QtmaHHD7aYtjjsJG7VTCxuUUipMqKk8s4w=
@ -99,13 +101,18 @@ github.com/unifi-poller/poller v0.0.8-0.20200621110949-33f1a1454d10 h1:1rGP4ISFp
github.com/unifi-poller/poller v0.0.8-0.20200621110949-33f1a1454d10/go.mod h1:+Ppksi2wBCrByJke0B0lTutxFtKfv1zx6L1haALBrN4=
github.com/unifi-poller/poller v0.0.8-0.20200621214016-5d1ed3324a46 h1:OhbVj3VVgbpUMQFSwD0NszDsbEL7DdbTcJuU+p9DwIM=
github.com/unifi-poller/poller v0.0.8-0.20200621214016-5d1ed3324a46/go.mod h1:pJ/MeYaakLOOpbyc7s4zeZ92UzNK/rir5jkA7t5jIjo=
github.com/unifi-poller/poller v0.0.8-0.20200626082958-a9a7092a5684 h1:r1B8GoI47czgGnQ7WY89qlSKqSE1d1pQmcLfdXVW/+Y=
github.com/unifi-poller/poller v0.0.8-0.20200626082958-a9a7092a5684/go.mod h1:pJ/MeYaakLOOpbyc7s4zeZ92UzNK/rir5jkA7t5jIjo=
github.com/unifi-poller/unifi v0.0.4/go.mod h1:bTUtctrf56aapjKH+L+98eThBaVFbQXw5iNGZI0g/+E=
github.com/unifi-poller/unifi v0.0.5-0.20200619092006-d24c776a42f5/go.mod h1:L1kMRH2buZhB31vZnRC1im7Tk/4uD3ET4biwl2faYy8=
github.com/unifi-poller/unifi v0.0.5-0.20200620103801-b927287ea1cd h1:jrwuiY1AdoPi+b+R8zjt/e8h8ZqULNB9izcyQnf3pSw=
github.com/unifi-poller/unifi v0.0.5-0.20200620103801-b927287ea1cd/go.mod h1:L1kMRH2buZhB31vZnRC1im7Tk/4uD3ET4biwl2faYy8=
github.com/unifi-poller/unifi v0.0.5 h1:Izeun32YxcQOeKZUXY0Sy4ltKYFuYxWGcN9JS6xkIJU=
github.com/unifi-poller/unifi v0.0.5/go.mod h1:L1kMRH2buZhB31vZnRC1im7Tk/4uD3ET4biwl2faYy8=
github.com/unifi-poller/webserver v0.0.0-20200628115531-e071827d7598 h1:Nmo1arOOln7V7YhjcwrT2AEFN0dl43tl5NgoNF4SgiA=
github.com/unifi-poller/webserver v0.0.0-20200628115531-e071827d7598/go.mod h1:rlM8tRx7wCxqj4+6ZkuFVt2voFoAlHhS/XTrHd7T57s=
golang.org/x/crypto v0.0.0-20180904163835-0709b304e793/go.mod h1:6SG95UA2DQfeDnfUPMdvaQW0Q7yPrPDi9nlGo2tz2b4=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2 h1:VklqNMn3ovrHsnt90PveolxSbWFaJdECFbxSq0Mqo2M=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/exp v0.0.0-20190121172915-509febef88a4/go.mod h1:CJ0aWSM057203Lf6IL+f9T1iT9GByDxfZKAQTCR3kQA=
golang.org/x/lint v0.0.0-20181026193005-c67002cb31c3/go.mod h1:UVdnD1Gm6xHRNCYTkRU2/jEulfH38KcIWyp/GAMgvoE=

View File

@ -0,0 +1,38 @@
package promunifi
import (
"fmt"
"time"
"github.com/unifi-poller/webserver"
)
// Logf logs a message.
func (u *promUnifi) Logf(msg string, v ...interface{}) {
webserver.NewInputEvent(PluginName, PluginName, &webserver.Event{
Ts: time.Now(),
Msg: fmt.Sprintf(msg, v...),
Tags: map[string]string{"type": "info"},
})
u.Collector.Logf(msg, v...)
}
// LogErrorf logs an error message.
func (u *promUnifi) LogErrorf(msg string, v ...interface{}) {
webserver.NewInputEvent(PluginName, PluginName, &webserver.Event{
Ts: time.Now(),
Msg: fmt.Sprintf(msg, v...),
Tags: map[string]string{"type": "error"},
})
u.Collector.LogErrorf(msg, v...)
}
// LogDebugf logs a debug message.
func (u *promUnifi) LogDebugf(msg string, v ...interface{}) {
webserver.NewInputEvent(PluginName, PluginName, &webserver.Event{
Ts: time.Now(),
Msg: fmt.Sprintf(msg, v...),
Tags: map[string]string{"type": "debug"},
})
u.Collector.LogDebugf(msg, v...)
}

View File

@ -16,7 +16,7 @@ type report interface {
done()
send([]*metric)
metrics() *poller.Metrics
report(c poller.Collect, descs map[*prometheus.Desc]bool)
report(c poller.Logger, descs map[*prometheus.Desc]bool)
export(m *metric, v float64) prometheus.Metric
error(ch chan<- prometheus.Metric, d *prometheus.Desc, v interface{})
addUDM()
@ -41,7 +41,7 @@ func (r *Report) metrics() *poller.Metrics {
return r.Metrics
}
func (r *Report) report(c poller.Collect, descs map[*prometheus.Desc]bool) {
func (r *Report) report(c poller.Logger, descs map[*prometheus.Desc]bool) {
m := r.Metrics
c.Logf("UniFi Measurements Exported. Site: %d, Client: %d, "+

View File

@ -78,7 +78,7 @@ func descSite(ns string) *site {
func (u *promUnifi) exportSiteDPI(r report, v interface{}) {
s, ok := v.(*unifi.DPITable)
if !ok {
u.Collector.LogErrorf("invalid type given to SiteDPI: %T", v)
u.LogErrorf("invalid type given to SiteDPI: %T", v)
return
}